mirror of
https://github.com/yuliskov/SmartTube.git
synced 2026-03-13 15:20:26 +08:00
Local backup/restore: unpack externally copied zip
This commit is contained in:
@@ -115,9 +115,6 @@ public class BackupAndRestoreHelper implements OnResult {
|
||||
if (data == null) return;
|
||||
|
||||
File mediaDir = FileHelpers.getExternalMediaDirectory(mContext);
|
||||
File dataDir = new File(mediaDir, "data");
|
||||
|
||||
if (!mediaDir.exists()) mediaDir.mkdirs();
|
||||
|
||||
Uri uri = data.getData();
|
||||
if (uri == null && data.getClipData() != null) {
|
||||
@@ -128,18 +125,7 @@ public class BackupAndRestoreHelper implements OnResult {
|
||||
File zipFile = new File(mediaDir, "restore.zip");
|
||||
copyUriToDir(uri, zipFile);
|
||||
|
||||
// Cleanup previous data
|
||||
//deleteRecursive(dataDir);
|
||||
FileHelpers.delete(dataDir);
|
||||
dataDir.mkdirs();
|
||||
|
||||
if (ZipHelper2.hasRootDir(zipFile, "data")) {
|
||||
ZipHelper2.unzip(zipFile, mediaDir);
|
||||
} else {
|
||||
ZipHelper2.unzip(zipFile, dataDir);
|
||||
}
|
||||
|
||||
zipFile.delete();
|
||||
unpackTempZip(zipFile);
|
||||
|
||||
mOnSuccess.run();
|
||||
}
|
||||
@@ -155,40 +141,45 @@ public class BackupAndRestoreHelper implements OnResult {
|
||||
}
|
||||
|
||||
try {
|
||||
// Target folder: /Android/media/<package>/data
|
||||
File mediaDir = FileHelpers.getExternalMediaDirectory(mContext);
|
||||
File dataDir = new File(mediaDir, "data");
|
||||
|
||||
// Remove old data
|
||||
//if (dataDir.exists()) deleteRecursive(dataDir);
|
||||
if (dataDir.exists()) FileHelpers.delete(dataDir);
|
||||
|
||||
// Copy ZIP from URI to temporary file
|
||||
File tempZip = new File(mediaDir, "imported_backup.zip");
|
||||
copyUriToFile(zipUri, tempZip);
|
||||
|
||||
if (ZipHelper2.hasRootDir(tempZip, "data")) {
|
||||
// Unpack ZIP with data folder
|
||||
ZipHelper2.unzip(tempZip, mediaDir);
|
||||
} else {
|
||||
// Seems we've packed the contents of the data dir not data itself
|
||||
ZipHelper2.unzip(tempZip, dataDir);
|
||||
}
|
||||
|
||||
// Delete the temporary ZIP
|
||||
tempZip.delete();
|
||||
unpackTempZip(tempZip);
|
||||
|
||||
BackupSettingsPresenter.instance(mContext).showLocalRestoreDialogApi30();
|
||||
|
||||
//Toast.makeText(mContext, "Backup restored successfully", Toast.LENGTH_SHORT).show();
|
||||
|
||||
// TODO: possibly launch restore dialog
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(mContext, "Failed to restore backup", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
public void unpackTempZip(File tempZip) {
|
||||
if (!tempZip.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Target folder: /Android/media/<package>/data
|
||||
File mediaDir = FileHelpers.getExternalMediaDirectory(mContext);
|
||||
File dataDir = new File(mediaDir, "data");
|
||||
|
||||
// Remove old data
|
||||
if (dataDir.exists()) FileHelpers.delete(dataDir);
|
||||
|
||||
if (ZipHelper2.hasRootDir(tempZip, "data")) {
|
||||
// Unpack ZIP with data folder
|
||||
ZipHelper2.unzip(tempZip, mediaDir);
|
||||
} else {
|
||||
// Seems we've packed the contents of the data dir not data itself
|
||||
ZipHelper2.unzip(tempZip, dataDir);
|
||||
}
|
||||
|
||||
// Delete the temporary ZIP
|
||||
tempZip.delete();
|
||||
}
|
||||
|
||||
private void copyUriToDir(Uri uri, File targetDir) {
|
||||
try {
|
||||
String fileName = getFileName(uri);
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.liskovsoft.smartyoutubetv2.common.misc;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build.VERSION;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
|
||||
@@ -29,7 +28,7 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
|
||||
private static final String BACKUP_DIR_NAME = "Backup";
|
||||
private final Context mContext;
|
||||
private static final String SHARED_PREFS_SUBDIR = "shared_prefs";
|
||||
private final File mDataDir;
|
||||
private final File mSharedPrefsDir;
|
||||
private final List<File> mBackupDirs;
|
||||
private final BackupAndRestoreHelper mHelper;
|
||||
private final boolean mForceApi30;
|
||||
@@ -50,7 +49,7 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
|
||||
|
||||
mHelper = new BackupAndRestoreHelper(context);
|
||||
|
||||
mDataDir = new File(mContext.getApplicationInfo().dataDir, SHARED_PREFS_SUBDIR);
|
||||
mSharedPrefsDir = new File(mContext.getApplicationInfo().dataDir, SHARED_PREFS_SUBDIR);
|
||||
|
||||
mBackupDirs = new ArrayList<>();
|
||||
}
|
||||
@@ -154,9 +153,9 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
|
||||
FileHelpers.delete(currentBackup);
|
||||
}
|
||||
|
||||
if (mDataDir.isDirectory() && !FileHelpers.isEmpty(mDataDir)) {
|
||||
File destination = new File(currentBackup, mDataDir.getName());
|
||||
FileHelpers.copy(mDataDir, destination, fileName -> Helpers.endsWithAny(fileName.toString(), Utils.BACKUP_PATTERNS));
|
||||
if (mSharedPrefsDir.isDirectory() && !FileHelpers.isEmpty(mSharedPrefsDir)) {
|
||||
File destination = new File(currentBackup, mSharedPrefsDir.getName());
|
||||
FileHelpers.copy(mSharedPrefsDir, destination, fileName -> Helpers.endsWithAny(fileName.toString(), Utils.BACKUP_PATTERNS));
|
||||
|
||||
// Don't store unique id
|
||||
FileHelpers.delete(new File(destination, HiddenPrefs.SHARED_PREFERENCES_NAME + ".xml"));
|
||||
@@ -182,13 +181,13 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mDataDir.isDirectory()) {
|
||||
if (mSharedPrefsDir.isDirectory()) {
|
||||
// remove old data
|
||||
FileHelpers.delete(mDataDir);
|
||||
FileHelpers.delete(mSharedPrefsDir);
|
||||
}
|
||||
|
||||
FileHelpers.copy(sourceBackupDir, mDataDir, fileName -> Helpers.endsWithAny(fileName.toString(), Utils.BACKUP_PATTERNS));
|
||||
fixFileNames(mDataDir);
|
||||
FileHelpers.copy(sourceBackupDir, mSharedPrefsDir, fileName -> Helpers.endsWithAny(fileName.toString(), Utils.BACKUP_PATTERNS));
|
||||
fixFileNames(mSharedPrefsDir);
|
||||
|
||||
MessageHelpers.showMessage(mContext, R.string.msg_done);
|
||||
|
||||
@@ -296,6 +295,10 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
|
||||
public void getBackupNames(OnBackupNames callback) {
|
||||
if (FileHelpers.isExternalStorageReadable()) {
|
||||
if (hasStoragePermissions(mContext)) {
|
||||
if (hasAccessOnlyToAppFolders()) {
|
||||
// Try to restore externally copied backup zip (if any)
|
||||
unpackBackupZip();
|
||||
}
|
||||
callback.onBackupNames(getBackupNames());
|
||||
} else {
|
||||
mPendingHandler = () -> callback.onBackupNames(getBackupNames());
|
||||
@@ -359,4 +362,16 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void unpackBackupZip() {
|
||||
File[] files = FileHelpers.getExternalMediaDirectory(mContext).listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (file.getName().endsWith(".zip")) {
|
||||
mHelper.unpackTempZip(file);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user