30
30
import android .view .View ;
31
31
import android .view .WindowManager ;
32
32
33
+ import androidx .activity .result .ActivityResult ;
33
34
import androidx .annotation .NonNull ;
34
35
import androidx .annotation .Nullable ;
35
36
import androidx .appcompat .app .ActionBar ;
36
- import androidx .appcompat .app .AppCompatActivity ;
37
37
import androidx .documentfile .provider .DocumentFile ;
38
38
import androidx .lifecycle .ViewModelProvider ;
39
39
import androidx .recyclerview .widget .GridLayoutManager ;
57
57
import se .arctosoft .vault .utils .Toaster ;
58
58
import se .arctosoft .vault .viewmodel .GalleryViewModel ;
59
59
60
- public class GalleryActivity extends AppCompatActivity {
60
+ public class GalleryActivity extends BaseActivity {
61
61
private static final String TAG = "GalleryActivity" ;
62
- private static final int REQUEST_ADD_DIRECTORY = 1 ;
63
- private static final int REQUEST_IMPORT_IMAGES = 3 ;
64
- private static final int REQUEST_IMPORT_VIDEOS = 4 ;
65
62
66
63
private static final Object LOCK = new Object ();
67
64
@@ -178,7 +175,7 @@ private void onSelectionModeChanged(boolean inSelectionMode) {
178
175
}
179
176
180
177
private void setClickListeners () {
181
- binding .btnAddFolder .setOnClickListener (v -> startActivityForResult (new Intent (Intent .ACTION_OPEN_DOCUMENT_TREE ), REQUEST_ADD_DIRECTORY ));
178
+ binding .btnAddFolder .setOnClickListener (v -> activityLauncher . launch (new Intent (Intent .ACTION_OPEN_DOCUMENT_TREE ), this :: addFolder ));
182
179
binding .btnImportFiles .setOnClickListener (v -> showImportOverlay (true ));
183
180
binding .btnRemoveFolder .setOnClickListener (v -> Dialogs .showConfirmationDialog (this , getString (R .string .dialog_remove_folder_title ),
184
181
getResources ().getQuantityString (R .plurals .dialog_remove_folder_message , galleryGridAdapter .getSelectedFiles ().size ()),
@@ -199,16 +196,61 @@ private void setClickListeners() {
199
196
galleryGridAdapter .onSelectionModeChanged (false );
200
197
}));
201
198
binding .btnImportImages .setOnClickListener (v -> {
202
- FileStuff .pickImageFiles (this , REQUEST_IMPORT_IMAGES );
199
+ FileStuff .pickImageFiles (activityLauncher , result -> onImportImagesOrVideos ( result . getData ()) );
203
200
showImportOverlay (false );
204
201
});
205
202
binding .btnImportVideos .setOnClickListener (v -> {
206
- FileStuff .pickVideoFiles (this , REQUEST_IMPORT_VIDEOS );
203
+ FileStuff .pickVideoFiles (activityLauncher , result -> onImportImagesOrVideos ( result . getData ()) );
207
204
showImportOverlay (false );
208
205
});
209
206
binding .importChooseOverlay .setOnClickListener (v -> showImportOverlay (false ));
210
207
}
211
208
209
+ private void onImportImagesOrVideos (@ Nullable Intent data ) {
210
+ if (data != null ) {
211
+ List <DocumentFile > documentFiles = FileStuff .getDocumentsFromDirectoryResult (this , data );
212
+ if (!documentFiles .isEmpty ()) {
213
+ importFiles (documentFiles );
214
+ }
215
+ }
216
+ }
217
+
218
+ private void addFolder (ActivityResult result ) {
219
+ if (result .getResultCode () == Activity .RESULT_OK ) {
220
+ Intent data = result .getData ();
221
+ if (data != null ) {
222
+ Uri uri = data .getData ();
223
+ DocumentFile documentFile = DocumentFile .fromTreeUri (this , uri );
224
+ getContentResolver ().takePersistableUriPermission (uri , Intent .FLAG_GRANT_READ_URI_PERMISSION | Intent .FLAG_GRANT_WRITE_URI_PERMISSION );
225
+ settings .addGalleryDirectory (documentFile .getUri (), new IOnDirectoryAdded () {
226
+ @ Override
227
+ public void onAddedAsRoot () {
228
+ Toaster .getInstance (GalleryActivity .this ).showLong (getString (R .string .gallery_added_folder , FileStuff .getFilenameWithPathFromUri (uri )));
229
+ addDirectory (documentFile .getUri ());
230
+ }
231
+
232
+ @ Override
233
+ public void onAddedAsChildOf (@ NonNull Uri parentUri ) {
234
+ Toaster .getInstance (GalleryActivity .this ).showLong (getString (R .string .gallery_added_folder_child , FileStuff .getFilenameWithPathFromUri (uri ), FileStuff .getFilenameWithPathFromUri (parentUri )));
235
+ }
236
+
237
+ @ Override
238
+ public void onAlreadyExists (boolean isRootDir ) {
239
+ Toaster .getInstance (GalleryActivity .this ).showLong (getString (R .string .gallery_added_folder_duplicate , FileStuff .getFilenameWithPathFromUri (uri )));
240
+ if (isRootDir ) {
241
+ findFolders ();
242
+ }
243
+ }
244
+ });
245
+ if (viewModel .getFilesToAdd () != null ) {
246
+ importFiles (viewModel .getFilesToAdd ());
247
+ }
248
+ }
249
+ } else if (result .getResultCode () == Activity .RESULT_CANCELED ) {
250
+ viewModel .setFilesToAdd (null );
251
+ }
252
+ }
253
+
212
254
private void showImportOverlay (boolean show ) {
213
255
binding .cLImportChoose .setVisibility (show ? View .VISIBLE : View .GONE );
214
256
}
@@ -254,52 +296,6 @@ private void findFolders() {
254
296
}).start ();
255
297
}
256
298
257
- @ Override
258
- protected void onActivityResult (int requestCode , int resultCode , @ Nullable Intent data ) {
259
- super .onActivityResult (requestCode , resultCode , data );
260
- if (requestCode == REQUEST_ADD_DIRECTORY ) {
261
- if (resultCode == Activity .RESULT_OK ) {
262
- if (data != null ) {
263
- Uri uri = data .getData ();
264
- DocumentFile documentFile = DocumentFile .fromTreeUri (this , uri );
265
- getContentResolver ().takePersistableUriPermission (uri , Intent .FLAG_GRANT_READ_URI_PERMISSION | Intent .FLAG_GRANT_WRITE_URI_PERMISSION );
266
- settings .addGalleryDirectory (documentFile .getUri (), new IOnDirectoryAdded () {
267
- @ Override
268
- public void onAddedAsRoot () {
269
- Toaster .getInstance (GalleryActivity .this ).showLong (getString (R .string .gallery_added_folder , FileStuff .getFilenameWithPathFromUri (uri )));
270
- addDirectory (documentFile .getUri ());
271
- }
272
-
273
- @ Override
274
- public void onAddedAsChildOf (@ NonNull Uri parentUri ) {
275
- Toaster .getInstance (GalleryActivity .this ).showLong (getString (R .string .gallery_added_folder_child , FileStuff .getFilenameWithPathFromUri (uri ), FileStuff .getFilenameWithPathFromUri (parentUri )));
276
- }
277
-
278
- @ Override
279
- public void onAlreadyExists (boolean isRootDir ) {
280
- Toaster .getInstance (GalleryActivity .this ).showLong (getString (R .string .gallery_added_folder_duplicate , FileStuff .getFilenameWithPathFromUri (uri )));
281
- if (isRootDir ) {
282
- findFolders ();
283
- }
284
- }
285
- });
286
- if (viewModel .getFilesToAdd () != null ) {
287
- importFiles (viewModel .getFilesToAdd ());
288
- }
289
- }
290
- } else if (resultCode == Activity .RESULT_CANCELED ) {
291
- viewModel .setFilesToAdd (null );
292
- }
293
- } else if ((requestCode == REQUEST_IMPORT_IMAGES || requestCode == REQUEST_IMPORT_VIDEOS ) && resultCode == Activity .RESULT_OK ) {
294
- if (data != null ) {
295
- List <DocumentFile > documentFiles = FileStuff .getDocumentsFromDirectoryResult (this , data );
296
- if (!documentFiles .isEmpty ()) {
297
- importFiles (documentFiles );
298
- }
299
- }
300
- }
301
- }
302
-
303
299
private void importFiles (List <DocumentFile > documentFiles ) {
304
300
Dialogs .showImportGalleryChooseDestinationDialog (this , settings , documentFiles .size (), new Dialogs .IOnDirectorySelected () {
305
301
@ Override
0 commit comments