Skip to content

Commit 3a80709

Browse files
committed
added autobackup setting
1 parent b1eaf56 commit 3a80709

14 files changed

Lines changed: 575 additions & 233 deletions

app/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ext {
5454
icepickLibVersion = '3.2.0'
5555
stethoLibVersion = '1.5.0'
5656
markwonVersion = '4.2.1'
57+
work_version = '2.3.2'
5758
}
5859

5960
dependencies {
@@ -112,4 +113,8 @@ dependencies {
112113

113114
implementation "io.noties.markwon:core:${markwonVersion}"
114115
implementation "io.noties.markwon:linkify:${markwonVersion}"
116+
117+
implementation "androidx.work:work-runtime:${work_version}"
118+
implementation "androidx.work:work-rxjava2:${work_version}"
119+
115120
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.schabi.newpipe.settings;
2+
3+
import android.content.Context;
4+
import android.os.Build;
5+
6+
import androidx.annotation.NonNull;
7+
import androidx.work.Worker;
8+
import androidx.work.WorkerParameters;
9+
10+
import org.schabi.newpipe.database.BackupRestoreHelper;
11+
12+
import java.io.File;
13+
14+
public class AutoBackupWorker extends Worker {
15+
16+
public AutoBackupWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
17+
super(context, workerParams);
18+
}
19+
20+
@NonNull
21+
@Override
22+
public Result doWork() {
23+
try {
24+
BackupRestoreHelper backupRestoreHelper = new BackupRestoreHelper(getApplicationContext());
25+
String autoBackupPath = backupRestoreHelper.getAutoBackupPath();
26+
new File(autoBackupPath).mkdirs();
27+
String path = autoBackupPath + File.separator + "NewPipeData-" + Build.MODEL + ".zip";
28+
backupRestoreHelper.exportDatabase(path);
29+
} catch (Exception e) {
30+
return Result.failure();
31+
}
32+
return Result.success();
33+
}
34+
}

0 commit comments

Comments
 (0)