From 9124788c4e496caacc73bb8fe0131d48e1753281 Mon Sep 17 00:00:00 2001 From: gregorybel Date: Mon, 12 Nov 2018 22:38:49 +0100 Subject: [PATCH] first commit --- app/src/main/AndroidManifest.xml | 3 + .../PersistentNotificationPlugin.java | 66 ++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 264132de25..19822e7724 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -34,6 +34,9 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> + diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java index cd69e88210..fdddc13990 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java @@ -13,6 +13,14 @@ import android.os.Build; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; +// Android Auto +import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationManagerCompat; +import android.support.v4.app.RemoteInput; + + + + import com.squareup.otto.Subscribe; import info.nightscout.androidaps.Config; @@ -60,6 +68,17 @@ public class PersistentNotificationPlugin extends PluginBase { public static final int ONGOING_NOTIFICATION_ID = 4711; private final Context ctx; + /// For Android Auto + private static final String PACKAGE = "info.nightscout"; + private static final String READ_ACTION = + "info.nightscout.androidaps.ACTION_MESSAGE_READ"; + private static final String REPLY_ACTION = + "info.nightscout.androidaps.ACTION_MESSAGE_REPLY"; + private static final String CONVERSATION_ID = "conversation_id"; + private static final String EXTRA_VOICE_REPLY = "extra_voice_reply"; + /// End Android Auto + + public PersistentNotificationPlugin(Context ctx) { super(new PluginDescription() .mainType(PluginType.GENERAL) @@ -143,7 +162,7 @@ public class PersistentNotificationPlugin extends PluginBase { IobTotal basalIob = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals().round(); - String line2 = MainApp.gs(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U " + MainApp.gs(R.string.cob)+": " + IobCobCalculatorPlugin.getPlugin().getCobInfo(false, "PersistentNotificationPlugin").generateCOBString();; + String line2 = MainApp.gs(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U " + MainApp.gs(R.string.cob)+": " + IobCobCalculatorPlugin.getPlugin().getCobInfo(false, "PersistentNotificationPlugin").generateCOBString(); String line3 = DecimalFormatter.to2Decimal(ConfigBuilderPlugin.getPlugin().getActivePump().getBaseBasalRate()) + " U/h"; @@ -151,6 +170,45 @@ public class PersistentNotificationPlugin extends PluginBase { line3 += " - " + ProfileFunctions.getInstance().getProfileName(); + /// For Android Auto + Intent msgReadIntent = new Intent() + .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) + .setAction(READ_ACTION) + .putExtra(CONVERSATION_ID, ONGOING_NOTIFICATION_ID) + .setPackage(PACKAGE); + + PendingIntent msgReadPendingIntent = + PendingIntent.getBroadcast(ctx, + ONGOING_NOTIFICATION_ID, + msgReadIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + + Intent msgReplyIntent = new Intent() + .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) + .setAction(REPLY_ACTION) + .putExtra(CONVERSATION_ID, ONGOING_NOTIFICATION_ID) + .setPackage(PACKAGE); + + PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast( + ctx, + ONGOING_NOTIFICATION_ID, + msgReplyIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + + // Build a RemoteInput for receiving voice input from devices + RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).build(); + + // Create the UnreadConversation + NotificationCompat.CarExtender.UnreadConversation.Builder unreadConversationBuilder = + new NotificationCompat.CarExtender.UnreadConversation.Builder(line1 + "\n" + line2) + .setLatestTimestamp(System.currentTimeMillis()) + .setReadPendingIntent(msgReadPendingIntent) + .setReplyAction(msgReplyPendingIntent, remoteInput); + + unreadConversationBuilder.addMessage(line1 + ". " + line2 + ". " + line3); + /// End Android Auto + + NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID); builder.setOngoing(true); builder.setOnlyAlertOnce(true); @@ -167,6 +225,12 @@ public class PersistentNotificationPlugin extends PluginBase { builder.setContentTitle(line1); builder.setContentText(line2); builder.setSubText(line3); + /// Android Auto + builder.extend(new NotificationCompat.CarExtender() + .setUnreadConversation(unreadConversationBuilder.build())); + unreadConversationBuilder.addMessage(line1+line2+line3); + /// End Android Auto + Intent resultIntent = new Intent(ctx, MainActivity.class);