diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/fm/FmFragment.java b/app/src/main/java/io/github/muntashirakon/AppManager/fm/FmFragment.java index 79b0a6ae232..5c26e4a31dd 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/fm/FmFragment.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/fm/FmFragment.java @@ -46,6 +46,7 @@ import com.google.android.material.button.MaterialButton; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.progressindicator.LinearProgressIndicator; import com.leinardi.android.speeddial.SpeedDialActionItem; import com.leinardi.android.speeddial.SpeedDialView; @@ -716,6 +717,7 @@ private void startBatchDeletion(@NonNull List paths) { // TODO: 27/6/23 Ideally, these should be done in a bound service AtomicReference> deletionThread = new AtomicReference<>(); View view = View.inflate(requireContext(), R.layout.dialog_progress, null); + LinearProgressIndicator progress = view.findViewById(R.id.progress_linear); TextView label = view.findViewById(android.R.id.text1); TextView counter = view.findViewById(android.R.id.text2); counter.setText(String.format(Locale.getDefault(), "%d/%d", 0, paths.size())); @@ -730,10 +732,17 @@ private void startBatchDeletion(@NonNull List paths) { .setCancelable(false) .show(); deletionThread.set(ThreadUtils.postOnBackgroundThread(() -> { + WeakReference progressRef = new WeakReference<>(progress); WeakReference labelRef = new WeakReference<>(label); WeakReference counterRef = new WeakReference<>(counter); WeakReference dialogRef = new WeakReference<>(dialog); try { + LinearProgressIndicator p = progressRef.get(); + if (p != null) { + p.setMax(paths.size()); + p.setProgress(0); + p.setIndeterminate(false); + } int i = 1; for (Path path : paths) { // Update label @@ -751,11 +760,15 @@ private void startBatchDeletion(@NonNull List paths) { } path.delete(); TextView c = counterRef.get(); - if (c != null) { - int finalI = i; - ThreadUtils.postOnMainThread(() -> - c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, paths.size()))); - } + int finalI = i; + ThreadUtils.postOnMainThread(() -> { + if (c != null) { + c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, paths.size())); + } + if (p != null) { + p.setProgress(finalI); + } + }); ++i; if (ThreadUtils.isInterrupted()) { break; @@ -777,6 +790,7 @@ private void startBatchDeletion(@NonNull List paths) { private void startBatchRenaming(List paths, String prefix, @Nullable String extension) { AtomicReference> renameThread = new AtomicReference<>(); View view = View.inflate(requireContext(), R.layout.dialog_progress, null); + LinearProgressIndicator progress = view.findViewById(R.id.progress_linear); TextView label = view.findViewById(android.R.id.text1); TextView counter = view.findViewById(android.R.id.text2); counter.setText(String.format(Locale.getDefault(), "%d/%d", 0, paths.size())); @@ -791,10 +805,17 @@ private void startBatchRenaming(List paths, String prefix, @Nullable Strin .setCancelable(false) .show(); renameThread.set(ThreadUtils.postOnBackgroundThread(() -> { + WeakReference progressRef = new WeakReference<>(progress); WeakReference labelRef = new WeakReference<>(label); WeakReference counterRef = new WeakReference<>(counter); WeakReference dialogRef = new WeakReference<>(dialog); try { + LinearProgressIndicator p = progressRef.get(); + if (p != null) { + p.setMax(paths.size()); + p.setProgress(0); + p.setIndeterminate(false); + } int i = 1; for (Path path : paths) { // Update label @@ -816,11 +837,15 @@ private void startBatchRenaming(List paths, String prefix, @Nullable Strin path.renameTo(displayName); } TextView c = counterRef.get(); - if (c != null) { - int finalI = i; - ThreadUtils.postOnMainThread(() -> - c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, paths.size()))); - } + int finalI = i; + ThreadUtils.postOnMainThread(() -> { + if (c != null) { + c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, paths.size())); + } + if (p != null) { + p.setProgress(finalI); + } + }); ++i; if (ThreadUtils.isInterrupted()) { break; @@ -846,6 +871,7 @@ private void startBatchPaste(@NonNull FmTasks.FmTask task) { } AtomicReference> pasteThread = new AtomicReference<>(); View view = View.inflate(requireContext(), R.layout.dialog_progress, null); + LinearProgressIndicator progress = view.findViewById(R.id.progress_linear); TextView label = view.findViewById(android.R.id.text1); TextView counter = view.findViewById(android.R.id.text2); counter.setText(String.format(Locale.getDefault(), "%d/%d", 0, task.files.size())); @@ -860,11 +886,18 @@ private void startBatchPaste(@NonNull FmTasks.FmTask task) { .setCancelable(false) .show(); pasteThread.set(ThreadUtils.postOnBackgroundThread(() -> { + WeakReference progressRef = new WeakReference<>(progress); WeakReference labelRef = new WeakReference<>(label); WeakReference counterRef = new WeakReference<>(counter); WeakReference dialogRef = new WeakReference<>(dialog); Path targetPath = Paths.get(uri); try { + LinearProgressIndicator p = progressRef.get(); + if (p != null) { + p.setMax(task.files.size()); + p.setProgress(0); + p.setIndeterminate(false); + } int i = 1; for (Path sourcePath : task.files) { // Update label @@ -901,11 +934,15 @@ private void startBatchPaste(@NonNull FmTasks.FmTask task) { } } TextView c = counterRef.get(); - if (c != null) { - int finalI = i; - ThreadUtils.postOnMainThread(() -> - c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, task.files.size()))); - } + int finalI = i; + ThreadUtils.postOnMainThread(() -> { + if (c != null) { + c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, task.files.size())); + } + if (p != null) { + p.setProgress(finalI); + } + }); ++i; if (ThreadUtils.isInterrupted()) { break;