couple edits to setup widget buttons and texts properly

This commit is contained in:
tibbi 2016-01-26 18:01:41 +01:00
parent fd9612058e
commit 2f4ffd8c9f

View file

@ -12,9 +12,8 @@ import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.ColorFilter; import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.os.Bundle; import android.os.Bundle;
import android.widget.RemoteViews; import android.widget.RemoteViews;
@ -40,12 +39,7 @@ public class MyWidgetProvider extends AppWidgetProvider {
@Override @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
initVariables(context); initVariables(cxt);
cxt = context;
intent = new Intent(context, MyWidgetProvider.class);
setupButtons(appWidgetManager);
updateSongInfo();
} }
private void setupIntent(String action, int id) { private void setupIntent(String action, int id) {
@ -57,46 +51,17 @@ public class MyWidgetProvider extends AppWidgetProvider {
} }
private void initVariables(Context context) { private void initVariables(Context context) {
final SharedPreferences prefs = initPrefs(context); cxt = context;
final int defaultColor = context.getResources().getColor(R.color.dark_grey); final ComponentName component = new ComponentName(cxt, MyWidgetProvider.class);
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); widgetManager = AppWidgetManager.getInstance(cxt);
widgetIds = widgetManager.getAppWidgetIds(component); widgetIds = widgetManager.getAppWidgetIds(component);
if (widgetIds.length == 0) if (widgetIds.length == 0)
return; return;
remoteViews = getRemoteViews(widgetManager, context, widgetIds[0]); remoteViews = getRemoteViews(widgetManager, cxt, widgetIds[0]);
remoteViews.setInt(R.id.widget_holder, "setBackgroundColor", newBgColor); intent = new Intent(cxt, MyWidgetProvider.class);
setupViews(cxt);
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) { if (bus == null) {
bus = BusProvider.getInstance(); bus = BusProvider.getInstance();
@ -104,18 +69,6 @@ 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) { private SharedPreferences initPrefs(Context context) {
return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
} }
@ -163,6 +116,47 @@ public class MyWidgetProvider extends AppWidgetProvider {
widgetManager.updateAppWidget(widgetIds, remoteViews); widgetManager.updateAppWidget(widgetIds, remoteViews);
} }
private void updateColors(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, Color.WHITE);
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.songArtist, "setTextColor", newTextColor);
Bitmap bmp = getColoredIcon(context, newTextColor, R.mipmap.previous_white);
remoteViews.setInt(R.id.previousBtn, "setAlpha", alpha);
remoteViews.setImageViewBitmap(R.id.previousBtn, bmp);
playBitmap = getColoredIcon(context, newTextColor, R.mipmap.play_white);
pauseBitmap = getColoredIcon(context, newTextColor, R.mipmap.pause_white);
remoteViews.setInt(R.id.playPauseBtn, "setAlpha", alpha);
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);
}
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 LightingColorFilter(newTextColor, 1);
paint.setColorFilter(filter);
final Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(bmp, 0, 0, paint);
return bmp;
}
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (remoteViews == null || widgetManager == null || widgetIds == null || bus == null) if (remoteViews == null || widgetManager == null || widgetIds == null || bus == null)
@ -198,20 +192,26 @@ public class MyWidgetProvider extends AppWidgetProvider {
} }
} }
private void setupButtons(AppWidgetManager appWidgetManager) { private void setupButtons() {
setupIntent(PREVIOUS, R.id.previousBtn); setupIntent(PREVIOUS, R.id.previousBtn);
setupIntent(PLAYPAUSE, R.id.playPauseBtn); setupIntent(PLAYPAUSE, R.id.playPauseBtn);
setupIntent(NEXT, R.id.nextBtn); setupIntent(NEXT, R.id.nextBtn);
setupIntent(STOP, R.id.stopBtn); setupIntent(STOP, R.id.stopBtn);
appWidgetManager.updateAppWidget(widgetIds, remoteViews); }
private void setupViews(Context context) {
updateColors(context);
setupButtons();
updateSongInfo();
updatePlayPauseButton();
updateWidget();
} }
@Override @Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int widgetId, Bundle newOptions) { public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int widgetId, Bundle newOptions) {
remoteViews = getRemoteViews(appWidgetManager, context, widgetId); remoteViews = getRemoteViews(appWidgetManager, context, widgetId);
setupButtons(appWidgetManager); widgetManager = appWidgetManager;
updateSongInfo(); setupViews(context);
updatePlayPauseButton();
super.onAppWidgetOptionsChanged(context, appWidgetManager, widgetId, newOptions); super.onAppWidgetOptionsChanged(context, appWidgetManager, widgetId, newOptions);
} }