Skip to content

Commit c62b680

Browse files
committed
New error notification channel for critical errors such as lack of permissions or unable to write file.
Issue #1053 Issue #1138
1 parent 943a894 commit c62b680

File tree

5 files changed

+88
-28
lines changed

5 files changed

+88
-28
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.

gpslogger/src/main/java/com/mendhak/gpslogger/GpsLoggingService.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
@SuppressLint("MissingPermission")
5656
public class GpsLoggingService extends Service {
5757
private static NotificationManager notificationManager;
58-
private static int NOTIFICATION_ID = 8675309;
5958
private final IBinder binder = new GpsLoggingBinder();
6059
AlarmManager nextPointAlarmManager;
6160
private NotificationCompat.Builder nfc;
@@ -89,10 +88,10 @@ public IBinder onBind(Intent arg0) {
8988
public void onCreate() {
9089
try {
9190
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
92-
startForeground(NOTIFICATION_ID, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
91+
startForeground(NotificationChannelNames.GPSLOGGER_DEFAULT_NOTIFICATION_ID, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
9392
}
9493
else {
95-
startForeground(NOTIFICATION_ID, getNotification());
94+
startForeground(NotificationChannelNames.GPSLOGGER_DEFAULT_NOTIFICATION_ID, getNotification());
9695
}
9796
} catch (Exception ex) {
9897
LOG.error("Could not start GPSLoggingService in foreground. ", ex);
@@ -126,10 +125,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {
126125
super.onStartCommand(intent, flags, startId);
127126
try {
128127
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
129-
startForeground(NOTIFICATION_ID, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
128+
startForeground(NotificationChannelNames.GPSLOGGER_DEFAULT_NOTIFICATION_ID, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
130129
}
131130
else {
132-
startForeground(NOTIFICATION_ID, getNotification());
131+
startForeground(NotificationChannelNames.GPSLOGGER_DEFAULT_NOTIFICATION_ID, getNotification());
133132
}
134133
} catch (Exception ex) {
135134
LOG.error("Could not start GPSLoggingService in foreground. ", ex);
@@ -179,8 +178,7 @@ private void handleIntent(Intent intent) {
179178

180179
if(!Systems.locationPermissionsGranted(this)){
181180
LOG.error("User has not granted permission to access location services. Will not continue!");
182-
stopLogging();
183-
stopSelf();
181+
Systems.showErrorNotification(this, getString(R.string.gpslogger_permissions_permanently_denied));
184182
return;
185183
}
186184

@@ -398,10 +396,10 @@ protected void startLogging() {
398396

399397
try {
400398
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
401-
startForeground(NOTIFICATION_ID, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
399+
startForeground(NotificationChannelNames.GPSLOGGER_DEFAULT_NOTIFICATION_ID, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
402400
}
403401
else {
404-
startForeground(NOTIFICATION_ID, getNotification());
402+
startForeground(NotificationChannelNames.GPSLOGGER_DEFAULT_NOTIFICATION_ID, getNotification());
405403
}
406404
} catch (Exception ex) {
407405
LOG.error("Could not start GPSLoggingService in foreground. ", ex);
@@ -529,21 +527,7 @@ private Notification getNotification() {
529527

530528
if (nfc == null) {
531529

532-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
533-
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
534-
535-
NotificationChannel channel = new NotificationChannel("gpslogger", getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT);
536-
channel.enableLights(false);
537-
channel.enableVibration(false);
538-
channel.setSound(null,null);
539-
channel.setLockscreenVisibility(preferenceHelper.shouldHideNotificationFromLockScreen() ? Notification.VISIBILITY_PRIVATE : Notification.VISIBILITY_PUBLIC);
540-
541-
channel.setShowBadge(true);
542-
manager.createNotificationChannel(channel);
543-
544-
}
545-
546-
nfc = new NotificationCompat.Builder(getApplicationContext(),"gpslogger")
530+
nfc = new NotificationCompat.Builder(getApplicationContext(), NotificationChannelNames.GPSLOGGER_DEFAULT)
547531
.setSmallIcon(R.drawable.notification)
548532
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.gpsloggericon3))
549533
.setPriority( preferenceHelper.shouldHideNotificationFromStatusBar() ? NotificationCompat.PRIORITY_MIN : NotificationCompat.PRIORITY_LOW)
@@ -566,22 +550,20 @@ private Notification getNotification() {
566550
}
567551
}
568552

569-
570-
571553
nfc.setContentTitle(contentTitle);
572554
nfc.setContentText(contentText);
573555
nfc.setStyle(new NotificationCompat.BigTextStyle().bigText(contentText).setBigContentTitle(contentTitle));
574556
nfc.setWhen(notificationTime);
575557

576558
//notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
577-
//notificationManager.notify(NOTIFICATION_ID, nfc.build());
559+
//notificationManager.notify(NotificationChannelNames.GPSLOGGER_DEFAULT_ID, nfc.build());
578560
return nfc.build();
579561
}
580562

581563
private void showNotification(){
582564
Notification notif = getNotification();
583565
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
584-
notificationManager.notify(NOTIFICATION_ID, notif);
566+
notificationManager.notify(NotificationChannelNames.GPSLOGGER_DEFAULT_NOTIFICATION_ID, notif);
585567
}
586568

587569
@SuppressWarnings("ResourceType")

gpslogger/src/main/java/com/mendhak/gpslogger/common/AppSettings.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
package com.mendhak.gpslogger.common;
2121

2222
import android.app.Application;
23+
import android.app.Notification;
24+
import android.app.NotificationChannel;
25+
import android.app.NotificationManager;
26+
import android.content.Context;
2327

2428

2529
import com.mendhak.gpslogger.BuildConfig;
30+
import com.mendhak.gpslogger.R;
2631
import com.mendhak.gpslogger.common.slf4j.Logs;
2732
import de.greenrobot.event.EventBus;
2833
import org.slf4j.Logger;
@@ -36,6 +41,7 @@ public class AppSettings extends Application {
3641

3742
@Override
3843
public void onCreate() {
44+
3945
Systems.setAppTheme(PreferenceHelper.getInstance().getAppThemeSetting());
4046
super.onCreate();
4147

@@ -48,7 +54,30 @@ public void onCreate() {
4854
EventBus.builder().logNoSubscriberMessages(false).sendNoSubscriberEvent(false).installDefaultEventBus();
4955
LOG.debug("EventBus configured");
5056

57+
createNotificationChannels();
58+
}
59+
60+
private void createNotificationChannels() {
61+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
62+
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
63+
64+
NotificationChannel channel = new NotificationChannel(NotificationChannelNames.GPSLOGGER_DEFAULT, getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT);
65+
channel.enableLights(false);
66+
channel.enableVibration(false);
67+
channel.setSound(null,null);
68+
channel.setLockscreenVisibility(PreferenceHelper.getInstance().shouldHideNotificationFromLockScreen() ? Notification.VISIBILITY_PRIVATE : Notification.VISIBILITY_PUBLIC);
69+
70+
channel.setShowBadge(true);
71+
manager.createNotificationChannel(channel);
72+
73+
NotificationChannel channelErrors = new NotificationChannel(NotificationChannelNames.GPSLOGGER_ERRORS, getString(R.string.error), NotificationManager.IMPORTANCE_HIGH);
74+
channelErrors.enableLights(true);
75+
channelErrors.enableVibration(true);
76+
channelErrors.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
77+
channelErrors.setShowBadge(true);
78+
manager.createNotificationChannel(channelErrors);
5179

80+
}
5281
}
5382

5483

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.mendhak.gpslogger.common;
2+
3+
public class NotificationChannelNames {
4+
public static final String GPSLOGGER_DEFAULT = "gpslogger";
5+
public static final int GPSLOGGER_DEFAULT_NOTIFICATION_ID = 8675309;
6+
public static final String GPSLOGGER_ERRORS = "gpslogger_errors";
7+
public static final int GPSLOGGER_ERRORS_NOTIFICATION_ID = 32202;
8+
}

gpslogger/src/main/java/com/mendhak/gpslogger/common/Systems.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import android.Manifest;
2424
import android.annotation.TargetApi;
25+
import android.app.NotificationManager;
26+
import android.app.PendingIntent;
2527
import android.content.Context;
2628
import android.content.Intent;
2729
import android.content.IntentFilter;
@@ -30,14 +32,19 @@
3032
import android.content.pm.Signature;
3133
import android.content.res.Configuration;
3234
import android.content.res.Resources;
35+
import android.graphics.BitmapFactory;
3336
import android.net.ConnectivityManager;
3437
import android.net.NetworkInfo;
3538
import android.os.BatteryManager;
3639
import android.os.Build;
3740
import android.os.PowerManager;
3841
import android.provider.Settings;
42+
import android.text.Html;
43+
3944
import androidx.appcompat.app.AppCompatDelegate;
45+
import androidx.core.app.NotificationCompat;
4046
import androidx.core.content.ContextCompat;
47+
import androidx.core.text.HtmlCompat;
4148
import androidx.fragment.app.FragmentActivity;
4249
import androidx.work.BackoffPolicy;
4350
import androidx.work.Constraints;
@@ -47,6 +54,8 @@
4754
import androidx.work.OneTimeWorkRequest;
4855
import androidx.work.WorkManager;
4956

57+
import com.mendhak.gpslogger.GpsMainActivity;
58+
import com.mendhak.gpslogger.R;
5059
import com.mendhak.gpslogger.common.slf4j.Logs;
5160

5261
import org.slf4j.Logger;
@@ -281,4 +290,35 @@ public static void sendFileUploadedBroadcast(Context context, String[] filePaths
281290
sendIntent.putExtra("sendertype", senderType);
282291
context.sendBroadcast(sendIntent);
283292
}
293+
294+
/**
295+
* Show an error notification with a warning emoji ⚠️, this is only used for important errors worth notifying the user for.
296+
* Such as location permissions missing, unable to write to storage.
297+
* @param context The application context, so that the notification service can be accessed.
298+
* @param message A single line message to show in the notification.
299+
*/
300+
public static void showErrorNotification(Context context, String message){
301+
LOG.debug("Showing fatal notification");
302+
303+
Intent contentIntent = new Intent(context, GpsMainActivity.class);
304+
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
305+
306+
NotificationCompat.Builder nfc = new NotificationCompat.Builder(context.getApplicationContext(), NotificationChannelNames.GPSLOGGER_ERRORS)
307+
.setSmallIcon(android.R.drawable.stat_notify_error)
308+
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), android.R.drawable.stat_notify_error))
309+
.setPriority(NotificationCompat.PRIORITY_HIGH)
310+
.setCategory(NotificationCompat.CATEGORY_ERROR)
311+
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
312+
.setContentTitle(context.getString(R.string.error))
313+
.setContentText(HtmlCompat.fromHtml(message, HtmlCompat.FROM_HTML_MODE_COMPACT).toString())
314+
.setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(message).toString()).setBigContentTitle(context.getString(R.string.error)))
315+
.setOngoing(false)
316+
.setOnlyAlertOnce(true)
317+
.setContentIntent(pendingIntent);
318+
319+
320+
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
321+
notificationManager.notify(NotificationChannelNames.GPSLOGGER_ERRORS_NOTIFICATION_ID, nfc.build());
322+
323+
}
284324
}

0 commit comments

Comments
 (0)