导航菜单

页面标题

页面副标题

xDrip+ v04633772025.07.16 - ImportDatabaseActivity.java 源代码

正在查看: xDrip+ v04633772025.07.16 应用的 ImportDatabaseActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.eveningoutpost.dexdrip;

import android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.utilitymodels.CollectionServiceStarter;
import com.eveningoutpost.dexdrip.utils.DatabaseUtil;
import com.eveningoutpost.dexdrip.utils.FileUtils;
import com.eveningoutpost.dexdrip.utils.ListActivityWithMenu;
import com.eveningoutpost.dexdrip.wearintegration.WatchUpdaterService;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ImportDatabaseActivity extends ListActivityWithMenu {
    private static final String TAG = "ImportDatabaseActivity";
    public static String menu_name = "Import Database";
    private ArrayList<String> databaseNames;
    private ArrayList<File> databases;
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle bundle) {
        setTheme(2131820787);
        super.onCreate(bundle);
        this.mHandler = new Handler();
        setContentView(2131492919);
        String stringExtra = getIntent().getStringExtra("importit");
        if (stringExtra != null && stringExtra.length() > 0) {
            importDB(new File(stringExtra), this);
        } else {
            showWarningAndInstructions();
        }
    }

    public void generateDBGui() {
        int checkSelfPermission = ContextCompat.checkSelfPermission(this, "android.permission.READ_EXTERNAL_STORAGE");
        if (checkSelfPermission == 0 && findAllDatabases()) {
            sortDatabasesAlphabetically();
            showDatabasesInList();
        } else if (checkSelfPermission != 0) {
            JoH.static_toast_long("Need permission for saved files");
            ActivityCompat.requestPermissions(this, new String[]{"android.permission.READ_EXTERNAL_STORAGE"}, 132);
        } else {
            postImportDB("'xdrip' is not a directory... aborting.");
        }
    }

    private void showWarningAndInstructions() {
        View inflate = LayoutInflater.from(this).inflate(2131492987, (ViewGroup) null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Restore Instructions");
        builder.setView(inflate);
        builder.setCancelable(false);
        builder.setPositiveButton(2131755953, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                ImportDatabaseActivity.this.generateDBGui();
            }
        });
        builder.create().show();
    }

    private void sortDatabasesAlphabetically() {
        Collections.sort(this.databases, new Comparator<File>() {
            @Override
            public int compare(File file, File file2) {
                return file2.getName().compareTo(file.getName());
            }
        });
    }

    private boolean findAllDatabases() {
        this.databases = new ArrayList<>();
        File file = new File(FileUtils.getExternalDir());
        if (!FileUtils.makeSureDirectoryExists(file.getAbsolutePath())) {
            return false;
        }
        addAllDatabases(file, this.databases);
        try {
            for (File file2 : file.listFiles(new FileFilter() {
                @Override
                public boolean accept(File file3) {
                    return file3.isDirectory();
                }
            })) {
                addAllDatabases(file2, this.databases);
            }
            return true;
        } catch (NullPointerException unused) {
            return true;
        }
    }

    private void showDatabasesInList() {
        this.databaseNames = new ArrayList<>();
        Iterator<File> it = this.databases.iterator();
        while (it.hasNext()) {
            this.databaseNames.add(it.next().getName());
        }
        setListAdapter(new ArrayAdapter(this, R.layout.simple_list_item_1, this.databaseNames));
        if (this.databaseNames.size() == 0) {
            postImportDB("No databases found.");
        }
    }

    private void addAllDatabases(File file, ArrayList<File> arrayList) {
        File[] listFiles = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file2) {
                return file2.getPath().endsWith(".sqlite") || file2.getPath().endsWith(".zip");
            }
        });
        if (arrayList == null || listFiles == null) {
            return;
        }
        Collections.addAll(arrayList, listFiles);
    }

    @Override
    protected void onListItemClick(ListView listView, View view, final int i, long j) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton(2131755953, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i2) {
                ImportDatabaseActivity.this.importDB(i);
            }
        });
        builder.setNegativeButton(2131755280, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i2) {
            }
        });
        builder.setTitle("Confirm Import");
        builder.setMessage("Do you really want to import '" + this.databases.get(i).getName() + "'?\n This may negatively affect the data integrity of your system!");
        builder.create().show();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public String getMenuName() {
        return menu_name;
    }

    @Override
    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        super.onRequestPermissionsResult(i, strArr, iArr);
        if (i == 132) {
            if (iArr[0] == 0) {
                generateDBGui();
            } else {
                finish();
            }
        }
    }

    public int getDBVersion() {
        try {
            return getPackageManager().getApplicationInfo(getPackageName(), 128).metaData.getInt("AA_DB_VERSION");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public void importDB(int i) {
        importDB(this.databases.get(i), this);
    }

    private void importDB(File file, Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("Importing, please wait");
        builder.setMessage("Importing, please wait");
        AlertDialog create = builder.create();
        create.show();
        create.setMessage("Step 1: checking prerequisites");
        create.setCancelable(false);
        new LoadTask(create, file).execute(new Void[0]);
    }

    protected void postImportDB(String str) {
        Home.startWatchUpdaterService(this, WatchUpdaterService.ACTION_RESET_DB, TAG);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton(2131755953, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                ImportDatabaseActivity.this.returnToHome();
            }
        });
        builder.setTitle("Import Result");
        builder.setMessage(str);
        builder.create().show();
    }

    public void returnToHome() {
        Intent intent = new Intent(this, (Class<?>) Home.class);
        CollectionServiceStarter.restartCollectionService(getApplicationContext());
        startActivity(intent);
        finish();
    }

    private class LoadTask extends AsyncTask<Void, Void, String> {
        private File dbFile;
        private final AlertDialog statusDialog;

        LoadTask(AlertDialog alertDialog, File file) {
            this.statusDialog = alertDialog;
            this.dbFile = file;
        }

        @Override
        public String doInBackground(Void... voidArr) {
            File file;
            try {
                if (this.dbFile.getAbsolutePath().endsWith(".zip")) {
                    try {
                        FileInputStream fileInputStream = new FileInputStream(this.dbFile.getAbsolutePath());
                        try {
                            ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
                            try {
                                ZipEntry nextEntry = zipInputStream.getNextEntry();
                                if (nextEntry != null && nextEntry.isDirectory()) {
                                    nextEntry = zipInputStream.getNextEntry();
                                }
                                if (nextEntry != null) {
                                    if (nextEntry.getName().endsWith(".sqlite")) {
                                        String replaceFirst = this.dbFile.getAbsolutePath().replaceFirst(".zip$", ".sqlite");
                                        FileOutputStream fileOutputStream = new FileOutputStream(replaceFirst);
                                        try {
                                            byte[] bArr = new byte[4096];
                                            while (true) {
                                                int read = zipInputStream.read(bArr);
                                                if (read == -1) {
                                                    break;
                                                }
                                                fileOutputStream.write(bArr, 0, read);
                                            }
                                            fileOutputStream.close();
                                            file = new File(replaceFirst);
                                            this.dbFile = file;
                                            Log.d(ImportDatabaseActivity.TAG, "New filename: " + replaceFirst);
                                            zipInputStream.closeEntry();
                                            zipInputStream.close();
                                            fileInputStream.close();
                                        } finally {
                                        }
                                    } else {
                                        JoH.static_toast_long("Cant find sqlite in zip file");
                                        zipInputStream.close();
                                        fileInputStream.close();
                                        return "Cant find sqlite in zip file";
                                    }
                                } else {
                                    JoH.static_toast_long("Invalid ZIP file");
                                    zipInputStream.close();
                                    fileInputStream.close();
                                    return "Invalid ZIP file";
                                }
                            } finally {
                            }
                        } catch (Throwable th) {
                            try {
                                fileInputStream.close();
                            } catch (Throwable th2) {
                                th.addSuppressed(th2);
                            }
                            throw th;
                        }
                    } catch (IOException unused) {
                        JoH.static_toast_long("Could not open file");
                        return "Could not open file";
                    }
                } else {
                    file = null;
                }
                SQLiteDatabase openDatabase = SQLiteDatabase.openDatabase(this.dbFile.getAbsolutePath(), null, 1);
                int version = openDatabase.getVersion();
                openDatabase.close();
                if (ImportDatabaseActivity.this.getDBVersion() == version) {
                    ImportDatabaseActivity.this.mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            LoadTask.this.statusDialog.setMessage("Step 2: exporting current DB");
                        }
                    });
                    if (DatabaseUtil.saveSql(xdrip.getAppContext(), "b4import") != null) {
                        ImportDatabaseActivity.this.mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                LoadTask.this.statusDialog.setMessage("Step 3: importing DB");
                            }
                        });
                        String loadSql = DatabaseUtil.loadSql(xdrip.getAppContext(), this.dbFile.getAbsolutePath());
                        if (file != null) {
                            file.delete();
                        }
                        this.statusDialog.dismiss();
                        return loadSql;
                    }
                    this.statusDialog.dismiss();
                    return "Exporting database not successfull... aborting.";
                }
                this.statusDialog.dismiss();
                return "Wrong Database version.\n(" + version + " instead of " + ImportDatabaseActivity.this.getDBVersion() + ")";
            } catch (SQLiteException unused2) {
                this.statusDialog.dismiss();
                return "Database cannot be opened... aborting.";
            }
        }

        @Override
        public void onPostExecute(String str) {
            super.onPostExecute((LoadTask) str);
            ImportDatabaseActivity.this.postImportDB(str);
        }
    }
}