From c9accb65f28cbabb84bfe416a8c4536f8f1c2627 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 30 Aug 2016 21:20:39 +0200 Subject: [PATCH] fix #10, allow changing the widgets active color --- app/src/main/AndroidManifest.xml | 2 +- .../flashlight/MyWidgetProvider.java | 9 +++- .../simplemobiletools/flashlight/Utils.java | 26 ++++------- .../flashlight/activities/MainActivity.java | 2 +- app/src/main/res/drawable/circles_big.xml | 6 +-- app/src/main/res/drawable/circles_small.xml | 43 ++++++++++++++++++ app/src/main/res/layout/widget.xml | 2 +- app/src/main/res/layout/widget_config.xml | 2 +- .../main/res/mipmap-hdpi/flashlight_small.png | Bin 4628 -> 0 bytes .../main/res/mipmap-mdpi/flashlight_small.png | Bin 3026 -> 0 bytes .../res/mipmap-xhdpi/flashlight_small.png | Bin 6446 -> 0 bytes .../res/mipmap-xxhdpi/flashlight_small.png | Bin 10228 -> 0 bytes .../res/mipmap-xxxhdpi/flashlight_small.png | Bin 13946 -> 0 bytes app/src/main/res/values/colors.xml | 2 +- 14 files changed, 67 insertions(+), 27 deletions(-) create mode 100644 app/src/main/res/drawable/circles_small.xml delete mode 100644 app/src/main/res/mipmap-hdpi/flashlight_small.png delete mode 100644 app/src/main/res/mipmap-mdpi/flashlight_small.png delete mode 100644 app/src/main/res/mipmap-xhdpi/flashlight_small.png delete mode 100644 app/src/main/res/mipmap-xxhdpi/flashlight_small.png delete mode 100644 app/src/main/res/mipmap-xxxhdpi/flashlight_small.png diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f95fe66..46719ef 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -57,7 +57,7 @@ + android:icon="@drawable/circles_small"> diff --git a/app/src/main/java/com/simplemobiletools/flashlight/MyWidgetProvider.java b/app/src/main/java/com/simplemobiletools/flashlight/MyWidgetProvider.java index c273fdb..36576ab 100644 --- a/app/src/main/java/com/simplemobiletools/flashlight/MyWidgetProvider.java +++ b/app/src/main/java/com/simplemobiletools/flashlight/MyWidgetProvider.java @@ -9,6 +9,8 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; import android.graphics.Bitmap; +import android.graphics.PorterDuff; +import android.graphics.drawable.Drawable; import android.widget.RemoteViews; import com.squareup.otto.Bus; @@ -49,7 +51,10 @@ public class MyWidgetProvider extends AppWidgetProvider { final Resources res = context.getResources(); final int defaultColor = res.getColor(R.color.colorPrimary); final int appColor = prefs.getInt(Constants.WIDGET_COLOR, defaultColor); - mColoredBmp = Utils.getColoredIcon(context.getResources(), appColor, R.mipmap.flashlight_small); + + final Drawable drawable = context.getResources().getDrawable(R.drawable.circles_small); + drawable.mutate().setColorFilter(appColor, PorterDuff.Mode.SRC_ATOP); + mColoredBmp = Utils.drawableToBitmap(drawable); if (mBus == null) { mBus = BusProvider.getInstance(); @@ -78,7 +83,7 @@ public class MyWidgetProvider extends AppWidgetProvider { } public void disableFlashlight() { - mRemoteViews.setImageViewResource(R.id.toggle_btn, R.mipmap.flashlight_small); + mRemoteViews.setImageViewResource(R.id.toggle_btn, R.drawable.circles_small); for (int widgetId : mWidgetIds) { mWidgetManager.updateAppWidget(widgetId, mRemoteViews); } diff --git a/app/src/main/java/com/simplemobiletools/flashlight/Utils.java b/app/src/main/java/com/simplemobiletools/flashlight/Utils.java index 5e335e7..60281c7 100644 --- a/app/src/main/java/com/simplemobiletools/flashlight/Utils.java +++ b/app/src/main/java/com/simplemobiletools/flashlight/Utils.java @@ -1,28 +1,20 @@ package com.simplemobiletools.flashlight; import android.content.Context; -import android.content.res.Resources; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.graphics.Canvas; -import android.graphics.ColorFilter; -import android.graphics.Paint; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffColorFilter; +import android.graphics.drawable.Drawable; import android.widget.Toast; public class Utils { - public static Bitmap getColoredIcon(Resources res, int newTextColor, int id) { - final BitmapFactory.Options options = new BitmapFactory.Options(); - options.inMutable = true; - final Bitmap bmp = BitmapFactory.decodeResource(res, 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; + public static Bitmap drawableToBitmap(Drawable drawable) { + final int width = drawable.getIntrinsicWidth(); + final int height = drawable.getIntrinsicHeight(); + final Bitmap mutableBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + final Canvas canvas = new Canvas(mutableBitmap); + drawable.setBounds(0, 0, width, height); + drawable.draw(canvas); + return mutableBitmap; } public static void showToast(Context context, int resId) { diff --git a/app/src/main/java/com/simplemobiletools/flashlight/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/flashlight/activities/MainActivity.java index ebbe9a3..66e1856 100644 --- a/app/src/main/java/com/simplemobiletools/flashlight/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/flashlight/activities/MainActivity.java @@ -133,7 +133,7 @@ public class MainActivity extends SimpleActivity { private void changeIconColor(int colorId, ImageView imageView) { final int appColor = getResources().getColor(colorId); - imageView.getDrawable().mutate().setColorFilter(appColor, PorterDuff.Mode.SRC_ATOP); + imageView.getDrawable().mutate().setColorFilter(appColor, PorterDuff.Mode.SRC_IN); } @Subscribe diff --git a/app/src/main/res/drawable/circles_big.xml b/app/src/main/res/drawable/circles_big.xml index ca8bb17..6c7160d 100644 --- a/app/src/main/res/drawable/circles_big.xml +++ b/app/src/main/res/drawable/circles_big.xml @@ -9,7 +9,7 @@ android:shape="oval"> + android:color="@color/translucent_white"/> @@ -23,7 +23,7 @@ android:shape="oval"> + android:color="@color/translucent_white"/> @@ -35,7 +35,7 @@ android:shape="oval"> + android:color="@color/translucent_white"/> diff --git a/app/src/main/res/drawable/circles_small.xml b/app/src/main/res/drawable/circles_small.xml new file mode 100644 index 0000000..989141c --- /dev/null +++ b/app/src/main/res/drawable/circles_small.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/widget.xml b/app/src/main/res/layout/widget.xml index 53b6a75..5f193f6 100644 --- a/app/src/main/res/layout/widget.xml +++ b/app/src/main/res/layout/widget.xml @@ -5,4 +5,4 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" - android:src="@mipmap/flashlight_small"/> + android:src="@drawable/circles_small"/> diff --git a/app/src/main/res/layout/widget_config.xml b/app/src/main/res/layout/widget_config.xml index d77c0d9..5146586 100644 --- a/app/src/main/res/layout/widget_config.xml +++ b/app/src/main/res/layout/widget_config.xml @@ -13,7 +13,7 @@ android:layout_height="match_parent" android:layout_above="@+id/config_widget_color" android:scaleType="centerInside" - android:src="@mipmap/flashlight_small"/> + android:src="@drawable/circles_big"/>