mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-03-17 12:00:21 +01:00
some improvements related to toggling widget ui
This commit is contained in:
parent
7f38f2aa3a
commit
30b348c1e1
@ -52,6 +52,7 @@ class MainActivity : SimpleActivity() {
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
mCameraImpl!!.handleCameraSetup()
|
||||
checkState(MyCameraImpl.isFlashlightOn)
|
||||
|
||||
bright_display_btn.beVisibleIf(config.brightDisplay)
|
||||
stroboscope_btn.beVisibleIf(config.stroboscope)
|
||||
@ -160,7 +161,11 @@ class MainActivity : SimpleActivity() {
|
||||
|
||||
@Subscribe
|
||||
fun stateChangedEvent(event: Events.StateChanged) {
|
||||
if (event.isEnabled) {
|
||||
checkState(event.isEnabled)
|
||||
}
|
||||
|
||||
private fun checkState(isEnabled: Boolean) {
|
||||
if (isEnabled) {
|
||||
enableFlashlight()
|
||||
} else {
|
||||
disableFlashlight()
|
||||
|
@ -4,19 +4,19 @@ import android.appwidget.AppWidgetManager
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.simplemobiletools.flashlight.R
|
||||
import com.simplemobiletools.flashlight.helpers.Config
|
||||
import com.simplemobiletools.flashlight.helpers.IS_ENABLED
|
||||
import com.simplemobiletools.flashlight.helpers.MyWidgetProvider
|
||||
import com.simplemobiletools.flashlight.helpers.TOGGLE_WIDGET_UI
|
||||
|
||||
val Context.config: Config get() = Config.newInstance(this)
|
||||
|
||||
fun Context.updateWidgets() {
|
||||
fun Context.updateWidgets(isEnabled: Boolean) {
|
||||
val widgetsCnt = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, MyWidgetProvider::class.java))
|
||||
if (widgetsCnt.isNotEmpty()) {
|
||||
val ids = intArrayOf(R.xml.widget_info)
|
||||
Intent(this, MyWidgetProvider::class.java).apply {
|
||||
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
|
||||
action = TOGGLE_WIDGET_UI
|
||||
putExtra(IS_ENABLED, isEnabled)
|
||||
sendBroadcast(this)
|
||||
}
|
||||
}
|
||||
|
@ -2,4 +2,6 @@ package com.simplemobiletools.flashlight.helpers
|
||||
|
||||
val BRIGHT_DISPLAY = "bright_display"
|
||||
val STROBOSCOPE = "stroboscope"
|
||||
val WIDGET_COLOR = "widget_color"
|
||||
val IS_ENABLED = "is_enabled"
|
||||
val TOGGLE = "toggle"
|
||||
val TOGGLE_WIDGET_UI = "toggle_widget_ui"
|
||||
|
@ -16,19 +16,19 @@ import java.io.IOException
|
||||
class MyCameraImpl(val context: Context) {
|
||||
var stroboFrequency = 1000
|
||||
|
||||
private var camera: Camera? = null
|
||||
private var params: Camera.Parameters? = null
|
||||
private var bus: Bus? = null
|
||||
|
||||
private var isFlashlightOn = false
|
||||
private var isMarshmallow = false
|
||||
private var shouldEnableFlashlight = false
|
||||
|
||||
private var marshmallowCamera: MarshmallowCamera? = null
|
||||
@Volatile private var shouldStroboscopeStop = false
|
||||
@Volatile private var isStroboscopeRunning = false
|
||||
|
||||
companion object {
|
||||
var isFlashlightOn = false
|
||||
|
||||
private var camera: Camera? = null
|
||||
private var params: Camera.Parameters? = null
|
||||
private var bus: Bus? = null
|
||||
private var isMarshmallow = false
|
||||
private var shouldEnableFlashlight = false
|
||||
|
||||
private var marshmallowCamera: MarshmallowCamera? = null
|
||||
@Volatile private var shouldStroboscopeStop = false
|
||||
@Volatile private var isStroboscopeRunning = false
|
||||
|
||||
fun newInstance(context: Context) = MyCameraImpl(context)
|
||||
}
|
||||
|
||||
@ -163,7 +163,7 @@ class MyCameraImpl(val context: Context) {
|
||||
private fun stateChanged(isEnabled: Boolean) {
|
||||
isFlashlightOn = isEnabled
|
||||
bus!!.post(Events.StateChanged(isEnabled))
|
||||
context.updateWidgets()
|
||||
context.updateWidgets(isEnabled)
|
||||
}
|
||||
|
||||
private fun toggleMarshmallowFlashlight(enable: Boolean) {
|
||||
|
@ -9,48 +9,30 @@ import android.content.Intent
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Color
|
||||
import android.graphics.PorterDuff
|
||||
import android.hardware.camera2.CameraAccessException
|
||||
import android.hardware.camera2.CameraManager
|
||||
import android.os.Build
|
||||
import android.support.annotation.RequiresApi
|
||||
import android.util.Log
|
||||
import android.widget.RemoteViews
|
||||
import com.simplemobiletools.commons.extensions.isMarshmallowPlus
|
||||
import com.simplemobiletools.flashlight.R
|
||||
import com.simplemobiletools.flashlight.extensions.config
|
||||
import com.simplemobiletools.flashlight.models.Events
|
||||
import com.squareup.otto.Subscribe
|
||||
|
||||
class MyWidgetProvider : AppWidgetProvider() {
|
||||
private val TOGGLE = "toggle"
|
||||
|
||||
companion object {
|
||||
private var mColoredBmp: Bitmap? = null
|
||||
private var mWhiteBmp: Bitmap? = null
|
||||
}
|
||||
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
performUpdate(context)
|
||||
}
|
||||
|
||||
private fun performUpdate(context: Context) {
|
||||
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
|
||||
|
||||
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)
|
||||
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
|
||||
val views = RemoteViews(context.packageName, R.layout.widget)
|
||||
|
||||
val intent = Intent(context, MyWidgetProvider::class.java)
|
||||
intent.action = TOGGLE
|
||||
|
||||
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)
|
||||
views.setOnClickPendingIntent(R.id.toggle_btn, pendingIntent)
|
||||
|
||||
val selectedColor = context.config.widgetBgColor
|
||||
val alpha = Color.alpha(selectedColor)
|
||||
|
||||
mColoredBmp = getColoredCircles(context, selectedColor, alpha)
|
||||
mWhiteBmp = getColoredCircles(context, Color.WHITE, alpha)
|
||||
views.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp)
|
||||
|
||||
views.setImageViewBitmap(R.id.toggle_btn, bmp)
|
||||
appWidgetManager.updateAppWidget(it, views)
|
||||
}
|
||||
}
|
||||
@ -58,21 +40,29 @@ class MyWidgetProvider : AppWidgetProvider() {
|
||||
private fun getComponentName(context: Context) = ComponentName(context, MyWidgetProvider::class.java)
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
Log.e("DEBUG", "received action ${intent.action}")
|
||||
when (intent.action) {
|
||||
//TOGGLE -> toggleFlashlight(context)
|
||||
TOGGLE -> toggleActualFlashlight(context)
|
||||
TOGGLE_WIDGET_UI -> toggleFlashlight(context, intent)
|
||||
else -> super.onReceive(context, intent)
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
||||
private fun toggleFlashlight(context: Context) {
|
||||
if (context.isMarshmallowPlus()) {
|
||||
val manager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
|
||||
try {
|
||||
val cameraId = manager.cameraIdList[0]
|
||||
//manager.setTorchMode(cameraId!!, enable)
|
||||
} catch (ignored: CameraAccessException) {
|
||||
private fun toggleActualFlashlight(context: Context) {
|
||||
MyCameraImpl.newInstance(context).toggleFlashlight()
|
||||
}
|
||||
|
||||
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 bmp = getColoredCircles(context, selectedColor, alpha)
|
||||
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
|
||||
val views = RemoteViews(context.packageName, R.layout.widget)
|
||||
views.setImageViewBitmap(R.id.toggle_btn, bmp)
|
||||
appWidgetManager.updateAppWidget(it, views)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -83,27 +73,4 @@ class MyWidgetProvider : AppWidgetProvider() {
|
||||
drawable.mutate().alpha = alpha
|
||||
return Utils.drawableToBitmap(drawable)
|
||||
}
|
||||
|
||||
private fun enableFlashlight() {
|
||||
//mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mColoredBmp)
|
||||
/*for (widgetId in mWidgetIds!!) {
|
||||
mWidgetManager!!.updateAppWidget(widgetId, mRemoteViews)
|
||||
}*/
|
||||
}
|
||||
|
||||
private fun disableFlashlight() {
|
||||
//mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp)
|
||||
/*for (widgetId in mWidgetIds!!) {
|
||||
mWidgetManager!!.updateAppWidget(widgetId, mRemoteViews)
|
||||
}*/
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
fun stateChangedEvent(event: Events.StateChanged) {
|
||||
if (event.isEnabled) {
|
||||
enableFlashlight()
|
||||
} else {
|
||||
disableFlashlight()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user