update the flashlight widget behaviour

This commit is contained in:
tibbi 2016-09-25 17:41:23 +02:00
parent b01b499bf2
commit 167c26dff4
3 changed files with 23 additions and 17 deletions

View File

@ -9,6 +9,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.widget.RemoteViews;
@ -22,6 +23,7 @@ public class MyWidgetProvider extends AppWidgetProvider {
private static RemoteViews mRemoteViews;
private static AppWidgetManager mWidgetManager;
private static Bitmap mColoredBmp;
private static Bitmap mWhiteBmp;
private static Bus mBus;
private static Context mContext;
@ -50,11 +52,12 @@ public class MyWidgetProvider extends AppWidgetProvider {
final SharedPreferences prefs = initPrefs(context);
final Resources res = context.getResources();
final int defaultColor = res.getColor(R.color.colorPrimary);
final int appColor = prefs.getInt(Constants.WIDGET_COLOR, defaultColor);
final int selectedColor = prefs.getInt(Constants.WIDGET_COLOR, defaultColor);
final int alpha = Color.alpha(selectedColor);
final Drawable drawable = context.getResources().getDrawable(R.drawable.circles_small);
drawable.mutate().setColorFilter(appColor, PorterDuff.Mode.SRC_ATOP);
mColoredBmp = Utils.drawableToBitmap(drawable);
mColoredBmp = getColoredCircles(selectedColor, alpha);
mWhiteBmp = getColoredCircles(Color.WHITE, alpha);
mRemoteViews.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp);
if (mBus == null) {
mBus = BusProvider.getInstance();
@ -75,6 +78,13 @@ public class MyWidgetProvider extends AppWidgetProvider {
super.onReceive(context, intent);
}
private Bitmap getColoredCircles(int color, int alpha) {
final Drawable drawable = mContext.getResources().getDrawable(R.drawable.circles_small);
drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
drawable.mutate().setAlpha(alpha);
return Utils.drawableToBitmap(drawable);
}
public void enableFlashlight() {
mRemoteViews.setImageViewBitmap(R.id.toggle_btn, mColoredBmp);
for (int widgetId : mWidgetIds) {
@ -83,7 +93,7 @@ public class MyWidgetProvider extends AppWidgetProvider {
}
public void disableFlashlight() {
mRemoteViews.setImageViewResource(R.id.toggle_btn, R.drawable.circles_small);
mRemoteViews.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp);
for (int widgetId : mWidgetIds) {
mWidgetManager.updateAppWidget(widgetId, mRemoteViews);
}

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="24dp"
android:height="24dp"
android:bottom="24dp"
android:left="24dp"
android:right="24dp"
android:top="24dp">
<shape
android:shape="oval">
@ -15,9 +15,9 @@
</item>
<item
android:width="48dp"
android:height="48dp"
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp">
<shape
android:shape="oval">
@ -28,9 +28,7 @@
</shape>
</item>
<item
android:width="72dp"
android:height="72dp">
<item>
<shape
android:shape="oval">
<stroke

View File

@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/toggle_btn"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:src="@drawable/circles_small"/>
android:id="@+id/toggle_btn"
android:layout_width="60dp"
android:layout_height="60dp"/>