ComplicationTapBroadcastReceiver -> kt

This commit is contained in:
Milos Kozak 2022-05-02 22:51:05 +02:00
parent 89f86df482
commit f8bfbf29cf
3 changed files with 155 additions and 183 deletions

View file

@ -233,7 +233,7 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid
// We pass the complication id, so we can only update the specific complication tapped.
final PendingIntent complicationPendingIntent =
ComplicationTapBroadcastReceiver.getTapActionIntent(
ComplicationTapBroadcastReceiver.Companion.getTapActionIntent(
getApplicationContext(), thisProvider, complicationId, getComplicationAction());
final RawDisplayData raw = new RawDisplayData();
@ -251,12 +251,14 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid
if (wearUtil.msSince(persistence.whenDataUpdated()) > Constants.STALE_MS) {
// no new data arrived - probably configuration or connection error
final PendingIntent infoToast = ComplicationTapBroadcastReceiver.getTapWarningSinceIntent(
final PendingIntent infoToast =
ComplicationTapBroadcastReceiver.Companion.getTapWarningSinceIntent(
getApplicationContext(), thisProvider, complicationId, ComplicationAction.WARNING_SYNC, persistence.whenDataUpdated());
complicationData = buildNoSyncComplicationData(dataType, raw, complicationPendingIntent, infoToast, persistence.whenDataUpdated());
} else if (wearUtil.msSince(raw.getSingleBg().getTimeStamp()) > Constants.STALE_MS) {
// data arriving from phone AAPS, but it is outdated (uploader/NS/xDrip/Sensor error)
final PendingIntent infoToast = ComplicationTapBroadcastReceiver.getTapWarningSinceIntent(
final PendingIntent infoToast =
ComplicationTapBroadcastReceiver.Companion.getTapWarningSinceIntent(
getApplicationContext(), thisProvider, complicationId, ComplicationAction.WARNING_OLD, raw.getSingleBg().getTimeStamp());
complicationData = buildOutdatedComplicationData(dataType, raw, complicationPendingIntent, infoToast, raw.getSingleBg().getTimeStamp());
} else {

View file

@ -1,180 +0,0 @@
package info.nightscout.androidaps.complications;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.wearable.complications.ProviderUpdateRequester;
import android.widget.Toast;
import androidx.annotation.StringRes;
import javax.inject.Inject;
import dagger.android.DaggerBroadcastReceiver;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interaction.actions.TreatmentActivity;
import info.nightscout.androidaps.interaction.actions.ECarbActivity;
import info.nightscout.androidaps.interaction.actions.WizardActivity;
import info.nightscout.androidaps.interaction.menus.MainMenuActivity;
import info.nightscout.androidaps.interaction.menus.StatusMenuActivity;
import info.nightscout.androidaps.interaction.utils.Constants;
import info.nightscout.androidaps.interaction.utils.DisplayFormat;
import info.nightscout.androidaps.interaction.utils.WearUtil;
import info.nightscout.shared.logging.AAPSLogger;
import info.nightscout.shared.logging.LTag;
import info.nightscout.shared.sharedPreferences.SP;
/*
* Created by dlvoy on 2019-11-12
*/
@SuppressWarnings("deprecation")
public class ComplicationTapBroadcastReceiver extends DaggerBroadcastReceiver {
@Inject WearUtil wearUtil;
@Inject DisplayFormat displayFormat;
@Inject SP sp;
@Inject AAPSLogger aapsLogger;
private static final String EXTRA_PROVIDER_COMPONENT =
"info.nightscout.androidaps.complications.action.PROVIDER_COMPONENT";
private static final String EXTRA_COMPLICATION_ID =
"info.nightscout.androidaps.complications.action.COMPLICATION_ID";
private static final String EXTRA_COMPLICATION_ACTION =
"info.nightscout.androidaps.complications.action.COMPLICATION_ACTION";
private static final String EXTRA_COMPLICATION_SINCE =
"info.nightscout.androidaps.complications.action.COMPLICATION_SINCE";
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Bundle extras = intent.getExtras();
ComponentName provider = extras.getParcelable(EXTRA_PROVIDER_COMPONENT);
int complicationId = extras.getInt(EXTRA_COMPLICATION_ID);
String complicationAction = extras.getString(EXTRA_COMPLICATION_ACTION, ComplicationAction.MENU.toString());
ComplicationAction action = ComplicationAction.MENU;
try {
action = ComplicationAction.valueOf(ComplicationAction.class, complicationAction);
} catch (IllegalArgumentException | NullPointerException ex) {
// but how?
aapsLogger.error(LTag.WEAR, "Cannot interpret complication action: " + complicationAction);
}
action = remapActionWithUserPreferences(action);
// Request an update for the complication that has just been tapped.
ProviderUpdateRequester requester = new ProviderUpdateRequester(context, provider);
requester.requestUpdate(complicationId);
Intent intentOpen = null;
switch (action) {
case NONE:
// do nothing
return;
case WIZARD:
intentOpen = new Intent(context, WizardActivity.class);
break;
case BOLUS:
intentOpen = new Intent(context, TreatmentActivity.class);
break;
case ECARB:
intentOpen = new Intent(context, ECarbActivity.class);
break;
case STATUS:
intentOpen = new Intent(context, StatusMenuActivity.class);
break;
case WARNING_OLD:
case WARNING_SYNC:
long oneAndHalfMinuteAgo =
wearUtil.timestamp() - (Constants.MINUTE_IN_MS + Constants.SECOND_IN_MS * 30);
long since = extras.getLong(EXTRA_COMPLICATION_SINCE, oneAndHalfMinuteAgo);
@StringRes int labelId = (action == ComplicationAction.WARNING_SYNC) ?
R.string.msg_warning_sync : R.string.msg_warning_old;
String msg = String.format(context.getString(labelId), displayFormat.shortTimeSince(since));
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
break;
case MENU:
default:
intentOpen = new Intent(context, MainMenuActivity.class);
}
if (intentOpen != null) {
// Perform intent - open dialog
intentOpen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentOpen);
}
}
private String getComplicationTapAction() {
return sp.getString("complication_tap_action", "default");
}
private ComplicationAction remapActionWithUserPreferences(ComplicationAction originalAction) {
final String userPrefAction = getComplicationTapAction();
switch (originalAction) {
case WARNING_OLD:
case WARNING_SYNC:
// warnings cannot be reconfigured by user
return originalAction;
default:
switch (userPrefAction) {
case "menu":
return ComplicationAction.MENU;
case "wizard":
return ComplicationAction.WIZARD;
case "bolus":
return ComplicationAction.BOLUS;
case "ecarb":
return ComplicationAction.ECARB;
case "status":
return ComplicationAction.STATUS;
case "none":
return ComplicationAction.NONE;
case "default":
default:
return originalAction;
}
}
}
/**
* Returns a pending intent, suitable for use as a tap intent, that causes a complication to be
* toggled and updated.
*/
static PendingIntent getTapActionIntent(
Context context, ComponentName provider, int complicationId, ComplicationAction action) {
Intent intent = new Intent(context, ComplicationTapBroadcastReceiver.class);
intent.putExtra(EXTRA_PROVIDER_COMPONENT, provider);
intent.putExtra(EXTRA_COMPLICATION_ID, complicationId);
intent.putExtra(EXTRA_COMPLICATION_ACTION, action.toString());
// Pass complicationId as the requestCode to ensure that different complications get
// different intents.
return PendingIntent.getBroadcast(
context, complicationId, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
}
/**
* Returns a pending intent, suitable for use as a tap intent, that causes a complication to be
* toggled and updated.
*/
static PendingIntent getTapWarningSinceIntent(
Context context, ComponentName provider, int complicationId, ComplicationAction action, long since) {
Intent intent = new Intent(context, ComplicationTapBroadcastReceiver.class);
intent.putExtra(EXTRA_PROVIDER_COMPONENT, provider);
intent.putExtra(EXTRA_COMPLICATION_ID, complicationId);
intent.putExtra(EXTRA_COMPLICATION_ACTION, action.toString());
intent.putExtra(EXTRA_COMPLICATION_SINCE, since);
// Pass complicationId as the requestCode to ensure that different complications get
// different intents.
return PendingIntent.getBroadcast(
context, complicationId, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
}
}

View file

@ -0,0 +1,150 @@
@file:Suppress("DEPRECATION")
package info.nightscout.androidaps.complications
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.support.wearable.complications.ProviderUpdateRequester
import android.widget.Toast
import androidx.annotation.StringRes
import dagger.android.DaggerBroadcastReceiver
import info.nightscout.androidaps.R
import info.nightscout.androidaps.interaction.actions.ECarbActivity
import info.nightscout.androidaps.interaction.actions.TreatmentActivity
import info.nightscout.androidaps.interaction.actions.WizardActivity
import info.nightscout.androidaps.interaction.menus.MainMenuActivity
import info.nightscout.androidaps.interaction.menus.StatusMenuActivity
import info.nightscout.androidaps.interaction.utils.Constants
import info.nightscout.androidaps.interaction.utils.DisplayFormat
import info.nightscout.androidaps.interaction.utils.WearUtil
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import info.nightscout.shared.sharedPreferences.SP
import javax.inject.Inject
/*
* Created by dlvoy on 2019-11-12
*/
class ComplicationTapBroadcastReceiver : DaggerBroadcastReceiver() {
@Inject lateinit var wearUtil: WearUtil
@Inject lateinit var displayFormat: DisplayFormat
@Inject lateinit var sp: SP
@Inject lateinit var aapsLogger: AAPSLogger
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
val extras = intent.extras
val provider = extras!!.getParcelable<ComponentName>(EXTRA_PROVIDER_COMPONENT)
val complicationId = extras.getInt(EXTRA_COMPLICATION_ID)
val complicationAction = extras.getString(EXTRA_COMPLICATION_ACTION, ComplicationAction.MENU.toString())
var action = ComplicationAction.MENU
try {
action = ComplicationAction.valueOf(complicationAction)
} catch (ex: IllegalArgumentException) {
// but how?
aapsLogger.error(LTag.WEAR, "Cannot interpret complication action: $complicationAction")
} catch (ex: NullPointerException) {
aapsLogger.error(LTag.WEAR, "Cannot interpret complication action: $complicationAction")
}
action = remapActionWithUserPreferences(action)
// Request an update for the complication that has just been tapped.
val requester = ProviderUpdateRequester(context, provider)
requester.requestUpdate(complicationId)
var intentOpen: Intent? = null
when (action) {
ComplicationAction.NONE -> // do nothing
return
ComplicationAction.WIZARD -> intentOpen = Intent(context, WizardActivity::class.java)
ComplicationAction.BOLUS -> intentOpen = Intent(context, TreatmentActivity::class.java)
ComplicationAction.ECARB -> intentOpen = Intent(context, ECarbActivity::class.java)
ComplicationAction.STATUS -> intentOpen = Intent(context, StatusMenuActivity::class.java)
ComplicationAction.WARNING_OLD, ComplicationAction.WARNING_SYNC -> {
val oneAndHalfMinuteAgo = wearUtil.timestamp() - (Constants.MINUTE_IN_MS + Constants.SECOND_IN_MS * 30)
val since = extras.getLong(EXTRA_COMPLICATION_SINCE, oneAndHalfMinuteAgo)
@StringRes val labelId = if (action == ComplicationAction.WARNING_SYNC) R.string.msg_warning_sync else R.string.msg_warning_old
val msg = String.format(context.getString(labelId), displayFormat.shortTimeSince(since))
Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
}
ComplicationAction.MENU -> intentOpen = Intent(context, MainMenuActivity::class.java)
}
if (intentOpen != null) {
// Perform intent - open dialog
intentOpen.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intentOpen)
}
}
private val complicationTapAction: String
get() = sp.getString("complication_tap_action", "default")
private fun remapActionWithUserPreferences(originalAction: ComplicationAction): ComplicationAction {
val userPrefAction = complicationTapAction
return when (originalAction) {
ComplicationAction.WARNING_OLD, ComplicationAction.WARNING_SYNC -> // warnings cannot be reconfigured by user
originalAction
else -> when (userPrefAction) {
"menu" -> ComplicationAction.MENU
"wizard" -> ComplicationAction.WIZARD
"bolus" -> ComplicationAction.BOLUS
"ecarb" -> ComplicationAction.ECARB
"status" -> ComplicationAction.STATUS
"none" -> ComplicationAction.NONE
"default" -> originalAction
else -> originalAction
}
}
}
companion object {
private const val EXTRA_PROVIDER_COMPONENT = "info.nightscout.androidaps.complications.action.PROVIDER_COMPONENT"
private const val EXTRA_COMPLICATION_ID = "info.nightscout.androidaps.complications.action.COMPLICATION_ID"
private const val EXTRA_COMPLICATION_ACTION = "info.nightscout.androidaps.complications.action.COMPLICATION_ACTION"
private const val EXTRA_COMPLICATION_SINCE = "info.nightscout.androidaps.complications.action.COMPLICATION_SINCE"
/**
* Returns a pending intent, suitable for use as a tap intent, that causes a complication to be
* toggled and updated.
*/
fun getTapActionIntent(
context: Context, provider: ComponentName?, complicationId: Int, action: ComplicationAction
): PendingIntent {
val intent = Intent(context, ComplicationTapBroadcastReceiver::class.java)
intent.putExtra(EXTRA_PROVIDER_COMPONENT, provider)
intent.putExtra(EXTRA_COMPLICATION_ID, complicationId)
intent.putExtra(EXTRA_COMPLICATION_ACTION, action.toString())
// Pass complicationId as the requestCode to ensure that different complications get
// different intents.
return PendingIntent.getBroadcast(
context, complicationId, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
}
/**
* Returns a pending intent, suitable for use as a tap intent, that causes a complication to be
* toggled and updated.
*/
fun getTapWarningSinceIntent(
context: Context?, provider: ComponentName?, complicationId: Int, action: ComplicationAction, since: Long
): PendingIntent {
val intent = Intent(context, ComplicationTapBroadcastReceiver::class.java)
intent.putExtra(EXTRA_PROVIDER_COMPONENT, provider)
intent.putExtra(EXTRA_COMPLICATION_ID, complicationId)
intent.putExtra(EXTRA_COMPLICATION_ACTION, action.toString())
intent.putExtra(EXTRA_COMPLICATION_SINCE, since)
// Pass complicationId as the requestCode to ensure that different complications get
// different intents.
return PendingIntent.getBroadcast(
context, complicationId, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
}
}
}