Added widget color change

This commit is contained in:
Agnieszka C 2021-10-18 19:30:07 +02:00
parent 47392e13f4
commit b09090e8ea
4 changed files with 41 additions and 6 deletions

View File

@ -1,10 +1,16 @@
package com.simplemobiletools.voicerecorder.extensions
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import com.simplemobiletools.voicerecorder.helpers.Config
import com.simplemobiletools.voicerecorder.helpers.IS_RECORDING
import com.simplemobiletools.voicerecorder.helpers.MyWidgetRecordDisplayProvider
import com.simplemobiletools.voicerecorder.helpers.TOGGLE_WIDGET_UI
val Context.config: Config get() = Config.newInstance(applicationContext)
@ -16,3 +22,15 @@ fun Context.drawableToBitmap(drawable: Drawable): Bitmap {
drawable.draw(canvas)
return mutableBitmap
}
fun Context.updateWidgets(isRecording: Boolean) {
val widgetIDs = AppWidgetManager.getInstance(applicationContext)
?.getAppWidgetIds(ComponentName(applicationContext, MyWidgetRecordDisplayProvider::class.java)) ?: return
if (widgetIDs.isNotEmpty()) {
Intent(applicationContext, MyWidgetRecordDisplayProvider::class.java).apply {
action = TOGGLE_WIDGET_UI
putExtra(IS_RECORDING, isRecording)
sendBroadcast(this)
}
}
}

View File

@ -25,6 +25,9 @@ const val RECORDING_RUNNING = 0
const val RECORDING_STOPPED = 1
const val RECORDING_PAUSED = 2
const val IS_RECORDING = "is_recording"
const val TOGGLE_WIDGET_UI = "toggle_widget_ui"
// shared preferences
const val HIDE_NOTIFICATION = "hide_notification"
const val SAVE_RECORDINGS = "save_recordings"

View File

@ -19,16 +19,27 @@ class MyWidgetRecordDisplayProvider : AppWidgetProvider() {
private val OPEN_APP_INTENT_ID = 1
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
changeWidgetIcon(appWidgetManager, context, Color.WHITE)
}
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == TOGGLE_WIDGET_UI && intent.extras?.containsKey(IS_RECORDING) == true) {
val appWidgetManager = AppWidgetManager.getInstance(context) ?: return
val color = if (intent.extras!!.getBoolean(IS_RECORDING)) context.config.widgetBgColor else Color.WHITE
changeWidgetIcon(appWidgetManager, context, color)
} else {
super.onReceive(context, intent)
}
}
private fun changeWidgetIcon(appWidgetManager: AppWidgetManager, context: Context, color: Int) {
val alpha = Color.alpha(context.config.widgetBgColor)
val bmp = getColoredIcon(context, color, alpha)
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
RemoteViews(context.packageName, R.layout.widget_record_display).apply {
setupAppOpenIntent(context, this)
val selectedColor = context.config.widgetBgColor
val alpha = Color.alpha(selectedColor)
val bmp = getColoredIcon(context, selectedColor, alpha)
setImageViewBitmap(R.id.record_display_btn, bmp)
appWidgetManager.updateAppWidget(it, this)
}
}

View File

@ -22,6 +22,7 @@ import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SplashActivity
import com.simplemobiletools.voicerecorder.extensions.config
import com.simplemobiletools.voicerecorder.extensions.updateWidgets
import com.simplemobiletools.voicerecorder.helpers.*
import com.simplemobiletools.voicerecorder.models.Events
import org.greenrobot.eventbus.EventBus
@ -61,11 +62,13 @@ class RecorderService : Service() {
super.onDestroy()
stopRecording()
isRunning = false
updateWidgets(false)
}
// mp4 output format with aac encoding should produce good enough m4a files according to https://stackoverflow.com/a/33054794/1967672
private fun startRecording() {
isRunning = true
updateWidgets(true)
if (status == RECORDING_RUNNING) {
return
}