Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/NightscoutFoundation/xDrip
Browse files Browse the repository at this point in the history
…into jamorhamvoice
  • Loading branch information
jamorham committed Oct 13, 2018
2 parents e1d8f15 + 6ee9084 commit af7027e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 5 deletions.
Expand Up @@ -40,7 +40,7 @@ public class NotificationChannels {
public static final String BG_PREDICTED_LOW_CHANNEL = "bgPredictedLowChannel";
public static final String BG_PERSISTENT_HIGH_CHANNEL = "bgPersistentHighChannel";
public static final String CALIBRATION_CHANNEL = "calibrationChannel";
//public static final String ONGOING_CHANNEL = "ongoingChannel";
public static final String ONGOING_CHANNEL = "ongoingChannel";

// get a localized string for each channel / group name
public static String getString(String id) {
Expand All @@ -64,6 +64,7 @@ private static synchronized void initialize_name_map() {
map.put(BG_PREDICTED_LOW_CHANNEL, xdrip.getAppContext().getString(R.string.low_predicted));
map.put(BG_PERSISTENT_HIGH_CHANNEL, xdrip.getAppContext().getString(R.string.persistent_high_alert));
map.put(CALIBRATION_CHANNEL, xdrip.getAppContext().getString(R.string.calibration_alerts));
map.put(ONGOING_CHANNEL, "Ongoing Notification");
}


Expand Down Expand Up @@ -146,6 +147,14 @@ public static void cleanAllNotificationChannels() {

}

private static boolean addChannelGroup() {
// If notifications are grouped, the BG number icon doesn't update
if (Pref.getBooleanDefaultFalse("use_number_icon")) {
return false;
}
return Pref.getBooleanDefaultFalse("notification_channels_grouping");
}

@TargetApi(26)
public static NotificationChannel getChan(NotificationCompat.Builder wip) {

Expand Down Expand Up @@ -185,7 +194,11 @@ public static NotificationChannel getChan(NotificationCompat.Builder wip) {

// mirror the settings from the previous channel
channel.setSound(template.getSound(), generic_audio);
channel.setGroup(template.getGroup());
if (addChannelGroup()) {
channel.setGroup(template.getGroup());
} else {
channel.setGroup(channel.getId());
}
channel.setDescription(template.getDescription());
channel.setVibrationPattern(template.getVibrationPattern());
template.setLightColor(wip.mNotification.ledARGB);
Expand All @@ -199,4 +212,63 @@ public static NotificationChannel getChan(NotificationCompat.Builder wip) {
getNotifManager().createNotificationChannel(channel);
return channel;
}

@TargetApi(26)
public static NotificationChannel getChan(Notification.Builder wip) {

final Notification temp = wip.build();
if (temp.getChannelId() == null) return null;

// create generic audio attributes
final AudioAttributes generic_audio = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.build();

// create notification channel for hashing purposes from the existing notification builder
NotificationChannel template = new NotificationChannel(
temp.getChannelId(),
getString(temp.getChannelId()),
NotificationManager.IMPORTANCE_DEFAULT);


// mirror the notification parameters in the channel
template.setGroup(temp.getChannelId());
template.setVibrationPattern(temp.vibrate);
template.setSound(temp.sound, generic_audio);
template.setLightColor(temp.ledARGB);
if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());

// get a nice string to identify the hash
final String mhash = my_text_hash(template);

// create another notification channel using the hash because id is immutable
final NotificationChannel channel = new NotificationChannel(
template.getId() + mhash,
getString(temp.getChannelId()) + mhash,
NotificationManager.IMPORTANCE_DEFAULT);

// mirror the settings from the previous channel
channel.setSound(template.getSound(), generic_audio);
if (addChannelGroup()) {
channel.setGroup(template.getGroup());
} else {
channel.setGroup(channel.getId());
}
channel.setDescription(template.getDescription());
channel.setVibrationPattern(template.getVibrationPattern());
template.setLightColor(temp.ledARGB);
if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());

// create a group to hold this channel if one doesn't exist or update text
getNotifManager().createNotificationChannelGroup(new NotificationChannelGroup(channel.getGroup(), getString(channel.getGroup())));
// create this channel if it doesn't exist or update text
getNotifManager().createNotificationChannel(channel);
return channel;
}

}
Expand Up @@ -558,7 +558,13 @@ private Bitmap createWearBitmap(long hours) {
.build();
}*/

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private boolean useOngoingChannel() {
return (Pref.getBooleanDefaultFalse("use_notification_channels") &&
Pref.getBooleanDefaultFalse("ongoing_notification_channel") &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O);
}

@TargetApi(Build.VERSION_CODES.O)
public synchronized Notification createOngoingNotification(BgGraphBuilder bgGraphBuilder, Context context) {
mContext = context;
ReadPerfs(mContext);
Expand All @@ -580,8 +586,16 @@ public synchronized Notification createOngoingNotification(BgGraphBuilder bgGrap

//final NotificationCompat.Builder b = new NotificationCompat.Builder(mContext, NotificationChannels.ONGOING_CHANNEL);
//final NotificationCompat.Builder b = new NotificationCompat.Builder(mContext); // temporary fix until ONGOING CHANNEL is silent by default on android 8+
final Notification.Builder b = new Notification.Builder(mContext); // temporary fix until ONGOING CHANNEL is silent by default on android 8+
//final Notification.Builder b = new Notification.Builder(mContext); // temporary fix until ONGOING CHANNEL is silent by default on android 8+
final Notification.Builder b;
if (useOngoingChannel()) {
b = new Notification.Builder(mContext, NotificationChannels.ONGOING_CHANNEL);
b.setSound(null);
} else {
b = new Notification.Builder(mContext);
}
b.setOngoing(true); // TODO CHECK THIS!!

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
b.setVisibility(Pref.getBooleanDefaultFalse("public_notifications") ? Notification.VISIBILITY_PUBLIC : Notification.VISIBILITY_PRIVATE);
b.setCategory(NotificationCompat.CATEGORY_STATUS);
Expand Down Expand Up @@ -669,7 +683,8 @@ public synchronized Notification createOngoingNotification(BgGraphBuilder bgGrap
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
b.setLocalOnly(true);
}
return b.build();
// strips channel ID if disabled
return XdripNotification.build(b);
}

private synchronized void bgOngoingNotification(final BgGraphBuilder bgGraphBuilder) {
Expand Down
@@ -0,0 +1,37 @@
package com.eveningoutpost.dexdrip.UtilityModels;

import android.annotation.TargetApi;
import android.app.Notification;
import android.os.Build;

/*
* Created by jwoglom on 5/17/2018
* <p>
* Wrapper for android.app.Notification.Builder that adds the necessary notification
* channel ID if enabled. Identical functionality-wise to XdripNotificationCompat.
*/

public class XdripNotification {

@TargetApi(Build.VERSION_CODES.O)
public static Notification build(Notification.Builder builder) {
if ((Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)) {
if (Pref.getBooleanDefaultFalse("use_notification_channels")) {
// get dynamic channel based on contents of the builder
try {
final String id = NotificationChannels.getChan(builder).getId();
builder.setChannelId(id);
} catch (NullPointerException e) {
//noinspection ConstantConditions
builder.setChannelId(null);
}
} else {
//noinspection ConstantConditions
builder.setChannelId(null);
}
return builder.build();
} else {
return builder.build(); // standard pre-oreo behaviour
}
}
}
14 changes: 14 additions & 0 deletions app/src/main/res/xml/pref_notifications.xml
Expand Up @@ -82,6 +82,20 @@
android:summary="Use the Android 8+ notification channel feature"
android:title="Notification Channels" />

<CheckBoxPreference
android:defaultValue="false"
android:dependency="use_notification_channels"
android:key="ongoing_notification_channel"
android:summary="May trigger ambient display notifications on some devices. Needs testing!"
android:title="Use ongoing notification channel" />

<CheckBoxPreference
android:defaultValue="false"
android:dependency="use_notification_channels"
android:key="notification_channels_grouping"
android:summary="Whether notifications should be grouped together. Incompatible with 'Number Item in Notification Area' option"
android:title="Group notifications together" />

</PreferenceScreen>


Expand Down

0 comments on commit af7027e

Please sign in to comment.