PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/misc/UpdateToastMediaScannerComp...

73 lines
2.6 KiB
Java
Raw Normal View History

2019-03-03 09:20:15 +00:00
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
2018-07-27 13:07:33 +00:00
package code.name.monkey.retromusic.misc;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.widget.Toast;
2020-10-12 17:22:53 +00:00
2020-10-06 08:46:04 +00:00
import java.lang.ref.WeakReference;
2020-10-12 17:22:53 +00:00
import java.util.List;
import code.name.monkey.retromusic.R;
2020-10-12 17:22:53 +00:00
/**
* @author Karim Abou Zeid (kabouzeid)
*/
2020-10-06 08:46:04 +00:00
public class UpdateToastMediaScannerCompletionListener
2020-10-12 17:22:53 +00:00
implements MediaScannerConnection.OnScanCompletedListener {
2020-01-06 05:34:09 +00:00
2020-10-12 17:22:53 +00:00
private final WeakReference<Activity> activityWeakReference;
2020-01-06 05:34:09 +00:00
2020-10-12 17:22:53 +00:00
private final String couldNotScanFiles;
private final String scannedFiles;
private final List<String> toBeScanned;
private int failed = 0;
private int scanned = 0;
2021-10-11 06:17:55 +00:00
private final Toast toast;
2018-07-27 13:07:33 +00:00
2020-10-12 17:22:53 +00:00
@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);
}
2018-07-27 13:07:33 +00:00
2020-10-12 17:22:53 +00:00
@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();
});
}
2018-07-27 13:07:33 +00:00
}
}