Mercurial > 510Connectbot
annotate app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java @ 489:b257517e9bef
Added tag stable-1.9.4-1 for changeset 869070df0e80
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 14 Oct 2020 12:26:53 -0700 |
parents | 869070df0e80 |
children | 7545103ec815 |
rev | line source |
---|---|
0 | 1 /* |
2 * ConnectBot: simple, powerful, open-source SSH client for Android | |
3 * Copyright 2010 Kenny Root, Jeffrey Sharkey | |
4 * | |
5 * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 * you may not use this file except in compliance with the License. | |
7 * You may obtain a copy of the License at | |
8 * | |
9 * http://www.apache.org/licenses/LICENSE-2.0 | |
10 * | |
11 * Unless required by applicable law or agreed to in writing, software | |
12 * distributed under the License is distributed on an "AS IS" BASIS, | |
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 * See the License for the specific language governing permissions and | |
15 * limitations under the License. | |
16 */ | |
17 | |
18 package com.five_ten_sg.connectbot.service; | |
19 | |
20 import com.five_ten_sg.connectbot.ConsoleActivity; | |
21 import com.five_ten_sg.connectbot.R; | |
22 import com.five_ten_sg.connectbot.bean.HostBean; | |
23 import com.five_ten_sg.connectbot.util.HostDatabase; | |
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 | 26 import android.app.NotificationManager; |
27 import android.app.PendingIntent; | |
28 import android.app.Service; | |
29 import android.content.Context; | |
30 import android.content.Intent; | |
31 import android.content.res.Resources; | |
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; |
0 | 34 |
35 /** | |
36 * @author Kenny Root | |
37 * | |
38 * Based on the concept from jasta's blog post. | |
39 */ | |
40 public class ConnectionNotifier { | |
41 private static final int ONLINE_NOTIFICATION = 1; | |
42 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
|
43 private NotificationChannel chan = null; |
0 | 44 |
45 protected NotificationManager getNotificationManager(Context context) { | |
46 return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); | |
47 } | |
48 | |
49 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
|
50 int rgb; |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 else |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
58 rgb = Color.WHITE; |
0 | 59 Resources res = context.getResources(); |
60 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
|
61 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
|
62 |
3afdeb535e9f
notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents:
457
diff
changeset
|
63 String channelID = "com.five_ten_sg.connectbot.service"; |
3afdeb535e9f
notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents:
457
diff
changeset
|
64 String channelName = "background"; |
488
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
65 if (chan == null) { |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
66 chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE); |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
67 chan.setLightColor(R.color.colorAccent); |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
68 chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
69 NotificationManager manager = getNotificationManager(context); |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
70 assert manager != null; |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
71 manager.createNotificationChannel(chan); |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
72 } |
487
3afdeb535e9f
notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents:
457
diff
changeset
|
73 |
457
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
74 Notification notification = new Notification.Builder(context) |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
75 .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
|
76 .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
|
77 .setContentIntent(contentIntent) |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
78 .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
|
79 .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
|
80 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon)) |
487
3afdeb535e9f
notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents:
457
diff
changeset
|
81 .setChannelId(channelID) |
457
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
82 .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
|
83 .build(); |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
84 notification.flags |= Notification.FLAG_AUTO_CANCEL; |
0 | 85 return notification; |
86 } | |
87 | |
88 protected Notification newRunningNotification(Context context) { | |
89 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
|
90 Intent notificationIntent = new Intent(context, ConsoleActivity.class); |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
91 PendingIntent contentIntent = PendingIntent.getActivity(context, ONLINE_NOTIFICATION, notificationIntent, 0); |
487
3afdeb535e9f
notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents:
457
diff
changeset
|
92 |
3afdeb535e9f
notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents:
457
diff
changeset
|
93 String channelID = "com.five_ten_sg.connectbot.service"; |
3afdeb535e9f
notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents:
457
diff
changeset
|
94 String channelName = "background"; |
488
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
95 if (chan == null) { |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
96 chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE); |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
97 chan.setLightColor(R.color.colorAccent); |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
98 chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
99 NotificationManager manager = getNotificationManager(context); |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
100 assert manager != null; |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
101 manager.createNotificationChannel(chan); |
869070df0e80
new notification channel needs android 8
Carl Byington <carl@five-ten-sg.com>
parents:
487
diff
changeset
|
102 } |
487
3afdeb535e9f
notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents:
457
diff
changeset
|
103 |
457
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
104 Notification notification = new Notification.Builder(context) |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
105 .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
|
106 .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
|
107 .setContentIntent(contentIntent) |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
108 .setWhen(0) |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
109 .setOngoing(true) |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
110 .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
|
111 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon)) |
487
3afdeb535e9f
notification needs a notification channel
Carl Byington <carl@five-ten-sg.com>
parents:
457
diff
changeset
|
112 .setChannelId(channelID) |
457
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
113 .build(); |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
114 notification.flags |= Notification.FLAG_NO_CLEAR; |
0 | 115 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
|
116 |
0 | 117 } |
118 | |
119 public void showActivityNotification(Service context, HostBean host) { | |
120 getNotificationManager(context).notify(ACTIVITY_NOTIFICATION, newActivityNotification(context, host)); | |
121 } | |
122 | |
123 public void hideActivityNotification(Service context) { | |
124 getNotificationManager(context).cancel(ACTIVITY_NOTIFICATION); | |
125 } | |
126 | |
127 public void showRunningNotification(Service context) { | |
128 context.startForeground(ONLINE_NOTIFICATION, newRunningNotification(context)); | |
129 } | |
130 | |
131 public void hideRunningNotification(Service context) { | |
132 context.stopForeground(true); | |
133 } | |
134 } |