aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/org/connectbot/service/ConnectionNotifier.java
blob: a68fffc13c414c23103075d432af2dbab1550bce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * ConnectBot: simple, powerful, open-source SSH client for Android
 * Copyright 2010 Kenny Root, Jeffrey Sharkey
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.connectbot.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.connectbot.ConsoleActivity;
import org.connectbot.HostListActivity;
import org.connectbot.R;
import org.connectbot.bean.HostBean;
import org.connectbot.util.HostDatabase;
import org.connectbot.util.PreferenceConstants;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;

/**
 * @author Kenny Root
 *
 * Based on the concept from jasta's blog post.
 */
public abstract class ConnectionNotifier {
	private static final int ONLINE_NOTIFICATION = 1;
	private static final int ACTIVITY_NOTIFICATION = 2;
	private static final int ONLINE_DISCONNECT_NOTIFICATION = 3;

	public static ConnectionNotifier getInstance() {
		if (PreferenceConstants.PRE_ECLAIR)
			return PreEclair.Holder.sInstance;
		else
			return EclairAndBeyond.Holder.sInstance;
	}

	protected NotificationManager getNotificationManager(Context context) {
		return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	protected NotificationCompat.Builder newNotificationBuilder(Context context) {
		NotificationCompat.Builder builder =
				new NotificationCompat.Builder(context)
				.setSmallIcon(R.drawable.notification_icon)
				.setWhen(System.currentTimeMillis());

		return builder;
	}

	protected Notification newActivityNotification(Context context, HostBean host) {
		NotificationCompat.Builder builder = newNotificationBuilder(context);

		Resources res = context.getResources();

		String contentText = res.getString(
				R.string.notification_text, host.getNickname());

		Intent notificationIntent = new Intent(context, ConsoleActivity.class);
		notificationIntent.setAction("android.intent.action.VIEW");
		notificationIntent.setData(host.getUri());

		PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
				notificationIntent, 0);

		builder.setContentTitle(res.getString(R.string.app_name))
				.setContentText(contentText)
				.setContentIntent(contentIntent);

		builder.setAutoCancel(true);

		int ledOnMS = 300;
		int ledOffMS = 1000;
		builder.setDefaults(Notification.DEFAULT_LIGHTS);
		if (HostDatabase.COLOR_RED.equals(host.getColor()))
			builder.setLights(Color.RED, ledOnMS, ledOffMS);
		else if (HostDatabase.COLOR_GREEN.equals(host.getColor()))
			builder.setLights(Color.GREEN, ledOnMS, ledOffMS);
		else if (HostDatabase.COLOR_BLUE.equals(host.getColor()))
			builder.setLights(Color.BLUE, ledOnMS, ledOffMS);
		else
			builder.setLights(Color.WHITE, ledOnMS, ledOffMS);

		return builder.build();
	}

	protected Notification newRunningNotification(Context context) {
		NotificationCompat.Builder builder = newNotificationBuilder(context);

		builder.setOngoing(true);
		builder.setWhen(0);

		builder.setContentIntent(PendingIntent.getActivity(context,
				ONLINE_NOTIFICATION,
				new Intent(context, ConsoleActivity.class), 0));

		Resources res = context.getResources();
		builder.setContentTitle(res.getString(R.string.app_name));
		builder.setContentText(res.getString(R.string.app_is_running));

		Intent disconnectIntent = new Intent(context, HostListActivity.class);
		disconnectIntent.setAction(HostListActivity.DISCONNECT_ACTION);
		builder.addAction(
				android.R.drawable.ic_menu_close_clear_cancel,
				res.getString(R.string.list_host_disconnect),
				PendingIntent.getActivity(
						context,
						ONLINE_DISCONNECT_NOTIFICATION,
						disconnectIntent,
						0));

		return builder.build();
	}

	public void showActivityNotification(Service context, HostBean host) {
		getNotificationManager(context).notify(ACTIVITY_NOTIFICATION, newActivityNotification(context, host));
	}

	public void hideActivityNotification(Service context) {
		getNotificationManager(context).cancel(ACTIVITY_NOTIFICATION);
	}

	public abstract void showRunningNotification(Service context);
	public abstract void hideRunningNotification(Service context);

	private static class PreEclair extends ConnectionNotifier {
		private static final Class<?>[] setForegroundSignature = new Class[] {boolean.class};
		private Method setForeground = null;

		private static class Holder {
			private static final PreEclair sInstance = new PreEclair();
		}

		public PreEclair() {
			try {
				setForeground = Service.class.getMethod("setForeground", setForegroundSignature);
			} catch (Exception e) {
			}
		}

		@Override
		public void showRunningNotification(Service context) {
			if (setForeground != null) {
				Object[] setForegroundArgs = new Object[1];
				setForegroundArgs[0] = Boolean.TRUE;
				try {
					setForeground.invoke(context, setForegroundArgs);
				} catch (InvocationTargetException e) {
				} catch (IllegalAccessException e) {
				}
				getNotificationManager(context).notify(ONLINE_NOTIFICATION, newRunningNotification(context));
			}
		}

		@Override
		public void hideRunningNotification(Service context) {
			if (setForeground != null) {
				Object[] setForegroundArgs = new Object[1];
				setForegroundArgs[0] = Boolean.FALSE;
				try {
					setForeground.invoke(context, setForegroundArgs);
				} catch (InvocationTargetException e) {
				} catch (IllegalAccessException e) {
				}
				getNotificationManager(context).cancel(ONLINE_NOTIFICATION);
			}
		}
	}

	@TargetApi(5)
	private static class EclairAndBeyond extends ConnectionNotifier {
		private static class Holder {
			private static final EclairAndBeyond sInstance = new EclairAndBeyond();
		}

		@Override
		public void showRunningNotification(Service context) {
			context.startForeground(ONLINE_NOTIFICATION, newRunningNotification(context));
		}

		@Override
		public void hideRunningNotification(Service context) {
			context.stopForeground(true);
		}
	}
}