Moved shortcut handling from MainActivity to MusicService. Cleaned up the code.

This commit is contained in:
Karim Abou Zeid 2017-03-27 12:28:37 +02:00
commit b0590bf5e1
7 changed files with 50 additions and 80 deletions

View file

@ -3,6 +3,7 @@ package com.kabouzeid.gramophone.appshortcuts;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.IntDef;
import com.kabouzeid.gramophone.appshortcuts.shortcuttype.LastAddedShortcutType; import com.kabouzeid.gramophone.appshortcuts.shortcuttype.LastAddedShortcutType;
import com.kabouzeid.gramophone.appshortcuts.shortcuttype.ShuffleAllShortcutType; import com.kabouzeid.gramophone.appshortcuts.shortcuttype.ShuffleAllShortcutType;
@ -11,8 +12,10 @@ import com.kabouzeid.gramophone.loader.LastAddedLoader;
import com.kabouzeid.gramophone.loader.SongLoader; import com.kabouzeid.gramophone.loader.SongLoader;
import com.kabouzeid.gramophone.loader.TopAndRecentlyPlayedTracksLoader; import com.kabouzeid.gramophone.loader.TopAndRecentlyPlayedTracksLoader;
import com.kabouzeid.gramophone.model.Song; import com.kabouzeid.gramophone.model.Song;
import com.kabouzeid.gramophone.ui.activities.MainActivity; import com.kabouzeid.gramophone.service.MusicService;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@ -20,40 +23,40 @@ import java.util.ArrayList;
*/ */
public class AppShortcutLauncherActivity extends Activity { public class AppShortcutLauncherActivity extends Activity {
public static final String KEY_SHORTCUT_TYPE = "com.kabouzeid.gramophone.appshortcuts.ShortcutType"; public static final String KEY_SHORTCUT_TYPE = "com.kabouzeid.gramophone.appshortcuts.ShortcutType";
public static final int SHORTCUT_TYPE_SHUFFLE_ALL = 0;
public static final int SHORTCUT_TYPE_TOP_TRACKS = 1;
public static final int SHORTCUT_TYPE_LAST_ADDED = 2;
public static final int SHORTCUT_TYPE_NONE = 3;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ShortcutType shortcutType = ShortcutType.NONE; @ShortcutType
int shortcutType = SHORTCUT_TYPE_NONE;
//Set shortcutType from the intent extras //Set shortcutType from the intent extras
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
if (extras != null) { if (extras != null) {
try { //noinspection WrongConstant
shortcutType = ShortcutType.valueOf(extras.getString(KEY_SHORTCUT_TYPE)); shortcutType = extras.getInt(KEY_SHORTCUT_TYPE, SHORTCUT_TYPE_NONE);
} catch (IllegalArgumentException e) { //In the event we're somehow passed an invalid enum string, don't crash.
e.printStackTrace();
shortcutType = ShortcutType.NONE;
}
} }
//Perform the action found in the extras
switch (shortcutType) { switch (shortcutType) {
case SHUFFLE_ALL: case SHORTCUT_TYPE_SHUFFLE_ALL:
launchMainActivityWithSongs(PlayMode.SHUFFLE, startServiceWithSongs(MusicService.SHUFFLE_MODE_SHUFFLE,
SongLoader.getAllSongs(getApplicationContext())); SongLoader.getAllSongs(getApplicationContext()));
DynamicShortcutManager.reportShortcutUsed(this, ShuffleAllShortcutType.getId()); DynamicShortcutManager.reportShortcutUsed(this, ShuffleAllShortcutType.getId());
break; break;
case TOP_TRACKS: case SHORTCUT_TYPE_TOP_TRACKS:
launchMainActivityWithSongs(PlayMode.NORMAL, startServiceWithSongs(MusicService.SHUFFLE_MODE_NONE,
TopAndRecentlyPlayedTracksLoader.getTopTracks(getApplicationContext())); TopAndRecentlyPlayedTracksLoader.getTopTracks(getApplicationContext()));
DynamicShortcutManager.reportShortcutUsed(this, TopTracksShortcutType.getId()); DynamicShortcutManager.reportShortcutUsed(this, TopTracksShortcutType.getId());
break; break;
case LAST_ADDED: case SHORTCUT_TYPE_LAST_ADDED:
launchMainActivityWithSongs(PlayMode.NORMAL, startServiceWithSongs(MusicService.SHUFFLE_MODE_NONE,
LastAddedLoader.getLastAddedSongs(getApplicationContext())); LastAddedLoader.getLastAddedSongs(getApplicationContext()));
DynamicShortcutManager.reportShortcutUsed(this, LastAddedShortcutType.getId()); DynamicShortcutManager.reportShortcutUsed(this, LastAddedShortcutType.getId());
break; break;
@ -62,36 +65,21 @@ public class AppShortcutLauncherActivity extends Activity {
finish(); finish();
} }
private void launchMainActivityWithSongs(PlayMode playMode, ArrayList<Song> songs) { private void startServiceWithSongs(int shuffleMode, ArrayList<Song> songs) {
//Create a new intent to launch MainActivity Intent intent = new Intent(this, MusicService.class);
Intent intent = new Intent(this, MainActivity.class); intent.setAction(MusicService.ACTION_PLAY);
switch (playMode) {
case NORMAL:
intent.setAction(MainActivity.INTENT_ACTION_MEDIA_PLAY);
break;
case SHUFFLE:
intent.setAction(MainActivity.INTENT_ACTION_MEDIA_PLAY_SHUFFLED);
break;
}
//Create a bundle to store the songs to shuffle through songs
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelableArrayList(MainActivity.INTENT_EXTRA_SONGS, songs); bundle.putParcelableArrayList(MusicService.INTENT_EXTRA_SONGS, songs);
bundle.putInt(MusicService.INTENT_EXTRA_SHUFFLE_MODE, shuffleMode);
//Put the bundle in the intent
intent.putExtras(bundle); intent.putExtras(bundle);
//If MainActivity's already running, don't launch another instance. Instead, bring it to the top and deliver the intent to onNewIntent() startService(intent);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//Finally, start MainActivity with those extras
startActivity(intent);
} }
private enum PlayMode {NORMAL, SHUFFLE} @Retention(RetentionPolicy.SOURCE)
@IntDef({SHORTCUT_TYPE_SHUFFLE_ALL, SHORTCUT_TYPE_TOP_TRACKS, SHORTCUT_TYPE_LAST_ADDED, SHORTCUT_TYPE_NONE})
public enum ShortcutType { public @interface ShortcutType {
SHUFFLE_ALL, TOP_TRACKS, LAST_ADDED, NONE
} }
} }

View file

@ -37,16 +37,13 @@ public abstract class BaseShortcutType {
* @param shortcutType Describes the type of shortcut to create (ShuffleAll, TopTracks, custom playlist, etc.) * @param shortcutType Describes the type of shortcut to create (ShuffleAll, TopTracks, custom playlist, etc.)
* @return * @return
*/ */
Intent getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType shortcutType) { Intent getPlaySongsIntent(@AppShortcutLauncherActivity.ShortcutType int shortcutType) {
//Create a new intent to launch MainActivity
Intent intent = new Intent(context, AppShortcutLauncherActivity.class); Intent intent = new Intent(context, AppShortcutLauncherActivity.class);
intent.setAction(Intent.ACTION_VIEW); intent.setAction(Intent.ACTION_VIEW);
//Create a bundle to store instructions for AppShortcutLauncherActivity
Bundle b = new Bundle(); Bundle b = new Bundle();
b.putString(AppShortcutLauncherActivity.KEY_SHORTCUT_TYPE, shortcutType.toString()); b.putInt(AppShortcutLauncherActivity.KEY_SHORTCUT_TYPE, shortcutType);
//Put bundle in intent
intent.putExtras(b); intent.putExtras(b);
return intent; return intent;

View file

@ -23,7 +23,7 @@ public final class LastAddedShortcutType extends BaseShortcutType {
.setShortLabel(context.getString(R.string.app_shortcut_last_added_short)) .setShortLabel(context.getString(R.string.app_shortcut_last_added_short))
.setLongLabel(context.getString(R.string.app_shortcut_last_added_long)) .setLongLabel(context.getString(R.string.app_shortcut_last_added_long))
.setIcon(AppShortcutIconGenerator.generateThemedIcon(context, R.drawable.ic_app_shortcut_last_added)) .setIcon(AppShortcutIconGenerator.generateThemedIcon(context, R.drawable.ic_app_shortcut_last_added))
.setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType.LAST_ADDED)) .setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.SHORTCUT_TYPE_LAST_ADDED))
.build(); .build();
} }

