Loading app/src/main/java/net/fabiszewski/ulogger/NotificationHelper.java +47 −25 Original line number Diff line number Diff line Loading @@ -12,56 +12,76 @@ package net.fabiszewski.ulogger; import static android.app.PendingIntent.FLAG_IMMUTABLE; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import android.util.Log; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.core.app.TaskStackBuilder; class NotificationHelper { private static final String TAG = NotificationHelper.class.getSimpleName(); private final int NOTIFICATION_ID = 1526756640; private final NotificationManager notificationManager; private static final int NOTIFICATION_LOGGER_ID = 1; private static final int NOTIFICATION_WEB_ID = 2; private final int notificationId; private final NotificationManagerCompat notificationManager; private final Context context; /** * Constructor * On APIs below 26 we must use separate ID for web service notifications, * because when web service terminates it also cancels notification for logger service * @param ctx Context * @param isWebService True for WebSyncService */ NotificationHelper(Context ctx) { NotificationHelper(Context ctx, boolean isWebService) { context = ctx; notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (notificationManager != null) { notificationManager = NotificationManagerCompat.from(context.getApplicationContext()); notificationManager.cancelAll(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O && isWebService) { notificationId = NOTIFICATION_WEB_ID; } else { notificationId = NOTIFICATION_LOGGER_ID; } } /** * Constructor * @param ctx Context */ NotificationHelper(Context ctx) { this(ctx, false); } /** * Show notification * @param mId Notification Id */ Notification showNotification() { if (Logger.DEBUG) { Log.d(TAG, "[showNotification " + NOTIFICATION_ID + "]"); } if (Logger.DEBUG) { Log.d(TAG, "[showNotification " + notificationId + "]"); } int priority = NotificationCompat.PRIORITY_LOW; String notificationText = String.format(context.getString(R.string.is_running), context.getString(R.string.app_name)); final String channelId = String.valueOf(NOTIFICATION_ID); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel(channelId); if (notificationId != NOTIFICATION_LOGGER_ID) { priority = NotificationCompat.PRIORITY_MIN; notificationText = String.format(context.getString(R.string.is_uploading), context.getString(R.string.app_name)); } NotificationCompat.Builder mBuilder = final String channelId = String.valueOf(notificationId); createNotificationChannel(channelId); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId) .setSmallIcon(R.drawable.ic_stat_notify_24dp) .setContentTitle(context.getString(R.string.app_name)) .setPriority(NotificationCompat.PRIORITY_LOW) .setPriority(priority) .setCategory(NotificationCompat.CATEGORY_SERVICE) .setOnlyAlertOnce(true) .setContentText(String.format(context.getString(R.string.is_running), context.getString(R.string.app_name))); .setContentText(notificationText); Intent resultIntent = new Intent(context, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); Loading @@ -72,19 +92,21 @@ class NotificationHelper { flags |= FLAG_IMMUTABLE; } PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, flags); mBuilder.setContentIntent(resultPendingIntent); Notification mNotification = mBuilder.build(); notificationManager.notify(NOTIFICATION_ID, mNotification); return mNotification; builder.setContentIntent(resultPendingIntent); Notification notification = builder.build(); notificationManager.notify(notificationId, notification); return notification; } /** * Create notification channel * @param channelId Channel Id */ @RequiresApi(Build.VERSION_CODES.O) private void createNotificationChannel(String channelId) { NotificationChannel channel = new NotificationChannel(channelId, context.getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW); final int importance = notificationId != NOTIFICATION_LOGGER_ID ? NotificationManagerCompat.IMPORTANCE_NONE : NotificationManagerCompat.IMPORTANCE_LOW; NotificationChannelCompat channel = new NotificationChannelCompat.Builder(channelId, importance) .setName(context.getString(R.string.app_name)) .build(); notificationManager.createNotificationChannel(channel); } Loading @@ -92,7 +114,7 @@ class NotificationHelper { * Cancel notification */ void cancelNotification() { notificationManager.cancel(NOTIFICATION_ID); notificationManager.cancel(notificationId); } /** Loading @@ -100,6 +122,6 @@ class NotificationHelper { * @return Notification ID */ int getId() { return NOTIFICATION_ID; return notificationId; } } app/src/main/java/net/fabiszewski/ulogger/WebSyncService.java +1 −1 Original line number Diff line number Diff line Loading @@ -72,7 +72,7 @@ public class WebSyncService extends Service { if (Logger.DEBUG) { Log.d(TAG, "[websync create]"); } web = new WebHelper(this); notificationHelper = new NotificationHelper(this); notificationHelper = new NotificationHelper(this, true); thread = new HandlerThread("WebSyncThread", THREAD_PRIORITY_BACKGROUND); thread.start(); Loading app/src/main/res/values/strings.xml +1 −0 Original line number Diff line number Diff line Loading @@ -13,6 +13,7 @@ <string name="button_newtrack">New track</string> <string name="button_upload">Upload</string> <string name="is_running" comment="substituted app_name">%s is running</string> <string name="is_uploading" comment="substituted app_name">%s is uploading positions</string> <string name="title_newtrack">New track</string> <string name="label_track">Current track</string> <string name="label_newtrack_name">New track name</string> Loading Loading
app/src/main/java/net/fabiszewski/ulogger/NotificationHelper.java +47 −25 Original line number Diff line number Diff line Loading @@ -12,56 +12,76 @@ package net.fabiszewski.ulogger; import static android.app.PendingIntent.FLAG_IMMUTABLE; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import android.util.Log; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.core.app.TaskStackBuilder; class NotificationHelper { private static final String TAG = NotificationHelper.class.getSimpleName(); private final int NOTIFICATION_ID = 1526756640; private final NotificationManager notificationManager; private static final int NOTIFICATION_LOGGER_ID = 1; private static final int NOTIFICATION_WEB_ID = 2; private final int notificationId; private final NotificationManagerCompat notificationManager; private final Context context; /** * Constructor * On APIs below 26 we must use separate ID for web service notifications, * because when web service terminates it also cancels notification for logger service * @param ctx Context * @param isWebService True for WebSyncService */ NotificationHelper(Context ctx) { NotificationHelper(Context ctx, boolean isWebService) { context = ctx; notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (notificationManager != null) { notificationManager = NotificationManagerCompat.from(context.getApplicationContext()); notificationManager.cancelAll(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O && isWebService) { notificationId = NOTIFICATION_WEB_ID; } else { notificationId = NOTIFICATION_LOGGER_ID; } } /** * Constructor * @param ctx Context */ NotificationHelper(Context ctx) { this(ctx, false); } /** * Show notification * @param mId Notification Id */ Notification showNotification() { if (Logger.DEBUG) { Log.d(TAG, "[showNotification " + NOTIFICATION_ID + "]"); } if (Logger.DEBUG) { Log.d(TAG, "[showNotification " + notificationId + "]"); } int priority = NotificationCompat.PRIORITY_LOW; String notificationText = String.format(context.getString(R.string.is_running), context.getString(R.string.app_name)); final String channelId = String.valueOf(NOTIFICATION_ID); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel(channelId); if (notificationId != NOTIFICATION_LOGGER_ID) { priority = NotificationCompat.PRIORITY_MIN; notificationText = String.format(context.getString(R.string.is_uploading), context.getString(R.string.app_name)); } NotificationCompat.Builder mBuilder = final String channelId = String.valueOf(notificationId); createNotificationChannel(channelId); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId) .setSmallIcon(R.drawable.ic_stat_notify_24dp) .setContentTitle(context.getString(R.string.app_name)) .setPriority(NotificationCompat.PRIORITY_LOW) .setPriority(priority) .setCategory(NotificationCompat.CATEGORY_SERVICE) .setOnlyAlertOnce(true) .setContentText(String.format(context.getString(R.string.is_running), context.getString(R.string.app_name))); .setContentText(notificationText); Intent resultIntent = new Intent(context, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); Loading @@ -72,19 +92,21 @@ class NotificationHelper { flags |= FLAG_IMMUTABLE; } PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, flags); mBuilder.setContentIntent(resultPendingIntent); Notification mNotification = mBuilder.build(); notificationManager.notify(NOTIFICATION_ID, mNotification); return mNotification; builder.setContentIntent(resultPendingIntent); Notification notification = builder.build(); notificationManager.notify(notificationId, notification); return notification; } /** * Create notification channel * @param channelId Channel Id */ @RequiresApi(Build.VERSION_CODES.O) private void createNotificationChannel(String channelId) { NotificationChannel channel = new NotificationChannel(channelId, context.getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW); final int importance = notificationId != NOTIFICATION_LOGGER_ID ? NotificationManagerCompat.IMPORTANCE_NONE : NotificationManagerCompat.IMPORTANCE_LOW; NotificationChannelCompat channel = new NotificationChannelCompat.Builder(channelId, importance) .setName(context.getString(R.string.app_name)) .build(); notificationManager.createNotificationChannel(channel); } Loading @@ -92,7 +114,7 @@ class NotificationHelper { * Cancel notification */ void cancelNotification() { notificationManager.cancel(NOTIFICATION_ID); notificationManager.cancel(notificationId); } /** Loading @@ -100,6 +122,6 @@ class NotificationHelper { * @return Notification ID */ int getId() { return NOTIFICATION_ID; return notificationId; } }
app/src/main/java/net/fabiszewski/ulogger/WebSyncService.java +1 −1 Original line number Diff line number Diff line Loading @@ -72,7 +72,7 @@ public class WebSyncService extends Service { if (Logger.DEBUG) { Log.d(TAG, "[websync create]"); } web = new WebHelper(this); notificationHelper = new NotificationHelper(this); notificationHelper = new NotificationHelper(this, true); thread = new HandlerThread("WebSyncThread", THREAD_PRIORITY_BACKGROUND); thread.start(); Loading
app/src/main/res/values/strings.xml +1 −0 Original line number Diff line number Diff line Loading @@ -13,6 +13,7 @@ <string name="button_newtrack">New track</string> <string name="button_upload">Upload</string> <string name="is_running" comment="substituted app_name">%s is running</string> <string name="is_uploading" comment="substituted app_name">%s is uploading positions</string> <string name="title_newtrack">New track</string> <string name="label_track">Current track</string> <string name="label_newtrack_name">New track name</string> Loading