media library prefs: do not poll.
It took a couple of years, but the Timer is finally history.
This commit is contained in:
parent
cf0aaf1a57
commit
5eb97409ec
app/src/main/java/ch/blinkenlights/android
@ -26,8 +26,9 @@ public class LibraryObserver {
|
||||
* by the receiver
|
||||
*/
|
||||
public enum Type {
|
||||
SONG, // Change affected song items.
|
||||
PLAYLIST, // Change affected playlists.
|
||||
SONG, // Change affected song items.
|
||||
PLAYLIST, // Change affected playlists.
|
||||
SCAN_PROGRESS, // Information about an ongoing scan.
|
||||
}
|
||||
/**
|
||||
* Special hint values
|
||||
|
@ -178,7 +178,7 @@ public class MediaScanner implements Handler.Callback {
|
||||
MediaLibrary.Preferences prefs = MediaLibrary.getPreferences(mContext);
|
||||
MediaScanPlan.Statistics stats = mScanPlan.getStatistics();
|
||||
|
||||
progress.isRunning = (stats.lastFile != null);
|
||||
progress.isRunning = mScanPlan.hasNextStep();
|
||||
progress.lastFile = stats.lastFile;
|
||||
progress.seen = stats.seen;
|
||||
progress.changed = stats.changed;
|
||||
@ -225,6 +225,7 @@ public class MediaScanner implements Handler.Callback {
|
||||
// for this scan.
|
||||
mHandler.removeMessages(MSG_NOTIFY_CHANGE);
|
||||
MediaLibrary.notifyObserver(LibraryObserver.Type.SONG, LibraryObserver.Value.UNKNOWN, false);
|
||||
MediaLibrary.notifyObserver(LibraryObserver.Type.SCAN_PROGRESS, LibraryObserver.Value.UNKNOWN, false);
|
||||
|
||||
updateNotification(false);
|
||||
break;
|
||||
@ -247,6 +248,7 @@ public class MediaScanner implements Handler.Callback {
|
||||
if (changed && !mHandler.hasMessages(MSG_NOTIFY_CHANGE)) {
|
||||
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_NOTIFY_CHANGE), 500);
|
||||
}
|
||||
MediaLibrary.notifyObserver(LibraryObserver.Type.SCAN_PROGRESS, LibraryObserver.Value.UNKNOWN, true);
|
||||
updateNotification(true);
|
||||
break;
|
||||
}
|
||||
@ -814,6 +816,13 @@ public class MediaScanner implements Handler.Callback {
|
||||
mStats.reset();
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the scan plan has a step to execute
|
||||
*/
|
||||
boolean hasNextStep() {
|
||||
return mSteps.size() > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1948,6 +1948,9 @@ public final class PlaybackService extends Service
|
||||
@Override
|
||||
public void onChange(LibraryObserver.Type type, long id, boolean ongoing)
|
||||
{
|
||||
if (type != LibraryObserver.Type.SONG && type != LibraryObserver.Type.PLAYLIST)
|
||||
return;
|
||||
|
||||
MediaUtils.onMediaChange();
|
||||
onMediaChange();
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2017-2018 Adrian Ulrich <adrian@blinkenlights.ch>
|
||||
* Copyright (C) 2017-2021 Adrian Ulrich <adrian@blinkenlights.ch>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -39,15 +39,7 @@ import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class PreferencesMediaLibrary extends Fragment implements View.OnClickListener
|
||||
{
|
||||
/**
|
||||
* The ugly timer which fires every 200ms
|
||||
*/
|
||||
private Timer mTimer;
|
||||
public class PreferencesMediaLibrary extends Fragment implements View.OnClickListener {
|
||||
/**
|
||||
* Our start button
|
||||
*/
|
||||
@ -113,6 +105,20 @@ public class PreferencesMediaLibrary extends Fragment implements View.OnClickLis
|
||||
*/
|
||||
private String mInitialMediaFolders;
|
||||
|
||||
private final LibraryObserver mLibraryObserver = new LibraryObserver() {
|
||||
@Override
|
||||
public void onChange(LibraryObserver.Type type, long id, boolean ongoing) {
|
||||
if (type != LibraryObserver.Type.SCAN_PROGRESS)
|
||||
return;
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateProgress();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.medialibrary_preferences, container, false);
|
||||
@ -149,18 +155,6 @@ public class PreferencesMediaLibrary extends Fragment implements View.OnClickLis
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
mTimer = new Timer();
|
||||
// Yep: its as ugly as it seems: we are POLLING
|
||||
// the database.
|
||||
mTimer.scheduleAtFixedRate((new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
getActivity().runOnUiThread(new Runnable(){
|
||||
public void run() {
|
||||
updateProgress();
|
||||
}
|
||||
});
|
||||
}}), 0, 200);
|
||||
|
||||
// We got freshly created and user didn't have a chance
|
||||
// to edit anything: remember this state
|
||||
@ -176,17 +170,15 @@ public class PreferencesMediaLibrary extends Fragment implements View.OnClickLis
|
||||
mFullScanPending = true;
|
||||
}
|
||||
|
||||
MediaLibrary.registerLibraryObserver(mLibraryObserver);
|
||||
updateProgress();
|
||||
updatePreferences(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
if (mTimer != null) {
|
||||
mTimer.cancel();
|
||||
mTimer = null;
|
||||
}
|
||||
|
||||
MediaLibrary.unregisterLibraryObserver(mLibraryObserver);
|
||||
// User exited this view -> scan if needed
|
||||
if (mFullScanPending && !mIsEditingDirectories) {
|
||||
MediaLibrary.startLibraryScan(getActivity(), true, true);
|
||||
@ -216,6 +208,7 @@ public class PreferencesMediaLibrary extends Fragment implements View.OnClickLis
|
||||
|
||||
private static final int MENU_DUMP_DB = 1;
|
||||
private static final int MENU_FORCE_M3U_IMPORT = 2;
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
@ -373,7 +366,6 @@ public class PreferencesMediaLibrary extends Fragment implements View.OnClickLis
|
||||
*/
|
||||
public void startButtonPressed(View view) {
|
||||
MediaLibrary.startLibraryScan(getActivity(), mFullScanCheck.isChecked(), mDropDbCheck.isChecked());
|
||||
updateProgress();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -383,7 +375,6 @@ public class PreferencesMediaLibrary extends Fragment implements View.OnClickLis
|
||||
*/
|
||||
public void cancelButtonPressed(View view) {
|
||||
MediaLibrary.abortLibraryScan(getActivity());
|
||||
updateProgress();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -134,7 +134,7 @@ public class ScheduledLibraryUpdate extends JobService {
|
||||
private final LibraryObserver mObserver = new LibraryObserver() {
|
||||
@Override
|
||||
public void onChange(LibraryObserver.Type type, long id, boolean ongoing) {
|
||||
if (!ongoing) {
|
||||
if (type == LibraryObserver.Type.SONG && !ongoing) {
|
||||
jobFinished(mJobParams, false);
|
||||
finalizeScan();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user