Skip to content

Commit 3d4362d

Browse files
committed
BackupAndRestore: improve timestamp usage
1 parent 0890d02 commit 3d4362d

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

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

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,20 @@ public void exportAppMediaFolder() {
4444

4545
String oldBackupZipName = getGeneralData().getBackupZipName();
4646
if (oldBackupZipName == null || !oldBackupZipName.endsWith(".zip")) {
47-
oldBackupZipName = createBackupZipNameWithTimestamp();
47+
oldBackupZipName = getBackupZipNameWithTimestamp();
4848
getGeneralData().setBackupZipName(oldBackupZipName);
4949
}
5050

5151
MediaStoreFile file = new MediaStoreFile(mContext, oldBackupZipName, BACKUP_FOLDER_NAME);
5252
if (!file.isWritable()) {
53-
oldBackupZipName = createBackupZipNameWithTimestamp();
53+
oldBackupZipName = getBackupZipNameWithTimestamp();
54+
getGeneralData().setBackupZipName(oldBackupZipName);
55+
file = new MediaStoreFile(mContext, oldBackupZipName, BACKUP_FOLDER_NAME);
56+
}
57+
58+
if (!file.isWritable()) {
59+
deleteTimeStamp(); // User copied full old media directory (with the old timestamp)
60+
oldBackupZipName = getBackupZipNameWithTimestamp();
5461
getGeneralData().setBackupZipName(oldBackupZipName);
5562
file = new MediaStoreFile(mContext, oldBackupZipName, BACKUP_FOLDER_NAME);
5663
}
@@ -191,7 +198,7 @@ private void unpackTempZip(Uri zipUri, Runnable onSuccess, Runnable onError) {
191198
// Copy ZIP from URI to the temporary file
192199
String backupZipName = getGeneralData().getBackupZipName();
193200
if (backupZipName == null || !backupZipName.endsWith(".zip")) {
194-
backupZipName = createBackupZipNameWithTimestamp();
201+
backupZipName = getBackupZipNameWithTimestamp();
195202
getGeneralData().setBackupZipName(backupZipName);
196203
}
197204
File tempZip = new File(mediaDir, backupZipName);
@@ -269,7 +276,31 @@ private GeneralData getGeneralData() {
269276
return GeneralData.instance(mContext);
270277
}
271278

272-
private String createBackupZipNameWithTimestamp() {
273-
return mContext.getPackageName() + "_" + System.currentTimeMillis() + ".zip";
279+
private String getBackupZipNameWithTimestamp() {
280+
return mContext.getPackageName() + "_" + getTimeStamp() + ".zip";
281+
}
282+
283+
private String getTimeStamp() {
284+
File timestampFile = getTimestampFile();
285+
if (timestampFile.exists()) {
286+
return FileHelpers.getFileContents(timestampFile);
287+
}
288+
289+
String timestamp = String.valueOf(System.currentTimeMillis());
290+
FileHelpers.stringToFile(timestamp, timestampFile);
291+
return timestamp;
292+
}
293+
294+
private void deleteTimeStamp() {
295+
File timestampFile = getTimestampFile();
296+
if (timestampFile.exists()) {
297+
timestampFile.delete();
298+
}
299+
}
300+
301+
private File getTimestampFile() {
302+
File mediaDir = FileHelpers.getExternalMediaDirectory(mContext);
303+
File timestampFile = new File(mediaDir, "timestamp.txt");
304+
return timestampFile;
274305
}
275306
}

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/misc/MediaStoreFile.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.liskovsoft.smartyoutubetv2.common.misc
22

33
import android.content.ContentValues
44
import android.content.Context
5+
import android.database.sqlite.SQLiteConstraintException
56
import android.net.Uri
67
import android.os.Environment
78
import android.provider.MediaStore
@@ -143,10 +144,16 @@ internal class MediaStoreFile @JvmOverloads constructor(
143144
put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath())
144145
}
145146

146-
val uri = resolver.insert(
147-
collectionUri(),
148-
values
149-
)
147+
val uri = try {
148+
resolver.insert(
149+
collectionUri(),
150+
values
151+
)
152+
} catch (e: SQLiteConstraintException) {
153+
// The file already exists (by me or someone else)
154+
// android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: files._data (code 2067 SQLITE_CONSTRAINT_UNIQUE)
155+
return false
156+
}
150157

151158
cachedUri = uri
152159
return uri != null

0 commit comments

Comments
 (0)