View file

@ -23,7 +23,7 @@ public final class ShuffleAllShortcutType extends BaseShortcutType {
.setShortLabel(context.getString(R.string.app_shortcut_shuffle_all_short)) .setShortLabel(context.getString(R.string.app_shortcut_shuffle_all_short))
.setLongLabel(context.getString(R.string.app_shortcut_shuffle_all_long)) .setLongLabel(context.getString(R.string.app_shortcut_shuffle_all_long))
.setIcon(AppShortcutIconGenerator.generateThemedIcon(context, R.drawable.ic_app_shortcut_shuffle_all)) .setIcon(AppShortcutIconGenerator.generateThemedIcon(context, R.drawable.ic_app_shortcut_shuffle_all))
.setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType.SHUFFLE_ALL)) .setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.SHORTCUT_TYPE_SHUFFLE_ALL))
.build(); .build();
} }

View file

@ -23,7 +23,7 @@ public final class TopTracksShortcutType extends BaseShortcutType {
.setShortLabel(context.getString(R.string.app_shortcut_top_tracks_short)) .setShortLabel(context.getString(R.string.app_shortcut_top_tracks_short))
.setLongLabel(context.getString(R.string.app_shortcut_top_tracks_long)) .setLongLabel(context.getString(R.string.app_shortcut_top_tracks_long))
.setIcon(AppShortcutIconGenerator.generateThemedIcon(context, R.drawable.ic_app_shortcut_top_tracks)) .setIcon(AppShortcutIconGenerator.generateThemedIcon(context, R.drawable.ic_app_shortcut_top_tracks))
.setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType.TOP_TRACKS)) .setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.SHORTCUT_TYPE_TOP_TRACKS))
.build(); .build();
} }

