Support theme colors in AppShortcut icons

This commit is contained in:
Adrian Campos 2017-03-18 17:47:11 -07:00 committed by Karim Abou Zeid
commit 49a3d0b28a
11 changed files with 95 additions and 48 deletions

View file

@ -0,0 +1,54 @@
package com.kabouzeid.gramophone.appshortcuts;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.TypedValue;
import com.kabouzeid.appthemehelper.ThemeStore;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.util.Util;
/**
* @author Adrian Campos
*/
@RequiresApi(Build.VERSION_CODES.N_MR1)
public final class AppShortcutIconGenerator {
public static Icon generateThemedIcon(Context context, int iconId) {
//Get background color from context's theme
final TypedValue typedColorBackground = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.colorBackground, typedColorBackground, true);
//Return an Icon of iconId with those colors
return generateThemedIcon(context, iconId,
ThemeStore.primaryColor(context),
typedColorBackground.data
);
}
public static Icon generateThemedIcon(Context context, int iconId, int foregroundColor, int backgroundColor) {
//Get and tint foreground and background drawables
Drawable vectorDrawable = Util.getTintedVectorDrawable(context, iconId, foregroundColor);
Drawable backgroundDrawable = Util.getTintedVectorDrawable(context, R.drawable.ic_app_shortcut_background, backgroundColor);
//Squash the two drawables together
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{backgroundDrawable, vectorDrawable});
//Return as an Icon
return Icon.createWithBitmap(drawableToBitmap(layerDrawable));
}
private static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}

View file

@ -47,6 +47,10 @@ public class DynamicShortcutManager {
}
}
public void updateDynamicShortcuts() {
shortcutManager.updateShortcuts(getDefaultShortcuts());
}
public List<ShortcutInfo> getDefaultShortcuts() {
return (Arrays.asList(
new ShuffleAllShortcutType(mContext).getShortcutInfo(),
@ -54,15 +58,4 @@ public class DynamicShortcutManager {
new LastAddedShortcutType(mContext).getShortcutInfo()
));
}
public void tintShortcutIcons(ArrayList<ShortcutInfo> shortcutInfos, Color color) {
for (ShortcutInfo shortcutInfo : shortcutInfos) {
tintShortcutIcon(shortcutInfo, color);
}
}
public void tintShortcutIcon(ShortcutInfo shortcutInfo, Color color) {
//TODO Tint icons here
}
}

View file

