diff --git a/app/build.gradle b/app/build.gradle index 899925b1..9c13bf94 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -117,37 +117,3 @@ dependencies { compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' } - -// Add support for placeholders in resource files. -// Required so that packagename can be referenced in xml. https://code.google.com/p/android/issues/detail?id=69224#c12 -def replacePlaceholdersInFile(basePath, fileName, placeholders) { - def file = new File(basePath, fileName); - - if (!file.exists()) { - logger.quiet("Unable to replace placeholders in " + file.toString() + ". File cannot be found.") - return; - } - - logger.debug("Replacing placeholders in " + file.toString()) - logger.debug("Placeholders: " + placeholders.toString()) - - def content = file.getText('UTF-8') - - placeholders.each { entry -> - content = content.replaceAll("\\\$\\{${entry.key}\\}", entry.value) - } - - file.write(content, 'UTF-8') -} - -afterEvaluate { - android.applicationVariants.all { variant -> - variant.outputs.each { output -> - output.processResources.doFirst { - // prepare placeholder map from manifestPlaceholders including applicationId placeholder - def placeholders = variant.mergedFlavor.manifestPlaceholders + [applicationId: variant.applicationId] - replacePlaceholdersInFile(resDir, 'xml/launcher_shortcuts.xml', placeholders) - } - } - } -} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 78046818..4df8d5b2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -29,8 +29,6 @@ - diff --git a/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/AppShortcutLauncherActivity.java b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/AppShortcutLauncherActivity.java index 372eca9e..47888b81 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/AppShortcutLauncherActivity.java +++ b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/AppShortcutLauncherActivity.java @@ -30,7 +30,7 @@ public class AppShortcutLauncherActivity extends Activity { Bundle extras = getIntent().getExtras(); if (extras!=null){ try { - shortcutType = ShortcutType.valueOf(extras.getString(getString(R.string.id_shortcuttype))); + 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; @@ -64,7 +64,7 @@ public class AppShortcutLauncherActivity extends Activity { } - enum PlayMode {NORMAL, SHUFFLE} + private enum PlayMode {NORMAL, SHUFFLE} private void launchMainActivityWithSongs(PlayMode playMode, ArrayList songs){ //Create a new intent to launch MainActivity Intent intent = new Intent(this, MainActivity.class); @@ -93,6 +93,7 @@ public class AppShortcutLauncherActivity extends Activity { Toast.makeText(getApplicationContext(), R.string.error_launching_shortcut, Toast.LENGTH_LONG).show(); } + public static final String KEY_SHORTCUT_TYPE = "com.kabouzeid.gramophone.appshortcuts.ShortcutType"; public enum ShortcutType { SHUFFLE_ALL, TOP_TRACKS, LAST_ADDED, NONE } diff --git a/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/DynamicShortcutManager.java b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/DynamicShortcutManager.java new file mode 100644 index 00000000..ed9b4ff1 --- /dev/null +++ b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/DynamicShortcutManager.java @@ -0,0 +1,66 @@ +package com.kabouzeid.gramophone.appshortcuts; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ShortcutInfo; +import android.content.pm.ShortcutManager; +import android.graphics.Color; +import android.graphics.drawable.Icon; + +import com.kabouzeid.gramophone.appshortcuts.shortcuttype.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @author Adrian Campos + */ + +@TargetApi(25) +public class DynamicShortcutManager { + + Context mContext; + ShortcutManager shortcutManager; + public DynamicShortcutManager(Context context){ + mContext = context; + shortcutManager = mContext.getSystemService(ShortcutManager.class); + } + + + public void initDynamicShortcuts(){ + if (shortcutManager.getDynamicShortcuts().size() == 0){ + shortcutManager.setDynamicShortcuts(getDefaultShortcuts()); + } + } + + public List getDefaultShortcuts(){ + return (Arrays.asList( + new ShuffleAllShortcutType(mContext).getShortcutInfo(), + new TopTracksShortcutType(mContext).getShortcutInfo(), + new LastAddedShortcutType(mContext).getShortcutInfo() + )); + } + + + + public static ShortcutInfo createShortcut(Context context, String id, String shortLabel, String longLabel, Icon icon, Intent intent){ + return new ShortcutInfo.Builder(context, id) + .setShortLabel(shortLabel) + .setLongLabel(longLabel) + .setIcon(icon) + .setIntent(intent) + .build(); + } + + public void tintShortcutIcons(ArrayList shortcutInfos, Color color){ + for (ShortcutInfo shortcutInfo : shortcutInfos) { + tintShortcutIcon(shortcutInfo, color); + } + } + public void tintShortcutIcon(ShortcutInfo shortcutInfo, Color color){ + //TODO Tint icons here + } + +} diff --git a/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/BaseShortcutType.java b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/BaseShortcutType.java new file mode 100644 index 00000000..fbcd274f --- /dev/null +++ b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/BaseShortcutType.java @@ -0,0 +1,55 @@ +package com.kabouzeid.gramophone.appshortcuts.shortcuttype; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ShortcutInfo; +import android.os.Bundle; + +import com.kabouzeid.gramophone.appshortcuts.AppShortcutLauncherActivity; +import com.kabouzeid.gramophone.model.Song; +import com.kabouzeid.gramophone.ui.activities.MainActivity; + +import java.util.ArrayList; + +/** + * @author Adrian Campos + */ + +@TargetApi(25) +public abstract class BaseShortcutType { + + static final String ID_PREFIX = "com.kabouzeid.gramophone.appshortcuts.id."; + + Context mContext; + + public BaseShortcutType(Context context) { + mContext = context; + } + + + abstract ShortcutInfo getShortcutInfo(); + + + + /** + * Creates an Intent that will launch MainActivtiy and immediately play {@param songs} in either shuffle or normal mode + * + * @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 intent = new Intent(mContext, 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()); + + //Put bundle in intent + intent.putExtras(b); + + return intent; + } +} diff --git a/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/LastAddedShortcutType.java b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/LastAddedShortcutType.java new file mode 100644 index 00000000..c9e4252a --- /dev/null +++ b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/LastAddedShortcutType.java @@ -0,0 +1,29 @@ +package com.kabouzeid.gramophone.appshortcuts.shortcuttype; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.pm.ShortcutInfo; +import android.graphics.drawable.Icon; + +import com.kabouzeid.gramophone.R; +import com.kabouzeid.gramophone.appshortcuts.AppShortcutLauncherActivity; + +/** + * @author Adrian Campos + */ + +@TargetApi(25) +public final class LastAddedShortcutType extends BaseShortcutType { + public LastAddedShortcutType(Context context) { + super(context); + } + + public ShortcutInfo getShortcutInfo() { + return new ShortcutInfo.Builder(mContext, ID_PREFIX + "last_added") + .setShortLabel(mContext.getString(R.string.appshortcut_lastadded_short)) + .setLongLabel(mContext.getString(R.string.appshortcut_lastadded_long)) + .setIcon(Icon.createWithResource(mContext, R.drawable.ic_library_add_white_24dp)) + .setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType.LAST_ADDED)) + .build(); + } +} diff --git a/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/ShuffleAllShortcutType.java b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/ShuffleAllShortcutType.java new file mode 100644 index 00000000..45dd4ee0 --- /dev/null +++ b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/ShuffleAllShortcutType.java @@ -0,0 +1,29 @@ +package com.kabouzeid.gramophone.appshortcuts.shortcuttype; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.pm.ShortcutInfo; +import android.graphics.drawable.Icon; + +import com.kabouzeid.gramophone.R; +import com.kabouzeid.gramophone.appshortcuts.AppShortcutLauncherActivity; + +/** + * @author Adrian Campos + */ + +@TargetApi(25) +public final class ShuffleAllShortcutType extends BaseShortcutType { + public ShuffleAllShortcutType(Context context) { + super(context); + } + + public ShortcutInfo getShortcutInfo() { + return new ShortcutInfo.Builder(mContext, ID_PREFIX + "shuffle_all") + .setShortLabel(mContext.getString(R.string.appshortcut_shuffleall_short)) + .setLongLabel(mContext.getString(R.string.appshortcut_shuffleall_long)) + .setIcon(Icon.createWithResource(mContext, R.drawable.ic_shuffle_white_24dp)) + .setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType.SHUFFLE_ALL)) + .build(); + } +} diff --git a/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/TopTracksShortcutType.java b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/TopTracksShortcutType.java new file mode 100644 index 00000000..119a916f --- /dev/null +++ b/app/src/main/java/com/kabouzeid/gramophone/appshortcuts/shortcuttype/TopTracksShortcutType.java @@ -0,0 +1,30 @@ +package com.kabouzeid.gramophone.appshortcuts.shortcuttype; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.pm.ShortcutInfo; +import android.graphics.drawable.Icon; + +import com.kabouzeid.gramophone.R; +import com.kabouzeid.gramophone.appshortcuts.AppShortcutLauncherActivity; +import com.kabouzeid.gramophone.loader.TopAndRecentlyPlayedTracksLoader; + +/** + * @author Adrian Campos + */ + +@TargetApi(25) +public final class TopTracksShortcutType extends BaseShortcutType { + public TopTracksShortcutType(Context context) { + super(context); + } + + public ShortcutInfo getShortcutInfo() { + return new ShortcutInfo.Builder(mContext, ID_PREFIX + "top_tracks") + .setShortLabel(mContext.getString(R.string.appshortcut_toptracks_short)) + .setLongLabel(mContext.getString(R.string.appshortcut_toptracks_long)) + .setIcon(Icon.createWithResource(mContext, R.drawable.ic_trending_up_white_24dp)) + .setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType.TOP_TRACKS)) + .build(); + } +} diff --git a/app/src/main/java/com/kabouzeid/gramophone/ui/activities/MainActivity.java b/app/src/main/java/com/kabouzeid/gramophone/ui/activities/MainActivity.java index fe46ea15..15234752 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/ui/activities/MainActivity.java +++ b/app/src/main/java/com/kabouzeid/gramophone/ui/activities/MainActivity.java @@ -28,6 +28,7 @@ import com.kabouzeid.appthemehelper.ThemeStore; import com.kabouzeid.appthemehelper.util.ATHUtil; import com.kabouzeid.appthemehelper.util.NavigationViewUtil; import com.kabouzeid.gramophone.R; +import com.kabouzeid.gramophone.appshortcuts.DynamicShortcutManager; import com.kabouzeid.gramophone.dialogs.ChangelogDialog; import com.kabouzeid.gramophone.dialogs.DonationsDialog; import com.kabouzeid.gramophone.glide.SongGlideRequest; @@ -112,6 +113,9 @@ public class MainActivity extends AbsSlidingMusicPanelActivity { if (!checkShowIntro()) { checkShowChangelog(); } + + //Set up dynamic shortcuts if needed + new DynamicShortcutManager(getApplicationContext()).initDynamicShortcuts(); } private void setMusicChooser(int key) { diff --git a/app/src/main/res/xml/launcher_shortcuts.xml b/app/src/main/res/xml/launcher_shortcuts.xml deleted file mode 100644 index 598fd3df..00000000 --- a/app/src/main/res/xml/launcher_shortcuts.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - - - - - - - \ No newline at end of file