mirror of
				https://github.com/SimpleMobileTools/Simple-Flashlight.git
				synced 2025-06-05 21:59:19 +02:00 
			
		
		
		
	couple more widget related improvements
This commit is contained in:
		| @@ -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" | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|     } | ||||
| } | ||||
							
								
								
									
										
											BIN
										
									
								
								app/src/main/res/drawable-hdpi/img_widget_preview.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								app/src/main/res/drawable-hdpi/img_widget_preview.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 2.4 KiB | 
							
								
								
									
										
											BIN
										
									
								
								app/src/main/res/drawable-xhdpi/img_widget_preview.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								app/src/main/res/drawable-xhdpi/img_widget_preview.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 3.1 KiB | 
							
								
								
									
										
											BIN
										
									
								
								app/src/main/res/drawable-xxhdpi/img_widget_preview.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								app/src/main/res/drawable-xxhdpi/img_widget_preview.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 7.1 KiB | 
							
								
								
									
										
											BIN
										
									
								
								app/src/main/res/drawable-xxxhdpi/img_widget_preview.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								app/src/main/res/drawable-xxxhdpi/img_widget_preview.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 7.7 KiB | 
| @@ -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"/> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user