From 5a1503ed7b32020ebd4de7a49d246c949bc23d62 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Sun, 28 Jun 2015 15:48:05 +0200 Subject: [PATCH] Added option to disable album art on lockscreen. --- .../gramophone/service/MusicService.java | 76 ++++++++++++------- .../ui/activities/SettingsActivity.java | 12 ++- .../gramophone/util/PreferenceUtils.java | 5 ++ app/src/main/res/values/strings.xml | 3 + app/src/main/res/xml/pref_lockscreen.xml | 15 ++++ 5 files changed, 81 insertions(+), 30 deletions(-) create mode 100644 app/src/main/res/xml/pref_lockscreen.xml diff --git a/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java b/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java index 7d0cedf0..96f4851f 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java +++ b/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java @@ -68,7 +68,8 @@ public class MusicService extends Service { public static final String SHUFFLE_MODE_CHANGED = "com.kabouzeid.gramophone.shufflemodechanged"; public static final String SETTING_GAPLESS_PLAYBACK_CHANGED = "com.kabouzeid.gramophone.SETTING_GAPLESS_PLAYBACK_CHANGED"; - public static final String SETTING_GAPLESS_PLAYBACK_CHANGED_VALUE_EXTRA = "com.kabouzeid.gramophone.SETTING_GAPLESS_PLAYBACK_CHANGED_VALUE_EXTRA"; + public static final String SETTING_ALBUM_ART_ON_LOCKSCREEN_CHANGED = "com.kabouzeid.gramophone.SETTING_ALBUM_ART_ON_LOCKSCREEN_CHANGED"; + public static final String SETTING_BOOLEAN_EXTRA = "com.kabouzeid.gramophone.SETTING_BOOLEAN_EXTRA"; public static final String SAVED_POSITION = "POSITION"; public static final String SAVED_POSITION_IN_TRACK = "POSITION_IN_TRACK"; @@ -108,7 +109,6 @@ public class MusicService extends Service { @SuppressWarnings("deprecation") private RemoteControlClient remoteControlClient; private PowerManager.WakeLock wakeLock; - private String currentAlbumArtUri; private MusicPlayerHandler playerHandler; private QueueSaveHandler queueSaveHandler; private boolean isFadingDown = false; @@ -128,11 +128,16 @@ public class MusicService extends Service { } }; - private final BroadcastReceiver gaplessPlaybackSettingChangedReceiver = new BroadcastReceiver() { + private final BroadcastReceiver preferencesChangedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (intent.getAction().equals(SETTING_GAPLESS_PLAYBACK_CHANGED)) { - setGaplessPlaybackEnabled(intent.getBooleanExtra(SETTING_GAPLESS_PLAYBACK_CHANGED_VALUE_EXTRA, true)); + switch (intent.getAction()) { + case SETTING_GAPLESS_PLAYBACK_CHANGED: + setGaplessPlaybackEnabled(intent.getBooleanExtra(SETTING_BOOLEAN_EXTRA, true)); + break; + case SETTING_ALBUM_ART_ON_LOCKSCREEN_CHANGED: + updateRemoteControlClientImpl(intent.getBooleanExtra(SETTING_BOOLEAN_EXTRA, true)); + break; } } }; @@ -183,7 +188,12 @@ public class MusicService extends Service { private void registerReceiversAndRemoteControlClient() { if (!receiversAndRemoteControlClientRegistered) { registerReceiver(becomingNoisyReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); - registerReceiver(gaplessPlaybackSettingChangedReceiver, new IntentFilter(SETTING_GAPLESS_PLAYBACK_CHANGED)); + + IntentFilter preferenceIntentFilter = new IntentFilter(); + preferenceIntentFilter.addAction(SETTING_GAPLESS_PLAYBACK_CHANGED); + preferenceIntentFilter.addAction(SETTING_ALBUM_ART_ON_LOCKSCREEN_CHANGED); + registerReceiver(preferencesChangedReceiver, preferenceIntentFilter); + //noinspection deprecation getAudioManager().registerMediaButtonEventReceiver(new ComponentName(getApplicationContext(), MediaButtonIntentReceiver.class)); initRemoteControlClient(); @@ -267,7 +277,7 @@ public class MusicService extends Service { private void unregisterReceiversAndRemoteControlClient() { if (receiversAndRemoteControlClientRegistered) { unregisterReceiver(becomingNoisyReceiver); - unregisterReceiver(gaplessPlaybackSettingChangedReceiver); + unregisterReceiver(preferencesChangedReceiver); //noinspection deprecation getAudioManager().unregisterRemoteControlClient(remoteControlClient); //noinspection deprecation @@ -394,38 +404,46 @@ public class MusicService extends Service { } private void updateRemoteControlClient() { + updateRemoteControlClientImpl(PreferenceUtils.getInstance(this).albumArtOnLockscrenn()); + } + + private void updateRemoteControlClientImpl(boolean showAlbumArt) { final Song song = getCurrentSong(); remoteControlClient - .editMetadata(false) + .editMetadata(!showAlbumArt) .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, song.artistName) .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, song.title) .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, song.duration) .apply(); - currentAlbumArtUri = MusicUtil.getAlbumArtUri(song.albumId).toString(); - ImageLoader.getInstance().displayImage(currentAlbumArtUri, new NonViewAware(new ImageSize(-1, -1), ViewScaleType.CROP), new SimpleImageLoadingListener() { - @Override - public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { - if (currentAlbumArtUri.equals(imageUri)) { - Bitmap albumArt = loadedImage; - if (albumArt != null) { - // RemoteControlClient wants to recycle the bitmaps thrown at it, so we need - // to make sure not to hand out our cache copy - Bitmap.Config config = albumArt.getConfig(); - if (config == null) { - config = Bitmap.Config.ARGB_8888; + if (showAlbumArt) { + final String currentAlbumArtUri = MusicUtil.getAlbumArtUri(song.albumId).toString(); + ImageLoader.getInstance().displayImage(currentAlbumArtUri, new NonViewAware(new ImageSize(-1, -1), ViewScaleType.CROP), new SimpleImageLoadingListener() { + @Override + public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { + if (currentAlbumArtUri.equals(imageUri)) { + Bitmap albumArt = loadedImage; + if (albumArt != null) { + // RemoteControlClient wants to recycle the bitmaps thrown at it, so we need + // to make sure not to hand out our cache copy + Bitmap.Config config = albumArt.getConfig(); + if (config == null) { + config = Bitmap.Config.ARGB_8888; + } + albumArt = albumArt.copy(config, false); + updateRemoteControlClientBitmap(albumArt.copy(albumArt.getConfig(), true)); } - albumArt = albumArt.copy(config, false); - updateRemoteControlClientBitmap(albumArt.copy(albumArt.getConfig(), true)); } } - } - @Override - public void onLoadingFailed(String imageUri, View view, FailReason failReason) { - if (currentAlbumArtUri.equals(imageUri)) - updateRemoteControlClientBitmap(null); - } - }); + @Override + public void onLoadingFailed(String imageUri, View view, FailReason failReason) { + if (currentAlbumArtUri.equals(imageUri)) + updateRemoteControlClientBitmap(null); + } + }); + } else { + updateRemoteControlClientBitmap(null); + } } private void updateRemoteControlClientBitmap(final Bitmap albumArt) { diff --git a/app/src/main/java/com/kabouzeid/gramophone/ui/activities/SettingsActivity.java b/app/src/main/java/com/kabouzeid/gramophone/ui/activities/SettingsActivity.java index 67e5e561..7eb55a22 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/ui/activities/SettingsActivity.java +++ b/app/src/main/java/com/kabouzeid/gramophone/ui/activities/SettingsActivity.java @@ -69,6 +69,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia addPreferencesFromResource(R.xml.pref_general); addPreferencesFromResource(R.xml.pref_colors); addPreferencesFromResource(R.xml.pref_now_playing_screen); + addPreferencesFromResource(R.xml.pref_lockscreen); addPreferencesFromResource(R.xml.pref_audio); final Preference defaultStartPage = findPreference("default_start_page"); @@ -147,11 +148,20 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia } }); + Preference albumArtOnLockscreen = findPreference("album_art_on_lockscreen"); + albumArtOnLockscreen.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + getActivity().sendBroadcast(new Intent(MusicService.SETTING_ALBUM_ART_ON_LOCKSCREEN_CHANGED).putExtra(MusicService.SETTING_BOOLEAN_EXTRA, (boolean) newValue)); + return true; + } + }); + Preference gaplessPlayback = findPreference("gapless_playback"); gaplessPlayback.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - getActivity().sendBroadcast(new Intent(MusicService.SETTING_GAPLESS_PLAYBACK_CHANGED).putExtra(MusicService.SETTING_GAPLESS_PLAYBACK_CHANGED_VALUE_EXTRA, (boolean) newValue)); + getActivity().sendBroadcast(new Intent(MusicService.SETTING_GAPLESS_PLAYBACK_CHANGED).putExtra(MusicService.SETTING_BOOLEAN_EXTRA, (boolean) newValue)); return true; } }); diff --git a/app/src/main/java/com/kabouzeid/gramophone/util/PreferenceUtils.java b/app/src/main/java/com/kabouzeid/gramophone/util/PreferenceUtils.java index 9521de1b..5fb23bcf 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/util/PreferenceUtils.java +++ b/app/src/main/java/com/kabouzeid/gramophone/util/PreferenceUtils.java @@ -44,6 +44,7 @@ public final class PreferenceUtils { public static final String COLORED_NOTIFICATION = "colored_notification"; public static final String GAPLESS_PLAYBACK = "gapless_playback"; public static final String LAST_ADDED_CUTOFF_TIMESTAMP = "last_added_cutoff_timestamp"; + public static final String ALBUM_ART_ON_LOCKSCREEN = "album_art_on_lockscreen"; private static PreferenceUtils sInstance; @@ -203,6 +204,10 @@ public final class PreferenceUtils { return mPreferences.getBoolean(GAPLESS_PLAYBACK, true); } + public final boolean albumArtOnLockscrenn() { + return mPreferences.getBoolean(ALBUM_ART_ON_LOCKSCREEN, true); + } + // public final boolean downloadMissingArtistImages() { // return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTIST_IMAGES, true); // } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c7a648f8..9d32f02a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -102,6 +102,7 @@ General Theme Audio General + Lockscreen In which views the navigation bar should be colored. Colored Navigation Bar Start Page @@ -112,6 +113,7 @@ Tag Editor Everywhere Else Colored Album Footers + Album Art on Lockscreen Colored Notification Fade Play/Pause Gapless Playback @@ -148,6 +150,7 @@ Song "Only available on Lollipop." "Album footers in the grid are colored with the album cover\'s vibrant color." + The album art is shown on the lockscreen. You might have to restart Phonograph in order for changes to take affect. "The notification is colored with the album cover\'s vibrant color." "Fades the song in/out on play/pause." "Eliminates the gap between two songs. Disabling this might fix playback issues." diff --git a/app/src/main/res/xml/pref_lockscreen.xml b/app/src/main/res/xml/pref_lockscreen.xml new file mode 100644 index 00000000..81cdaa4c --- /dev/null +++ b/app/src/main/res/xml/pref_lockscreen.xml @@ -0,0 +1,15 @@ + + + + + + + + +