diff --git a/wear/src/main/AndroidManifest.xml b/wear/src/main/AndroidManifest.xml index 86dc19a239..1640dc254c 100644 --- a/wear/src/main/AndroidManifest.xml +++ b/wear/src/main/AndroidManifest.xml @@ -512,6 +512,7 @@ @@ -528,6 +529,7 @@ @@ -607,10 +609,11 @@ - + + diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/TileConfigurationActivity.java b/wear/src/main/java/info/nightscout/androidaps/interaction/TileConfigurationActivity.java deleted file mode 100644 index ef1fee57f6..0000000000 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/TileConfigurationActivity.java +++ /dev/null @@ -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); - } - -} diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/TileConfigurationActivity.kt b/wear/src/main/java/info/nightscout/androidaps/interaction/TileConfigurationActivity.kt new file mode 100644 index 0000000000..0b73d7fa96 --- /dev/null +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/TileConfigurationActivity.kt @@ -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") + } + } +} diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/ActionSource.kt b/wear/src/main/java/info/nightscout/androidaps/tile/ActionSource.kt index 196f9e0b96..7c614f0765 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/ActionSource.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/ActionSource.kt @@ -6,7 +6,7 @@ import info.nightscout.androidaps.interaction.actions.BolusActivity import info.nightscout.androidaps.interaction.actions.ECarbActivity import info.nightscout.androidaps.interaction.actions.TempTargetActivity -object ActionSource: TileSource { +object ActionSource : TileSource { override fun getActions(): List { return listOf( @@ -48,4 +48,14 @@ object ActionSource: TileSource { ) ) } + + override fun getDefaultConfig(): Map { + return mapOf( + "tile_action_1" to "wizzard", + "tile_action_2" to "bolus", + "tile_action_3" to "carbs", + "tile_action_4" to "temp_target" + ) + } + } diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/ActionsTileService.kt b/wear/src/main/java/info/nightscout/androidaps/tile/ActionsTileService.kt index 75867735b7..77cdd3af2b 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/ActionsTileService.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/ActionsTileService.kt @@ -1,6 +1,7 @@ package info.nightscout.androidaps.tile -class ActionsTileService : TileBase(){ +class ActionsTileService : TileBase() { + override val preferencePrefix = "tile_action_" override val resourceVersion = "1" override val idIconActionPrefix = "ic_action_" diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetSource.kt b/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetSource.kt index 210d4215ff..758ed45c5a 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetSource.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetSource.kt @@ -3,7 +3,7 @@ package info.nightscout.androidaps.tile import info.nightscout.androidaps.R import info.nightscout.androidaps.interaction.actions.TempTargetActivity -object TempTargetSource: TileSource { +object TempTargetSource : TileSource { override fun getActions(): List { return listOf( @@ -57,4 +57,13 @@ object TempTargetSource: TileSource { ) ) } + + override fun getDefaultConfig(): Map { + return mapOf( + "tile_tempt_1" to "activity", + "tile_tempt_2" to "eating_soon", + "tile_tempt_3" to "hypo", + "tile_tempt_4" to "manual" + ) + } } diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetTileService.kt b/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetTileService.kt index fa93d1b238..1c7c6ebe39 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetTileService.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetTileService.kt @@ -1,6 +1,6 @@ package info.nightscout.androidaps.tile -class TempTargetTileService : TileBase(){ +class TempTargetTileService : TileBase() { override val preferencePrefix = "tile_tempt_" override val resourceVersion = "1" diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/TileBase.kt b/wear/src/main/java/info/nightscout/androidaps/tile/TileBase.kt index 80d77fa28d..c1d2c18c0a 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/TileBase.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/TileBase.kt @@ -1,13 +1,11 @@ package info.nightscout.androidaps.tile import android.util.Log +import android.content.SharedPreferences import androidx.annotation.DrawableRes import androidx.annotation.StringRes - -import android.content.SharedPreferences import androidx.preference.PreferenceManager import androidx.core.content.ContextCompat - import androidx.wear.tiles.ActionBuilders import androidx.wear.tiles.ColorBuilders.argb 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.TimelineBuilders.Timeline import androidx.wear.tiles.TimelineBuilders.TimelineEntry - import com.google.common.util.concurrent.ListenableFuture - import info.nightscout.androidaps.R - import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -49,10 +44,12 @@ private const val CIRCLE_SIZE = 75f private val ICON_SIZE = dp(25f) private val SPACING_ACTIONS = dp(3f) private const val BUTTON_COLOR = R.color.gray_850 -private var sharedPrefs: SharedPreferences? = null +const val TAG = "ASTAG-tile" interface TileSource { + fun getActions(): List + fun getDefaultConfig(): Map } data class Action( @@ -65,9 +62,7 @@ data class Action( val actionString: String?, ) -const val TAG = "ASTAG-tile" - -open class TileBase : TileService(), SharedPreferences.OnSharedPreferenceChangeListener { +abstract open class TileBase : TileService() { open val preferencePrefix = "tile_action_" open val resourceVersion = "1" @@ -76,22 +71,12 @@ open class TileBase : TileService(), SharedPreferences.OnSharedPreferenceChangeL private val serviceJob = Job() private val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob) - private val actionsSelected: MutableList = mutableListOf() - private val actionsAll: MutableList = 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( requestParams: RequestBuilders.TileRequest ): ListenableFuture = serviceScope.future { Log.i(TAG, "onTileRequest: ") + val actionsSelected = getSelectedActions() Tile.Builder() .setResourcesVersion(resourceVersion) .setTimeline( @@ -111,7 +96,7 @@ open class TileBase : TileService(), SharedPreferences.OnSharedPreferenceChangeL Resources.Builder() .setVersion(resourceVersion) .apply { - actionsAll.mapNotNull { action -> + source.getActions().mapNotNull { action -> addIdToImageMapping( idIconActionPrefix + action.id, ImageResource.Builder() @@ -228,26 +213,42 @@ open class TileBase : TileService(), SharedPreferences.OnSharedPreferenceChangeL ) .build() - override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { - Log.i(TAG, "onSharedPreferenceChanged: ") - getUpdater(this).requestUpdate(this::class.java) - } - private fun getSelectedActions(): List { + val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this) + setDefaultSettings(sharedPrefs) + val actionList: MutableList = mutableListOf() for (i in 0..4) { - val action = getActionFromPreference(i) + val action = getActionFromPreference(sharedPrefs, i) if (action != null) { 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 } - private fun getActionFromPreference(index: Int): Action? { - val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this) - val actionPref = sharedPrefs.getString(preferencePrefix + index, "none") - return actionsAll.find { a -> a.settingName == actionPref } + private fun getActionFromPreference(sharedPrefs: SharedPreferences, index: Int): Action? { + val actionPref = sharedPrefs?.getString(preferencePrefix + index, "none") + return source.getActions().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() + } + } + }