comparison src/com/five_ten_sg/connectbot/service/ConnectionNotifier.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children cb9e359ea2bd
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
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;
32
33 /**
34 * @author Kenny Root
35 *
36 * Based on the concept from jasta's blog post.
37 */
38 public class ConnectionNotifier {
39 private static final int ONLINE_NOTIFICATION = 1;
40 private static final int ACTIVITY_NOTIFICATION = 2;
41
42 protected NotificationManager getNotificationManager(Context context) {
43 return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
44 }
45
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) {
54 Notification notification = newNotification(context);
55 Resources res = context.getResources();
56 String contentText = res.getString(
57 R.string.notification_text, host.getNickname());
58 Intent notificationIntent = new Intent(context, ConsoleActivity.class);
59 notificationIntent.setAction("android.intent.action.VIEW");
60 notificationIntent.setData(host.getUri());
61 PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
62 notificationIntent, 0);
63 notification.setLatestEventInfo(context, res.getString(R.string.app_name), contentText, contentIntent);
64 notification.flags = Notification.FLAG_AUTO_CANCEL;
65 notification.flags |= Notification.DEFAULT_LIGHTS;
66
67 if (HostDatabase.COLOR_RED.equals(host.getColor()))
68 notification.ledARGB = Color.RED;
69 else if (HostDatabase.COLOR_GREEN.equals(host.getColor()))
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;
80 }
81
82 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();
91 notification.setLatestEventInfo(context,
92 res.getString(R.string.app_name),
93 res.getString(R.string.app_is_running),
94 notification.contentIntent);
95 return notification;
96 }
97
98 public void showActivityNotification(Service context, HostBean host) {
99 getNotificationManager(context).notify(ACTIVITY_NOTIFICATION, newActivityNotification(context, host));
100 }
101
102 public void hideActivityNotification(Service context) {
103 getNotificationManager(context).cancel(ACTIVITY_NOTIFICATION);
104 }
105
106 public void showRunningNotification(Service context) {
107 context.startForeground(ONLINE_NOTIFICATION, newRunningNotification(context));
108 }
109
110 public void hideRunningNotification(Service context) {
111 context.stopForeground(true);
112 }
113 }