@ -3,9 +3,9 @@ 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.AppShortcutIconGenerator;
import com.kabouzeid.gramophone.appshortcuts.AppShortcutLauncherActivity;
/**
@ -22,7 +22,7 @@ public final class LastAddedShortcutType extends BaseShortcutType {
return new ShortcutInfo.Builder(mContext, ID_PREFIX + "last_added")
.setShortLabel(mContext.getString(R.string.app_shortcut_last_added_short))
.setLongLabel(mContext.getString(R.string.app_shortcut_last_added_long))
.setIcon(Icon.createWithResource(mContext, R.drawable.ic_app_shortcut_last_added))
.setIcon(AppShortcutIconGenerator.generateThemedIcon(mContext, R.drawable.ic_app_shortcut_last_added))
.setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType.LAST_ADDED))
.build();
}

View file

@ -3,9 +3,9 @@ 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.AppShortcutIconGenerator;
import com.kabouzeid.gramophone.appshortcuts.AppShortcutLauncherActivity;
/**
@ -22,7 +22,7 @@ public final class ShuffleAllShortcutType extends BaseShortcutType {
return new ShortcutInfo.Builder(mContext, ID_PREFIX + "shuffle_all")
.setShortLabel(mContext.getString(R.string.app_shortcut_shuffle_all_short))
.setLongLabel(mContext.getString(R.string.app_shortcut_shuffle_all_long))
.setIcon(Icon.createWithResource(mContext, R.drawable.ic_app_shortcut_shuffle_all))
.setIcon(AppShortcutIconGenerator.generateThemedIcon(mContext, R.drawable.ic_app_shortcut_shuffle_all))
.setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType.SHUFFLE_ALL))
.build();
}

View file

@ -3,9 +3,9 @@ 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.AppShortcutIconGenerator;
import com.kabouzeid.gramophone.appshortcuts.AppShortcutLauncherActivity;
/**
@ -22,7 +22,7 @@ public final class TopTracksShortcutType extends BaseShortcutType {
return new ShortcutInfo.Builder(mContext, ID_PREFIX + "top_tracks")
.setShortLabel(mContext.getString(R.string.app_shortcut_top_tracks_short))
.setLongLabel(mContext.getString(R.string.app_shortcut_top_tracks_long))
.setIcon(Icon.createWithResource(mContext, R.drawable.ic_app_shortcut_top_tracks))
.setIcon(AppShortcutIconGenerator.generateThemedIcon(mContext, R.drawable.ic_app_shortcut_top_tracks))
.setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.ShortcutType.TOP_TRACKS))
.build();
}

View file

@ -116,7 +116,7 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
//Set up dynamic shortcuts
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
new DynamicShortcutManager(getApplicationContext()).initDynamicShortcuts();
new DynamicShortcutManager(this).initDynamicShortcuts();
}
}

View file

@ -25,6 +25,7 @@ import com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEColorPreference;
import com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEPreferenceFragmentCompat;
import com.kabouzeid.appthemehelper.util.ColorUtil;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.appshortcuts.DynamicShortcutManager;
import com.kabouzeid.gramophone.preferences.NowPlayingScreenPreference;
import com.kabouzeid.gramophone.preferences.NowPlayingScreenPreferenceDialog;
import com.kabouzeid.gramophone.ui.activities.base.AbsBaseActivity;
@ -78,6 +79,10 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
.commit();
break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
new DynamicShortcutManager(this).updateDynamicShortcuts();
}
recreate();
}
@ -171,6 +176,13 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
ThemeStore.editTheme(getActivity())
.activityTheme(PreferenceUtil.getThemeResFromPrefValue((String) o))
.commit();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
//Set the new theme so that updateAppShortcuts can pull it
getActivity().setTheme(PreferenceUtil.getThemeResFromPrefValue((String) o));
new DynamicShortcutManager(getActivity()).updateDynamicShortcuts();
}
getActivity().recreate();
return true;
}

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="176dp"
android:height="176dp"
android:viewportHeight="176"
android:viewportWidth="176">
<group
android:pivotX="88"
android:pivotY="88"
android:scaleX="0.916"
android:scaleY="0.916">
<path
android:name="ic_app_shortcut_background"
android:fillColor="#f5f5f5"
android:pathData="M 88 0 C 136.601057985 0 176 39.3989420149 176 88 C 176 136.601057985 136.601057985 176 88 176 C 39.3989420149 176 0 136.601057985 0 88 C 0 39.3989420149 39.3989420149 0 88 0 Z" />
</group>
</vector>

View file

@ -5,16 +5,6 @@
android:viewportHeight="176"
android:viewportWidth="176">
<group
android:pivotX="88"
android:pivotY="88"
android:scaleX="0.916"
android:scaleY="0.916">
<path
android:name="ic_app_shortcut_last_added_bg"
android:fillColor="#f5f5f5"
android:pathData="M 88 0 C 136.601057985 0 176 39.3989420149 176 88 C 176 136.601057985 136.601057985 176 88 176 C 39.3989420149 176 0 136.601057985 0 88 C 0 39.3989420149 39.3989420149 0 88 0 Z" />
</group>
<group
android:pivotX="88"
android:pivotY="88"

View file

@ -5,16 +5,6 @@
android:viewportHeight="176"
android:viewportWidth="176">
<group
android:pivotX="88"
android:pivotY="88"
android:scaleX="0.916"
android:scaleY="0.916">
<path
android:name="ic_app_shortcut_shuffle_bg"
android:fillColor="#f5f5f5"
android:pathData="M 88 0 C 136.601057985 0 176 39.3989420149 176 88 C 176 136.601057985 136.601057985 176 88 176 C 39.3989420149 176 0 136.601057985 0 88 C 0 39.3989420149 39.3989420149 0 88 0 Z" />
</group>
<group
android:pivotX="88"
android:pivotY="88"

View file

@ -5,16 +5,6 @@
android:viewportHeight="176"
android:viewportWidth="176">
<group
android:pivotX="88"
android:pivotY="88"
android:scaleX="0.916"
android:scaleY="0.916">
<path
android:name="ic_app_shortcut_top_tracks_bg"
android:fillColor="#f5f5f5"
android:pathData="M 88 0 C 136.601057985 0 176 39.3989420149 176 88 C 176 136.601057985 136.601057985 176 88 176 C 39.3989420149 176 0 136.601057985 0 88 C 0 39.3989420149 39.3989420149 0 88 0 Z" />
</group>
<group
android:pivotX="88"
android:pivotY="88"