2222import java .io .OutputStream ;
2323
2424public 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}
0 commit comments