Merge pull request #95 from AdrianLxM/ongoingnotif

Ongoing Notification
This commit is contained in:
Milos Kozak 2016-12-28 15:22:05 +01:00 committed by GitHub
commit 4108e57589
17 changed files with 259 additions and 0 deletions

View file

@ -39,6 +39,7 @@ import info.nightscout.androidaps.plugins.TempBasals.TempBasalsFragment;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment; import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment;
import info.nightscout.androidaps.plugins.Wear.WearFragment; import info.nightscout.androidaps.plugins.Wear.WearFragment;
import info.nightscout.androidaps.plugins.persistentnotification.PersistentNotificationPlugin;
import info.nightscout.androidaps.receivers.KeepAliveReceiver; import info.nightscout.androidaps.receivers.KeepAliveReceiver;
import io.fabric.sdk.android.Fabric; import io.fabric.sdk.android.Fabric;
@ -93,6 +94,7 @@ public class MainApp extends Application {
pluginsList.add(SmsCommunicatorFragment.getPlugin()); pluginsList.add(SmsCommunicatorFragment.getPlugin());
if (Config.WEAR) pluginsList.add(WearFragment.getPlugin(this)); if (Config.WEAR) pluginsList.add(WearFragment.getPlugin(this));
pluginsList.add(new PersistentNotificationPlugin(this));
pluginsList.add(sConfigBuilder = ConfigBuilderFragment.getPlugin()); pluginsList.add(sConfigBuilder = ConfigBuilderFragment.getPlugin());

View file

@ -0,0 +1,14 @@
package info.nightscout.androidaps.plugins.persistentnotification;
import android.support.v4.app.Fragment;
import info.nightscout.androidaps.interfaces.FragmentBase;
/**
* Created by adrian on 23/12/16.
*/
public class PersistentNotificationFragment extends Fragment implements FragmentBase {
}

View file

@ -0,0 +1,241 @@
package info.nightscout.androidaps.plugins.persistentnotification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.NotificationCompat;
import com.squareup.otto.Subscribe;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.events.EventInitializationChanged;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DecimalFormatter;
/**
* Created by adrian on 23/12/16.
*/
public class PersistentNotificationPlugin implements PluginBase{
private static final int ONGOING_NOTIFICATION_ID = 4711;
static boolean fragmentEnabled = false;
private final Context ctx;
public PersistentNotificationPlugin(Context ctx) {
this.ctx = ctx;
}
@Override
public int getType() {
return GENERAL;
}
@Override
public String getFragmentClass() {
return PersistentNotificationFragment.class.getName();
}
@Override
public String getName() {
return ctx.getString(R.string.ongoingnotificaction);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
if(getType() == type){
this.fragmentEnabled = fragmentEnabled;
checkBusRegistration();
updateNotification();
}
}
private void updateNotification() {
if(!fragmentEnabled){
NotificationManager mNotificationManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(ONGOING_NOTIFICATION_ID);
return;
}
String line1 = ctx.getString(R.string.noprofile);
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
BgReading lastBG = MainApp.getDbHelper().lastBg();
DatabaseHelper.GlucoseStatus glucoseStatus = MainApp.getDbHelper().getGlucoseStatusData();
if(profile != null && lastBG != null) {
line1 = lastBG.valueToUnitsToString(profile.getUnits());
}
if (glucoseStatus != null) {
line1 += " Δ" + deltastring(glucoseStatus.delta, glucoseStatus.delta * Constants.MGDL_TO_MMOLL, profile.getUnits())
+ " avgΔ" + deltastring(glucoseStatus.avgdelta, glucoseStatus.avgdelta * Constants.MGDL_TO_MMOLL, profile.getUnits());
} else {
line1 += " " +
ctx.getString(R.string.old_data) +
" ";
}
PumpInterface pump = MainApp.getConfigBuilder();
if (pump.isTempBasalInProgress()) {
TempBasal activeTemp = pump.getTempBasal();
line1 += " " + activeTemp.toStringShort();
}
//IOB
MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB();
IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round();
if (bolusIob == null) bolusIob = new IobTotal();
MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB();
IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round();
if (basalIob == null) basalIob = new IobTotal();
String line2 = ctx.getString(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U ("
+ ctx.getString(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U "
+ ctx.getString(R.string.basal) + ": " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U)";
String line3 = DecimalFormatter.to2Decimal(pump.getBaseBasalRate()) + " U/h";
if (profile != null && profile.getActiveProfile() != null)
line3 += " - " + profile.getActiveProfile();
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setOngoing(true);
builder.setCategory(NotificationCompat.CATEGORY_STATUS);
builder.setSmallIcon(R.drawable.ic_notification);
Bitmap largeIcon = BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(largeIcon);
builder.setContentTitle(line1);
builder.setContentText(line2);
builder.setSubText(line3);
Intent resultIntent = new Intent(ctx, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
android.app.Notification notification = builder.build();
mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification);
}
private void checkBusRegistration() {
if(fragmentEnabled){
MainApp.bus().register(this);
} else {
MainApp.bus().unregister(this);
}
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
//no visible fragment
}
private String deltastring(double deltaMGDL, double deltaMMOL, String units) {
String deltastring = "";
if (deltaMGDL >=0){
deltastring += "+";
} else{
deltastring += "-";
}
if (units.equals(Constants.MGDL)){
deltastring += DecimalFormatter.to1Decimal(Math.abs(deltaMGDL));
}
else {
deltastring += DecimalFormatter.to1Decimal(Math.abs(deltaMMOL));
}
return deltastring;
}
@Subscribe
public void onStatusEvent(final EventPreferenceChange ev) {
updateNotification();
}
@Subscribe
public void onStatusEvent(final EventTreatmentChange ev) {
updateNotification();
}
@Subscribe
public void onStatusEvent(final EventTempBasalChange ev) {
updateNotification();
}
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
updateNotification();
}
@Subscribe
public void onStatusEvent(final EventNewBasalProfile ev) {
updateNotification();
}
@Subscribe
public void onStatusEvent(final EventInitializationChanged ev) {
updateNotification();
}
@Subscribe
public void onStatusEvent(final EventRefreshGui ev) {
updateNotification();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 972 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 907 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 569 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 506 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 836 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

View file

@ -387,4 +387,6 @@
<string name="actualbg">BG:</string> <string name="actualbg">BG:</string>
<string name="lastbg">Last BG:</string> <string name="lastbg">Last BG:</string>
<string name="mdi">MDI</string> <string name="mdi">MDI</string>
<string name="ongoingnotificaction">Ongoing Notification</string>
<string name="old_data">OLD DATA</string>
</resources> </resources>