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
@ -26,8 +26,9 @@ public class LibraryObserver {
|
|||||||
* by the receiver
|
* by the receiver
|
||||||
*/
|
*/
|
||||||
public enum Type {
|
public enum Type {
|
||||||
SONG, // Change affected song items.
|
SONG, // Change affected song items.
|
||||||
PLAYLIST, // Change affected playlists.
|
PLAYLIST, // Change affected playlists.
|
||||||
|
SCAN_PROGRESS, // Information about an ongoing scan.
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Special hint values
|
* Special hint values
|
||||||
|
@ -178,7 +178,7 @@ public class MediaScanner implements Handler.Callback {
|
|||||||
MediaLibrary.Preferences prefs = MediaLibrary.getPreferences(mContext);
|
MediaLibrary.Preferences prefs = MediaLibrary.getPreferences(mContext);
|
||||||
MediaScanPlan.Statistics stats = mScanPlan.getStatistics();
|
MediaScanPlan.Statistics stats = mScanPlan.getStatistics();
|
||||||
|
|
||||||
progress.isRunning = (stats.lastFile != null);
|
progress.isRunning = mScanPlan.hasNextStep();
|
||||||
progress.lastFile = stats.lastFile;
|
progress.lastFile = stats.lastFile;
|
||||||
progress.seen = stats.seen;
|
progress.seen = stats.seen;
|
||||||
progress.changed = stats.changed;
|
progress.changed = stats.changed;
|
||||||
@ -225,6 +225,7 @@ public class MediaScanner implements Handler.Callback {
|
|||||||
// for this scan.
|
// for this scan.
|
||||||
mHandler.removeMessages(MSG_NOTIFY_CHANGE);
|
mHandler.removeMessages(MSG_NOTIFY_CHANGE);
|
||||||
MediaLibrary.notifyObserver(LibraryObserver.Type.SONG, LibraryObserver.Value.UNKNOWN, false);
|
MediaLibrary.notifyObserver(LibraryObserver.Type.SONG, LibraryObserver.Value.UNKNOWN, false);
|
||||||
|
MediaLibrary.notifyObserver(LibraryObserver.Type.SCAN_PROGRESS, LibraryObserver.Value.UNKNOWN, false);
|
||||||
|
|
||||||
updateNotification(false);
|
updateNotification(false);
|
||||||
break;
|
break;
|
||||||
@ -247,6 +248,7 @@ 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);
|
updateNotification(true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -814,6 +816,13 @@ public class MediaScanner implements Handler.Callback {
|
|||||||
mStats.reset();
|
mStats.reset();
|
||||||
return next;
|
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
|
@Override
|
||||||
public void onChange(LibraryObserver.Type type, long id, boolean ongoing)
|
public void onChange(LibraryObserver.Type type, long id, boolean ongoing)
|
||||||
{
|
{
|
||||||
|
if (type != LibraryObserver.Type.SONG && type != LibraryObserver.Type.PLAYLIST)
|
||||||
|
return;
|
||||||
|
|
||||||
MediaUtils.onMediaChange();
|
MediaUtils.onMediaChange();
|
||||||
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
|
* 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
|
* 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.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.util.Timer;
|
public class PreferencesMediaLibrary extends Fragment implements View.OnClickListener {
|
||||||
import java.util.TimerTask;
|
|
||||||
|
|
||||||
public class PreferencesMediaLibrary extends Fragment implements View.OnClickListener
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The ugly timer which fires every 200ms
|
|
||||||
*/
|
|
||||||
private Timer mTimer;
|
|
||||||
/**
|
/**
|
||||||
* Our start button
|
* Our start button
|
||||||
*/
|
*/
|
||||||
@ -113,6 +105,20 @@ public class PreferencesMediaLibrary extends Fragment implements View.OnClickLis
|
|||||||
*/
|
*/
|
||||||
private String mInitialMediaFolders;
|
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
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
return inflater.inflate(R.layout.medialibrary_preferences, container, false);
|
return inflater.inflate(R.layout.medialibrary_preferences, container, false);
|
||||||
@ -149,18 +155,6 @@ public class PreferencesMediaLibrary extends Fragment implements View.OnClickLis
|
|||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.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
|
// We got freshly created and user didn't have a chance
|
||||||
// to edit anything: remember this state
|
// to edit anything: remember this state
|
||||||
@ -176,17 +170,15 @@ public class PreferencesMediaLibrary extends Fragment implements View.OnClickLis
|
|||||||
mFullScanPending = true;
|
mFullScanPending = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MediaLibrary.registerLibraryObserver(mLibraryObserver);
|
||||||
|
updateProgress();
|
||||||
updatePreferences(null);
|
updatePreferences(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
if (mTimer != null) {
|
MediaLibrary.unregisterLibraryObserver(mLibraryObserver);
|
||||||
mTimer.cancel();
|
|
||||||
mTimer = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// User exited this view -> scan if needed
|
// User exited this view -> scan if needed
|
||||||
if (mFullScanPending && !mIsEditingDirectories) {
|
if (mFullScanPending && !mIsEditingDirectories) {
|
||||||
MediaLibrary.startLibraryScan(getActivity(), true, true);
|
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_DUMP_DB = 1;
|
||||||
private static final int MENU_FORCE_M3U_IMPORT = 2;
|
private static final int MENU_FORCE_M3U_IMPORT = 2;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
super.onCreateOptionsMenu(menu, inflater);
|
super.onCreateOptionsMenu(menu, inflater);
|
||||||
@ -373,7 +366,6 @@ public class PreferencesMediaLibrary extends Fragment implements View.OnClickLis
|
|||||||
*/
|
*/
|
||||||
public void startButtonPressed(View view) {
|
public void startButtonPressed(View view) {
|
||||||
MediaLibrary.startLibraryScan(getActivity(), mFullScanCheck.isChecked(), mDropDbCheck.isChecked());
|
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) {
|
public void cancelButtonPressed(View view) {
|
||||||
MediaLibrary.abortLibraryScan(getActivity());
|
MediaLibrary.abortLibraryScan(getActivity());
|
||||||
updateProgress();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -134,7 +134,7 @@ public class ScheduledLibraryUpdate extends JobService {
|
|||||||
private final LibraryObserver mObserver = new LibraryObserver() {
|
private final LibraryObserver mObserver = new LibraryObserver() {
|
||||||
@Override
|
@Override
|
||||||
public void onChange(LibraryObserver.Type type, long id, boolean ongoing) {
|
public void onChange(LibraryObserver.Type type, long id, boolean ongoing) {
|
||||||
if (!ongoing) {
|
if (type == LibraryObserver.Type.SONG && !ongoing) {
|
||||||
jobFinished(mJobParams, false);
|
jobFinished(mJobParams, false);
|
||||||
finalizeScan();
|
finalizeScan();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user