local backup/restore fixes

This commit is contained in:
Yuriy Liskov
2025-12-08 06:19:54 +02:00
parent 6f54c7d211
commit 8f88dcf70e
5 changed files with 41 additions and 15 deletions
@@ -123,10 +123,11 @@ public class BackupSettingsPresenter extends BasePresenter<Void> {
BackupAndRestoreManager backupManager = new BackupAndRestoreManager(getContext());
String backupPath = backupManager.getBackupPathRoot();
String backupPath = backupManager.getBackupRootPath();
options.add(UiOptionItem.from(
String.format("%s:\n%s", getContext().getString(R.string.app_backup), backupPath),
backupPath == null ? getContext().getString(R.string.app_backup) :
String.format("%s:\n%s", getContext().getString(R.string.app_backup), backupPath),
option -> {
AppDialogUtil.showConfirmationDialog(getContext(), getContext().getString(R.string.app_backup), () -> {
mSidebarService.enableSection(MediaGroup.TYPE_SETTINGS, true); // prevent Settings lock
@@ -136,7 +137,8 @@ public class BackupSettingsPresenter extends BasePresenter<Void> {
}));
options.add(UiOptionItem.from(
String.format("%s:\n%s", getContext().getString(R.string.app_restore), backupPath),
backupPath == null ? getContext().getString(R.string.app_restore) :
String.format("%s:\n%s", getContext().getString(R.string.app_restore), backupPath),
option -> {
backupManager.getBackupNames(names -> showLocalRestoreDialog(backupManager, names));
}));
@@ -12,7 +12,6 @@ import com.liskovsoft.sharedutils.helpers.PermissionHelpers;
import com.liskovsoft.sharedutils.mylogger.Log;
import com.liskovsoft.smartyoutubetv2.common.R;
import com.liskovsoft.smartyoutubetv2.common.prefs.HiddenPrefs;
import com.liskovsoft.smartyoutubetv2.common.utils.Utils;
import java.io.File;
import java.util.ArrayList;
@@ -47,11 +46,13 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
mDataDir = new File(mContext.getApplicationInfo().dataDir, SHARED_PREFS_SUBDIR);
mBackupDirs = new ArrayList<>();
initBackupDirs();
}
private void initBackupDirs() {
if (!mBackupDirs.isEmpty()) {
return;
}
File externalDir = getExternalStorageDirectory();
// Main backup dir
mBackupDirs.add(createBackupDir(new File(externalDir, String.format("data/%s", mContext.getPackageName()))));
@@ -85,7 +86,7 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
private File getExternalStorageDirectory() {
File result;
if (VERSION.SDK_INT > 29) {
if (hasAccessOnlyToAppFolders()) {
result = mContext.getExternalMediaDirs()[0];
if (!result.exists()) {
@@ -155,7 +156,9 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
FileHelpers.delete(new File(destination, HiddenPrefs.SHARED_PREFERENCES_NAME + ".xml"));
}
mHelper.exportAppMediaFolder();
if (hasAccessOnlyToAppFolders()) {
mHelper.exportAppMediaFolder();
}
}
private void restoreData(String backupName) {
@@ -211,6 +214,8 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
}
private File getBackup() {
initBackupDirs();
File currentBackup = null;
for (File backupDir : mBackupDirs) {
@@ -222,6 +227,8 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
}
private File getBackupCheck(String backupName) {
initBackupDirs();
File currentBackup = null;
for (File backupDir : mBackupDirs) {
@@ -241,6 +248,8 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
}
private File getBackupCheck() {
initBackupDirs();
for (File backupDir : mBackupDirs) {
if (backupDir.exists()) {
return backupDir.getParentFile();
@@ -270,7 +279,11 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
return currentBackup != null ? currentBackup.toString() : null;
}
public String getBackupPathRoot() {
public String getBackupRootPath() {
if (hasAccessOnlyToAppFolders()) {
return null; // Android 11+: only backup through the file manager (no shared dir)
}
return String.format("%s/data", getExternalStorageDirectory());
}
@@ -292,6 +305,8 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
}
private List<String> getBackupNames() {
initBackupDirs();
List<String> names = new ArrayList<>();
for (File backupDir : mBackupDirs) {
@@ -309,11 +324,20 @@ public class BackupAndRestoreManager implements MotherActivity.OnPermissions {
return names;
}
private static boolean hasStoragePermissions(Context context) {
return VERSION.SDK_INT > 29 || PermissionHelpers.hasStoragePermissions(context);
private boolean hasStoragePermissions(Context context) {
return hasAccessOnlyToAppFolders() || PermissionHelpers.hasStoragePermissions(context);
}
public boolean hasBackup() {
return getBackupCheck() != null;
}
// Android 11+: only backup through the file manager (no shared dir)
private boolean hasAccessOnlyToAppFolders() {
return getTargetSdkVersion() > 29;
}
private int getTargetSdkVersion() {
return mContext.getApplicationInfo().targetSdkVersion;
}
}
+2 -2
View File
@@ -50,8 +50,8 @@ android {
applicationId "org.smarttube"
minSdkVersion project.properties.minSdkVersion
targetSdkVersion project.properties.targetSdkVersion
versionCode 2249
versionName "30.59"
versionCode 2253
versionName "30.63"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "long", "TIMESTAMP", System.currentTimeMillis() + "L"