Mercurial > 510Connectbot
annotate app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java @ 465:7c8aebcc882a
request permissions if not already granted
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 19 Aug 2019 11:12:40 -0700 |
parents | 105815cce146 |
children | 3afdeb535e9f |
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; | |
25 import android.app.NotificationManager; | |
26 import android.app.PendingIntent; | |
27 import android.app.Service; | |
28 import android.content.Context; | |
29 import android.content.Intent; | |
30 import android.content.res.Resources; | |
31 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
|
32 import android.graphics.BitmapFactory; |
0 | 33 |
34 /** | |
35 * @author Kenny Root | |
36 * | |
37 * Based on the concept from jasta's blog post. | |
38 */ | |
39 public class ConnectionNotifier { | |
40 private static final int ONLINE_NOTIFICATION = 1; | |
41 private static final int ACTIVITY_NOTIFICATION = 2; | |
42 | |
43 protected NotificationManager getNotificationManager(Context context) { | |
44 return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); | |
45 } | |
46 | |
47 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
|
48 int rgb; |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 else |
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.WHITE; |
0 | 57 Resources res = context.getResources(); |
58 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
|
59 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
60 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
|
61 .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
|
62 .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
|
63 .setContentIntent(contentIntent) |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
64 .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
|
65 .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
|
66 .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
|
67 .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
|
68 .build(); |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
69 notification.flags |= Notification.FLAG_AUTO_CANCEL; |
0 | 70 return notification; |
71 } | |
72 | |
73 protected Notification newRunningNotification(Context context) { | |
74 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
|
75 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
|
76 PendingIntent contentIntent = PendingIntent.getActivity(context, ONLINE_NOTIFICATION, notificationIntent, 0); |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
77 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
|
78 .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
|
79 .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
|
80 .setContentIntent(contentIntent) |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
81 .setWhen(0) |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
82 .setOngoing(true) |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
83 .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
|
84 .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
|
85 .build(); |
105815cce146
minimum version android 5, target and compile version api 28
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
86 notification.flags |= Notification.FLAG_NO_CLEAR; |
0 | 87 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
|
88 |
0 | 89 } |
90 | |
91 public void showActivityNotification(Service context, HostBean host) { | |
92 getNotificationManager(context).notify(ACTIVITY_NOTIFICATION, newActivityNotification(context, host)); | |
93 } | |
94 | |
95 public void hideActivityNotification(Service context) { | |
96 getNotificationManager(context).cancel(ACTIVITY_NOTIFICATION); | |
97 } | |
98 | |
99 public void showRunningNotification(Service context) { | |
100 context.startForeground(ONLINE_NOTIFICATION, newRunningNotification(context)); | |
101 } | |
102 | |
103 public void hideRunningNotification(Service context) { | |
104 context.stopForeground(true); | |
105 } | |
106 } |