allow changing the texts and buttons color too

This commit is contained in:
tibbi 2016-01-25 23:56:08 +01:00
parent a4c753897a
commit fd9612058e
4 changed files with 198 additions and 44 deletions

View file

@ -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";
}

View file

@ -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);
}
private void updateBackgroundColor() {
newBgColor = adjustAlpha(bgColorWithoutTransparency, bgAlpha);
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();
}
private void updateBackgroundColor() {
newBgColor = adjustAlpha(bgColorWithoutTransparency, bgAlpha);
background.setBackgroundColor(newBgColor);
saveBtn.setBackgroundColor(newBgColor);
}
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);
return Color.argb(alpha, red, green, blue);
}
@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) {
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);
}
}

View file

@ -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,6 +51,8 @@ public class MyWidgetProvider extends AppWidgetProvider {
private void setupIntent(String action, int id) {
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getBroadcast(cxt, 0, intent, 0);
if (remoteViews != null)
remoteViews.setOnClickPendingIntent(id, pendingIntent);
}
@ -48,6 +60,7 @@ public class MyWidgetProvider extends AppWidgetProvider {
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();
}

View file

@ -17,23 +17,47 @@
android:layout_marginBottom="@dimen/activity_margin"/>
<Button
android:id="@+id/config_background_color"
android:id="@+id/config_bg_color"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@+id/config_text_color"/>
<RelativeLayout
android:id="@+id/config_bg_seekbar_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/config_bg_color"
android:layout_alignTop="@+id/config_bg_color"
android:layout_toRightOf="@+id/config_bg_color"
android:background="@android:color/white">
<SeekBar
android:id="@+id/config_bg_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"/>
</RelativeLayout>
<Button
android:id="@+id/config_text_color"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"/>
<RelativeLayout
android:id="@+id/config_seekbar_holder"
android:id="@+id/config_text_seekbar_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignTop="@+id/config_background_color"
android:layout_alignTop="@+id/config_text_color"
android:layout_toLeftOf="@+id/config_save"
android:layout_toRightOf="@+id/config_background_color"
android:layout_toRightOf="@+id/config_text_color"
android:background="@android:color/white">
<SeekBar
android:id="@+id/config_seekbar"
android:id="@+id/config_text_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
@ -45,9 +69,9 @@
android:id="@+id/config_save"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@id/config_background_color"
android:layout_alignBottom="@+id/config_text_color"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/config_background_color"
android:layout_alignTop="@+id/config_text_color"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:text="Save"