From fd9612058ec29bca8f2ca887156369d5e2908523 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 25 Jan 2016 23:56:08 +0100 Subject: [PATCH] allow changing the texts and buttons color too --- .../simplemobiletools/com/Constants.java | 1 + .../com/MyWidgetConfigure.java | 140 +++++++++++++----- .../com/MyWidgetProvider.java | 63 +++++++- app/src/main/res/layout/widget_config.xml | 38 ++++- 4 files changed, 198 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/musicplayer/simplemobiletools/com/Constants.java b/app/src/main/java/musicplayer/simplemobiletools/com/Constants.java index 9c4f5b99..110e5ceb 100644 --- a/app/src/main/java/musicplayer/simplemobiletools/com/Constants.java +++ b/app/src/main/java/musicplayer/simplemobiletools/com/Constants.java @@ -3,4 +3,5 @@ package musicplayer.simplemobiletools.com; public class Constants { public static final String PREFS = "prefs"; public static final String WIDGET_BG_COLOR = "widget_bg_color"; + public static final String WIDGET_TEXT_COLOR = "widget_text_color"; } diff --git a/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetConfigure.java b/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetConfigure.java index c5d6c7fc..144c5ea6 100644 --- a/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetConfigure.java +++ b/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetConfigure.java @@ -6,9 +6,11 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; +import android.graphics.PorterDuff; import android.os.Bundle; import android.view.View; import android.widget.Button; +import android.widget.ImageView; import android.widget.RemoteViews; import android.widget.SeekBar; import android.widget.TextView; @@ -18,18 +20,31 @@ import butterknife.ButterKnife; import butterknife.OnClick; import yuku.ambilwarna.AmbilWarnaDialog; -public class MyWidgetConfigure extends Activity implements SeekBar.OnSeekBarChangeListener { - @Bind(R.id.config_seekbar) SeekBar seekBar; - @Bind(R.id.config_player) View background; - @Bind(R.id.config_background_color) View backgroundColorPicker; +public class MyWidgetConfigure extends Activity { + @Bind(R.id.config_bg_seekbar) SeekBar bgSeekBar; + @Bind(R.id.config_text_seekbar) SeekBar textSeekBar; + @Bind(R.id.config_player) View widgetBackground; + @Bind(R.id.config_bg_color) View bgColorPicker; + @Bind(R.id.config_text_color) View textColorPicker; + @Bind(R.id.config_save) Button saveBtn; + @Bind(R.id.songTitle) TextView songTitle; @Bind(R.id.songArtist) TextView songArtist; - @Bind(R.id.config_save) Button saveBtn; + + @Bind(R.id.previousBtn) ImageView prevBtn; + @Bind(R.id.playPauseBtn) ImageView playPauseBtn; + @Bind(R.id.nextBtn) ImageView nextBtn; + @Bind(R.id.stopBtn) ImageView stopBtn; + private int widgetId; private int newBgColor; private int bgColorWithoutTransparency; private float bgAlpha; + private int newTextColor; + private int textColorWithoutTransparency; + private float textAlpha; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -50,14 +65,19 @@ public class MyWidgetConfigure extends Activity implements SeekBar.OnSeekBarChan private void initVariables() { bgAlpha = 0.5f; - seekBar.setOnSeekBarChangeListener(this); - seekBar.setProgress((int) (bgAlpha * 100)); + bgSeekBar.setOnSeekBarChangeListener(bgSeekbarChangeListener); + bgSeekBar.setProgress((int) (bgAlpha * 100)); newBgColor = Color.BLACK; - newBgColor = adjustAlpha(newBgColor, bgAlpha); - bgColorWithoutTransparency = Color.BLACK; - background.setBackgroundColor(newBgColor); - saveBtn.setBackgroundColor(newBgColor); - backgroundColorPicker.setBackgroundColor(Color.BLACK); + bgColorWithoutTransparency = newBgColor; + updateBackgroundColor(); + + textAlpha = 1.f; + textSeekBar.setOnSeekBarChangeListener(textSeekbarChangeListener); + textSeekBar.setProgress((int) (textAlpha * 100)); + newTextColor = Color.WHITE; + textColorWithoutTransparency = newTextColor; + updateTextColor(); + songTitle.setText("Song Title"); songArtist.setText("Song Artist"); } @@ -78,7 +98,7 @@ public class MyWidgetConfigure extends Activity implements SeekBar.OnSeekBarChan finish(); } - @OnClick(R.id.config_background_color) + @OnClick(R.id.config_bg_color) public void pickBackgroundColor() { AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, bgColorWithoutTransparency, new AmbilWarnaDialog.OnAmbilWarnaListener() { @Override @@ -87,7 +107,6 @@ public class MyWidgetConfigure extends Activity implements SeekBar.OnSeekBarChan @Override public void onOk(AmbilWarnaDialog dialog, int color) { - backgroundColorPicker.setBackgroundColor(color); bgColorWithoutTransparency = color; updateBackgroundColor(); } @@ -96,9 +115,27 @@ public class MyWidgetConfigure extends Activity implements SeekBar.OnSeekBarChan dialog.show(); } + @OnClick(R.id.config_text_color) + public void pickTextColor() { + AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, textColorWithoutTransparency, new AmbilWarnaDialog.OnAmbilWarnaListener() { + @Override + public void onCancel(AmbilWarnaDialog dialog) { + } + + @Override + public void onOk(AmbilWarnaDialog dialog, int color) { + textColorWithoutTransparency = color; + updateTextColor(); + } + }); + + dialog.show(); + } + private void storeWidgetBackground() { final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); prefs.edit().putInt(Constants.WIDGET_BG_COLOR, newBgColor).apply(); + prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, newTextColor).apply(); } private void requestWidgetUpdate() { @@ -107,31 +144,68 @@ public class MyWidgetConfigure extends Activity implements SeekBar.OnSeekBarChan sendBroadcast(intent); } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - bgAlpha = (float) progress / (float) 100; - updateBackgroundColor(); - } - private void updateBackgroundColor() { newBgColor = adjustAlpha(bgColorWithoutTransparency, bgAlpha); - background.setBackgroundColor(newBgColor); + widgetBackground.setBackgroundColor(newBgColor); + bgColorPicker.setBackgroundColor(newBgColor); saveBtn.setBackgroundColor(newBgColor); } + private void updateTextColor() { + newTextColor = adjustAlpha(textColorWithoutTransparency, textAlpha); + textColorPicker.setBackgroundColor(newTextColor); + + saveBtn.setTextColor(newTextColor); + songTitle.setTextColor(newTextColor); + songArtist.setTextColor(newTextColor); + + prevBtn.getDrawable().mutate().setColorFilter(newTextColor, PorterDuff.Mode.SRC_IN); + playPauseBtn.getDrawable().mutate().setColorFilter(newTextColor, PorterDuff.Mode.SRC_IN); + nextBtn.getDrawable().mutate().setColorFilter(newTextColor, PorterDuff.Mode.SRC_IN); + stopBtn.getDrawable().mutate().setColorFilter(newTextColor, PorterDuff.Mode.SRC_IN); + } + + private SeekBar.OnSeekBarChangeListener bgSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + bgAlpha = (float) progress / (float) 100; + updateBackgroundColor(); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + + } + }; + + private SeekBar.OnSeekBarChangeListener textSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + textAlpha = (float) progress / (float) 100; + updateTextColor(); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + + } + }; + private int adjustAlpha(int color, float factor) { - int alpha = Math.round(Color.alpha(color) * factor); - int red = Color.red(color); - int green = Color.green(color); - int blue = Color.blue(color); + final int alpha = Math.round(Color.alpha(color) * factor); + final int red = Color.red(color); + final int green = Color.green(color); + final int blue = Color.blue(color); return Color.argb(alpha, red, green, blue); } - - @Override - public void onStartTrackingTouch(SeekBar seekBar) { - } - - @Override - public void onStopTrackingTouch(SeekBar seekBar) { - } } diff --git a/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetProvider.java b/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetProvider.java index 7fb69719..8ce46a91 100644 --- a/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetProvider.java +++ b/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetProvider.java @@ -7,6 +7,14 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.ColorFilter; +import android.graphics.Paint; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; import android.os.Bundle; import android.widget.RemoteViews; @@ -27,6 +35,8 @@ public class MyWidgetProvider extends AppWidgetProvider { private static Bus bus; private static Song currSong; private static boolean isPlaying; + private static Bitmap playBitmap; + private static Bitmap pauseBitmap; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { @@ -41,13 +51,16 @@ public class MyWidgetProvider extends AppWidgetProvider { private void setupIntent(String action, int id) { intent.setAction(action); PendingIntent pendingIntent = PendingIntent.getBroadcast(cxt, 0, intent, 0); - remoteViews.setOnClickPendingIntent(id, pendingIntent); + + if (remoteViews != null) + remoteViews.setOnClickPendingIntent(id, pendingIntent); } private void initVariables(Context context) { final SharedPreferences prefs = initPrefs(context); final int defaultColor = context.getResources().getColor(R.color.dark_grey); final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor); + final int newTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, defaultColor); final ComponentName component = new ComponentName(context, MyWidgetProvider.class); widgetManager = AppWidgetManager.getInstance(context); @@ -57,6 +70,32 @@ public class MyWidgetProvider extends AppWidgetProvider { remoteViews = getRemoteViews(widgetManager, context, widgetIds[0]); remoteViews.setInt(R.id.widget_holder, "setBackgroundColor", newBgColor); + + final int alpha = Color.alpha(newTextColor); + remoteViews.setInt(R.id.songTitle, "setTextColor", newTextColor); + remoteViews.setInt(R.id.songTitle, "setAlpha", alpha); + remoteViews.setInt(R.id.songArtist, "setTextColor", newTextColor); + remoteViews.setInt(R.id.songArtist, "setAlpha", alpha); + + Bitmap bmp = getColoredIcon(context, newTextColor, R.mipmap.previous_white); + remoteViews.setInt(R.id.previousBtn, "setAlpha", alpha); + remoteViews.setImageViewBitmap(R.id.previousBtn, bmp); + updateWidget(); + + playBitmap = getColoredIcon(context, newTextColor, R.mipmap.play_white); + pauseBitmap = getColoredIcon(context, newTextColor, R.mipmap.pause_white); + remoteViews.setInt(R.id.playPauseBtn, "setAlpha", alpha); + updatePlayPauseButton(); + + bmp = getColoredIcon(context, newTextColor, R.mipmap.next_white); + remoteViews.setInt(R.id.nextBtn, "setAlpha", alpha); + remoteViews.setImageViewBitmap(R.id.nextBtn, bmp); + + bmp = getColoredIcon(context, newTextColor, R.mipmap.stop_white); + remoteViews.setInt(R.id.stopBtn, "setAlpha", alpha); + remoteViews.setImageViewBitmap(R.id.stopBtn, bmp); + + updateWidget(); updateSongInfo(); if (bus == null) { @@ -65,6 +104,18 @@ public class MyWidgetProvider extends AppWidgetProvider { } } + private Bitmap getColoredIcon(Context context, int newTextColor, int id) { + final BitmapFactory.Options options = new BitmapFactory.Options(); + options.inMutable = true; + final Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), id, options); + final Paint paint = new Paint(); + final ColorFilter filter = new PorterDuffColorFilter(newTextColor, PorterDuff.Mode.SRC_IN); + paint.setColorFilter(filter); + final Canvas canvas = new Canvas(bmp); + canvas.drawBitmap(bmp, 0, 0, paint); + return bmp; + } + private SharedPreferences initPrefs(Context context) { return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); } @@ -82,6 +133,10 @@ public class MyWidgetProvider extends AppWidgetProvider { title = currSong.getTitle(); artist = currSong.getArtist(); } + + if (remoteViews == null) + return; + remoteViews.setTextViewText(R.id.songTitle, title); remoteViews.setTextViewText(R.id.songArtist, artist); updateWidget(); @@ -95,12 +150,12 @@ public class MyWidgetProvider extends AppWidgetProvider { } private void updatePlayPauseButton() { - int icon = R.mipmap.play_white; + Bitmap bmp = playBitmap; if (isPlaying) - icon = R.mipmap.pause_white; + bmp = pauseBitmap; - remoteViews.setImageViewResource(R.id.playPauseBtn, icon); + remoteViews.setImageViewBitmap(R.id.playPauseBtn, bmp); updateWidget(); } diff --git a/app/src/main/res/layout/widget_config.xml b/app/src/main/res/layout/widget_config.xml index 141d5ac0..b8440fa5 100644 --- a/app/src/main/res/layout/widget_config.xml +++ b/app/src/main/res/layout/widget_config.xml @@ -17,23 +17,47 @@ android:layout_marginBottom="@dimen/activity_margin"/>