diff options
author | Kenny Root <kenny@the-b.org> | 2009-12-18 06:03:01 +0000 |
---|---|---|
committer | Kenny Root <kenny@the-b.org> | 2009-12-18 06:03:01 +0000 |
commit | 0aae5eaa718ed21ab8ed78c8e26944572c5a9611 (patch) | |
tree | ec748b5b10ba66956ac213b7dd711a6094348a80 | |
parent | 5473b4a448deb18d230d02cad797cb21a76bcd9f (diff) | |
download | connectbot-0aae5eaa718ed21ab8ed78c8e26944572c5a9611.tar.gz connectbot-0aae5eaa718ed21ab8ed78c8e26944572c5a9611.tar.bz2 connectbot-0aae5eaa718ed21ab8ed78c8e26944572c5a9611.zip |
Update service to stay open on Eclair
setForeground(true) trick doesn't work on API 5 and above anymore, so
use the pattern jasta suggested on a blog post to support both >=5 and
<5 simultaneously.
git-svn-id: https://connectbot.googlecode.com/svn/trunk/connectbot@444 df292f66-193f-0410-a5fc-6d59da041ff2
-rw-r--r-- | AndroidManifest.xml | 2 | ||||
-rw-r--r-- | res/values/strings.xml | 3 | ||||
-rw-r--r-- | src/org/connectbot/service/ConnectionNotifier.java | 133 | ||||
-rw-r--r-- | src/org/connectbot/service/TerminalManager.java | 54 | ||||
-rw-r--r-- | src/org/connectbot/util/PreferenceConstants.java | 4 |
5 files changed, 156 insertions, 40 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index fb6998f..0f12d62 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2,7 +2,7 @@ <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.connectbot" android:versionName="1.6-dev" - android:versionCode="221"> + android:versionCode="222"> <application android:icon="@drawable/icon" diff --git a/res/values/strings.xml b/res/values/strings.xml index 0a06b05..3106fbe 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -431,6 +431,9 @@ <!-- Menu selection to reset colors to their defaults. --> <string name="menu_colors_reset">Reset</string> + <!-- Displayed in the notification bar that connections are active --> + <string name="app_is_running">ConnectBot is running</string> + <string name="color_red">red</string> <string name="color_green">green</string> <string name="color_blue">blue</string> diff --git a/src/org/connectbot/service/ConnectionNotifier.java b/src/org/connectbot/service/ConnectionNotifier.java new file mode 100644 index 0000000..50b8527 --- /dev/null +++ b/src/org/connectbot/service/ConnectionNotifier.java @@ -0,0 +1,133 @@ +/** + * + */ +package org.connectbot.service; + +import org.connectbot.ConsoleActivity; +import org.connectbot.R; +import org.connectbot.bean.HostBean; +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; + +/** + * @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; + + return notification; + } + + protected Notification newRunningNotification(Context context) { + Notification notification = newNotification(context); + + notification.flags = Notification.FLAG_ONGOING_EVENT + | Notification.FLAG_NO_CLEAR; + + 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 class Holder { + private static final PreEclair sInstance = new PreEclair(); + } + + @Override + public void showRunningNotification(Service context) { + context.setForeground(true); + getNotificationManager(context).notify(ONLINE_NOTIFICATION, newRunningNotification(context)); + } + + @Override + public void hideRunningNotification(Service context) { + context.setForeground(false); + 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); + } + } +} diff --git a/src/org/connectbot/service/TerminalManager.java b/src/org/connectbot/service/TerminalManager.java index 1e057af..e4f4841 100644 --- a/src/org/connectbot/service/TerminalManager.java +++ b/src/org/connectbot/service/TerminalManager.java @@ -30,7 +30,6 @@ import java.util.Timer; import java.util.TimerTask; import java.util.Map.Entry; -import org.connectbot.ConsoleActivity; import org.connectbot.R; import org.connectbot.bean.HostBean; import org.connectbot.bean.PubkeyBean; @@ -40,9 +39,6 @@ import org.connectbot.util.PreferenceConstants; import org.connectbot.util.PubkeyDatabase; import org.connectbot.util.PubkeyUtils; -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; @@ -109,14 +105,10 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen private volatile boolean wantKeyVibration; public static final long VIBRATE_DURATION = 30; - private NotificationManager notificationManager; - private boolean wantBellVibration; private boolean resizeAllowed = true; - private static final int NOTIFICATION_ID = 1; - @Override public void onCreate() { Log.i(TAG, "Starting background service"); @@ -158,15 +150,6 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen wantBellVibration = prefs.getBoolean(PreferenceConstants.BELL_VIBRATE, true); enableMediaPlayer(); - - notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - - /* If user wants the connections to stay alive at all costs, - * set the service to be "foreground." - */ - if (prefs.getBoolean(PreferenceConstants.CONNECTION_PERSIST, true)) { - setForeground(true); - } } @Override @@ -402,8 +385,10 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen } protected void stopNow() { - if (bridges.size() == 0) + if (bridges.size() == 0) { + ConnectionNotifier.getInstance().hideRunningNotification(this); stopSelf(); + } } private synchronized void stopIdleTimer() { @@ -439,6 +424,8 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen setResizeAllowed(true); + ConnectionNotifier.getInstance().hideRunningNotification(this); + Log.i(TAG, "Someone rebound to TerminalManager"); stopIdleTimer(); @@ -450,8 +437,16 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen setResizeAllowed(true); - if (bridges.size() == 0) + if (bridges.size() == 0) { stopWithDelay(); + } else { + /* If user wants the connections to stay alive at all costs, + * set the service to be "foreground." + */ + if (prefs.getBoolean(PreferenceConstants.CONNECTION_PERSIST, true)) { + ConnectionNotifier.getInstance().showRunningNotification(this); + } + } return true; } @@ -530,26 +525,7 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen if (!prefs.getBoolean(PreferenceConstants.BELL_NOTIFICATION, false)) return; - String contentText = res.getString( - R.string.notification_text, host.getNickname()); - - Notification notification = new Notification( - R.drawable.notification_icon, contentText, - System.currentTimeMillis()); - notification.flags |= Notification.FLAG_AUTO_CANCEL; - - Context context = getApplicationContext(); - Intent notificationIntent = new Intent(this, ConsoleActivity.class); - notificationIntent.setAction("android.intent.action.VIEW"); - notificationIntent.setData(host.getUri()); - - PendingIntent contentIntent = PendingIntent.getActivity(this, 0, - notificationIntent, 0); - - notification.setLatestEventInfo(context, res.getString(R.string.app_name), - contentText, contentIntent); - - notificationManager.notify(NOTIFICATION_ID, notification); + ConnectionNotifier.getInstance().showActivityNotification(this, host); } /* (non-Javadoc) diff --git a/src/org/connectbot/util/PreferenceConstants.java b/src/org/connectbot/util/PreferenceConstants.java index 7231d8f..ae6f0ad 100644 --- a/src/org/connectbot/util/PreferenceConstants.java +++ b/src/org/connectbot/util/PreferenceConstants.java @@ -17,11 +17,15 @@ */ package org.connectbot.util; +import android.os.Build; + /** * @author Kenny Root * */ public class PreferenceConstants { + public static final boolean PRE_ECLAIR = (Integer.parseInt(Build.VERSION.SDK) <= 4); + public static final String MEMKEYS = "memkeys"; public static final String UPDATE = "update"; |