Move media content observer into ContextApplication
This commit is contained in:
parent
3fd61bcafe
commit
d456821891
@ -29,6 +29,8 @@ import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.ContentObserver;
|
||||
import android.provider.MediaStore;
|
||||
|
||||
/**
|
||||
* Subclass of Application that provides various static utility functions
|
||||
@ -44,6 +46,29 @@ public class ContextApplication extends Application {
|
||||
mInstance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate()
|
||||
{
|
||||
getContentResolver().registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, mObserver);
|
||||
}
|
||||
|
||||
private ContentObserver mObserver = new ContentObserver(null) {
|
||||
@Override
|
||||
public void onChange(boolean selfChange)
|
||||
{
|
||||
if (mService != null)
|
||||
mService.onMediaChange();
|
||||
ArrayList<Activity> list = mActivities;
|
||||
if (list != null) {
|
||||
for (int i = list.size(); --i != -1; ) {
|
||||
Activity activity = list.get(i);
|
||||
if (activity instanceof PlaybackActivity)
|
||||
((PlaybackActivity)activity).onMediaChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a shared, application-wide Random instance.
|
||||
*/
|
||||
|
@ -170,6 +170,13 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
|
||||
setState(intent.getIntExtra("state", 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the content of the media store has changed.
|
||||
*/
|
||||
public void onMediaChange()
|
||||
{
|
||||
}
|
||||
|
||||
static final int MENU_QUIT = 0;
|
||||
static final int MENU_DISPLAY = 1;
|
||||
static final int MENU_PREFS = 2;
|
||||
|
@ -37,7 +37,6 @@ import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.ContentObserver;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.Build;
|
||||
@ -184,7 +183,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
||||
boolean mPlayingBeforeCall;
|
||||
private int mPendingSeek;
|
||||
private Song mLastSongBroadcast;
|
||||
private ContentObserver mMediaObserver;
|
||||
public Receiver mReceiver;
|
||||
public InCallListener mCallListener;
|
||||
private boolean mLoaded;
|
||||
@ -509,16 +507,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
||||
Toast.makeText(this, R.string.repeat_disabling, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
if ((state & FLAG_NO_MEDIA) != 0 && (oldState & FLAG_NO_MEDIA) == 0) {
|
||||
ContentResolver resolver = ContextApplication.getContext().getContentResolver();
|
||||
mMediaObserver = new MediaContentObserver(mHandler);
|
||||
resolver.registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, mMediaObserver);
|
||||
} else if ((state & FLAG_NO_MEDIA) == 0 && (oldState & FLAG_NO_MEDIA) != 0) {
|
||||
ContentResolver resolver = ContextApplication.getContext().getContentResolver();
|
||||
resolver.unregisterContentObserver(mMediaObserver);
|
||||
mMediaObserver = null;
|
||||
}
|
||||
|
||||
if ((state & FLAG_PLAYING) != 0 && (oldState & FLAG_PLAYING) == 0) {
|
||||
if (mNotificationMode != NEVER)
|
||||
startForegroundCompat(NOTIFICATION_ID, mNotification);
|
||||
@ -628,8 +616,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
||||
public boolean onError(MediaPlayer player, int what, int extra)
|
||||
{
|
||||
Log.e("VanillaMusic", "MediaPlayer error: " + what + " " + extra);
|
||||
if (!Song.isSongAvailable())
|
||||
setFlag(FLAG_NO_MEDIA);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -710,21 +696,13 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
||||
}
|
||||
};
|
||||
|
||||
private class MediaContentObserver extends ContentObserver {
|
||||
public MediaContentObserver(Handler handler)
|
||||
{
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange)
|
||||
{
|
||||
if (Song.isSongAvailable()) {
|
||||
if ((mState & FLAG_NO_MEDIA) != 0)
|
||||
setCurrentSong(0);
|
||||
} else {
|
||||
setFlag(FLAG_NO_MEDIA);
|
||||
}
|
||||
public void onMediaChange()
|
||||
{
|
||||
if (Song.isSongAvailable()) {
|
||||
if ((mState & FLAG_NO_MEDIA) != 0)
|
||||
setCurrentSong(0);
|
||||
} else {
|
||||
setFlag(FLAG_NO_MEDIA);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,6 @@ import android.content.ContentResolver;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Resources;
|
||||
import android.database.ContentObserver;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.PaintDrawable;
|
||||
@ -135,8 +134,6 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
|
||||
getAdapter(i).setLimiter(state.getStringArray("limiter_" + i), true);
|
||||
updateLimiterViews();
|
||||
}
|
||||
|
||||
mHandler.sendEmptyMessage(MSG_INIT);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -568,11 +565,6 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
|
||||
view.setAdapter(adapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the initialization that may be done in the background outside
|
||||
* of onCreate.
|
||||
*/
|
||||
private static final int MSG_INIT = 10;
|
||||
/**
|
||||
* Call addToPlaylist with the parameters from the given message. The
|
||||
* message must contain the type and id of the media to be added in
|
||||
@ -600,11 +592,6 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
|
||||
public boolean handleMessage(Message message)
|
||||
{
|
||||
switch (message.what) {
|
||||
case MSG_INIT:
|
||||
ContentResolver resolver = getContentResolver();
|
||||
Observer observer = new Observer(mHandler);
|
||||
resolver.registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, observer);
|
||||
break;
|
||||
case MSG_NEW_PLAYLIST: {
|
||||
NewPlaylistDialog dialog = (NewPlaylistDialog)message.obj;
|
||||
if (dialog.isAccepted()) {
|
||||
@ -629,24 +616,17 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
|
||||
return true;
|
||||
}
|
||||
|
||||
private class Observer extends ContentObserver {
|
||||
public Observer(Handler handler)
|
||||
{
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange)
|
||||
{
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run()
|
||||
{
|
||||
for (int i = 0; i != TAB_COUNT; ++i)
|
||||
getAdapter(i).requery();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public void onMediaChange()
|
||||
{
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run()
|
||||
{
|
||||
for (int i = 0; i != TAB_COUNT; ++i)
|
||||
getAdapter(i).requery();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setSearchBoxVisible(boolean visible)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user