2 * ConnectBot: simple, powerful, open-source SSH client for Android
3 * Copyright 2010 Kenny Root, Jeffrey Sharkey
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.connectbot.service;
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;
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;
38 * Based on the concept from jasta's blog post.
40 public abstract class ConnectionNotifier {
41 private static final int ONLINE_NOTIFICATION = 1;
42 private static final int ACTIVITY_NOTIFICATION = 2;
44 public static ConnectionNotifier getInstance() {
45 if (PreferenceConstants.PRE_ECLAIR)
46 return PreEclair.Holder.sInstance;
48 return EclairAndBeyond.Holder.sInstance;
51 protected NotificationManager getNotificationManager(Context context) {
52 return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
55 protected Notification newNotification(Context context) {
56 Notification notification = new Notification();
57 notification.icon = R.drawable.notification_icon;
58 notification.when = System.currentTimeMillis();
63 protected Notification newActivityNotification(Context context, HostBean host) {
64 Notification notification = newNotification(context);
66 Resources res = context.getResources();
68 String contentText = res.getString(
69 R.string.notification_text, host.getNickname());
71 Intent notificationIntent = new Intent(context, ConsoleActivity.class);
72 notificationIntent.setAction("android.intent.action.VIEW");
73 notificationIntent.setData(host.getUri());
75 PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
76 notificationIntent, 0);
78 notification.setLatestEventInfo(context, res.getString(R.string.app_name), contentText, contentIntent);
80 notification.flags = Notification.FLAG_AUTO_CANCEL;
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;
90 notification.ledARGB = Color.WHITE;
91 notification.ledOnMS = 300;
92 notification.ledOffMS = 1000;
93 notification.flags |= Notification.FLAG_SHOW_LIGHTS;
98 protected Notification newRunningNotification(Context context) {
99 Notification notification = newNotification(context);
101 notification.flags = Notification.FLAG_ONGOING_EVENT
102 | Notification.FLAG_NO_CLEAR;
103 notification.when = 0;
105 notification.contentIntent = PendingIntent.getActivity(context,
107 new Intent(context, ConsoleActivity.class), 0);
109 Resources res = context.getResources();
111 notification.setLatestEventInfo(context,
112 res.getString(R.string.app_name),
113 res.getString(R.string.app_is_running),
114 notification.contentIntent);
119 public void showActivityNotification(Service context, HostBean host) {
120 getNotificationManager(context).notify(ACTIVITY_NOTIFICATION, newActivityNotification(context, host));
123 public void hideActivityNotification(Service context) {
124 getNotificationManager(context).cancel(ACTIVITY_NOTIFICATION);
127 public abstract void showRunningNotification(Service context);
128 public abstract void hideRunningNotification(Service context);
130 private static class PreEclair extends ConnectionNotifier {
131 private static class Holder {
132 private static final PreEclair sInstance = new PreEclair();
136 public void showRunningNotification(Service context) {
137 context.setForeground(true);
138 getNotificationManager(context).notify(ONLINE_NOTIFICATION, newRunningNotification(context));
142 public void hideRunningNotification(Service context) {
143 context.setForeground(false);
144 getNotificationManager(context).cancel(ONLINE_NOTIFICATION);
148 private static class EclairAndBeyond extends ConnectionNotifier {
149 private static class Holder {
150 private static final EclairAndBeyond sInstance = new EclairAndBeyond();
154 public void showRunningNotification(Service context) {
155 // context.startForeground(ONLINE_NOTIFICATION, newRunningNotification(context));
159 public void hideRunningNotification(Service context) {
160 // context.stopForeground(true);