diff --git a/app/src/main/java/us/koller/cameraroll/data/fileOperations/FileOperation.java b/app/src/main/java/us/koller/cameraroll/data/fileOperations/FileOperation.java index badabd12..6f5557ed 100644 --- a/app/src/main/java/us/koller/cameraroll/data/fileOperations/FileOperation.java +++ b/app/src/main/java/us/koller/cameraroll/data/fileOperations/FileOperation.java @@ -47,6 +47,7 @@ import us.koller.cameraroll.util.StorageUtil; public abstract class FileOperation extends IntentService implements Parcelable { + protected static final String TAG = Rename.class.getSimpleName(); private static final int NOTIFICATION_ID = 6; @@ -69,7 +70,7 @@ public abstract class FileOperation extends IntentService implements Parcelable public static final String REMOVABLE_STORAGE_TREE_URI = "REMOVABLE_STORAGE_TREE_URI"; private NotificationCompat.Builder notifBuilder; - + private boolean sentFailedBroadcast; private ArrayList pathsToScan; public FileOperation() { @@ -80,6 +81,7 @@ public FileOperation() { @Override protected void onHandleIntent(Intent workIntent) { + sentFailedBroadcast = false; notifBuilder = createNotificationBuilder(); notifBuilder.setProgress(1, 0, false); Notification notification = notifBuilder.build(); @@ -102,7 +104,6 @@ protected void onHandleIntent(Intent workIntent) { public void onAllPathsScanned() { sendDoneBroadcast(); stopForeground(true); - } }); } else { @@ -176,10 +177,13 @@ public void sendDoneBroadcast(boolean showToastDone) { ContentObserver.selfChange = false; if (showToastDone) showToast(getString(R.string.done)); Intent intent = getDoneIntent(); + if (sentFailedBroadcast) + intent.putExtra(FAILED, true); sendLocalBroadcast(intent); } public void sendFailedBroadcast(Intent workIntent, String path) { + sentFailedBroadcast = true; Intent intent = new Intent(FAILED); intent.putExtra(FILES, path); intent.putExtra(WORK_INTENT, workIntent); @@ -483,7 +487,6 @@ public void run() { manager.notify(NOTIFICATION_ID, notifBuilder.build()); } } - } if (manager != null) { diff --git a/app/src/main/java/us/koller/cameraroll/data/fileOperations/Rename.java b/app/src/main/java/us/koller/cameraroll/data/fileOperations/Rename.java index 8796410a..10487a4a 100644 --- a/app/src/main/java/us/koller/cameraroll/data/fileOperations/Rename.java +++ b/app/src/main/java/us/koller/cameraroll/data/fileOperations/Rename.java @@ -11,6 +11,7 @@ import android.widget.EditText; import android.widget.Toast; +import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.documentfile.provider.DocumentFile; @@ -23,6 +24,7 @@ import us.koller.cameraroll.util.StorageUtil; public class Rename extends FileOperation { + protected static final String TAG = Rename.class.getSimpleName(); public static final String NEW_FILE_PATH = "NEW_FILE_PATH"; @@ -59,13 +61,12 @@ public void execute(Intent workIntent) { if (!result) { sendFailedBroadcast(workIntent, file.getPath()); } else { - runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(getApplicationContext(), getString(R.string.successfully_renamed_file), - Toast.LENGTH_SHORT).show(); - } - }); + runOnUiThread(() -> + Toast.makeText( + getApplicationContext(), getString(R.string.successfully_renamed_file), + Toast.LENGTH_SHORT + ).show() + ); } } } @@ -139,7 +140,7 @@ public static class Util { public static AlertDialog getRenameDialog(final BaseActivity activity, final File_POJO file, - final BroadcastReceiver broadcastReceiver) { + @Nullable final BroadcastReceiver broadcastReceiver) { @SuppressLint("InflateParams") View dialogLayout = activity.getLayoutInflater() @@ -167,7 +168,7 @@ public void onClick(DialogInterface dialogInterface, int i) { final File_POJO[] files = new File_POJO[]{file}; Intent intent = FileOperation.getDefaultIntent(activity, FileOperation.RENAME, files) - .putExtra(FileOperation.NEW_FILE_NAME, newFileName); + .putExtra(FileOperation.NEW_FILE_NAME, newFileName); activity.startService(intent); } }) @@ -178,4 +179,9 @@ public void onClick(DialogInterface dialogInterface, int i) { return dialog; } } + + @Override + public void sendDoneBroadcast() { + sendDoneBroadcast(false); + } } diff --git a/app/src/main/java/us/koller/cameraroll/data/provider/MediaProvider.java b/app/src/main/java/us/koller/cameraroll/data/provider/MediaProvider.java index 269755db..31186362 100644 --- a/app/src/main/java/us/koller/cameraroll/data/provider/MediaProvider.java +++ b/app/src/main/java/us/koller/cameraroll/data/provider/MediaProvider.java @@ -6,6 +6,7 @@ import android.content.pm.PackageManager; import android.os.Build; +import androidx.annotation.Nullable; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; @@ -38,7 +39,7 @@ public abstract static class OnMediaLoadedCallback implements Provider.Callback } public interface OnAlbumLoadedCallback { - void onAlbumLoaded(Album album); + void onAlbumLoaded(@Nullable Album album); } public MediaProvider(Context context) { diff --git a/app/src/main/java/us/koller/cameraroll/ui/AlbumActivity.java b/app/src/main/java/us/koller/cameraroll/ui/AlbumActivity.java index 336b044e..16a95df8 100644 --- a/app/src/main/java/us/koller/cameraroll/ui/AlbumActivity.java +++ b/app/src/main/java/us/koller/cameraroll/ui/AlbumActivity.java @@ -29,6 +29,7 @@ import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.view.WindowInsets; +import android.widget.Toast; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; @@ -556,37 +557,7 @@ public boolean onOptionsItemSelected(MenuItem item) { Rename.Util.getRenameDialog(this, file, new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - final Activity activity = AlbumActivity.this; - final String newFilePath = intent.getStringExtra(Rename.NEW_FILE_PATH); - getIntent().putExtra(ALBUM_PATH, newFilePath); - - boolean hiddenFolders = Settings.getInstance(activity).getHiddenFolders(); - new MediaProvider(activity).loadAlbums(activity, hiddenFolders, - new MediaProvider.OnMediaLoadedCallback() { - @Override - public void onMediaLoaded(ArrayList albums) { - //reload activity - MediaProvider.getAlbum(activity, newFilePath, - new MediaProvider.OnAlbumLoadedCallback() { - @Override - public void onAlbumLoaded(Album album) { - AlbumActivity.this.album = album; - AlbumActivity.this.onAlbumLoaded(null); - } - }); - } - - @Override - public void timeout() { - supportFinishAfterTransition(); - } - - @Override - public void needPermission() { - supportFinishAfterTransition(); - } - }); } }).show(); break; @@ -1126,7 +1097,7 @@ public void onReceive(Context context, Intent intent) { } break; } - case FileOperation.COPY: + case FileOperation.COPY: { //todo animate String albumPath = getIntent().getStringExtra(ALBUM_PATH); MediaProvider.getAlbum(AlbumActivity.this, albumPath, @@ -1153,14 +1124,53 @@ public void onAlbumLoaded(Album newAlbum) { } }, true); break; + } + case FileOperation.RENAME: { + if (intent.getBooleanExtra(FileOperation.FAILED, false)) { + Toast.makeText(context, R.string.error, Toast.LENGTH_SHORT).show(); + break; + } + final Activity activity = AlbumActivity.this; + + final String newFilePath = intent.getStringExtra(Rename.NEW_FILE_PATH); + getIntent().putExtra(ALBUM_PATH, newFilePath); + + boolean hiddenFolders = Settings.getInstance(activity).getHiddenFolders(); + new MediaProvider(activity).loadAlbums(activity, hiddenFolders, + new MediaProvider.OnMediaLoadedCallback() { + @Override + public void onMediaLoaded(ArrayList albums) { + //reload activity + MediaProvider.getAlbum(activity, newFilePath, + new MediaProvider.OnAlbumLoadedCallback() { + @Override + public void onAlbumLoaded(@Nullable Album album) { + AlbumActivity.this.album = album; + AlbumActivity.this.onAlbumLoaded(null); + } + }); + } + + @Override + public void timeout() { + supportFinishAfterTransition(); + } + + @Override + public void needPermission() { + supportFinishAfterTransition(); + } + }); + } } break; - case ALBUM_ITEM_REMOVED: + case ALBUM_ITEM_REMOVED: { String path = intent.getStringExtra(ItemActivity.ALBUM_ITEM_PATH); removeAlbumItem(path); break; + } case ALBUM_ITEM_RENAMED: - case DATA_CHANGED: + case DATA_CHANGED: { String albumPath = getIntent().getStringExtra(ALBUM_PATH); MediaProvider.getAlbum(AlbumActivity.this, albumPath, new MediaProvider.OnAlbumLoadedCallback() { @@ -1171,6 +1181,7 @@ public void onAlbumLoaded(Album album) { } }); break; + } default: break; }