changeset 490:7545103ec815 stable-1.9.4-2

use foreground service and notification channel on Android 8+
author Carl Byington <carl@five-ten-sg.com>
date Wed, 14 Oct 2020 14:48:55 -0700
parents b257517e9bef
children c89833b1614b
files app/build.gradle app/src/main/AndroidManifest.xml app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java
diffstat 3 files changed, 38 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/app/build.gradle	Wed Oct 14 12:26:53 2020 -0700
+++ b/app/build.gradle	Wed Oct 14 14:48:55 2020 -0700
@@ -60,7 +60,7 @@
 
     defaultConfig {
         applicationId = "com.five_ten_sg.connectbot"
-        minSdkVersion = 26
+        minSdkVersion = 21
         targetSdkVersion = 28
         ndk {
             moduleName = "com_google_ase_Exec"
--- a/app/src/main/AndroidManifest.xml	Wed Oct 14 12:26:53 2020 -0700
+++ b/app/src/main/AndroidManifest.xml	Wed Oct 14 14:48:55 2020 -0700
@@ -17,8 +17,8 @@
 -->
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 	package="com.five_ten_sg.connectbot"
-	android:versionName="1.9.4-1"
-	android:versionCode="1941"
+	android:versionName="1.9.4-2"
+	android:versionCode="1942"
 	android:installLocation="auto">
 
     <!--  permissions must match HostListActivity.java -->
--- a/app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java	Wed Oct 14 12:26:53 2020 -0700
+++ b/app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java	Wed Oct 14 14:48:55 2020 -0700
@@ -31,6 +31,7 @@
 import android.content.res.Resources;
 import android.graphics.Color;
 import android.graphics.BitmapFactory;
+import android.os.Build;
 
 /**
  * @author Kenny Root
@@ -60,25 +61,31 @@
         Intent notificationIntent = new Intent(context, ConsoleActivity.class);
         PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
 
-        String channelID   = "com.five_ten_sg.connectbot.service";
-        String channelName = "background";
-        if (chan == null) {
-            chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE);
-            chan.setLightColor(R.color.colorAccent);
-            chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
-            NotificationManager manager = getNotificationManager(context);
-            assert manager != null;
-            manager.createNotificationChannel(chan);
+        Notification.Builder nb;
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            String channelID   = "com.five_ten_sg.connectbot.service";
+            String channelName = "background";
+            if (chan == null) {
+                chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE);
+                chan.setLightColor(R.color.colorAccent);
+                chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
+                NotificationManager manager = getNotificationManager(context);
+                assert manager != null;
+                manager.createNotificationChannel(chan);
+            }
+            nb = new Notification.Builder(context, channelID);
+        }
+        else {
+            nb = new Notification.Builder(context);
         }
 
-        Notification notification = new Notification.Builder(context)
+        Notification notification = nb
             .setContentTitle(res.getString(R.string.app_name))
             .setContentText(res.getString(R.string.notification_text, host.getNickname()))
             .setContentIntent(contentIntent)
             .setWhen(System.currentTimeMillis())
             .setSmallIcon(R.drawable.notification_icon)
             .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon))
-            .setChannelId(channelID)
             .setLights(rgb, 300, 1000)
             .build();
         notification.flags |= Notification.FLAG_AUTO_CANCEL;
@@ -90,18 +97,25 @@
         Intent notificationIntent = new Intent(context, ConsoleActivity.class);
         PendingIntent contentIntent = PendingIntent.getActivity(context, ONLINE_NOTIFICATION, notificationIntent, 0);
 
-        String channelID   = "com.five_ten_sg.connectbot.service";
-        String channelName = "background";
-        if (chan == null) {
-            chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE);
-            chan.setLightColor(R.color.colorAccent);
-            chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
-            NotificationManager manager = getNotificationManager(context);
-            assert manager != null;
-            manager.createNotificationChannel(chan);
+        Notification.Builder nb;
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            String channelID   = "com.five_ten_sg.connectbot.service";
+            String channelName = "background";
+            if (chan == null) {
+                chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE);
+                chan.setLightColor(R.color.colorAccent);
+                chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
+                NotificationManager manager = getNotificationManager(context);
+                assert manager != null;
+                manager.createNotificationChannel(chan);
+            }
+            nb = new Notification.Builder(context, channelID);
+        }
+        else {
+            nb = new Notification.Builder(context);
         }
 
-        Notification notification = new Notification.Builder(context)
+        Notification notification = nb
             .setContentTitle(res.getString(R.string.app_name))
             .setContentText(res.getString(R.string.app_is_running))
             .setContentIntent(contentIntent)
@@ -109,7 +123,6 @@
             .setOngoing(true)
             .setSmallIcon(R.drawable.notification_icon)
             .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon))
-            .setChannelId(channelID)
             .build();
         notification.flags |= Notification.FLAG_NO_CLEAR;
         return notification;