don't broadcast scan progress updates on every inspect call

This commit is contained in:
Adrian Ulrich 2021-04-02 10:25:03 +02:00
parent 3b2449d000
commit 1147d94c98

View File

@ -78,9 +78,9 @@ public class MediaScanner implements Handler.Callback {
*/ */
private NotificationHelper mNotificationHelper; private NotificationHelper mNotificationHelper;
/** /**
* Timestamp in half-seconds since last notification * uptimeMillis ts at which we will dispatch the next scan update report
*/ */
private int mLastNotification; private long mNextProgressReportAt;
/** /**
* The id we are using for the scan notification * The id we are using for the scan notification
*/ */
@ -253,8 +253,16 @@ public class MediaScanner implements Handler.Callback {
if (changed && !mHandler.hasMessages(MSG_NOTIFY_CHANGE)) { if (changed && !mHandler.hasMessages(MSG_NOTIFY_CHANGE)) {
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_NOTIFY_CHANGE), 500); mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_NOTIFY_CHANGE), 500);
} }
MediaLibrary.notifyObserver(LibraryObserver.Type.SCAN_PROGRESS, LibraryObserver.Value.UNKNOWN, true);
updateNotification(true); // Unlike MSG_NOTIFY_CHANGE, we don't want the progress report to lag behind, but we also don't want
// to call it on EVERY file inspection; so we add our own delay which will not be influenced by
// the message queue size.
long now = SystemClock.uptimeMillis();
if (now >= mNextProgressReportAt) {
mNextProgressReportAt = now + 80;
MediaLibrary.notifyObserver(LibraryObserver.Type.SCAN_PROGRESS, LibraryObserver.Value.UNKNOWN, true);
updateNotification(true);
}
break; break;
} }
case RPC_READ_DIR: { case RPC_READ_DIR: {
@ -310,22 +318,19 @@ public class MediaScanner implements Handler.Callback {
MediaLibrary.ScanProgress progress = describeScanProgress(); MediaLibrary.ScanProgress progress = describeScanProgress();
if (visible) { if (visible) {
int nowTime = (int)(SystemClock.uptimeMillis() / 500); // We there are 5 drawables, pick one based on the 'uptime-seconds'.
if (nowTime != mLastNotification) { int tick = (int)(SystemClock.uptimeMillis() / 1000) % 5;
mLastNotification = nowTime; int icon = R.drawable.status_scan_0 + tick;
int icon = R.drawable.status_scan_0 + (mLastNotification % 5); String title = mContext.getResources().getString(R.string.media_library_scan_running);
String title = mContext.getResources().getString(R.string.media_library_scan_running); String content = progress.lastFile;
String content = progress.lastFile; Notification notification = mNotificationHelper.getNewBuilder(mContext)
.setProgress(progress.total, progress.seen, false)
Notification notification = mNotificationHelper.getNewBuilder(mContext) .setContentTitle(title)
.setProgress(progress.total, progress.seen, false) .setContentText(content)
.setContentTitle(title) .setSmallIcon(icon)
.setContentText(content) .setOngoing(true)
.setSmallIcon(icon) .getNotification(); // build() is API 16 :-/
.setOngoing(true) mNotificationHelper.notify(NOTIFICATION_ID, notification);
.getNotification(); // build() is API 16 :-/
mNotificationHelper.notify(NOTIFICATION_ID, notification);
}
if (!mWakeLock.isHeld()) if (!mWakeLock.isHeld())
mWakeLock.acquire(); mWakeLock.acquire();