Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ ext {
icepickLibVersion = '3.2.0'
stethoLibVersion = '1.5.0'
markwonVersion = '4.2.1'
work_version = '2.3.2'
zip4j_version = '2.3.2'
preference_version = '1.1.0'
}

dependencies {
Expand Down Expand Up @@ -112,4 +115,13 @@ dependencies {

implementation "io.noties.markwon:core:${markwonVersion}"
implementation "io.noties.markwon:linkify:${markwonVersion}"

implementation "androidx.work:work-runtime:${work_version}"
implementation "androidx.work:work-rxjava2:${work_version}"

implementation "net.lingala.zip4j:zip4j:${zip4j_version}"

implementation "androidx.preference:preference:${preference_version}"

implementation 'com.github.yausername:EncryptedSharedPreferences:1.0.0-beta01'
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<application
android:name=".App"
android:allowBackup="true"
android:fullBackupContent="@xml/android_auto_backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:logo="@mipmap/ic_launcher"
Expand Down
170 changes: 170 additions & 0 deletions app/src/main/java/org/schabi/newpipe/database/BackupRestoreHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package org.schabi.newpipe.database;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.widget.Toast;

import androidx.annotation.MainThread;

import org.schabi.newpipe.NewPipeDatabase;
import org.schabi.newpipe.R;
import org.schabi.newpipe.util.ZipHelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;

public class BackupRestoreHelper {

private File databasesDir;
private File newpipe_db;
private File newpipe_db_journal;
private File newpipe_db_shm;
private File newpipe_db_wal;
private File newpipe_settings;

private Context ctx;

public BackupRestoreHelper(Context ctx) {
this.ctx = ctx;
String homeDir = ctx.getApplicationInfo().dataDir;
databasesDir = new File(homeDir + "/databases");
newpipe_db = new File(homeDir + "/databases/newpipe.db");
newpipe_db_journal = new File(homeDir + "/databases/newpipe.db-journal");
newpipe_db_shm = new File(homeDir + "/databases/newpipe.db-shm");
newpipe_db_wal = new File(homeDir + "/databases/newpipe.db-wal");

newpipe_settings = new File(homeDir + "/databases/newpipe.settings");
newpipe_settings.delete();
}

public String getAutoBackupPath(){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
String autoBackupPath = sharedPreferences.getString(ctx.getString(R.string.backup_path_key), null);
if(null == autoBackupPath){
autoBackupPath = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOCUMENTS + File.separator + "NewPipeAutoBackup").getAbsolutePath();
}
return autoBackupPath;
}

public void exportDatabase(String path, char[] password) throws Exception {

//checkpoint before export
NewPipeDatabase.checkpoint();

ZipHelper.addFileToZip(path, newpipe_db.getPath(), "newpipe.db", password);
saveSharedPreferencesToFile(newpipe_settings);
ZipHelper.addFileToZip(path, newpipe_settings.getPath(), "newpipe.settings", password);
}

private void saveSharedPreferencesToFile(File dst) {
ObjectOutputStream output = null;
try {
output = new ObjectOutputStream(new FileOutputStream(dst));
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ctx);
output.writeObject(pref.getAll());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (output != null) {
output.flush();
output.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

@MainThread
public void importDatabase(String filePath, char[] password) throws Exception {

if (!databasesDir.exists() && !databasesDir.mkdir()) {
throw new Exception("Could not create databases dir");
}

final boolean isDbFileExtracted = ZipHelper.extractFileFromZip(filePath, "newpipe.db", databasesDir.getPath(), password);

if (isDbFileExtracted) {
newpipe_db_journal.delete();
newpipe_db_wal.delete();
newpipe_db_shm.delete();

} else {

Toast.makeText(ctx, R.string.could_not_import_all_files, Toast.LENGTH_LONG)
.show();
}

//If settings file exist, ask if it should be imported.
if (ZipHelper.extractFileFromZip(filePath, "newpipe.settings", databasesDir.getPath(), password)) {
AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
alert.setTitle(R.string.import_settings);

alert.setNegativeButton(android.R.string.no, (dialog, which) -> {
dialog.dismiss();
// restart app to properly load db
System.exit(0);
});
alert.setPositiveButton(ctx.getString(R.string.finish), (dialog, which) -> {
dialog.dismiss();
loadSharedPreferences(newpipe_settings);
// restart app to properly load db
System.exit(0);
});
alert.show();
} else {
// restart app to properly load db
System.exit(0);
}
}

private void loadSharedPreferences(File src) {
ObjectInputStream input = null;
try {
input = new ObjectInputStream(new FileInputStream(src));
SharedPreferences.Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
prefEdit.clear();
Map<String, ?> entries = (Map<String, ?>) input.readObject();
for (Map.Entry<String, ?> entry : entries.entrySet()) {
Object v = entry.getValue();
String key = entry.getKey();

if (v instanceof Boolean)
prefEdit.putBoolean(key, (Boolean) v);
else if (v instanceof Float)
prefEdit.putFloat(key, (Float) v);
else if (v instanceof Integer)
prefEdit.putInt(key, (Integer) v);
else if (v instanceof Long)
prefEdit.putLong(key, (Long) v);
else if (v instanceof String)
prefEdit.putString(key, ((String) v));
}
prefEdit.commit();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.schabi.newpipe.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import org.schabi.newpipe.R;
import org.schabi.newpipe.database.BackupRestoreHelper;

import java.io.File;

public class AutoBackupWorker extends Worker {

public AutoBackupWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}

@NonNull
@Override
public Result doWork() {
Context ctx = getApplicationContext();
BackupRestoreHelper backupRestoreHelper = new BackupRestoreHelper(ctx);
String autoBackupPath = backupRestoreHelper.getAutoBackupPath();
try {
new File(autoBackupPath).mkdirs();
String path = autoBackupPath + File.separator + "NewPipeData-" + Build.MODEL + ".zip";
SharedPreferences encryptedSharedPreferences = EncryptedSharedPreferencesHelper.create(ctx);
String password = (encryptedSharedPreferences != null) ? encryptedSharedPreferences.getString(ctx.getString(R.string.backup_password_key), null) : null;
if(TextUtils.isEmpty(password)) return Result.failure();
backupRestoreHelper.exportDatabase(path, password.toCharArray());
} catch (Exception e) {
return Result.failure();
}
return Result.success();
}
}
Loading