Replace ContentObserver with own LibraryObserver class

This commit is contained in:
Adrian Ulrich committed 2018-06-25 20:49:12 +02:00
1 parent 34ef6893d2
commit 11dde262ee
6 files changed
+85 -48

No files matched your search

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2018 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ch.blinkenlights.android.medialibrary;
/**
* Receives callbacks if the media library changes
*/
public class LibraryObserver {
/**
* Type of the event, to be used as a hint
* by the receiver
*/
public enum Type {
ANY, // Any type may have changed
SONG, // Change only affected song entries
PLAYLIST, // Change only affected playlists
}
/**
* Return new LibraryObserver object.
*/
public LibraryObserver() {
}
/**
* Called if there was a change, expected
* to be overriden by the registered observer.
*
* @param type one of LibraryObserver.Type
* @param ongoing whether or not to expect more events soon.
*/
public void onChange(Type type, boolean ongoing) {
// NOOP, should be overriden.
}
}
@@ -20,7 +20,6 @@ package ch.blinkenlights.android.medialibrary;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.ContentObserver;
import android.provider.MediaStore;
import android.os.Build;
import android.os.Environment;
@@ -103,7 +102,7 @@ public class MediaLibrary {
/**
* The observer to call-back during database changes
*/
private static final ArrayList<ContentObserver> sContentObservers = new ArrayList<ContentObserver>(2);
private static final ArrayList<LibraryObserver> sLibraryObservers = new ArrayList<LibraryObserver>(2);
/**
* The lock we are using during object creation
*/
@@ -292,9 +291,9 @@ public class MediaLibrary {
}
/**
* Registers a new content observer for the media library
* Registers a new library observer for the media library
*
* The MediaLibrary will call `onChange(boolean ongoing)` if
* The MediaLibrary will call `onLibraryChanged()` if
* the media library changed.
*
* `ongoing` will be set to `true` if you are expected to receive
@@ -303,33 +302,33 @@ public class MediaLibrary {
*
* @param observer the content observer we are going to call on changes
*/
public static void registerContentObserver(ContentObserver observer) {
if (sContentObservers.contains(observer))
throw new IllegalStateException("ContentObserver was already registered");
public static void registerLibraryObserver(LibraryObserver observer) {
if (sLibraryObservers.contains(observer))
throw new IllegalStateException("LibraryObserver was already registered");
sContentObservers.add(observer);
sLibraryObservers.add(observer);
}
/**
* Unregisters a content observer which was previously registered
* by calling registerContentObserver().
* Unregisters a library observer which was previously registered
* by calling registerLibraryObserver().
*
* @param observer the content observer to unregister.
*/
public static void unregisterContentObserver(ContentObserver observer) {
boolean removed = sContentObservers.remove(observer);
public static void unregisterLibraryObserver(LibraryObserver observer) {
boolean removed = sLibraryObservers.remove(observer);
if (!removed)
throw new IllegalArgumentException("This content observer was never registered!");
throw new IllegalArgumentException("This library observer was never registered!");
}
/**
* Broadcasts a change to all registered observers
*/
static void notifyObserver(boolean ongoing) {
ArrayList<ContentObserver> list = sContentObservers;
static void notifyObserver(LibraryObserver.Type type, boolean ongoing) {
ArrayList<LibraryObserver> list = sLibraryObservers;
for (int i = list.size(); --i != -1; )
list.get(i).onChange(ongoing);
list.get(i).onChange(type, ongoing);
}
/**
@@ -358,7 +357,7 @@ public class MediaLibrary {
if (rows > 0) {
getBackend(context).cleanOrphanedEntries(true);
notifyObserver(false);
notifyObserver(LibraryObserver.Type.ANY, false);
}
return rows;
}
@@ -390,7 +389,7 @@ public class MediaLibrary {
long id = getBackend(context).insert(MediaLibrary.TABLE_PLAYLISTS, null, v);
if (id != -1)
notifyObserver(false);
notifyObserver(LibraryObserver.Type.PLAYLIST, false);
return id;
}
@@ -408,7 +407,7 @@ public class MediaLibrary {
boolean removed = (rows > 0);
if (removed)
notifyObserver(false);
notifyObserver(LibraryObserver.Type.PLAYLIST, false);
return removed;
}
@@ -446,7 +445,7 @@ public class MediaLibrary {
int rows = getBackend(context).bulkInsert(MediaLibrary.TABLE_PLAYLISTS_SONGS, null, bulk);
if (rows > 0)
notifyObserver(false);
notifyObserver(LibraryObserver.Type.PLAYLIST, false);
return rows;
}
@@ -462,7 +461,7 @@ public class MediaLibrary {
int rows = getBackend(context).delete(MediaLibrary.TABLE_PLAYLISTS_SONGS, selection, selectionArgs);
if (rows > 0)
notifyObserver(false);
notifyObserver(LibraryObserver.Type.PLAYLIST, false);
return rows;
}
@@ -485,7 +484,7 @@ public class MediaLibrary {
}
if (newId != -1)
notifyObserver(false);
notifyObserver(LibraryObserver.Type.PLAYLIST, false);
return newId;
}
@@ -530,7 +529,7 @@ public class MediaLibrary {
selection = MediaLibrary.PlaylistSongColumns._ID+"="+from;
getBackend(context).update(MediaLibrary.TABLE_PLAYLISTS_SONGS, v, selection, null);
notifyObserver(false);
notifyObserver(LibraryObserver.Type.PLAYLIST, false);
}
/**
@@ -205,7 +205,7 @@ public class MediaScanner implements Handler.Callback {
switch (rpc) {
case MSG_NOTIFY_CHANGE: {
MediaLibrary.notifyObserver(true);
MediaLibrary.notifyObserver(LibraryObserver.Type.SONG, true);
break;
}
case MSG_SCAN_FINISHED: {
@@ -224,7 +224,7 @@ public class MediaScanner implements Handler.Callback {
// also signals that this will be our last update
// for this scan.
mHandler.removeMessages(MSG_NOTIFY_CHANGE);
MediaLibrary.notifyObserver(false);
MediaLibrary.notifyObserver(LibraryObserver.Type.ANY, false);
updateNotification(false);
break;
@@ -25,7 +25,6 @@ package ch.blinkenlights.android.vanilla;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
@@ -192,17 +191,6 @@ public class LibraryPagerAdapter
*/
private String mFilter;
private final ContentObserver mPlaylistObserver = new ContentObserver(null) {
@Override
public void onChange(boolean selfChange)
{
if (mPlaylistAdapter != null) {
postRequestRequery(mPlaylistAdapter);
}
}
};
/**
* Create the LibraryPager.
*
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2017 Adrian Ulrich <adrian@blinkenlights.ch>
* Copyright (C) 2012-2018 Adrian Ulrich <adrian@blinkenlights.ch>
* Copyright (C) 2010, 2011 Christopher Eby <kreed@kreed.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -24,6 +24,7 @@
package ch.blinkenlights.android.vanilla;
import ch.blinkenlights.android.medialibrary.MediaLibrary;
import ch.blinkenlights.android.medialibrary.LibraryObserver;
import android.app.Notification;
import android.app.PendingIntent;
@@ -36,7 +37,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.hardware.Sensor;
@@ -497,7 +497,7 @@ public final class PlaybackService extends Service
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(mReceiver, filter);
MediaLibrary.registerContentObserver(mObserver);
MediaLibrary.registerLibraryObserver(mObserver);
mRemoteControlClient = new RemoteControl().getClient(this);
mRemoteControlClient.initializeRemote();
@@ -615,7 +615,7 @@ public final class PlaybackService extends Service
// defer wakelock and close audioFX
enterSleepState();
MediaLibrary.unregisterContentObserver(mObserver);
MediaLibrary.unregisterLibraryObserver(mObserver);
if (mMediaPlayer != null) {
mMediaPlayer.release();
@@ -1930,9 +1930,9 @@ public final class PlaybackService extends Service
list.get(i).onPositionInfoChanged();
}
private final ContentObserver mObserver = new ContentObserver(null) {
private final LibraryObserver mObserver = new LibraryObserver() {
@Override
public void onChange(boolean selfChange)
public void onChange(LibraryObserver.Type type, boolean ongoing)
{
MediaUtils.onMediaChange();
onMediaChange();
@@ -18,6 +18,7 @@
package ch.blinkenlights.android.vanilla;
import ch.blinkenlights.android.medialibrary.MediaLibrary;
import ch.blinkenlights.android.medialibrary.LibraryObserver;
import android.annotation.TargetApi;
import android.app.job.JobInfo;
@@ -26,7 +27,6 @@ import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.Context;
import android.content.ComponentName;
import android.database.ContentObserver;
@TargetApi(21)
@@ -98,7 +98,7 @@ public class ScheduledLibraryUpdate extends JobService {
final boolean fullScan = (Math.random() > 0.7);
mJobParams = params;
MediaLibrary.registerContentObserver(mObserver);
MediaLibrary.registerLibraryObserver(mObserver);
MediaLibrary.startLibraryScan(this, fullScan, false);
return true;
@@ -120,7 +120,7 @@ public class ScheduledLibraryUpdate extends JobService {
* Aborts a running scan job
*/
private void finalizeScan() {
MediaLibrary.unregisterContentObserver(mObserver);
MediaLibrary.unregisterLibraryObserver(mObserver);
MediaLibrary.abortLibraryScan(this);
mJobParams = null;
}
@@ -131,9 +131,9 @@ public class ScheduledLibraryUpdate extends JobService {
* the last callback will have `ongoing` set to `false`,
* which indicates that our job completed.
*/
private final ContentObserver mObserver = new ContentObserver(null) {
private final LibraryObserver mObserver = new LibraryObserver() {
@Override
public void onChange(boolean ongoing) {
public void onChange(LibraryObserver.Type type, boolean ongoing) {
if (!ongoing) {
jobFinished(mJobParams, false);
finalizeScan();