couple more widget related improvements

This commit is contained in:
tibbi 2017-11-06 13:54:07 +01:00
parent 93c4cd7226
commit f4357a863d
9 changed files with 21 additions and 28 deletions

View File

@ -37,7 +37,7 @@ android {
}
dependencies {
compile 'com.simplemobiletools:commons:2.35.6'
compile 'com.simplemobiletools:commons:2.35.7'
compile 'com.squareup:otto:1.3.8'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

View File

@ -4,6 +4,9 @@ import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import com.simplemobiletools.flashlight.helpers.Config
import com.simplemobiletools.flashlight.helpers.IS_ENABLED
import com.simplemobiletools.flashlight.helpers.MyWidgetProvider
@ -21,3 +24,12 @@ fun Context.updateWidgets(isEnabled: Boolean) {
}
}
}
fun Context.drawableToBitmap(drawable: Drawable): Bitmap {
val size = (60 * resources.displayMetrics.density).toInt()
val mutableBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
val canvas = Canvas(mutableBitmap)
drawable.setBounds(0, 0, size, size)
drawable.draw(canvas)
return mutableBitmap
}

View File

@ -8,10 +8,11 @@ import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.PorterDuff
import android.widget.RemoteViews
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
import com.simplemobiletools.flashlight.R
import com.simplemobiletools.flashlight.extensions.config
import com.simplemobiletools.flashlight.extensions.drawableToBitmap
class MyWidgetProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
@ -22,7 +23,6 @@ class MyWidgetProvider : AppWidgetProvider() {
val selectedColor = context.config.widgetBgColor
val alpha = Color.alpha(selectedColor)
val bmp = getColoredCircles(context, Color.WHITE, alpha)
val intent = Intent(context, MyWidgetProvider::class.java)
intent.action = TOGGLE
@ -54,8 +54,9 @@ class MyWidgetProvider : AppWidgetProvider() {
private fun toggleFlashlight(context: Context, intent: Intent) {
if (intent.extras?.containsKey(IS_ENABLED) == true) {
val enable = intent.extras.getBoolean(IS_ENABLED)
val selectedColor = if (enable) context.config.widgetBgColor else Color.WHITE
val alpha = Color.alpha(selectedColor)
val widgetBgColor = context.config.widgetBgColor
val alpha = Color.alpha(widgetBgColor)
val selectedColor = if (enable) widgetBgColor else Color.WHITE
val bmp = getColoredCircles(context, selectedColor, alpha)
val appWidgetManager = AppWidgetManager.getInstance(context)
@ -68,9 +69,7 @@ class MyWidgetProvider : AppWidgetProvider() {
}
private fun getColoredCircles(context: Context, color: Int, alpha: Int): Bitmap {
val drawable = context.resources.getDrawable(R.drawable.circles_small)
drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
drawable.mutate().alpha = alpha
return Utils.drawableToBitmap(drawable)
val drawable = context.resources.getColoredDrawableWithColor(R.drawable.circles_small, color, alpha)
return context.drawableToBitmap(drawable)
}
}

View File

@ -1,18 +0,0 @@
package com.simplemobiletools.flashlight.helpers
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.Drawable
object Utils {
fun drawableToBitmap(drawable: Drawable): Bitmap {
val width = drawable.intrinsicWidth
val height = drawable.intrinsicHeight
val mutableBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(mutableBitmap)
drawable.setBounds(0, 0, width, height)
drawable.draw(canvas)
return mutableBitmap
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

View File

@ -5,4 +5,4 @@
android:initialLayout="@layout/widget"
android:minHeight="40dp"
android:minWidth="40dp"
android:previewImage="@drawable/circles_small"/>
android:previewImage="@drawable/img_widget_preview"/>