mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-06-05 21:59:19 +02:00
few more updates here and there
This commit is contained in:
@ -6,65 +6,74 @@ import android.appwidget.AppWidgetProvider
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Color
|
||||
import android.graphics.PorterDuff
|
||||
import android.widget.RemoteViews
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import com.simplemobiletools.commons.helpers.PREFS_KEY
|
||||
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) {
|
||||
initVariables(context)
|
||||
appWidgetManager.updateAppWidget(appWidgetIds, mRemoteViews)
|
||||
performUpdate(context)
|
||||
}
|
||||
|
||||
private fun initVariables(context: Context) {
|
||||
private fun performUpdate(context: Context) {
|
||||
mContext = context
|
||||
val component = ComponentName(context, MyWidgetProvider::class.java)
|
||||
mWidgetManager = AppWidgetManager.getInstance(context)
|
||||
mWidgetIds = mWidgetManager!!.getAppWidgetIds(component)
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
|
||||
val intent = Intent(context, MyWidgetProvider::class.java)
|
||||
intent.action = TOGGLE
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
val selectedColor = context.config.widgetBgColor
|
||||
val alpha = Color.alpha(selectedColor)
|
||||
|
||||
val prefs = initPrefs(context)
|
||||
val res = context.resources
|
||||
val defaultColor = res.getColor(R.color.color_primary)
|
||||
val selectedColor = prefs.getInt(WIDGET_COLOR, defaultColor)
|
||||
val alpha = Color.alpha(selectedColor)
|
||||
mColoredBmp = getColoredCircles(selectedColor, alpha)
|
||||
mWhiteBmp = getColoredCircles(Color.WHITE, alpha)
|
||||
mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp)
|
||||
|
||||
mColoredBmp = getColoredCircles(selectedColor, alpha)
|
||||
mWhiteBmp = getColoredCircles(Color.WHITE, alpha)
|
||||
mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp)
|
||||
|
||||
if (mBus == null) {
|
||||
mBus = BusProvider.instance
|
||||
if (mBus == null) {
|
||||
mBus = BusProvider.instance
|
||||
}
|
||||
registerBus()
|
||||
}
|
||||
registerBus()
|
||||
}
|
||||
|
||||
private fun getComponentName(context: Context) = ComponentName(context, MyWidgetProvider::class.java)
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val action = intent.action
|
||||
if (action == TOGGLE) {
|
||||
if (mCameraImpl == null || mBus == null) {
|
||||
initVariables(context)
|
||||
}
|
||||
when (intent.action) {
|
||||
TOGGLE -> toggleFlashlight(context)
|
||||
else -> super.onReceive(context, intent)
|
||||
}
|
||||
}
|
||||
|
||||
mCameraImpl!!.toggleFlashlight()
|
||||
} else
|
||||
super.onReceive(context, intent)
|
||||
private fun toggleFlashlight(context: Context) {
|
||||
if (mCameraImpl == null || mBus == null) {
|
||||
performUpdate(context)
|
||||
}
|
||||
|
||||
mCameraImpl!!.toggleFlashlight()
|
||||
}
|
||||
|
||||
private fun getColoredCircles(color: Int, alpha: Int): Bitmap {
|
||||
@ -74,22 +83,18 @@ class MyWidgetProvider : AppWidgetProvider() {
|
||||
return Utils.drawableToBitmap(drawable)
|
||||
}
|
||||
|
||||
fun enableFlashlight() {
|
||||
private fun enableFlashlight() {
|
||||
mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mColoredBmp)
|
||||
for (widgetId in mWidgetIds!!) {
|
||||
/*for (widgetId in mWidgetIds!!) {
|
||||
mWidgetManager!!.updateAppWidget(widgetId, mRemoteViews)
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
fun disableFlashlight() {
|
||||
private fun disableFlashlight() {
|
||||
mRemoteViews!!.setImageViewBitmap(R.id.toggle_btn, mWhiteBmp)
|
||||
for (widgetId in mWidgetIds!!) {
|
||||
/*for (widgetId in mWidgetIds!!) {
|
||||
mWidgetManager!!.updateAppWidget(widgetId, mRemoteViews)
|
||||
}
|
||||
}
|
||||
|
||||
private fun initPrefs(context: Context): SharedPreferences {
|
||||
return context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
|
||||
}*/
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
@ -116,8 +121,9 @@ class MyWidgetProvider : AppWidgetProvider() {
|
||||
}
|
||||
|
||||
private fun releaseCamera(context: Context) {
|
||||
if (mCameraImpl == null)
|
||||
initVariables(context)
|
||||
if (mCameraImpl == null) {
|
||||
performUpdate(context)
|
||||
}
|
||||
|
||||
mCameraImpl!!.releaseCamera()
|
||||
}
|
||||
@ -137,17 +143,4 @@ class MyWidgetProvider : AppWidgetProvider() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TOGGLE = "toggle"
|
||||
private var mCameraImpl: MyCameraImpl? = null
|
||||
private var mRemoteViews: RemoteViews? = null
|
||||
private var mWidgetManager: AppWidgetManager? = null
|
||||
private var mColoredBmp: Bitmap? = null
|
||||
private var mWhiteBmp: Bitmap? = null
|
||||
private var mBus: Bus? = null
|
||||
private var mContext: Context? = null
|
||||
|
||||
private var mWidgetIds: IntArray? = null
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user