Wear: fix tile condig update

This commit is contained in:
Andries Smit 2022-01-11 10:38:20 +01:00
parent ad43f57f7f
commit c5c4064c8b
8 changed files with 106 additions and 78 deletions

View file

@ -512,6 +512,7 @@
<service <service
android:name=".tile.ActionsTileService" android:name=".tile.ActionsTileService"
android:exported="true"
android:label="@string/label_actions_tile" android:label="@string/label_actions_tile"
android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER"> android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER">
<intent-filter> <intent-filter>
@ -528,6 +529,7 @@
<service <service
android:name=".tile.TempTargetTileService" android:name=".tile.TempTargetTileService"
android:exported="true"
android:label="@string/label_temp_target_tile" android:label="@string/label_temp_target_tile"
android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER"> android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER">
<intent-filter> <intent-filter>
@ -607,10 +609,11 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".interaction.TileConfigurationActivity" android:exported="true"> <activity android:name=".interaction.TileConfigurationActivity">
<intent-filter> <intent-filter>
<action android:name="tile_configuration_activity" /> <action android:name="tile_configuration_activity" />
<action android:name="tile_configuration_tempt" /> <action android:name="tile_configuration_tempt" />
<category android:name="com.google.android.clockwork.tiles.category.PROVIDER_CONFIG" /> <category android:name="com.google.android.clockwork.tiles.category.PROVIDER_CONFIG" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>

View file

@ -1,40 +0,0 @@
package info.nightscout.androidaps.interaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import androidx.core.content.ContextCompat;
import info.nightscout.androidaps.R;
import preference.WearPreferenceActivity;
public class TileConfigurationActivity extends WearPreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Tile");
String configFileName = getIntent().getAction();
int resXmlId = getResources().getIdentifier(configFileName, "xml", getApplicationContext().getPackageName());
Log.d("ConfigurationActivity::onCreate --->> getIntent().getAction()", configFileName);
Log.d("ConfigurationActivity::onCreate --->> resXmlId", String.valueOf(resXmlId));
addPreferencesFromResource(resXmlId);
ViewGroup view = (ViewGroup) getWindow().getDecorView();
removeBackgroundRecursively(view);
view.setBackground(ContextCompat.getDrawable(this, R.drawable.settings_background));
view.requestFocus();
}
void removeBackgroundRecursively(View parent) {
if (parent instanceof ViewGroup) {
ViewGroup group = (ViewGroup) parent;
for (int i = 0; i < group.getChildCount(); i++) {
removeBackgroundRecursively(group.getChildAt(i));
}
}
parent.setBackground(null);
}
}

View file

@ -0,0 +1,44 @@
package info.nightscout.androidaps.interaction
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import androidx.wear.tiles.TileService
import preference.WearPreferenceActivity
import info.nightscout.androidaps.tile.ActionsTileService
import info.nightscout.androidaps.tile.TempTargetTileService
var TAG = "ASTAG-config"
class TileConfigurationActivity : WearPreferenceActivity() {
private var configFileName: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = "Tile"
configFileName = intent.action
val resXmlId = resources.getIdentifier(configFileName, "xml", applicationContext.packageName)
Log.d("ConfigurationActivity::onCreate --->> getIntent().getAction()", configFileName!!)
Log.d("ConfigurationActivity::onCreate --->> resXmlId", resXmlId.toString())
addPreferencesFromResource(resXmlId)
val view = window.decorView as ViewGroup
view.requestFocus()
}
override fun onDestroy() {
super.onDestroy()
// Note that TileService updates are hard limited to once every 20 seconds.
if (configFileName === "tile_configuration_activity") {
Log.i(TAG, "onDestroy a: requestUpdate!!")
TileService.getUpdater(this)
.requestUpdate(ActionsTileService::class.java)
} else if (configFileName === "tile_configuration_tempt") {
Log.i(TAG, "onDestroy tt: requestUpdate!!")
TileService.getUpdater(this)
.requestUpdate(TempTargetTileService::class.java)
} else {
Log.i(TAG, "onDestroy : NO tile service available for $configFileName")
}
}
}

View file

