mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-06-05 21:59:19 +02:00
couple improvements to flashlight toggling
This commit is contained in:
@ -9,24 +9,24 @@ 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.toast
|
||||
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.Bus
|
||||
import com.squareup.otto.Subscribe
|
||||
|
||||
class MyWidgetProvider : AppWidgetProvider() {
|
||||
private val TOGGLE = "toggle"
|
||||
|
||||
companion object {
|
||||
private var mCameraImpl: MyCameraImpl? = null
|
||||
private var mRemoteViews: RemoteViews? = null
|
||||
private var mColoredBmp: Bitmap? = null
|
||||
private var mWhiteBmp: Bitmap? = null
|
||||
private var mBus: Bus? = null
|
||||
private var mContext: Context? = null
|
||||
}
|
||||
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
@ -34,77 +34,70 @@ class MyWidgetProvider : AppWidgetProvider() {
|
||||
}
|
||||
|
||||
private fun performUpdate(context: Context) {
|
||||
mContext = context
|
||||
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)
|
||||
mRemoteViews = RemoteViews(context.packageName, R.layout.widget)
|
||||
mRemoteViews!!.setOnClickPendingIntent(R.id.toggle_btn, pendingIntent)
|
||||
mCameraImpl = MyCameraImpl(context)
|
||||
views.setOnClickPendingIntent(R.id.toggle_btn, pendingIntent)
|
||||
|
||||
val selectedColor = context.config.widgetBgColor
|
||||
val alpha = Color.alpha(selectedColor)
|
||||
|
||||
mColoredBmp = getColoredCircles(selectedColor, alpha)
|
||||
mWhiteBmp = getColoredCircles(Color.WHITE, alpha)
|
||||
mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp)
|
||||
mColoredBmp = getColoredCircles(context, selectedColor, alpha)
|
||||
mWhiteBmp = getColoredCircles(context, Color.WHITE, alpha)
|
||||
views.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp)
|
||||
|
||||
if (mBus == null) {
|
||||
mBus = BusProvider.instance
|
||||
}
|
||||
registerBus()
|
||||
appWidgetManager.updateAppWidget(it, views)
|
||||
}
|
||||
}
|
||||
|
||||
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 -> toggleFlashlight(context)
|
||||
else -> super.onReceive(context, intent)
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
||||
private fun toggleFlashlight(context: Context) {
|
||||
if (mCameraImpl == null || mBus == null) {
|
||||
performUpdate(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) {
|
||||
}
|
||||
}
|
||||
|
||||
mCameraImpl!!.toggleFlashlight()
|
||||
}
|
||||
|
||||
private fun getColoredCircles(color: Int, alpha: Int): Bitmap {
|
||||
val drawable = mContext!!.resources.getDrawable(R.drawable.circles_small)
|
||||
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)
|
||||
}
|
||||
|
||||
private fun enableFlashlight() {
|
||||
mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mColoredBmp)
|
||||
//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)
|
||||
//mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp)
|
||||
/*for (widgetId in mWidgetIds!!) {
|
||||
mWidgetManager!!.updateAppWidget(widgetId, mRemoteViews)
|
||||
}*/
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
fun cameraUnavailable(event: Events.CameraUnavailable) {
|
||||
if (mContext != null) {
|
||||
mContext!!.toast(R.string.camera_error)
|
||||
disableFlashlight()
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
fun stateChangedEvent(event: Events.StateChanged) {
|
||||
if (event.isEnabled) {
|
||||
@ -113,34 +106,4 @@ class MyWidgetProvider : AppWidgetProvider() {
|
||||
disableFlashlight()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
||||
super.onDeleted(context, appWidgetIds)
|
||||
unregisterBus()
|
||||
releaseCamera(context)
|
||||
}
|
||||
|
||||
private fun releaseCamera(context: Context) {
|
||||
if (mCameraImpl == null) {
|
||||
performUpdate(context)
|
||||
}
|
||||
|
||||
mCameraImpl!!.releaseCamera()
|
||||
}
|
||||
|
||||
private fun registerBus() {
|
||||
try {
|
||||
mBus!!.register(this)
|
||||
} catch (ignored: Exception) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun unregisterBus() {
|
||||
try {
|
||||
mBus!!.unregister(this)
|
||||
} catch (ignored: Exception) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user