@@ -9,70 +9,60 @@ import android.content.Intent
99import android.graphics.Bitmap
1010import android.graphics.Color
1111import android.graphics.PorterDuff
12- import android.hardware.camera2.CameraAccessException
13- import android.hardware.camera2.CameraManager
14- import android.os.Build
15- import android.support.annotation.RequiresApi
16- import android.util.Log
1712import android.widget.RemoteViews
18- import com.simplemobiletools.commons.extensions.isMarshmallowPlus
1913import com.simplemobiletools.flashlight.R
2014import com.simplemobiletools.flashlight.extensions.config
21- import com.simplemobiletools.flashlight.models.Events
22- import com.squareup.otto.Subscribe
2315
2416class MyWidgetProvider : AppWidgetProvider () {
25- private val TOGGLE = " toggle"
26-
27- companion object {
28- private var mColoredBmp: Bitmap ? = null
29- private var mWhiteBmp: Bitmap ? = null
30- }
31-
3217 override fun onUpdate (context : Context , appWidgetManager : AppWidgetManager , appWidgetIds : IntArray ) {
3318 performUpdate(context)
3419 }
3520
3621 private fun performUpdate (context : Context ) {
22+ val selectedColor = context.config.widgetBgColor
23+ val alpha = Color .alpha(selectedColor)
24+ val bmp = getColoredCircles(context, Color .WHITE , alpha)
25+
26+ val intent = Intent (context, MyWidgetProvider ::class .java)
27+ intent.action = TOGGLE
28+
29+ val pendingIntent = PendingIntent .getBroadcast(context, 0 , intent, 0 )
30+
3731 val appWidgetManager = AppWidgetManager .getInstance(context)
3832 appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
3933 val views = RemoteViews (context.packageName, R .layout.widget)
40-
41- val intent = Intent (context, MyWidgetProvider ::class .java)
42- intent.action = TOGGLE
43-
44- val pendingIntent = PendingIntent .getBroadcast(context, 0 , intent, 0 )
4534 views.setOnClickPendingIntent(R .id.toggle_btn, pendingIntent)
46-
47- val selectedColor = context.config.widgetBgColor
48- val alpha = Color .alpha(selectedColor)
49-
50- mColoredBmp = getColoredCircles(context, selectedColor, alpha)
51- mWhiteBmp = getColoredCircles(context, Color .WHITE , alpha)
52- views.setImageViewBitmap(R .id.toggle_btn, mWhiteBmp)
53-
35+ views.setImageViewBitmap(R .id.toggle_btn, bmp)
5436 appWidgetManager.updateAppWidget(it, views)
5537 }
5638 }
5739
5840 private fun getComponentName (context : Context ) = ComponentName (context, MyWidgetProvider ::class .java)
5941
6042 override fun onReceive (context : Context , intent : Intent ) {
61- Log .e(" DEBUG" , " received action ${intent.action} " )
6243 when (intent.action) {
63- // TOGGLE -> toggleFlashlight(context)
44+ TOGGLE -> toggleActualFlashlight(context)
45+ TOGGLE_WIDGET_UI -> toggleFlashlight(context, intent)
6446 else -> super .onReceive(context, intent)
6547 }
6648 }
6749
68- @RequiresApi(Build .VERSION_CODES .LOLLIPOP )
69- private fun toggleFlashlight (context : Context ) {
70- if (context.isMarshmallowPlus()) {
71- val manager = context.getSystemService(Context .CAMERA_SERVICE ) as CameraManager
72- try {
73- val cameraId = manager.cameraIdList[0 ]
74- // manager.setTorchMode(cameraId!!, enable)
75- } catch (ignored: CameraAccessException ) {
50+ private fun toggleActualFlashlight (context : Context ) {
51+ MyCameraImpl .newInstance(context).toggleFlashlight()
52+ }
53+
54+ private fun toggleFlashlight (context : Context , intent : Intent ) {
55+ if (intent.extras?.containsKey(IS_ENABLED ) == true ) {
56+ val enable = intent.extras.getBoolean(IS_ENABLED )
57+ val selectedColor = if (enable) context.config.widgetBgColor else Color .WHITE
58+ val alpha = Color .alpha(selectedColor)
59+ val bmp = getColoredCircles(context, selectedColor, alpha)
60+
61+ val appWidgetManager = AppWidgetManager .getInstance(context)
62+ appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
63+ val views = RemoteViews (context.packageName, R .layout.widget)
64+ views.setImageViewBitmap(R .id.toggle_btn, bmp)
65+ appWidgetManager.updateAppWidget(it, views)
7666 }
7767 }
7868 }
@@ -83,27 +73,4 @@ class MyWidgetProvider : AppWidgetProvider() {
8373 drawable.mutate().alpha = alpha
8474 return Utils .drawableToBitmap(drawable)
8575 }
86-
87- private fun enableFlashlight () {
88- // mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mColoredBmp)
89- /* for (widgetId in mWidgetIds!!) {
90- mWidgetManager!!.updateAppWidget(widgetId, mRemoteViews)
91- }*/
92- }
93-
94- private fun disableFlashlight () {
95- // mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp)
96- /* for (widgetId in mWidgetIds!!) {
97- mWidgetManager!!.updateAppWidget(widgetId, mRemoteViews)
98- }*/
99- }
100-
101- @Subscribe
102- fun stateChangedEvent (event : Events .StateChanged ) {
103- if (event.isEnabled) {
104- enableFlashlight()
105- } else {
106- disableFlashlight()
107- }
108- }
10976}
0 commit comments