@ -6,7 +6,7 @@ import info.nightscout.androidaps.interaction.actions.BolusActivity
import info.nightscout.androidaps.interaction.actions.ECarbActivity import info.nightscout.androidaps.interaction.actions.ECarbActivity
import info.nightscout.androidaps.interaction.actions.TempTargetActivity import info.nightscout.androidaps.interaction.actions.TempTargetActivity
object ActionSource: TileSource { object ActionSource : TileSource {
override fun getActions(): List<Action> { override fun getActions(): List<Action> {
return listOf( return listOf(
@ -48,4 +48,14 @@ object ActionSource: TileSource {
) )
) )
} }
override fun getDefaultConfig(): Map<String, String> {
return mapOf(
"tile_action_1" to "wizzard",
"tile_action_2" to "bolus",
"tile_action_3" to "carbs",
"tile_action_4" to "temp_target"
)
}
} }

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.tile package info.nightscout.androidaps.tile
class ActionsTileService : TileBase(){ class ActionsTileService : TileBase() {
override val preferencePrefix = "tile_action_" override val preferencePrefix = "tile_action_"
override val resourceVersion = "1" override val resourceVersion = "1"
override val idIconActionPrefix = "ic_action_" override val idIconActionPrefix = "ic_action_"

View file

@ -3,7 +3,7 @@ package info.nightscout.androidaps.tile
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.interaction.actions.TempTargetActivity import info.nightscout.androidaps.interaction.actions.TempTargetActivity
object TempTargetSource: TileSource { object TempTargetSource : TileSource {
override fun getActions(): List<Action> { override fun getActions(): List<Action> {
return listOf( return listOf(
@ -57,4 +57,13 @@ object TempTargetSource: TileSource {
) )
) )
} }
override fun getDefaultConfig(): Map<String, String> {
return mapOf(
"tile_tempt_1" to "activity",
"tile_tempt_2" to "eating_soon",
"tile_tempt_3" to "hypo",
"tile_tempt_4" to "manual"
)
}
} }

View file

@ -1,6 +1,6 @@
package info.nightscout.androidaps.tile package info.nightscout.androidaps.tile
class TempTargetTileService : TileBase(){ class TempTargetTileService : TileBase() {
override val preferencePrefix = "tile_tempt_" override val preferencePrefix = "tile_tempt_"
override val resourceVersion = "1" override val resourceVersion = "1"

View file

@ -1,13 +1,11 @@
package info.nightscout.androidaps.tile package info.nightscout.androidaps.tile
import android.util.Log import android.util.Log
import android.content.SharedPreferences
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
import android.content.SharedPreferences
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.wear.tiles.ActionBuilders import androidx.wear.tiles.ActionBuilders
import androidx.wear.tiles.ColorBuilders.argb import androidx.wear.tiles.ColorBuilders.argb
import androidx.wear.tiles.DeviceParametersBuilders.DeviceParameters import androidx.wear.tiles.DeviceParametersBuilders.DeviceParameters
@ -35,11 +33,8 @@ import androidx.wear.tiles.TileBuilders.Tile
import androidx.wear.tiles.TileService import androidx.wear.tiles.TileService
import androidx.wear.tiles.TimelineBuilders.Timeline import androidx.wear.tiles.TimelineBuilders.Timeline
import androidx.wear.tiles.TimelineBuilders.TimelineEntry import androidx.wear.tiles.TimelineBuilders.TimelineEntry
import com.google.common.util.concurrent.ListenableFuture import com.google.common.util.concurrent.ListenableFuture
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@ -49,10 +44,12 @@ private const val CIRCLE_SIZE = 75f
private val ICON_SIZE = dp(25f) private val ICON_SIZE = dp(25f)
private val SPACING_ACTIONS = dp(3f) private val SPACING_ACTIONS = dp(3f)
private const val BUTTON_COLOR = R.color.gray_850 private const val BUTTON_COLOR = R.color.gray_850
private var sharedPrefs: SharedPreferences? = null const val TAG = "ASTAG-tile"
interface TileSource { interface TileSource {
fun getActions(): List<Action> fun getActions(): List<Action>
fun getDefaultConfig(): Map<String, String>
} }
data class Action( data class Action(
@ -65,9 +62,7 @@ data class Action(
val actionString: String?, val actionString: String?,
) )
const val TAG = "ASTAG-tile" abstract open class TileBase : TileService() {
open class TileBase : TileService(), SharedPreferences.OnSharedPreferenceChangeListener {
open val preferencePrefix = "tile_action_" open val preferencePrefix = "tile_action_"
open val resourceVersion = "1" open val resourceVersion = "1"
@ -76,22 +71,12 @@ open class TileBase : TileService(), SharedPreferences.OnSharedPreferenceChangeL
private val serviceJob = Job() private val serviceJob = Job()
private val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob) private val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob)
private val actionsSelected: MutableList<Action> = mutableListOf()
private val actionsAll: MutableList<Action> = mutableListOf()
override fun onCreate() {
Log.i(TAG, "onCreate: ")
super.onCreate()
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
sharedPrefs?.registerOnSharedPreferenceChangeListener(this)
actionsAll.addAll(source.getActions())
actionsSelected.addAll(getSelectedActions())
}
override fun onTileRequest( override fun onTileRequest(
requestParams: RequestBuilders.TileRequest requestParams: RequestBuilders.TileRequest
): ListenableFuture<Tile> = serviceScope.future { ): ListenableFuture<Tile> = serviceScope.future {
Log.i(TAG, "onTileRequest: ") Log.i(TAG, "onTileRequest: ")
val actionsSelected = getSelectedActions()
Tile.Builder() Tile.Builder()
.setResourcesVersion(resourceVersion) .setResourcesVersion(resourceVersion)
.setTimeline( .setTimeline(
@ -111,7 +96,7 @@ open class TileBase : TileService(), SharedPreferences.OnSharedPreferenceChangeL
Resources.Builder() Resources.Builder()
.setVersion(resourceVersion) .setVersion(resourceVersion)
.apply { .apply {
actionsAll.mapNotNull { action -> source.getActions().mapNotNull { action ->
addIdToImageMapping( addIdToImageMapping(
idIconActionPrefix + action.id, idIconActionPrefix + action.id,
ImageResource.Builder() ImageResource.Builder()
@ -228,26 +213,42 @@ open class TileBase : TileService(), SharedPreferences.OnSharedPreferenceChangeL
) )
.build() .build()
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
Log.i(TAG, "onSharedPreferenceChanged: ")
getUpdater(this).requestUpdate(this::class.java)
}
private fun getSelectedActions(): List<Action> { private fun getSelectedActions(): List<Action> {
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
setDefaultSettings(sharedPrefs)
val actionList: MutableList<Action> = mutableListOf() val actionList: MutableList<Action> = mutableListOf()
for (i in 0..4) { for (i in 0..4) {
val action = getActionFromPreference(i) val action = getActionFromPreference(sharedPrefs, i)
if (action != null) { if (action != null) {
actionList.add(action) actionList.add(action)
} }
} }
Log.i(TAG, "getSelectedActions: " + actionList.toString()) Log.i(TAG, this::class.java.name + ".getSelectedActions: " + actionList.size + " " + actionList.toString())
if (actionList.isEmpty()) {
Log.i(TAG, "getSelectedActions: default")
return source.getActions().take(4)
}
return actionList return actionList
} }
private fun getActionFromPreference(index: Int): Action? { private fun getActionFromPreference(sharedPrefs: SharedPreferences, index: Int): Action? {
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this) val actionPref = sharedPrefs?.getString(preferencePrefix + index, "none")
val actionPref = sharedPrefs.getString(preferencePrefix + index, "none") return source.getActions().find { a -> a.settingName == actionPref }
return actionsAll.find { a -> a.settingName == actionPref }
} }
fun setDefaultSettings(sharedPrefs: SharedPreferences) {
val defaults = source.getDefaultConfig()
val firstKey = defaults.firstNotNullOf { d -> d.key }
if (!sharedPrefs.contains(firstKey)) {
Log.i(TAG, "setDefaultSettings: set defaults")
val editor = sharedPrefs.edit()
for ((key, value) in defaults) {
println("$key = $value")
editor.putString(key, value)
}
editor.apply()
}
}
} }