OSDN Git Service

Update to r505
[android-x86/packages-apps-ConnectBot.git] / src / org / connectbot / service / ConnectionNotifier.java
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 org.connectbot.service;
19
20 import org.connectbot.ConsoleActivity;
21 import org.connectbot.R;
22 import org.connectbot.bean.HostBean;
23 import org.connectbot.util.HostDatabase;
24 import org.connectbot.util.PreferenceConstants;
25
26 import android.app.Notification;
27 import android.app.NotificationManager;
28 import android.app.PendingIntent;
29 import android.app.Service;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.content.res.Resources;
33 import android.graphics.Color;
34
35 /**
36  * @author Kenny Root
37  *
38  * Based on the concept from jasta's blog post.
39  */
40 public abstract class ConnectionNotifier {
41         private static final int ONLINE_NOTIFICATION = 1;
42         private static final int ACTIVITY_NOTIFICATION = 2;
43
44          public static ConnectionNotifier getInstance() {
45                 if (PreferenceConstants.PRE_ECLAIR)
46                         return PreEclair.Holder.sInstance;
47                 else
48                         return EclairAndBeyond.Holder.sInstance;
49         }
50
51         protected NotificationManager getNotificationManager(Context context) {
52                 return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
53         }
54
55         protected Notification newNotification(Context context) {
56                 Notification notification = new Notification();
57                 notification.icon = R.drawable.notification_icon;
58                 notification.when = System.currentTimeMillis();
59
60                 return notification;
61         }
62
63         protected Notification newActivityNotification(Context context, HostBean host) {
64                 Notification notification = newNotification(context);
65
66                 Resources res = context.getResources();
67
68                 String contentText = res.getString(
69                                 R.string.notification_text, host.getNickname());
70
71                 Intent notificationIntent = new Intent(context, ConsoleActivity.class);
72                 notificationIntent.setAction("android.intent.action.VIEW");
73                 notificationIntent.setData(host.getUri());
74
75                 PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
76                                 notificationIntent, 0);
77
78                 notification.setLatestEventInfo(context, res.getString(R.string.app_name), contentText, contentIntent);
79
80                 notification.flags = Notification.FLAG_AUTO_CANCEL;
81
82                 notification.flags |= Notification.DEFAULT_LIGHTS;
83                 if (HostDatabase.COLOR_RED.equals(host.getColor()))
84                         notification.ledARGB = Color.RED;
85                 else if (HostDatabase.COLOR_GREEN.equals(host.getColor()))
86                         notification.ledARGB = Color.GREEN;
87                 else if (HostDatabase.COLOR_BLUE.equals(host.getColor()))
88                         notification.ledARGB = Color.BLUE;
89                 else
90                         notification.ledARGB = Color.WHITE;
91                 notification.ledOnMS = 300;
92                 notification.ledOffMS = 1000;
93                 notification.flags |= Notification.FLAG_SHOW_LIGHTS;
94
95                 return notification;
96         }
97
98         protected Notification newRunningNotification(Context context) {
99                 Notification notification = newNotification(context);
100
101                 notification.flags = Notification.FLAG_ONGOING_EVENT
102                                 | Notification.FLAG_NO_CLEAR;
103                 notification.when = 0;
104
105                 notification.contentIntent = PendingIntent.getActivity(context,
106                                 ONLINE_NOTIFICATION,
107                                 new Intent(context, ConsoleActivity.class), 0);
108
109                 Resources res = context.getResources();
110
111                 notification.setLatestEventInfo(context,
112                                 res.getString(R.string.app_name),
113                                 res.getString(R.string.app_is_running),
114                                 notification.contentIntent);
115
116                 return notification;
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 abstract void showRunningNotification(Service context);
128         public abstract void hideRunningNotification(Service context);
129
130         private static class PreEclair extends ConnectionNotifier {
131                 private static class Holder {
132                         private static final PreEclair sInstance = new PreEclair();
133                 }
134
135                 @Override
136                 public void showRunningNotification(Service context) {
137                         context.setForeground(true);
138                         getNotificationManager(context).notify(ONLINE_NOTIFICATION, newRunningNotification(context));
139                 }
140
141                 @Override
142                 public void hideRunningNotification(Service context) {
143                         context.setForeground(false);
144                         getNotificationManager(context).cancel(ONLINE_NOTIFICATION);
145                 }
146         }
147
148         private static class EclairAndBeyond extends ConnectionNotifier {
149                 private static class Holder {
150                         private static final EclairAndBeyond sInstance = new EclairAndBeyond();
151                 }
152
153                 @Override
154                 public void showRunningNotification(Service context) {
155 //                      context.startForeground(ONLINE_NOTIFICATION, newRunningNotification(context));
156                 }
157
158                 @Override
159                 public void hideRunningNotification(Service context) {
160 //                      context.stopForeground(true);
161                 }
162
163         }
164 }