Skip to content

Commit f091f17

Browse files
committed
Backup and restore: imporve silent backup on api 29+
1 parent 2eedfe8 commit f091f17

File tree

3 files changed

+68
-47
lines changed

3 files changed

+68
-47
lines changed

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/misc/BackupAndRestoreHelper.java

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.OutputStream;
2323

2424
public class BackupAndRestoreHelper implements OnResult {
25+
public static final String BACKUP_FOLDER_NAME = "SmartTubeBackup";
2526
private static final int REQ_PICK_FILES = 1001;
2627
private final Context mContext;
2728
private Runnable mOnSuccess;
@@ -39,26 +40,27 @@ public BackupAndRestoreHelper(Context context) {
3940
public void exportAppMediaFolder() {
4041
File mediaDir = FileHelpers.getExternalMediaDirectory(mContext);
4142
File dataDir = new File(mediaDir, "data");
42-
if (!dataDir.exists() || FileHelpers.isEmpty(dataDir)) return;
43+
if (!dataDir.exists() || FileHelpers.isEmpty(dataDir) || VERSION.SDK_INT < 29) return;
4344

44-
String backupZipName = "backup_" + mContext.getPackageName() + ".zip";
45-
File zipFile = new File(mediaDir, backupZipName);
46-
ZipHelper2.zipDirectory(dataDir, zipFile);
45+
String oldBackupZipName = getGeneralData().getBackupZipName();
46+
if (oldBackupZipName == null || !oldBackupZipName.endsWith(".zip")) {
47+
oldBackupZipName = createBackupZipNameWithTimestamp();
48+
getGeneralData().setBackupZipName(oldBackupZipName);
49+
}
4750

48-
if (VERSION.SDK_INT >= 29 && zipFile.exists()) {
49-
String oldBackupZipName = getGeneralData().getBackupZipName();
50-
if (oldBackupZipName != null && oldBackupZipName.endsWith(".zip")) {
51-
backupZipName = oldBackupZipName;
52-
}
53-
MediaStoreFile file = new MediaStoreFile(mContext, backupZipName, "SmartTubeBackup");
54-
if (!file.isWritable()) {
55-
backupZipName = "backup_" + mContext.getPackageName() + "_" + System.currentTimeMillis() + ".zip";
56-
file = new MediaStoreFile(mContext, backupZipName, "SmartTubeBackup");
57-
}
51+
MediaStoreFile file = new MediaStoreFile(mContext, oldBackupZipName, BACKUP_FOLDER_NAME);
52+
if (!file.isWritable()) {
53+
oldBackupZipName = createBackupZipNameWithTimestamp();
54+
getGeneralData().setBackupZipName(oldBackupZipName);
55+
file = new MediaStoreFile(mContext, oldBackupZipName, BACKUP_FOLDER_NAME);
56+
}
57+
58+
if (file.isWritable()) {
59+
final File zipFile = new File(mediaDir, oldBackupZipName);
60+
ZipHelper2.zipDirectory(dataDir, zipFile);
5861

59-
if (file.isWritable()) {
62+
if (zipFile.exists()) {
6063
file.copyFrom(zipFile);
61-
getGeneralData().setBackupZipName(backupZipName);
6264
}
6365
}
6466

@@ -128,49 +130,27 @@ public void importAppMediaFolder(Runnable onSuccess) {
128130
@Override
129131
public void onResult(int requestCode, int resultCode, Intent data) {
130132
if (requestCode == REQ_PICK_FILES && resultCode == Activity.RESULT_OK) {
131-
132133
if (data == null) return;
133134

134-
File mediaDir = FileHelpers.getExternalMediaDirectory(mContext);
135-
136135
Uri uri = data.getData();
137136
if (uri == null && data.getClipData() != null) {
138137
uri = data.getClipData().getItemAt(0).getUri();
139138
}
140-
if (uri == null) return;
141139

142-
File zipFile = new File(mediaDir, "restore.zip");
143-
copyUriToDir(uri, zipFile);
144-
145-
unpackTempZip(zipFile);
146-
147-
mOnSuccess.run();
140+
unpackTempZip(uri, () -> mOnSuccess.run(), null);
148141
}
149142
}
150143

151144
public void handleIncomingZip(Intent intent) {
152145
if (intent == null || !Intent.ACTION_SEND.equals(intent.getAction())) return;
153146

154147
Uri zipUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
155-
if (zipUri == null) {
156-
Toast.makeText(mContext, "No ZIP received", Toast.LENGTH_SHORT).show();
157-
return;
158-
}
159-
160-
try {
161-
File mediaDir = FileHelpers.getExternalMediaDirectory(mContext);
162148

163-
// Copy ZIP from URI to temporary file
164-
File tempZip = new File(mediaDir, "imported_backup.zip");
165-
copyUriToFile(zipUri, tempZip);
166-
167-
unpackTempZip(tempZip);
168-
169-
BackupSettingsPresenter.instance(mContext).showLocalRestoreDialogApi30();
170-
} catch (Exception e) {
171-
e.printStackTrace();
172-
Toast.makeText(mContext, "Failed to restore backup", Toast.LENGTH_SHORT).show();
173-
}
149+
unpackTempZip(
150+
zipUri,
151+
() -> BackupSettingsPresenter.instance(mContext).showLocalRestoreDialogApi30(),
152+
() -> Toast.makeText(mContext, "Failed to restore backup", Toast.LENGTH_SHORT).show()
153+
);
174154
}
175155

176156
public void unpackTempZip(File tempZip) {
@@ -194,7 +174,38 @@ public void unpackTempZip(File tempZip) {
194174
}
195175

196176
// Delete the temporary ZIP
197-
tempZip.delete();
177+
//tempZip.delete();
178+
}
179+
180+
private void unpackTempZip(Uri zipUri, Runnable onSuccess, Runnable onError) {
181+
if (zipUri == null) {
182+
Toast.makeText(mContext, "No ZIP received", Toast.LENGTH_SHORT).show();
183+
return;
184+
}
185+
186+
try {
187+
File mediaDir = FileHelpers.getExternalMediaDirectory(mContext);
188+
189+
// Copy ZIP from URI to the temporary file
190+
String backupZipName = getGeneralData().getBackupZipName();
191+
if (backupZipName == null || !backupZipName.endsWith(".zip")) {
192+
backupZipName = createBackupZipNameWithTimestamp();
193+
getGeneralData().setBackupZipName(backupZipName);
194+
}
195+
File tempZip = new File(mediaDir, backupZipName);
196+
copyUriToFile(zipUri, tempZip);
197+
198+
unpackTempZip(tempZip);
199+
200+
if (onSuccess != null) {
201+
onSuccess.run();
202+
}
203+
} catch (Exception e) {
204+
e.printStackTrace();
205+
if (onError != null) {
206+
onError.run();
207+
}
208+
}
198209
}
199210

200211
private void copyUriToDir(Uri uri, File targetDir) {
@@ -255,4 +266,8 @@ private String getFileName(Uri uri) {
255266
private GeneralData getGeneralData() {
256267
return GeneralData.instance(mContext);
257268
}
269+
270+
private String createBackupZipNameWithTimestamp() {
271+
return mContext.getPackageName() + "_" + System.currentTimeMillis() + ".zip";
272+
}
258273
}

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/misc/BackupAndRestoreManager.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.content.pm.PackageManager;
5+
import android.os.Build.VERSION;
56
import android.os.Environment;
67
import android.os.Handler;
78

@@ -139,7 +140,7 @@ public void backupData() {
139140

140141
File currentBackup = getBackup();
141142

142-
if (currentBackup == null) {
143+
if (currentBackup == null || !hasStoragePermissions(mContext)) {
143144
Log.d(TAG, "Oops. Backup location not writable.");
144145
return;
145146
}
@@ -283,6 +284,11 @@ public String getBackupPath() {
283284

284285
public String getBackupRootPath() {
285286
// NOTE: Android 11+ only backup through the file manager (no shared dir)
287+
if (hasAccessOnlyToAppFolders() && VERSION.SDK_INT > 29) {
288+
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath()
289+
+ "/" + BackupAndRestoreHelper.BACKUP_FOLDER_NAME;
290+
}
291+
286292
return String.format("%s/data", getExternalStorageDirectory());
287293
}
288294

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/prefs/GeneralData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ private synchronized void restoreState() {
657657
//mIsRemapPageDownToSpeedEnabled = Helpers.parseBoolean(split, 67, false);
658658
mSearchExitShortcut = Helpers.parseInt(split, 68, EXIT_SINGLE_BACK);
659659
mGDriveBackupFreqDays = Helpers.parseInt(split, 69, -1);
660-
mLocalDriveBackupFreqDays = Helpers.parseInt(split, 70, -1);
660+
mLocalDriveBackupFreqDays = Helpers.parseInt(split, 70, 7);
661661
//mIsRemapFastForwardToSpeedToggleEnabled = Helpers.parseBoolean(split, 71, false);
662662
mIsRemapSToSpeedToggleEnabled = Helpers.parseBoolean(split, 72, true);
663663
}

0 commit comments

Comments
 (0)