|
| 1 | +package org.schabi.newpipe.database; |
| 2 | + |
| 3 | +import android.app.AlertDialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.SharedPreferences; |
| 6 | +import android.os.Environment; |
| 7 | +import android.preference.PreferenceManager; |
| 8 | +import android.widget.Toast; |
| 9 | + |
| 10 | +import androidx.annotation.MainThread; |
| 11 | + |
| 12 | +import org.schabi.newpipe.R; |
| 13 | +import org.schabi.newpipe.util.ZipHelper; |
| 14 | + |
| 15 | +import java.io.BufferedOutputStream; |
| 16 | +import java.io.File; |
| 17 | +import java.io.FileInputStream; |
| 18 | +import java.io.FileNotFoundException; |
| 19 | +import java.io.FileOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.ObjectInputStream; |
| 22 | +import java.io.ObjectOutputStream; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.zip.ZipFile; |
| 25 | +import java.util.zip.ZipOutputStream; |
| 26 | + |
| 27 | +public class BackupRestoreHelper { |
| 28 | + |
| 29 | + private File databasesDir; |
| 30 | + private File newpipe_db; |
| 31 | + private File newpipe_db_journal; |
| 32 | + private File newpipe_db_shm; |
| 33 | + private File newpipe_db_wal; |
| 34 | + private File newpipe_settings; |
| 35 | + |
| 36 | + private Context ctx; |
| 37 | + |
| 38 | + public BackupRestoreHelper(Context ctx) { |
| 39 | + this.ctx = ctx; |
| 40 | + String homeDir = ctx.getApplicationInfo().dataDir; |
| 41 | + databasesDir = new File(homeDir + "/databases"); |
| 42 | + newpipe_db = new File(homeDir + "/databases/newpipe.db"); |
| 43 | + newpipe_db_journal = new File(homeDir + "/databases/newpipe.db-journal"); |
| 44 | + newpipe_db_shm = new File(homeDir + "/databases/newpipe.db-shm"); |
| 45 | + newpipe_db_wal = new File(homeDir + "/databases/newpipe.db-wal"); |
| 46 | + |
| 47 | + newpipe_settings = new File(homeDir + "/databases/newpipe.settings"); |
| 48 | + newpipe_settings.delete(); |
| 49 | + } |
| 50 | + |
| 51 | + public String getAutoBackupPath(){ |
| 52 | + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx); |
| 53 | + String autoBackupPath = sharedPreferences.getString(ctx.getString(R.string.backup_path_key), null); |
| 54 | + if(null == autoBackupPath){ |
| 55 | + autoBackupPath = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOCUMENTS + File.separator + "NewPipeAutoBackup").getAbsolutePath(); |
| 56 | + } |
| 57 | + return autoBackupPath; |
| 58 | + } |
| 59 | + |
| 60 | + public void exportDatabase(String path) throws Exception { |
| 61 | + ZipOutputStream outZip = new ZipOutputStream( |
| 62 | + new BufferedOutputStream( |
| 63 | + new FileOutputStream(path))); |
| 64 | + ZipHelper.addFileToZip(outZip, newpipe_db.getPath(), "newpipe.db"); |
| 65 | + |
| 66 | + saveSharedPreferencesToFile(newpipe_settings); |
| 67 | + ZipHelper.addFileToZip(outZip, newpipe_settings.getPath(), "newpipe.settings"); |
| 68 | + |
| 69 | + outZip.close(); |
| 70 | + } |
| 71 | + |
| 72 | + private void saveSharedPreferencesToFile(File dst) { |
| 73 | + ObjectOutputStream output = null; |
| 74 | + try { |
| 75 | + output = new ObjectOutputStream(new FileOutputStream(dst)); |
| 76 | + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ctx); |
| 77 | + output.writeObject(pref.getAll()); |
| 78 | + |
| 79 | + } catch (FileNotFoundException e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } catch (IOException e) { |
| 82 | + e.printStackTrace(); |
| 83 | + } finally { |
| 84 | + try { |
| 85 | + if (output != null) { |
| 86 | + output.flush(); |
| 87 | + output.close(); |
| 88 | + } |
| 89 | + } catch (IOException ex) { |
| 90 | + ex.printStackTrace(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @MainThread |
| 96 | + public void importDatabase(String filePath) throws Exception { |
| 97 | + // check if file is supported |
| 98 | + ZipFile zipFile = null; |
| 99 | + try { |
| 100 | + zipFile = new ZipFile(filePath); |
| 101 | + } catch (IOException ioe) { |
| 102 | + Toast.makeText(ctx, R.string.no_valid_zip_file, Toast.LENGTH_SHORT) |
| 103 | + .show(); |
| 104 | + return; |
| 105 | + } finally { |
| 106 | + try { |
| 107 | + zipFile.close(); |
| 108 | + } catch (Exception ignored) { |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (!databasesDir.exists() && !databasesDir.mkdir()) { |
| 113 | + throw new Exception("Could not create databases dir"); |
| 114 | + } |
| 115 | + |
| 116 | + final boolean isDbFileExtracted = ZipHelper.extractFileFromZip(filePath, |
| 117 | + newpipe_db.getPath(), "newpipe.db"); |
| 118 | + |
| 119 | + if (isDbFileExtracted) { |
| 120 | + newpipe_db_journal.delete(); |
| 121 | + newpipe_db_wal.delete(); |
| 122 | + newpipe_db_shm.delete(); |
| 123 | + |
| 124 | + } else { |
| 125 | + |
| 126 | + Toast.makeText(ctx, R.string.could_not_import_all_files, Toast.LENGTH_LONG) |
| 127 | + .show(); |
| 128 | + } |
| 129 | + |
| 130 | + //If settings file exist, ask if it should be imported. |
| 131 | + if (ZipHelper.extractFileFromZip(filePath, newpipe_settings.getPath(), "newpipe.settings")) { |
| 132 | + AlertDialog.Builder alert = new AlertDialog.Builder(ctx); |
| 133 | + alert.setTitle(R.string.import_settings); |
| 134 | + |
| 135 | + alert.setNegativeButton(android.R.string.no, (dialog, which) -> { |
| 136 | + dialog.dismiss(); |
| 137 | + // restart app to properly load db |
| 138 | + System.exit(0); |
| 139 | + }); |
| 140 | + alert.setPositiveButton(ctx.getString(R.string.finish), (dialog, which) -> { |
| 141 | + dialog.dismiss(); |
| 142 | + loadSharedPreferences(newpipe_settings); |
| 143 | + // restart app to properly load db |
| 144 | + System.exit(0); |
| 145 | + }); |
| 146 | + alert.show(); |
| 147 | + } else { |
| 148 | + // restart app to properly load db |
| 149 | + System.exit(0); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private void loadSharedPreferences(File src) { |
| 154 | + ObjectInputStream input = null; |
| 155 | + try { |
| 156 | + input = new ObjectInputStream(new FileInputStream(src)); |
| 157 | + SharedPreferences.Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(ctx).edit(); |
| 158 | + prefEdit.clear(); |
| 159 | + Map<String, ?> entries = (Map<String, ?>) input.readObject(); |
| 160 | + for (Map.Entry<String, ?> entry : entries.entrySet()) { |
| 161 | + Object v = entry.getValue(); |
| 162 | + String key = entry.getKey(); |
| 163 | + |
| 164 | + if (v instanceof Boolean) |
| 165 | + prefEdit.putBoolean(key, (Boolean) v); |
| 166 | + else if (v instanceof Float) |
| 167 | + prefEdit.putFloat(key, (Float) v); |
| 168 | + else if (v instanceof Integer) |
| 169 | + prefEdit.putInt(key, (Integer) v); |
| 170 | + else if (v instanceof Long) |
| 171 | + prefEdit.putLong(key, (Long) v); |
| 172 | + else if (v instanceof String) |
| 173 | + prefEdit.putString(key, ((String) v)); |
| 174 | + } |
| 175 | + prefEdit.commit(); |
| 176 | + } catch (FileNotFoundException e) { |
| 177 | + e.printStackTrace(); |
| 178 | + } catch (IOException e) { |
| 179 | + e.printStackTrace(); |
| 180 | + } catch (ClassNotFoundException e) { |
| 181 | + e.printStackTrace(); |
| 182 | + } finally { |
| 183 | + try { |
| 184 | + if (input != null) { |
| 185 | + input.close(); |
| 186 | + } |
| 187 | + } catch (IOException ex) { |
| 188 | + ex.printStackTrace(); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments