Moved shortcut handling from MainActivity to MusicService. Cleaned up the code.
This commit is contained in:
parent
d60ca48c2b
commit
b0590bf5e1
7 changed files with 50 additions and 80 deletions
|
|
@ -3,6 +3,7 @@ package com.kabouzeid.gramophone.appshortcuts;
|
|||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.IntDef;
|
||||
|
||||
import com.kabouzeid.gramophone.appshortcuts.shortcuttype.LastAddedShortcutType;
|
||||
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.TopAndRecentlyPlayedTracksLoader;
|
||||
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;
|
||||
|
||||
/**
|
||||
|
|
@ -20,40 +23,40 @@ import java.util.ArrayList;
|
|||
*/
|
||||
|
||||
public class AppShortcutLauncherActivity extends Activity {
|
||||
|
||||
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
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
ShortcutType shortcutType = ShortcutType.NONE;
|
||||
@ShortcutType
|
||||
int shortcutType = SHORTCUT_TYPE_NONE;
|
||||
|
||||
//Set shortcutType from the intent extras
|
||||
Bundle extras = getIntent().getExtras();
|
||||
if (extras != null) {
|
||||
try {
|
||||
shortcutType = ShortcutType.valueOf(extras.getString(KEY_SHORTCUT_TYPE));
|
||||
} catch (IllegalArgumentException e) { //In the event we're somehow passed an invalid enum string, don't crash.
|
||||
e.printStackTrace();
|
||||
shortcutType = ShortcutType.NONE;
|
||||
}
|
||||
//noinspection WrongConstant
|
||||
shortcutType = extras.getInt(KEY_SHORTCUT_TYPE, SHORTCUT_TYPE_NONE);
|
||||
}
|
||||
|
||||
//Perform the action found in the extras
|
||||
switch (shortcutType) {
|
||||
case SHUFFLE_ALL:
|
||||
launchMainActivityWithSongs(PlayMode.SHUFFLE,
|
||||
case SHORTCUT_TYPE_SHUFFLE_ALL:
|
||||
startServiceWithSongs(MusicService.SHUFFLE_MODE_SHUFFLE,
|
||||
SongLoader.getAllSongs(getApplicationContext()));
|
||||
DynamicShortcutManager.reportShortcutUsed(this, ShuffleAllShortcutType.getId());
|
||||
break;
|
||||
case TOP_TRACKS:
|
||||
launchMainActivityWithSongs(PlayMode.NORMAL,
|
||||
case SHORTCUT_TYPE_TOP_TRACKS:
|
||||
startServiceWithSongs(MusicService.SHUFFLE_MODE_NONE,
|
||||
TopAndRecentlyPlayedTracksLoader.getTopTracks(getApplicationContext()));
|
||||
DynamicShortcutManager.reportShortcutUsed(this, TopTracksShortcutType.getId());
|
||||
break;
|
||||
case LAST_ADDED:
|
||||
launchMainActivityWithSongs(PlayMode.NORMAL,
|
||||
case SHORTCUT_TYPE_LAST_ADDED:
|
||||
startServiceWithSongs(MusicService.SHUFFLE_MODE_NONE,
|
||||
LastAddedLoader.getLastAddedSongs(getApplicationContext()));
|
||||
DynamicShortcutManager.reportShortcutUsed(this, LastAddedShortcutType.getId());
|
||||
break;
|
||||
|
|
@ -62,36 +65,21 @@ public class AppShortcutLauncherActivity extends Activity {
|
|||
finish();
|
||||
}
|
||||
|
||||
private void launchMainActivityWithSongs(PlayMode playMode, ArrayList<Song> songs) {
|
||||
//Create a new intent to launch MainActivity
|
||||
Intent intent = new Intent(this, MainActivity.class);
|
||||
switch (playMode) {
|
||||
case NORMAL:
|
||||
intent.setAction(MainActivity.INTENT_ACTION_MEDIA_PLAY);
|
||||
break;
|
||||
case SHUFFLE:
|
||||
intent.setAction(MainActivity.INTENT_ACTION_MEDIA_PLAY_SHUFFLED);
|
||||
break;
|
||||
}
|
||||
private void startServiceWithSongs(int shuffleMode, ArrayList<Song> songs) {
|
||||
Intent intent = new Intent(this, MusicService.class);
|
||||
intent.setAction(MusicService.ACTION_PLAY);
|
||||
|
||||
|
||||
//Create a bundle to store the songs to shuffle through songs
|
||||
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);
|
||||
|
||||
//If MainActivity's already running, don't launch another instance. Instead, bring it to the top and deliver the intent to onNewIntent()
|
||||
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);
|
||||
startService(intent);
|
||||
}
|
||||
|
||||
private enum PlayMode {NORMAL, SHUFFLE}
|
||||
|
||||
public enum ShortcutType {
|
||||
SHUFFLE_ALL, TOP_TRACKS, LAST_ADDED, NONE
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef({SHORTCUT_TYPE_SHUFFLE_ALL, SHORTCUT_TYPE_TOP_TRACKS, SHORTCUT_TYPE_LAST_ADDED, SHORTCUT_TYPE_NONE})
|
||||
public @interface ShortcutType {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,16 +37,13 @@ public abstract class BaseShortcutType {
|
|||
* @param shortcutType Describes the type of shortcut to create (ShuffleAll, TopTracks, custom playlist, etc.)
|
||||
* @return
|
||||
*/
|
||||
Intent getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType shortcutType) {
|
||||
//Create a new intent to launch MainActivity
|
||||
Intent getPlaySongsIntent(@AppShortcutLauncherActivity.ShortcutType int shortcutType) {
|
||||
Intent intent = new Intent(context, AppShortcutLauncherActivity.class);
|
||||
intent.setAction(Intent.ACTION_VIEW);
|
||||
|
||||
//Create a bundle to store instructions for AppShortcutLauncherActivity
|
||||
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);
|
||||
|
||||
return intent;
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ public final class LastAddedShortcutType extends BaseShortcutType {
|
|||
.setShortLabel(context.getString(R.string.app_shortcut_last_added_short))
|
||||
.setLongLabel(context.getString(R.string.app_shortcut_last_added_long))
|
||||
.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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ public final class ShuffleAllShortcutType extends BaseShortcutType {
|
|||
.setShortLabel(context.getString(R.string.app_shortcut_shuffle_all_short))
|
||||
.setLongLabel(context.getString(R.string.app_shortcut_shuffle_all_long))
|
||||
.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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ public final class TopTracksShortcutType extends BaseShortcutType {
|
|||
.setShortLabel(context.getString(R.string.app_shortcut_top_tracks_short))
|
||||
.setLongLabel(context.getString(R.string.app_shortcut_top_tracks_long))
|
||||
.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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -61,6 +61,7 @@ import com.kabouzeid.gramophone.util.Util;
|
|||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @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_REWIND = PHONOGRAPH_PACKAGE_NAME + ".rewind";
|
||||
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 EXTRA_APP_WIDGET_NAME = PHONOGRAPH_PACKAGE_NAME + "app_widget_name";
|
||||
|
|
@ -294,6 +297,20 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
|||
pause();
|
||||
break;
|
||||
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();
|
||||
break;
|
||||
case ACTION_REWIND:
|
||||
|
|
|
|||
|
|
@ -60,12 +60,6 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
|
|||
private static final int LIBRARY = 0;
|
||||
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)
|
||||
NavigationView navigationView;
|
||||
@BindView(R.id.drawer_layout)
|
||||
|
|
@ -264,13 +258,6 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
|
|||
handlePlaybackIntent(getIntent());
|
||||
}
|
||||
|
||||
//Called when there's already an instance of MainActivity
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
handlePlaybackIntent(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
|
|
@ -309,25 +296,6 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
|
|||
} else {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue