fast >>>Loading ...<<< notification on app start

This commit is contained in:
Milos Kozak 2019-08-26 17:33:20 +02:00
parent 1eff4697fa
commit 4c7b8ef210
4 changed files with 44 additions and 25 deletions

View file

@ -31,7 +31,7 @@ public class DummyService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().updateNotification()); startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().getLastNotification());
return START_STICKY; return START_STICKY;
} }
@ -48,8 +48,7 @@ public class DummyService extends Service {
super.onCreate(); super.onCreate();
// TODO: I guess this was moved here in order to adhere to the 5 seconds rule to call "startForeground" after a Service was called as Foreground service? // TODO: I guess this was moved here in order to adhere to the 5 seconds rule to call "startForeground" after a Service was called as Foreground service?
// As onCreate() is not called every time a service is started, copied to onStartCommand(). // As onCreate() is not called every time a service is started, copied to onStartCommand().
Notification notification = PersistentNotificationPlugin.getPlugin().updateNotification(); startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().getLastNotification());
startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, notification);
MainApp.bus().register(this); MainApp.bus().register(this);
} }

View file

@ -89,10 +89,10 @@ public class PersistentNotificationPlugin extends PluginBase {
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart();
createNotificationChannel(); // make sure channels exist before triggering updates through the bus createNotificationChannel(); // make sure channels exist before triggering updates through the bus
MainApp.bus().register(this); MainApp.bus().register(this);
triggerNotificationUpdate(); triggerNotificationUpdate(true);
super.onStart();
} }
private void createNotificationChannel() { private void createNotificationChannel() {
@ -114,21 +114,23 @@ public class PersistentNotificationPlugin extends PluginBase {
super.onStop(); super.onStop();
} }
private void triggerNotificationUpdate() { private void triggerNotificationUpdate(boolean boot) {
updateNotification(boot);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
MainApp.instance().startForegroundService(new Intent(MainApp.instance(), DummyService.class)); MainApp.instance().startForegroundService(new Intent(MainApp.instance(), DummyService.class));
else else
MainApp.instance().startService(new Intent(MainApp.instance(), DummyService.class)); MainApp.instance().startService(new Intent(MainApp.instance(), DummyService.class));
} }
@Nonnull private void updateNotification(boolean boot) {
public Notification updateNotification() { String line1;
String line1 = null;
String line2 = null; String line2 = null;
String line3 = null; String line3 = null;
NotificationCompat.CarExtender.UnreadConversation.Builder unreadConversationBuilder = null; NotificationCompat.CarExtender.UnreadConversation.Builder unreadConversationBuilder = null;
if (ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null && ProfileFunctions.getInstance().isProfileValid("Notification")) { if (boot) {
line1 = MainApp.gs(R.string.loading);
} else if (ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null && ProfileFunctions.getInstance().isProfileValid("Notification")) {
String line1_aa; String line1_aa;
String units = ProfileFunctions.getInstance().getProfileUnits(); String units = ProfileFunctions.getInstance().getProfileUnits();
@ -214,6 +216,8 @@ public class PersistentNotificationPlugin extends PluginBase {
/// Add dot to produce a "more natural sounding result" /// Add dot to produce a "more natural sounding result"
unreadConversationBuilder.addMessage(line3_aa); unreadConversationBuilder.addMessage(line3_aa);
/// End Android Auto /// End Android Auto
} else {
line1 = MainApp.gs(R.string.noprofileset);
} }
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainApp.instance(), CHANNEL_ID); NotificationCompat.Builder builder = new NotificationCompat.Builder(MainApp.instance(), CHANNEL_ID);
@ -223,9 +227,9 @@ public class PersistentNotificationPlugin extends PluginBase {
builder.setSmallIcon(MainApp.getNotificationIcon()); builder.setSmallIcon(MainApp.getNotificationIcon());
Bitmap largeIcon = BitmapFactory.decodeResource(MainApp.instance().getResources(), MainApp.getIcon()); Bitmap largeIcon = BitmapFactory.decodeResource(MainApp.instance().getResources(), MainApp.getIcon());
builder.setLargeIcon(largeIcon); builder.setLargeIcon(largeIcon);
builder.setContentTitle(line1 != null ? line1 : MainApp.gs(R.string.noprofileset)); if (line1 != null) builder.setContentTitle(line1);
builder.setContentText(line2 != null ? line2 : MainApp.gs(R.string.noprofileset)); if (line2 != null) builder.setContentText(line2);
builder.setSubText(line3 != null ? line3 : MainApp.gs(R.string.noprofileset)); if (line3 != null) builder.setSubText(line3);
/// Android Auto /// Android Auto
if (unreadConversationBuilder != null) { if (unreadConversationBuilder != null) {
builder.extend(new NotificationCompat.CarExtender() builder.extend(new NotificationCompat.CarExtender()
@ -251,7 +255,6 @@ public class PersistentNotificationPlugin extends PluginBase {
android.app.Notification notification = builder.build(); android.app.Notification notification = builder.build();
mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification); mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification);
this.notification = notification; this.notification = notification;
return notification;
} }
private String deltastring(double deltaMGDL, double deltaMMOL, String units) { private String deltastring(double deltaMGDL, double deltaMMOL, String units) {
@ -278,48 +281,50 @@ public class PersistentNotificationPlugin extends PluginBase {
public Notification getLastNotification() { public Notification getLastNotification() {
if (notification != null) return notification; if (notification != null) return notification;
else return new Notification(); else {
throw new IllegalStateException("Notification is null");
}
} }
@Subscribe @Subscribe
public void onStatusEvent(final EventPreferenceChange ev) { public void onStatusEvent(final EventPreferenceChange ev) {
triggerNotificationUpdate(); triggerNotificationUpdate(false);
} }
@Subscribe @Subscribe
public void onStatusEvent(final EventTreatmentChange ev) { public void onStatusEvent(final EventTreatmentChange ev) {
triggerNotificationUpdate(); triggerNotificationUpdate(false);
} }
@Subscribe @Subscribe
public void onStatusEvent(final EventTempBasalChange ev) { public void onStatusEvent(final EventTempBasalChange ev) {
triggerNotificationUpdate(); triggerNotificationUpdate(false);
} }
@Subscribe @Subscribe
public void onStatusEvent(final EventExtendedBolusChange ev) { public void onStatusEvent(final EventExtendedBolusChange ev) {
triggerNotificationUpdate(); triggerNotificationUpdate(false);
} }
@Subscribe @Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ev) { public void onStatusEvent(final EventAutosensCalculationFinished ev) {
triggerNotificationUpdate(); triggerNotificationUpdate(false);
} }
@Subscribe @Subscribe
public void onStatusEvent(final EventNewBasalProfile ev) { public void onStatusEvent(final EventNewBasalProfile ev) {
triggerNotificationUpdate(); triggerNotificationUpdate(false);
} }
@Subscribe @Subscribe
public void onStatusEvent(final EventInitializationChanged ev) { public void onStatusEvent(final EventInitializationChanged ev) {
triggerNotificationUpdate(); triggerNotificationUpdate(false);
} }
@Subscribe @Subscribe
public void onStatusEvent(final EventRefreshOverview ev) { public void onStatusEvent(final EventRefreshOverview ev) {
triggerNotificationUpdate(); triggerNotificationUpdate(false);
} }
} }

View file

@ -11,10 +11,14 @@ import android.os.IBinder;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventLocationChange; import info.nightscout.androidaps.events.EventLocationChange;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.bus.RxBus;
@ -80,14 +84,14 @@ public class LocationService extends Service {
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
if (L.isEnabled(L.LOCATION)) if (L.isEnabled(L.LOCATION))
log.debug("onStartCommand"); log.debug("onStartCommand");
startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().updateNotification()); startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().getLastNotification());
return START_STICKY; return START_STICKY;
} }
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().updateNotification()); startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().getLastNotification());
if (L.isEnabled(L.LOCATION)) if (L.isEnabled(L.LOCATION))
log.debug("onCreate"); log.debug("onCreate");
@ -121,6 +125,7 @@ public class LocationService extends Service {
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
log.error("network provider does not exist, " + ex.getMessage()); log.error("network provider does not exist, " + ex.getMessage());
} }
MainApp.bus().register(this);
} }
@Override @Override
@ -138,6 +143,15 @@ public class LocationService extends Service {
log.error("fail to remove location listener, ignore", ex); log.error("fail to remove location listener, ignore", ex);
} }
} }
MainApp.bus().unregister(this);
}
@Subscribe
public void onStatusEvent(EventAppExit event) {
if (L.isEnabled(L.CORE))
log.debug("EventAppExit received");
stopSelf();
} }
private void initializeLocationManager() { private void initializeLocationManager() {

View file

@ -1668,6 +1668,7 @@
<string name="bolusconstraintappliedwarning"><![CDATA[<font color=\'%1$s\'>Bolus constraint applied: %2$.2fU to %3$.2fU</font>]]></string> <string name="bolusconstraintappliedwarning"><![CDATA[<font color=\'%1$s\'>Bolus constraint applied: %2$.2fU to %3$.2fU</font>]]></string>
<string name="slowabsorptiondetected"><![CDATA[<font color=\'%1$s\'>!!!!! Slow carbs absorption detected: %2$d%% of time. Double check your calculation. COB can be really off !!!!!</font>]]></string> <string name="slowabsorptiondetected"><![CDATA[<font color=\'%1$s\'>!!!!! Slow carbs absorption detected: %2$d%% of time. Double check your calculation. COB can be really off !!!!!</font>]]></string>
<string name="reservoirvalue">%1$.0f / %2$d U</string> <string name="reservoirvalue">%1$.0f / %2$d U</string>
<string name="loading">Loading ...</string>
<plurals name="objective_days"> <plurals name="objective_days">
<item quantity="one">%1$d day</item> <item quantity="one">%1$d day</item>