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:
		| @@ -60,6 +60,7 @@ class MainActivity : SimpleActivity() { | ||||
|             mCameraImpl!!.stopStroboscope() | ||||
|             stroboscope_bar.beInvisible() | ||||
|         } | ||||
|         updateTextColors(main_holder) | ||||
|     } | ||||
|  | ||||
|     override fun onStart() { | ||||
| @@ -118,8 +119,7 @@ class MainActivity : SimpleActivity() { | ||||
|         stroboscope_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { | ||||
|             override fun onProgressChanged(seekBar: SeekBar, progress: Int, b: Boolean) { | ||||
|                 val frequency = stroboscope_bar.max - progress + MIN_STROBO_DELAY | ||||
|                 if (mCameraImpl != null) | ||||
|                     mCameraImpl!!.stroboFrequency = frequency | ||||
|                 mCameraImpl?.stroboFrequency = frequency | ||||
|             } | ||||
|  | ||||
|             override fun onStartTrackingTouch(seekBar: SeekBar) { | ||||
|   | ||||
| @@ -2,29 +2,24 @@ package com.simplemobiletools.flashlight.activities | ||||
|  | ||||
| import android.app.Activity | ||||
| import android.appwidget.AppWidgetManager | ||||
| import android.content.Context | ||||
| import android.content.Intent | ||||
| import android.graphics.Color | ||||
| import android.graphics.PorterDuff | ||||
| import android.os.Bundle | ||||
| import android.support.v7.app.AppCompatActivity | ||||
| import android.widget.RemoteViews | ||||
| import android.widget.SeekBar | ||||
| import com.simplemobiletools.commons.dialogs.ColorPickerDialog | ||||
| import com.simplemobiletools.commons.extensions.adjustAlpha | ||||
| import com.simplemobiletools.commons.helpers.PREFS_KEY | ||||
| import com.simplemobiletools.flashlight.R | ||||
| import com.simplemobiletools.flashlight.extensions.config | ||||
| import com.simplemobiletools.flashlight.helpers.MyWidgetProvider | ||||
| import com.simplemobiletools.flashlight.helpers.WIDGET_COLOR | ||||
| import kotlinx.android.synthetic.main.widget_config.* | ||||
|  | ||||
| class WidgetConfigureActivity : AppCompatActivity() { | ||||
|     companion object { | ||||
|         private var mWidgetAlpha = 0f | ||||
|         private var mWidgetId = 0 | ||||
|         private var mWidgetColor = 0 | ||||
|         private var mWidgetColorWithoutTransparency = 0 | ||||
|     } | ||||
|     private var mWidgetAlpha = 0f | ||||
|     private var mWidgetId = 0 | ||||
|     private var mWidgetColor = 0 | ||||
|     private var mWidgetColorWithoutTransparency = 0 | ||||
|  | ||||
|     public override fun onCreate(savedInstanceState: Bundle?) { | ||||
|         super.onCreate(savedInstanceState) | ||||
| @@ -32,7 +27,6 @@ class WidgetConfigureActivity : AppCompatActivity() { | ||||
|         setContentView(R.layout.widget_config) | ||||
|         initVariables() | ||||
|  | ||||
|         val intent = intent | ||||
|         val extras = intent.extras | ||||
|         if (extras != null) | ||||
|             mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID) | ||||
| @@ -45,8 +39,7 @@ class WidgetConfigureActivity : AppCompatActivity() { | ||||
|     } | ||||
|  | ||||
|     private fun initVariables() { | ||||
|         val prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) | ||||
|         mWidgetColor = prefs.getInt(WIDGET_COLOR, 1) | ||||
|         mWidgetColor = config.widgetBgColor | ||||
|         if (mWidgetColor == 1) { | ||||
|             mWidgetColor = resources.getColor(R.color.color_primary) | ||||
|             mWidgetAlpha = 1f | ||||
| @@ -60,36 +53,29 @@ class WidgetConfigureActivity : AppCompatActivity() { | ||||
|         updateColors() | ||||
|     } | ||||
|  | ||||
|     fun saveConfig() { | ||||
|         val appWidgetManager = AppWidgetManager.getInstance(this) | ||||
|         val views = RemoteViews(packageName, R.layout.widget) | ||||
|         appWidgetManager.updateAppWidget(mWidgetId, views) | ||||
|  | ||||
|         storeWidgetColors() | ||||
|     private fun saveConfig() { | ||||
|         config.widgetBgColor = mWidgetColor | ||||
|         requestWidgetUpdate() | ||||
|  | ||||
|         val resultValue = Intent() | ||||
|         resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId) | ||||
|         setResult(Activity.RESULT_OK, resultValue) | ||||
|         Intent().apply { | ||||
|             putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId) | ||||
|             setResult(Activity.RESULT_OK, this) | ||||
|         } | ||||
|         finish() | ||||
|     } | ||||
|  | ||||
|     fun pickBackgroundColor() { | ||||
|     private fun pickBackgroundColor() { | ||||
|         ColorPickerDialog(this, mWidgetColorWithoutTransparency) { | ||||
|             mWidgetColorWithoutTransparency = it | ||||
|             updateColors() | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private fun storeWidgetColors() { | ||||
|         val prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) | ||||
|         prefs.edit().putInt(WIDGET_COLOR, mWidgetColor).apply() | ||||
|     } | ||||
|  | ||||
|     private fun requestWidgetUpdate() { | ||||
|         val intent = Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider::class.java) | ||||
|         intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId)) | ||||
|         sendBroadcast(intent) | ||||
|         Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider::class.java).apply { | ||||
|             putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId)) | ||||
|             sendBroadcast(this) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private fun updateColors() { | ||||
| @@ -104,12 +90,8 @@ class WidgetConfigureActivity : AppCompatActivity() { | ||||
|             updateColors() | ||||
|         } | ||||
|  | ||||
|         override fun onStartTrackingTouch(seekBar: SeekBar) { | ||||
|         override fun onStartTrackingTouch(seekBar: SeekBar) {} | ||||
|  | ||||
|         } | ||||
|  | ||||
|         override fun onStopTrackingTouch(seekBar: SeekBar) { | ||||
|  | ||||
|         } | ||||
|         override fun onStopTrackingTouch(seekBar: SeekBar) {} | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -33,7 +33,7 @@ | ||||
|         android:background="@mipmap/stroboscope" | ||||
|         android:padding="@dimen/activity_margin"/> | ||||
|  | ||||
|     <SeekBar | ||||
|     <com.simplemobiletools.commons.views.MySeekBar | ||||
|         android:id="@+id/stroboscope_bar" | ||||
|         android:layout_width="@dimen/seekbar_width" | ||||
|         android:layout_height="wrap_content" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user