aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/org/connectbot/service/ConnectionNotifier.java
diff options
context:
space:
mode:
authorKenny Root <kenny@the-b.org>2014-10-01 23:04:51 +0100
committerKenny Root <kenny@the-b.org>2014-10-01 12:48:19 +0100
commit49b779dcaf03e3598d2709b321e20ea029b25163 (patch)
tree05af547b1f1433d7dd6f7373d0b25a455e053a03 /app/src/main/java/org/connectbot/service/ConnectionNotifier.java
parentd64786d9197090c74072b648e487e3d34817bb57 (diff)
downloadconnectbot-49b779dcaf03e3598d2709b321e20ea029b25163.tar.gz
connectbot-49b779dcaf03e3598d2709b321e20ea029b25163.tar.bz2
connectbot-49b779dcaf03e3598d2709b321e20ea029b25163.zip
Convert to gradle build system
Diffstat (limited to 'app/src/main/java/org/connectbot/service/ConnectionNotifier.java')
-rw-r--r--app/src/main/java/org/connectbot/service/ConnectionNotifier.java192
1 files changed, 192 insertions, 0 deletions
diff --git a/app/src/main/java/org/connectbot/service/ConnectionNotifier.java b/app/src/main/java/org/connectbot/service/ConnectionNotifier.java
new file mode 100644
index 0000000..d276761
--- /dev/null
+++ b/app/src/main/java/org/connectbot/service/ConnectionNotifier.java
@@ -0,0 +1,192 @@
+/*
+ * 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.R;
+import org.connectbot.bean.HostBean;
+import org.connectbot.util.HostDatabase;
+import org.connectbot.util.PreferenceConstants;
+
+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;
+
+/**
+ * @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;
+
+ 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 Notification newNotification(Context context) {
+ Notification notification = new Notification();
+ notification.icon = R.drawable.notification_icon;
+ notification.when = System.currentTimeMillis();
+
+ return notification;
+ }
+
+ protected Notification newActivityNotification(Context context, HostBean host) {
+ Notification notification = newNotification(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);
+
+ notification.setLatestEventInfo(context, res.getString(R.string.app_name), contentText, contentIntent);
+
+ notification.flags = Notification.FLAG_AUTO_CANCEL;
+
+ notification.flags |= Notification.DEFAULT_LIGHTS;
+ if (HostDatabase.COLOR_RED.equals(host.getColor()))
+ notification.ledARGB = Color.RED;
+ else if (HostDatabase.COLOR_GREEN.equals(host.getColor()))
+ notification.ledARGB = Color.GREEN;
+ else if (HostDatabase.COLOR_BLUE.equals(host.getColor()))
+ notification.ledARGB = Color.BLUE;
+ else
+ notification.ledARGB = Color.WHITE;
+ notification.ledOnMS = 300;
+ notification.ledOffMS = 1000;
+ notification.flags |= Notification.FLAG_SHOW_LIGHTS;
+
+ return notification;
+ }
+
+ protected Notification newRunningNotification(Context context) {
+ Notification notification = newNotification(context);
+
+ notification.flags = Notification.FLAG_ONGOING_EVENT
+ | Notification.FLAG_NO_CLEAR;
+ notification.when = 0;
+
+ notification.contentIntent = PendingIntent.getActivity(context,
+ ONLINE_NOTIFICATION,
+ new Intent(context, ConsoleActivity.class), 0);
+
+ Resources res = context.getResources();
+
+ notification.setLatestEventInfo(context,
+ res.getString(R.string.app_name),
+ res.getString(R.string.app_is_running),
+ notification.contentIntent);
+
+ return notification;
+ }
+
+ 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);
+ }
+ }
+ }
+
+ 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);
+ }
+ }
+}