annotate app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java @ 508:d6c107dedb04

allow multiple screen watch areas
author Carl Byington <carl@five-ten-sg.com>
date Thu, 17 Nov 2022 10:56:48 -0700
parents cbdb219e9ff5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 * ConnectBot: simple, powerful, open-source SSH client for Android
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 * Copyright 2010 Kenny Root, Jeffrey Sharkey
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 *
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 * Licensed under the Apache License, Version 2.0 (the "License");
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 * you may not use this file except in compliance with the License.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 * You may obtain a copy of the License at
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 *
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 * http://www.apache.org/licenses/LICENSE-2.0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 *
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 * Unless required by applicable law or agreed to in writing, software
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 * distributed under the License is distributed on an "AS IS" BASIS,
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 * See the License for the specific language governing permissions and
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 * limitations under the License.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 package com.five_ten_sg.connectbot.service;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 import com.five_ten_sg.connectbot.ConsoleActivity;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 import com.five_ten_sg.connectbot.R;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 import com.five_ten_sg.connectbot.bean.HostBean;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 import com.five_ten_sg.connectbot.util.HostDatabase;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 import android.app.Notification;
487
3afdeb535e9f notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents: 457
diff changeset
25 import android.app.NotificationChannel;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 import android.app.NotificationManager;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 import android.app.PendingIntent;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 import android.app.Service;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 import android.content.Context;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 import android.content.Intent;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 import android.content.res.Resources;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 import android.graphics.Color;
457
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
33 import android.graphics.BitmapFactory;
490
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
34 import android.os.Build;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36 /**
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 * @author Kenny Root
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 *
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39 * Based on the concept from jasta's blog post.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 public class ConnectionNotifier {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42 private static final int ONLINE_NOTIFICATION = 1;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43 private static final int ACTIVITY_NOTIFICATION = 2;
488
869070df0e80 new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents: 487
diff changeset
44 private NotificationChannel chan = null;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 protected NotificationManager getNotificationManager(Context context) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 protected Notification newActivityNotification(Context context, HostBean host) {
457
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
51 int rgb;
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
52 if (HostDatabase.COLOR_RED.equals(host.getColor()))
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
53 rgb = Color.RED;
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
54 else if (HostDatabase.COLOR_GREEN.equals(host.getColor()))
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
55 rgb = Color.GREEN;
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
56 else if (HostDatabase.COLOR_BLUE.equals(host.getColor()))
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
57 rgb = Color.BLUE;
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
58 else
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
59 rgb = Color.WHITE;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 Resources res = context.getResources();
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61 Intent notificationIntent = new Intent(context, ConsoleActivity.class);
457
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
62 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
487
3afdeb535e9f notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents: 457
diff changeset
63
490
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
64 Notification.Builder nb;
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
65 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
66 String channelID = "com.five_ten_sg.connectbot.service";
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
67 String channelName = "background";
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
68 if (chan == null) {
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
69 chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
70 chan.setLightColor(R.color.colorAccent);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
71 chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
72 NotificationManager manager = getNotificationManager(context);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
73 assert manager != null;
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
74 manager.createNotificationChannel(chan);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
75 }
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
76 nb = new Notification.Builder(context, channelID);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
77 }
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
78 else {
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
79 nb = new Notification.Builder(context);
488
869070df0e80 new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents: 487
diff changeset
80 }
487
3afdeb535e9f notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents: 457
diff changeset
81
490
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
82 Notification notification = nb
457
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
83 .setContentTitle(res.getString(R.string.app_name))
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
84 .setContentText(res.getString(R.string.notification_text, host.getNickname()))
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
85 .setContentIntent(contentIntent)
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
86 .setWhen(System.currentTimeMillis())
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
87 .setSmallIcon(R.drawable.notification_icon)
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
88 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon))
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
89 .setLights(rgb, 300, 1000)
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
90 .build();
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
91 notification.flags |= Notification.FLAG_AUTO_CANCEL;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 return notification;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 protected Notification newRunningNotification(Context context) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96 Resources res = context.getResources();
457
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
97 Intent notificationIntent = new Intent(context, ConsoleActivity.class);
503
cbdb219e9ff5 updates for android10+
Carl Byington <carl@five-ten-sg.com>
parents: 490
diff changeset
98 PendingIntent contentIntent = PendingIntent.getActivity(context, ONLINE_NOTIFICATION, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
487
3afdeb535e9f notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents: 457
diff changeset
99
490
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
100 Notification.Builder nb;
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
101 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
102 String channelID = "com.five_ten_sg.connectbot.service";
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
103 String channelName = "background";
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
104 if (chan == null) {
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
105 chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
106 chan.setLightColor(R.color.colorAccent);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
107 chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
108 NotificationManager manager = getNotificationManager(context);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
109 assert manager != null;
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
110 manager.createNotificationChannel(chan);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
111 }
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
112 nb = new Notification.Builder(context, channelID);
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
113 }
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
114 else {
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
115 nb = new Notification.Builder(context);
488
869070df0e80 new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents: 487
diff changeset
116 }
487
3afdeb535e9f notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents: 457
diff changeset
117
490
7545103ec815 use foreground service and notification channel on Android 8+
Carl Byington <carl@five-ten-sg.com>
parents: 488
diff changeset
118 Notification notification = nb
457
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
119 .setContentTitle(res.getString(R.string.app_name))
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
120 .setContentText(res.getString(R.string.app_is_running))
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
121 .setContentIntent(contentIntent)
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
122 .setWhen(0)
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
123 .setOngoing(true)
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
124 .setSmallIcon(R.drawable.notification_icon)
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
125 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon))
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
126 .build();
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
127 notification.flags |= Notification.FLAG_NO_CLEAR;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
128 return notification;
457
105815cce146 minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
129
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
130 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
131
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
132 public void showActivityNotification(Service context, HostBean host) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
133 getNotificationManager(context).notify(ACTIVITY_NOTIFICATION, newActivityNotification(context, host));
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
134 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
135
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
136 public void hideActivityNotification(Service context) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
137 getNotificationManager(context).cancel(ACTIVITY_NOTIFICATION);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
138 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
139
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
140 public void showRunningNotification(Service context) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
141 context.startForeground(ONLINE_NOTIFICATION, newRunningNotification(context));
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
142 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
143
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
144 public void hideRunningNotification(Service context) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
145 context.stopForeground(true);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
146 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
147 }