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.app.Application;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.database.ContentObserver;
|
||||||
|
import android.provider.MediaStore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of Application that provides various static utility functions
|
* Subclass of Application that provides various static utility functions
|
||||||
@ -44,6 +46,29 @@ public class ContextApplication extends Application {
|
|||||||
mInstance = this;
|
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.
|
* 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));
|
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_QUIT = 0;
|
||||||
static final int MENU_DISPLAY = 1;
|
static final int MENU_DISPLAY = 1;
|
||||||
static final int MENU_PREFS = 2;
|
static final int MENU_PREFS = 2;
|
||||||
|
@ -37,7 +37,6 @@ import android.content.Intent;
|
|||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.database.ContentObserver;
|
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
@ -184,7 +183,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
boolean mPlayingBeforeCall;
|
boolean mPlayingBeforeCall;
|
||||||
private int mPendingSeek;
|
private int mPendingSeek;
|
||||||
private Song mLastSongBroadcast;
|
private Song mLastSongBroadcast;
|
||||||
private ContentObserver mMediaObserver;
|
|
||||||
public Receiver mReceiver;
|
public Receiver mReceiver;
|
||||||
public InCallListener mCallListener;
|
public InCallListener mCallListener;
|
||||||
private boolean mLoaded;
|
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();
|
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 ((state & FLAG_PLAYING) != 0 && (oldState & FLAG_PLAYING) == 0) {
|
||||||
if (mNotificationMode != NEVER)
|
if (mNotificationMode != NEVER)
|
||||||
startForegroundCompat(NOTIFICATION_ID, mNotification);
|
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)
|
public boolean onError(MediaPlayer player, int what, int extra)
|
||||||
{
|
{
|
||||||
Log.e("VanillaMusic", "MediaPlayer error: " + what + " " + extra);
|
Log.e("VanillaMusic", "MediaPlayer error: " + what + " " + extra);
|
||||||
if (!Song.isSongAvailable())
|
|
||||||
setFlag(FLAG_NO_MEDIA);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -710,14 +696,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private class MediaContentObserver extends ContentObserver {
|
public void onMediaChange()
|
||||||
public MediaContentObserver(Handler handler)
|
|
||||||
{
|
|
||||||
super(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onChange(boolean selfChange)
|
|
||||||
{
|
{
|
||||||
if (Song.isSongAvailable()) {
|
if (Song.isSongAvailable()) {
|
||||||
if ((mState & FLAG_NO_MEDIA) != 0)
|
if ((mState & FLAG_NO_MEDIA) != 0)
|
||||||
@ -726,7 +705,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
setFlag(FLAG_NO_MEDIA);
|
setFlag(FLAG_NO_MEDIA);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void onSharedPreferenceChanged(SharedPreferences settings, String key)
|
public void onSharedPreferenceChanged(SharedPreferences settings, String key)
|
||||||
{
|
{
|
||||||
|
@ -26,7 +26,6 @@ import android.content.ContentResolver;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.database.ContentObserver;
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.drawable.PaintDrawable;
|
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);
|
getAdapter(i).setLimiter(state.getStringArray("limiter_" + i), true);
|
||||||
updateLimiterViews();
|
updateLimiterViews();
|
||||||
}
|
}
|
||||||
|
|
||||||
mHandler.sendEmptyMessage(MSG_INIT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -568,11 +565,6 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
|
|||||||
view.setAdapter(adapter);
|
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
|
* Call addToPlaylist with the parameters from the given message. The
|
||||||
* message must contain the type and id of the media to be added in
|
* 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)
|
public boolean handleMessage(Message message)
|
||||||
{
|
{
|
||||||
switch (message.what) {
|
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: {
|
case MSG_NEW_PLAYLIST: {
|
||||||
NewPlaylistDialog dialog = (NewPlaylistDialog)message.obj;
|
NewPlaylistDialog dialog = (NewPlaylistDialog)message.obj;
|
||||||
if (dialog.isAccepted()) {
|
if (dialog.isAccepted()) {
|
||||||
@ -629,14 +616,8 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Observer extends ContentObserver {
|
|
||||||
public Observer(Handler handler)
|
|
||||||
{
|
|
||||||
super(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onChange(boolean selfChange)
|
public void onMediaChange()
|
||||||
{
|
{
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
public void run()
|
public void run()
|
||||||
@ -646,7 +627,6 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
private void setSearchBoxVisible(boolean visible)
|
private void setSearchBoxVisible(boolean visible)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user