mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-28 21:06:50 +01:00
Oreo fixes (#2)
* Updated to use the right service API. * Created a channel for notifications on Oreo and later. * Switched all notification builders to use the correct channel. * Internationalized the channel name.
This commit is contained in:
parent
4bd30964f1
commit
3b6da66643
@ -172,7 +172,11 @@ public class GBApplication extends Application {
|
||||
if (isRunningMarshmallowOrLater()) {
|
||||
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
//the following will ensure the notification manager is kept alive
|
||||
if(!isRunningOreoOrLater()) {
|
||||
startService(new Intent(this, NotificationCollectorMonitorService.class));
|
||||
} else {
|
||||
startForegroundService(new Intent(this, NotificationCollectorMonitorService.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -290,6 +294,10 @@ public class GBApplication extends Application {
|
||||
return VERSION.SDK_INT >= Build.VERSION_CODES.M;
|
||||
}
|
||||
|
||||
public static boolean isRunningOreoOrLater(){
|
||||
return VERSION.SDK_INT >= Build.VERSION_CODES.O;
|
||||
}
|
||||
|
||||
private static boolean isPrioritySender(int prioritySenders, String number) {
|
||||
if (prioritySenders == Policy.PRIORITY_SENDERS_ANY) {
|
||||
return true;
|
||||
|
@ -52,6 +52,8 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.service.NotificationCollectorMonitorService.NOTIFICATION_CHANNEL_ID;
|
||||
|
||||
|
||||
public class DebugActivity extends AbstractGBActivity {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DebugActivity.class);
|
||||
@ -266,7 +268,7 @@ public class DebugActivity extends AbstractGBActivity {
|
||||
|
||||
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender().addAction(action);
|
||||
|
||||
NotificationCompat.Builder ncomp = new NotificationCompat.Builder(this)
|
||||
NotificationCompat.Builder ncomp = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
|
||||
.setContentTitle(getString(R.string.test_notification))
|
||||
.setContentText(getString(R.string.this_is_a_test_notification_from_gadgetbridge))
|
||||
.setTicker(getString(R.string.this_is_a_test_notification_from_gadgetbridge))
|
||||
|
@ -66,6 +66,8 @@ import nodomain.freeyourgadget.gadgetbridge.service.receivers.GBMusicControlRece
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.service.NotificationCollectorMonitorService.NOTIFICATION_CHANNEL_ID;
|
||||
|
||||
// TODO: support option for a single reminder notification when notifications could not be delivered?
|
||||
// conditions: app was running and received notifications, but device was not connected.
|
||||
// maybe need to check for "unread notifications" on device for that.
|
||||
@ -257,7 +259,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
|
||||
|
||||
NotificationCompat.Action action = new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_share, "share", pendingShareIntent).build();
|
||||
|
||||
Notification notif = new NotificationCompat.Builder(context)
|
||||
Notification notif = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
|
||||
.setContentTitle("Screenshot taken")
|
||||
.setTicker("Screenshot taken")
|
||||
.setContentText(filename)
|
||||
|
@ -16,7 +16,11 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
package nodomain.freeyourgadget.gadgetbridge.service;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.ActivityManager;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.Service;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
@ -24,29 +28,50 @@ import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.IBinder;
|
||||
import android.os.Process;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.NotificationListener;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.GBApplication.isRunningMarshmallowOrLater;
|
||||
|
||||
/**
|
||||
* Original source by xinghui - see https://gist.github.com/xinghui/b2ddd8cffe55c4b62f5d8846d5545bf9
|
||||
* NB: no license specified in the source code as of 2017-04-19
|
||||
*/
|
||||
public class NotificationCollectorMonitorService extends Service {
|
||||
|
||||
public static final String NOTIFICATION_CHANNEL_ID = "gadgetbridge";
|
||||
private static final Logger LOG = LoggerFactory.getLogger(NotificationCollectorMonitorService.class);
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressLint("NewApi")
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
ensureCollectorRunning();
|
||||
if (isRunningMarshmallowOrLater()) {
|
||||
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
|
||||
getString(R.string.notification_channel_name),
|
||||
NotificationManager.IMPORTANCE_LOW);
|
||||
|
||||
// Create notification channel required
|
||||
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
|
||||
.createNotificationChannel(channel);
|
||||
|
||||
// Placeholder notification
|
||||
Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
|
||||
.setContentTitle("Gadgetbridge")
|
||||
.setContentText("")
|
||||
.build();
|
||||
|
||||
startForeground(1, notification);
|
||||
}
|
||||
|
||||
ensureCollectorRunning();
|
||||
}
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
return START_STICKY;
|
||||
|
@ -50,6 +50,8 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.service.NotificationCollectorMonitorService.NOTIFICATION_CHANNEL_ID;
|
||||
|
||||
public class GB {
|
||||
public static final int NOTIFICATION_ID = 1;
|
||||
public static final int NOTIFICATION_ID_INSTALL = 2;
|
||||
@ -84,7 +86,7 @@ public class GB {
|
||||
}
|
||||
|
||||
Boolean connected = device.isInitialized();
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
|
||||
builder.setContentTitle(deviceName)
|
||||
.setTicker(deviceName + " - " + text)
|
||||
.setContentText(text)
|
||||
@ -119,7 +121,7 @@ public class GB {
|
||||
}
|
||||
|
||||
public static Notification createNotification(String text, Context context) {
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
|
||||
builder.setTicker(text)
|
||||
.setContentText(text)
|
||||
.setSmallIcon(R.drawable.ic_notification_disconnected)
|
||||
@ -324,7 +326,7 @@ public class GB {
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
|
||||
notificationIntent, 0);
|
||||
|
||||
NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
|
||||
NotificationCompat.Builder nb = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
.setContentTitle(context.getString(R.string.app_name))
|
||||
.setContentText(text)
|
||||
@ -365,7 +367,7 @@ public class GB {
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
|
||||
notificationIntent, 0);
|
||||
|
||||
NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
|
||||
NotificationCompat.Builder nb = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
|
||||
.setContentTitle(context.getString(R.string.app_name))
|
||||
.setContentText(text)
|
||||
.setTicker(text)
|
||||
@ -395,7 +397,7 @@ public class GB {
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
|
||||
notificationIntent, 0);
|
||||
|
||||
NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
|
||||
NotificationCompat.Builder nb = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
|
||||
.setContentTitle(context.getString(R.string.notif_battery_low_title))
|
||||
.setContentText(text)
|
||||
.setContentIntent(pendingIntent)
|
||||
@ -429,7 +431,7 @@ public class GB {
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
|
||||
notificationIntent, 0);
|
||||
|
||||
NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
|
||||
NotificationCompat.Builder nb = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
|
||||
.setContentTitle(context.getString(R.string.notif_export_failed_title))
|
||||
.setContentText(text)
|
||||
.setContentIntent(pendingIntent)
|
||||
|
@ -531,4 +531,5 @@
|
||||
<string name="devicetype_teclast_h30">Teclast H30</string>
|
||||
|
||||
<string name="choose_auto_export_location">Choose export location</string>
|
||||
<string name="notification_channel_name">Gadgetbridge notifications</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user