Added basic support for App Shortcuts

Shuffle All, Top Tracks, and Last added. Still need to fix icons and add dynamic shortcuts for playlists
This commit is contained in:
Adrian Campos 2017-03-08 23:44:27 -08:00 committed by Karim Abou Zeid
commit 33ff49de0f
7 changed files with 232 additions and 0 deletions

View file

@ -0,0 +1,99 @@
package com.kabouzeid.gramophone.appshortcuts;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.kabouzeid.gramophone.R;
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 java.util.ArrayList;
/**
* @author Adrian Campos
*/
public class AppShortcutLauncherActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ShortcutType shortcutType = ShortcutType.NONE;
//Set shortcutType from the intent extras
Bundle extras = getIntent().getExtras();
if (extras!=null){
try {
shortcutType = ShortcutType.valueOf(extras.getString(getString(R.string.id_shortcuttype)));
} 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) {
case SHUFFLE_ALL:
launchMainActivityWithSongs(PlayMode.SHUFFLE,
SongLoader.getAllSongs(getApplicationContext()));
break;
case TOP_TRACKS:
launchMainActivityWithSongs(PlayMode.NORMAL,
TopAndRecentlyPlayedTracksLoader.getRecentlyPlayedTracks(getApplicationContext()));
break;
case LAST_ADDED:
launchMainActivityWithSongs(PlayMode.NORMAL,
LastAddedLoader.getLastAddedSongs(getApplicationContext()));
break;
case NONE:
shortcutError();
break;
default:
shortcutError();
break;
}
finish();
}
enum PlayMode {NORMAL, SHUFFLE}
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;
}
//Create a bundle to store the songs to shuffle through songs
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(MainActivity.INTENT_EXTRA_SONGS, songs);
//Put the bundle in the intent
intent.putExtras(bundle);
//Finally, start MainActivity with those extras
startActivity(intent);
}
private void shortcutError(){
Toast.makeText(getApplicationContext(), R.string.error_launching_shortcut, Toast.LENGTH_LONG).show();
}
public enum ShortcutType {
SHUFFLE_ALL, TOP_TRACKS, LAST_ADDED, NONE
}
}

View file

@ -60,6 +60,12 @@ 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)
@ -296,6 +302,24 @@ 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);
} 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);
}
}
if (uri != null && uri.toString().length() > 0) {