Fix scaning and updateing error
This commit is contained in:
parent
9dc2da66e1
commit
52dc1d9f22
11 changed files with 215 additions and 260 deletions
|
@ -19,49 +19,54 @@ import android.app.Activity;
|
|||
import android.media.MediaScannerConnection;
|
||||
import android.net.Uri;
|
||||
import android.widget.Toast;
|
||||
import code.name.monkey.retromusic.R;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.List;
|
||||
|
||||
/** @author Karim Abou Zeid (kabouzeid) */
|
||||
import code.name.monkey.retromusic.R;
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class UpdateToastMediaScannerCompletionListener
|
||||
implements MediaScannerConnection.OnScanCompletedListener {
|
||||
implements MediaScannerConnection.OnScanCompletedListener {
|
||||
|
||||
private final WeakReference<Activity> activityWeakReference;
|
||||
private final WeakReference<Activity> activityWeakReference;
|
||||
|
||||
private final String couldNotScanFiles;
|
||||
private final String scannedFiles;
|
||||
private final String[] toBeScanned;
|
||||
private int failed = 0;
|
||||
private int scanned = 0;
|
||||
private Toast toast;
|
||||
private final String couldNotScanFiles;
|
||||
private final String scannedFiles;
|
||||
private final List<String> toBeScanned;
|
||||
private int failed = 0;
|
||||
private int scanned = 0;
|
||||
private Toast toast;
|
||||
|
||||
@SuppressLint("ShowToast")
|
||||
public UpdateToastMediaScannerCompletionListener(Activity activity, String[] toBeScanned) {
|
||||
this.toBeScanned = toBeScanned;
|
||||
scannedFiles = activity.getString(R.string.scanned_files);
|
||||
couldNotScanFiles = activity.getString(R.string.could_not_scan_files);
|
||||
toast = Toast.makeText(activity.getApplicationContext(), "", Toast.LENGTH_SHORT);
|
||||
activityWeakReference = new WeakReference<>(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanCompleted(final String path, final Uri uri) {
|
||||
Activity activity = activityWeakReference.get();
|
||||
if (activity != null) {
|
||||
activity.runOnUiThread(
|
||||
() -> {
|
||||
if (uri == null) {
|
||||
failed++;
|
||||
} else {
|
||||
scanned++;
|
||||
}
|
||||
String text =
|
||||
" "
|
||||
+ String.format(scannedFiles, scanned, toBeScanned.length)
|
||||
+ (failed > 0 ? " " + String.format(couldNotScanFiles, failed) : "");
|
||||
toast.setText(text);
|
||||
toast.show();
|
||||
});
|
||||
@SuppressLint("ShowToast")
|
||||
public UpdateToastMediaScannerCompletionListener(Activity activity, List<String> toBeScanned) {
|
||||
this.toBeScanned = toBeScanned;
|
||||
scannedFiles = activity.getString(R.string.scanned_files);
|
||||
couldNotScanFiles = activity.getString(R.string.could_not_scan_files);
|
||||
toast = Toast.makeText(activity.getApplicationContext(), "", Toast.LENGTH_SHORT);
|
||||
activityWeakReference = new WeakReference<>(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanCompleted(final String path, final Uri uri) {
|
||||
Activity activity = activityWeakReference.get();
|
||||
if (activity != null) {
|
||||
activity.runOnUiThread(
|
||||
() -> {
|
||||
if (uri == null) {
|
||||
failed++;
|
||||
} else {
|
||||
scanned++;
|
||||
}
|
||||
String text =
|
||||
" "
|
||||
+ String.format(scannedFiles, scanned, toBeScanned.size())
|
||||
+ (failed > 0 ? " " + String.format(couldNotScanFiles, failed) : "");
|
||||
toast.setText(text);
|
||||
toast.show();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue