Mercurial > 510Connectbot
comparison app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java @ 457:105815cce146 stable-1.9.3-3
minimum version android 5, target and compile version api 28
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 29 Jan 2019 11:21:57 -0800 |
parents | d29cce60f393 |
children | 3afdeb535e9f |
comparison
equal
deleted
inserted
replaced
456:b00031b2d6ac | 457:105815cce146 |
---|---|
27 import android.app.Service; | 27 import android.app.Service; |
28 import android.content.Context; | 28 import android.content.Context; |
29 import android.content.Intent; | 29 import android.content.Intent; |
30 import android.content.res.Resources; | 30 import android.content.res.Resources; |
31 import android.graphics.Color; | 31 import android.graphics.Color; |
32 import android.graphics.BitmapFactory; | |
32 | 33 |
33 /** | 34 /** |
34 * @author Kenny Root | 35 * @author Kenny Root |
35 * | 36 * |
36 * Based on the concept from jasta's blog post. | 37 * Based on the concept from jasta's blog post. |
41 | 42 |
42 protected NotificationManager getNotificationManager(Context context) { | 43 protected NotificationManager getNotificationManager(Context context) { |
43 return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); | 44 return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); |
44 } | 45 } |
45 | 46 |
46 protected Notification newNotification(Context context) { | |
47 Notification notification = new Notification(); | |
48 notification.icon = R.drawable.notification_icon; | |
49 notification.when = System.currentTimeMillis(); | |
50 return notification; | |
51 } | |
52 | |
53 protected Notification newActivityNotification(Context context, HostBean host) { | 47 protected Notification newActivityNotification(Context context, HostBean host) { |
54 Notification notification = newNotification(context); | 48 int rgb; |
49 if (HostDatabase.COLOR_RED.equals(host.getColor())) | |
50 rgb = Color.RED; | |
51 else if (HostDatabase.COLOR_GREEN.equals(host.getColor())) | |
52 rgb = Color.GREEN; | |
53 else if (HostDatabase.COLOR_BLUE.equals(host.getColor())) | |
54 rgb = Color.BLUE; | |
55 else | |
56 rgb = Color.WHITE; | |
55 Resources res = context.getResources(); | 57 Resources res = context.getResources(); |
56 String contentText = res.getString( | |
57 R.string.notification_text, host.getNickname()); | |
58 Intent notificationIntent = new Intent(context, ConsoleActivity.class); | 58 Intent notificationIntent = new Intent(context, ConsoleActivity.class); |
59 notificationIntent.setAction(Intent.ACTION_VIEW); | 59 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); |
60 notificationIntent.setData(host.getUri()); | 60 Notification notification = new Notification.Builder(context) |
61 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, | 61 .setContentTitle(res.getString(R.string.app_name)) |
62 notificationIntent, 0); | 62 .setContentText(res.getString(R.string.notification_text, host.getNickname())) |
63 notification.setLatestEventInfo(context, res.getString(R.string.app_name), contentText, contentIntent); | 63 .setContentIntent(contentIntent) |
64 notification.flags = Notification.FLAG_AUTO_CANCEL; | 64 .setWhen(System.currentTimeMillis()) |
65 notification.flags |= Notification.DEFAULT_LIGHTS; | 65 .setSmallIcon(R.drawable.notification_icon) |
66 | 66 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon)) |
67 if (HostDatabase.COLOR_RED.equals(host.getColor())) | 67 .setLights(rgb, 300, 1000) |
68 notification.ledARGB = Color.RED; | 68 .build(); |
69 else if (HostDatabase.COLOR_GREEN.equals(host.getColor())) | 69 notification.flags |= Notification.FLAG_AUTO_CANCEL; |
70 notification.ledARGB = Color.GREEN; | |
71 else if (HostDatabase.COLOR_BLUE.equals(host.getColor())) | |
72 notification.ledARGB = Color.BLUE; | |
73 else | |
74 notification.ledARGB = Color.WHITE; | |
75 | |
76 notification.ledOnMS = 300; | |
77 notification.ledOffMS = 1000; | |
78 notification.flags |= Notification.FLAG_SHOW_LIGHTS; | |
79 return notification; | 70 return notification; |
80 } | 71 } |
81 | 72 |
82 protected Notification newRunningNotification(Context context) { | 73 protected Notification newRunningNotification(Context context) { |
83 Notification notification = newNotification(context); | |
84 notification.flags = Notification.FLAG_ONGOING_EVENT | |
85 | Notification.FLAG_NO_CLEAR; | |
86 notification.when = 0; | |
87 notification.contentIntent = PendingIntent.getActivity(context, | |
88 ONLINE_NOTIFICATION, | |
89 new Intent(context, ConsoleActivity.class), 0); | |
90 Resources res = context.getResources(); | 74 Resources res = context.getResources(); |
91 notification.setLatestEventInfo(context, | 75 Intent notificationIntent = new Intent(context, ConsoleActivity.class); |
92 res.getString(R.string.app_name), | 76 PendingIntent contentIntent = PendingIntent.getActivity(context, ONLINE_NOTIFICATION, notificationIntent, 0); |
93 res.getString(R.string.app_is_running), | 77 Notification notification = new Notification.Builder(context) |
94 notification.contentIntent); | 78 .setContentTitle(res.getString(R.string.app_name)) |
79 .setContentText(res.getString(R.string.app_is_running)) | |
80 .setContentIntent(contentIntent) | |
81 .setWhen(0) | |
82 .setOngoing(true) | |
83 .setSmallIcon(R.drawable.notification_icon) | |
84 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon)) | |
85 .build(); | |
86 notification.flags |= Notification.FLAG_NO_CLEAR; | |
95 return notification; | 87 return notification; |
88 | |
96 } | 89 } |
97 | 90 |
98 public void showActivityNotification(Service context, HostBean host) { | 91 public void showActivityNotification(Service context, HostBean host) { |
99 getNotificationManager(context).notify(ACTIVITY_NOTIFICATION, newActivityNotification(context, host)); | 92 getNotificationManager(context).notify(ACTIVITY_NOTIFICATION, newActivityNotification(context, host)); |
100 } | 93 } |