Skip to content

Commit 30b348c

Browse files
committed
some improvements related to toggling widget ui
1 parent 7f38f2a commit 30b348c

File tree

5 files changed

+53
-79
lines changed

5 files changed

+53
-79
lines changed

app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class MainActivity : SimpleActivity() {
5252
override fun onResume() {
5353
super.onResume()
5454
mCameraImpl!!.handleCameraSetup()
55+
checkState(MyCameraImpl.isFlashlightOn)
5556

5657
bright_display_btn.beVisibleIf(config.brightDisplay)
5758
stroboscope_btn.beVisibleIf(config.stroboscope)
@@ -160,7 +161,11 @@ class MainActivity : SimpleActivity() {
160161

161162
@Subscribe
162163
fun stateChangedEvent(event: Events.StateChanged) {
163-
if (event.isEnabled) {
164+
checkState(event.isEnabled)
165+
}
166+
167+
private fun checkState(isEnabled: Boolean) {
168+
if (isEnabled) {
164169
enableFlashlight()
165170
} else {
166171
disableFlashlight()

app/src/main/kotlin/com/simplemobiletools/flashlight/extensions/Context.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import android.appwidget.AppWidgetManager
44
import android.content.ComponentName
55
import android.content.Context
66
import android.content.Intent
7-
import com.simplemobiletools.flashlight.R
87
import com.simplemobiletools.flashlight.helpers.Config
8+
import com.simplemobiletools.flashlight.helpers.IS_ENABLED
99
import com.simplemobiletools.flashlight.helpers.MyWidgetProvider
10+
import com.simplemobiletools.flashlight.helpers.TOGGLE_WIDGET_UI
1011

1112
val Context.config: Config get() = Config.newInstance(this)
1213

13-
fun Context.updateWidgets() {
14+
fun Context.updateWidgets(isEnabled: Boolean) {
1415
val widgetsCnt = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, MyWidgetProvider::class.java))
1516
if (widgetsCnt.isNotEmpty()) {
16-
val ids = intArrayOf(R.xml.widget_info)
1717
Intent(this, MyWidgetProvider::class.java).apply {
18-
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
19-
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
18+
action = TOGGLE_WIDGET_UI
19+
putExtra(IS_ENABLED, isEnabled)
2020
sendBroadcast(this)
2121
}
2222
}

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Constants.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ package com.simplemobiletools.flashlight.helpers
22

33
val BRIGHT_DISPLAY = "bright_display"
44
val STROBOSCOPE = "stroboscope"
5-
val WIDGET_COLOR = "widget_color"
5+
val IS_ENABLED = "is_enabled"
6+
val TOGGLE = "toggle"
7+
val TOGGLE_WIDGET_UI = "toggle_widget_ui"

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ import java.io.IOException
1616
class MyCameraImpl(val context: Context) {
1717
var stroboFrequency = 1000
1818

19-
private var camera: Camera? = null
20-
private var params: Camera.Parameters? = null
21-
private var bus: Bus? = null
19+
companion object {
20+
var isFlashlightOn = false
2221

23-
private var isFlashlightOn = false
24-
private var isMarshmallow = false
25-
private var shouldEnableFlashlight = false
22+
private var camera: Camera? = null
23+
private var params: Camera.Parameters? = null
24+
private var bus: Bus? = null
25+
private var isMarshmallow = false
26+
private var shouldEnableFlashlight = false
2627

27-
private var marshmallowCamera: MarshmallowCamera? = null
28-
@Volatile private var shouldStroboscopeStop = false
29-
@Volatile private var isStroboscopeRunning = false
28+
private var marshmallowCamera: MarshmallowCamera? = null
29+
@Volatile private var shouldStroboscopeStop = false
30+
@Volatile private var isStroboscopeRunning = false
3031

31-
companion object {
3232
fun newInstance(context: Context) = MyCameraImpl(context)
3333
}
3434

@@ -163,7 +163,7 @@ class MyCameraImpl(val context: Context) {
163163
private fun stateChanged(isEnabled: Boolean) {
164164
isFlashlightOn = isEnabled
165165
bus!!.post(Events.StateChanged(isEnabled))
166-
context.updateWidgets()
166+
context.updateWidgets(isEnabled)
167167
}
168168

169169
private fun toggleMarshmallowFlashlight(enable: Boolean) {

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyWidgetProvider.kt

Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,60 @@ import android.content.Intent
99
import android.graphics.Bitmap
1010
import android.graphics.Color
1111
import 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
1712
import android.widget.RemoteViews
18-
import com.simplemobiletools.commons.extensions.isMarshmallowPlus
1913
import com.simplemobiletools.flashlight.R
2014
import com.simplemobiletools.flashlight.extensions.config
21-
import com.simplemobiletools.flashlight.models.Events
22-
import com.squareup.otto.Subscribe
2315

2416
class 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

Comments
 (0)