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