View file

@ -61,6 +61,7 @@ import com.kabouzeid.gramophone.util.Util;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random;
/** /**
* @author Karim Abou Zeid (kabouzeid), Andrew Neal * @author Karim Abou Zeid (kabouzeid), Andrew Neal
@ -78,6 +79,8 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
public static final String ACTION_SKIP = PHONOGRAPH_PACKAGE_NAME + ".skip"; public static final String ACTION_SKIP = PHONOGRAPH_PACKAGE_NAME + ".skip";
public static final String ACTION_REWIND = PHONOGRAPH_PACKAGE_NAME + ".rewind"; public static final String ACTION_REWIND = PHONOGRAPH_PACKAGE_NAME + ".rewind";
public static final String ACTION_QUIT = PHONOGRAPH_PACKAGE_NAME + ".quitservice"; public static final String ACTION_QUIT = PHONOGRAPH_PACKAGE_NAME + ".quitservice";
public static final String INTENT_EXTRA_SONGS = PHONOGRAPH_PACKAGE_NAME + ".intentextra.songs";
public static final String INTENT_EXTRA_SHUFFLE_MODE = PHONOGRAPH_PACKAGE_NAME + ".intentextra.shufflemode";
public static final String APP_WIDGET_UPDATE = PHONOGRAPH_PACKAGE_NAME + ".appwidgetupdate"; public static final String APP_WIDGET_UPDATE = PHONOGRAPH_PACKAGE_NAME + ".appwidgetupdate";
public static final String EXTRA_APP_WIDGET_NAME = PHONOGRAPH_PACKAGE_NAME + "app_widget_name"; public static final String EXTRA_APP_WIDGET_NAME = PHONOGRAPH_PACKAGE_NAME + "app_widget_name";
@ -294,6 +297,20 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
pause(); pause();
break; break;
case ACTION_PLAY: case ACTION_PLAY:
ArrayList<Song> songs = intent.getParcelableArrayListExtra(INTENT_EXTRA_SONGS);
if (songs != null) {
int shuffleMode = intent.getIntExtra(INTENT_EXTRA_SHUFFLE_MODE, getShuffleMode());
if (intent.hasExtra(INTENT_EXTRA_SHUFFLE_MODE) && intent.getIntExtra(INTENT_EXTRA_SHUFFLE_MODE, 0) == SHUFFLE_MODE_SHUFFLE) {
int startPosition = 0;
if (!songs.isEmpty()) {
startPosition = new Random().nextInt(songs.size());
}
openQueue(songs, startPosition, false);
setShuffleMode(shuffleMode);
} else {
openQueue(songs, 0, false);
}
}
play(); play();
break; break;
case ACTION_REWIND: case ACTION_REWIND:

View file

@ -60,12 +60,6 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
private static final int LIBRARY = 0; private static final int LIBRARY = 0;
private static final int FOLDERS = 1; private static final int FOLDERS = 1;
public static final String PHONOGRAPH_PACKAGE_NAME = "com.kabouzeid.gramophone";
public static final String INTENT_ACTION_MEDIA_PLAY_SHUFFLED = PHONOGRAPH_PACKAGE_NAME + ".intent_action.play_shuffled";
public static final String INTENT_ACTION_MEDIA_PLAY = PHONOGRAPH_PACKAGE_NAME + ".intent_action.play";
public static final String INTENT_EXTRA_SONGS = PHONOGRAPH_PACKAGE_NAME + ".intent_extra.songs";
@BindView(R.id.navigation_view) @BindView(R.id.navigation_view)
NavigationView navigationView; NavigationView navigationView;
@BindView(R.id.drawer_layout) @BindView(R.id.drawer_layout)
@ -264,13 +258,6 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
handlePlaybackIntent(getIntent()); handlePlaybackIntent(getIntent());
} }
//Called when there's already an instance of MainActivity
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handlePlaybackIntent(intent);
}
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) { if (item.getItemId() == android.R.id.home) {
@ -309,25 +296,6 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
} else { } else {
MusicPlayerRemote.openQueue(songs, 0, true); MusicPlayerRemote.openQueue(songs, 0, true);
} }
} else if (intent.getAction() != null && intent.getAction().equals(MainActivity.INTENT_ACTION_MEDIA_PLAY_SHUFFLED)){
//Shuffle songs in extras
final ArrayList<Song> songs = intent.getExtras().getParcelableArrayList(INTENT_EXTRA_SONGS);
//Start the songs, setting the shuffle mode to shuffle
MusicPlayerRemote.openAndShuffleQueue(songs, true);
handled = true;
} else if (intent.getAction() != null && intent.getAction().equals(MainActivity.INTENT_ACTION_MEDIA_PLAY)){
//Shuffle songs in extras
final ArrayList<Song> songs = intent.getExtras().getParcelableArrayList(INTENT_EXTRA_SONGS);
//Start the songs, preserving the user's shuffle mode
if (MusicPlayerRemote.getShuffleMode() == MusicService.SHUFFLE_MODE_SHUFFLE) {
MusicPlayerRemote.openAndShuffleQueue(songs, true);
} else {
MusicPlayerRemote.openQueue(songs, 0, true);
}
handled = true; handled = true;
} }