From 20065b316e552d49d2aea17dfb255b03a9b4a0bb Mon Sep 17 00:00:00 2001 From: Jeremy Klein Date: Tue, 21 Jul 2015 19:40:07 -0700 Subject: Add a disconnect button to the active notification dialog. --- app/src/main/AndroidManifest.xml | 1 + .../main/java/org/connectbot/ConsoleActivity.java | 33 ++++++++++++++++++++++ .../org/connectbot/service/ConnectionNotifier.java | 12 ++++++++ 3 files changed, 46 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 406f3b9..2bfc525 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -78,6 +78,7 @@ android:launchMode="singleTop" android:hardwareAccelerated="false"> + diff --git a/app/src/main/java/org/connectbot/ConsoleActivity.java b/app/src/main/java/org/connectbot/ConsoleActivity.java index e7a8f7d..4c7fa38 100644 --- a/app/src/main/java/org/connectbot/ConsoleActivity.java +++ b/app/src/main/java/org/connectbot/ConsoleActivity.java @@ -18,6 +18,8 @@ package org.connectbot; import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.connectbot.bean.SelectionArea; @@ -80,6 +82,7 @@ import de.mud.terminal.vt320; public class ConsoleActivity extends Activity { public final static String TAG = "CB.ConsoleActivity"; + public static final String DISCONNECT_ACTION = "org.connectbot.action.DISCONNECT"; protected static final int REQUEST_EDIT = 1; @@ -139,6 +142,8 @@ public class ConsoleActivity extends Activity { private boolean inActionBarMenu = false; private boolean titleBarHide; + private boolean waitingForDisconnectAll = false; + private ServiceConnection connection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { bound = ((TerminalManager.TerminalBinder) service).getService(); @@ -176,6 +181,13 @@ public class ConsoleActivity extends Activity { } setDisplayedTerminal(requestedIndex); + + // This needs to happen after the views are constructed in order to + // know when it's safe to kill this activity. + if (waitingForDisconnectAll) { + waitingForDisconnectAll = false; + disconnectAll(); + } } public void onServiceDisconnected(ComponentName className) { @@ -908,6 +920,11 @@ public class ConsoleActivity extends Activity { if (forcedOrientation && bound != null) bound.setResizeAllowed(true); + + if (getIntent().getAction() == DISCONNECT_ACTION) { + Log.d(TAG, "Got disconnect request"); + disconnectAll(); + } } /* (non-Javadoc) @@ -918,6 +935,7 @@ public class ConsoleActivity extends Activity { super.onNewIntent(intent); Log.d(TAG, "onNewIntent called"); + setIntent(intent); requested = intent.getData(); @@ -1114,6 +1132,21 @@ public class ConsoleActivity extends Activity { } } + /** + * Disconnects and closes all open terminals. + */ + private void disconnectAll() { + // TODO(jklein24): Show a confirm dialog before actually disconnecting. + if (bound == null) { + waitingForDisconnectAll = true; + return; + } + // Copy the bridges list because bridges are removed from the array when disconnected. + for (TerminalBridge bridge : new ArrayList(bound.bridges)) { + bridge.dispatchDisconnect(true); + } + } + /** * Adds a new TerminalBridge to the current set of views in our ViewFlipper. * diff --git a/app/src/main/java/org/connectbot/service/ConnectionNotifier.java b/app/src/main/java/org/connectbot/service/ConnectionNotifier.java index c480143..38b911b 100644 --- a/app/src/main/java/org/connectbot/service/ConnectionNotifier.java +++ b/app/src/main/java/org/connectbot/service/ConnectionNotifier.java @@ -45,6 +45,7 @@ import android.support.v4.app.NotificationCompat; 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) @@ -116,6 +117,17 @@ public abstract class ConnectionNotifier { builder.setContentTitle(res.getString(R.string.app_name)); builder.setContentText(res.getString(R.string.app_is_running)); + Intent notificationIntent = new Intent(context, ConsoleActivity.class); + notificationIntent.setAction(ConsoleActivity.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, + notificationIntent, + 0)); + return builder.build(); } -- cgit v1.2.3 From 53236de2e0b4299b58913c811e03500bc909eb2f Mon Sep 17 00:00:00 2001 From: Jeremy Klein Date: Wed, 22 Jul 2015 12:07:12 -0700 Subject: Switch the handling of the disconnect all intent to HostListActivity. --- app/src/main/AndroidManifest.xml | 3 +- .../main/java/org/connectbot/ConsoleActivity.java | 33 ----------- .../main/java/org/connectbot/HostListActivity.java | 64 +++++++++++++++++++++- .../org/connectbot/service/ConnectionNotifier.java | 7 ++- 4 files changed, 68 insertions(+), 39 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2bfc525..61bace5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -41,7 +41,7 @@ android:backupAgent=".service.BackupAgent" android:killAfterRestore="true"> - + @@ -78,7 +78,6 @@ android:launchMode="singleTop" android:hardwareAccelerated="false"> - diff --git a/app/src/main/java/org/connectbot/ConsoleActivity.java b/app/src/main/java/org/connectbot/ConsoleActivity.java index 4c7fa38..e7a8f7d 100644 --- a/app/src/main/java/org/connectbot/ConsoleActivity.java +++ b/app/src/main/java/org/connectbot/ConsoleActivity.java @@ -18,8 +18,6 @@ package org.connectbot; import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; import org.connectbot.bean.SelectionArea; @@ -82,7 +80,6 @@ import de.mud.terminal.vt320; public class ConsoleActivity extends Activity { public final static String TAG = "CB.ConsoleActivity"; - public static final String DISCONNECT_ACTION = "org.connectbot.action.DISCONNECT"; protected static final int REQUEST_EDIT = 1; @@ -142,8 +139,6 @@ public class ConsoleActivity extends Activity { private boolean inActionBarMenu = false; private boolean titleBarHide; - private boolean waitingForDisconnectAll = false; - private ServiceConnection connection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { bound = ((TerminalManager.TerminalBinder) service).getService(); @@ -181,13 +176,6 @@ public class ConsoleActivity extends Activity { } setDisplayedTerminal(requestedIndex); - - // This needs to happen after the views are constructed in order to - // know when it's safe to kill this activity. - if (waitingForDisconnectAll) { - waitingForDisconnectAll = false; - disconnectAll(); - } } public void onServiceDisconnected(ComponentName className) { @@ -920,11 +908,6 @@ public class ConsoleActivity extends Activity { if (forcedOrientation && bound != null) bound.setResizeAllowed(true); - - if (getIntent().getAction() == DISCONNECT_ACTION) { - Log.d(TAG, "Got disconnect request"); - disconnectAll(); - } } /* (non-Javadoc) @@ -935,7 +918,6 @@ public class ConsoleActivity extends Activity { super.onNewIntent(intent); Log.d(TAG, "onNewIntent called"); - setIntent(intent); requested = intent.getData(); @@ -1132,21 +1114,6 @@ public class ConsoleActivity extends Activity { } } - /** - * Disconnects and closes all open terminals. - */ - private void disconnectAll() { - // TODO(jklein24): Show a confirm dialog before actually disconnecting. - if (bound == null) { - waitingForDisconnectAll = true; - return; - } - // Copy the bridges list because bridges are removed from the array when disconnected. - for (TerminalBridge bridge : new ArrayList(bound.bridges)) { - bridge.dispatchDisconnect(true); - } - } - /** * Adds a new TerminalBridge to the current set of views in our ViewFlipper. * diff --git a/app/src/main/java/org/connectbot/HostListActivity.java b/app/src/main/java/org/connectbot/HostListActivity.java index 2fd9560..0e0deab 100644 --- a/app/src/main/java/org/connectbot/HostListActivity.java +++ b/app/src/main/java/org/connectbot/HostListActivity.java @@ -17,6 +17,7 @@ package org.connectbot; +import java.util.ArrayList; import java.util.List; import org.connectbot.bean.HostBean; @@ -65,6 +66,9 @@ import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class HostListActivity extends ListActivity { + public final static String TAG = "CB.HostListActivity"; + public static final String DISCONNECT_ACTION = "org.connectbot.action.DISCONNECT"; + public final static int REQUEST_EDIT = 1; public final static int REQUEST_EULA = 2; @@ -88,6 +92,14 @@ public class HostListActivity extends ListActivity { protected boolean makingShortcut = false; + private boolean waitingForDisconnectAll = false; + + /** + * Whether to close the activity when disconnectAll is called. True if this activity was + * only brought to the foreground via the notification button to disconnect all hosts. + */ + private boolean closeOnDisconnectAll = true; + protected Handler updateHandler = new Handler() { @Override public void handleMessage(Message msg) { @@ -101,6 +113,10 @@ public class HostListActivity extends ListActivity { // update our listview binder to find the service HostListActivity.this.updateList(); + + if (waitingForDisconnectAll) { + disconnectAll(); + } } public void onServiceDisconnected(ComponentName className) { @@ -129,11 +145,33 @@ public class HostListActivity extends ListActivity { this.hostdb.close(); this.hostdb = null; } + + closeOnDisconnectAll = true; } @Override public void onResume() { super.onResume(); + + // Must disconnectAll before setting closeOnDisconnectAll to know whether to keep the + // activity open after disconnecting. + if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0 && + getIntent().getAction() == DISCONNECT_ACTION) { + Log.d(TAG, "Got disconnect all request"); + disconnectAll(); + } + + // Still close on disconnect if waiting for a disconnect. + closeOnDisconnectAll = waitingForDisconnectAll && closeOnDisconnectAll; + } + + /* (non-Javadoc) + * @see android.app.Activity#onNewIntent(android.content.Intent) + */ + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + setIntent(intent); } @Override @@ -394,13 +432,37 @@ public class HostListActivity extends ListActivity { hostdb.deleteHost(host); updateHandler.sendEmptyMessage(-1); } - }) + }) .setNegativeButton(R.string.delete_neg, null).create().show(); return true; } }); } + /** + * Disconnects all active connections and closes the activity if appropriate. + */ + private void disconnectAll() { + // TODO(jklein24): Show a confirm dialog before actually disconnecting. + if (bound == null) { + waitingForDisconnectAll = true; + return; + } + // Copy the bridges list because bridges are removed from the array when disconnected. + for (TerminalBridge bridge : new ArrayList(bound.bridges)) { + bridge.dispatchDisconnect(true); + } + updateHandler.sendEmptyMessage(-1); + waitingForDisconnectAll = false; + + if (closeOnDisconnectAll) { + // Clear the intent so that the activity can be relaunched without closing. + // TODO(jlklein): Find a better way to do this. + setIntent(new Intent()); + finish(); + } + } + /** * @param text diff --git a/app/src/main/java/org/connectbot/service/ConnectionNotifier.java b/app/src/main/java/org/connectbot/service/ConnectionNotifier.java index 38b911b..a68fffc 100644 --- a/app/src/main/java/org/connectbot/service/ConnectionNotifier.java +++ b/app/src/main/java/org/connectbot/service/ConnectionNotifier.java @@ -21,6 +21,7 @@ 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; @@ -117,15 +118,15 @@ public abstract class ConnectionNotifier { builder.setContentTitle(res.getString(R.string.app_name)); builder.setContentText(res.getString(R.string.app_is_running)); - Intent notificationIntent = new Intent(context, ConsoleActivity.class); - notificationIntent.setAction(ConsoleActivity.DISCONNECT_ACTION); + 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, - notificationIntent, + disconnectIntent, 0)); return builder.build(); -- cgit v1.2.3 From f68f157c37890744acea3423b9d4a86394cb2bba Mon Sep 17 00:00:00 2001 From: Jeremy Klein Date: Mon, 27 Jul 2015 11:31:19 -0700 Subject: Address review comments. Make TerminalManager.disconnectAll public. --- .../main/java/org/connectbot/HostListActivity.java | 33 ++++++++++------------ .../org/connectbot/service/TerminalManager.java | 2 +- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/connectbot/HostListActivity.java b/app/src/main/java/org/connectbot/HostListActivity.java index 0e0deab..f0fdf16 100644 --- a/app/src/main/java/org/connectbot/HostListActivity.java +++ b/app/src/main/java/org/connectbot/HostListActivity.java @@ -17,7 +17,6 @@ package org.connectbot; -import java.util.ArrayList; import java.util.List; import org.connectbot.bean.HostBean; @@ -156,7 +155,7 @@ public class HostListActivity extends ListActivity { // Must disconnectAll before setting closeOnDisconnectAll to know whether to keep the // activity open after disconnecting. if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0 && - getIntent().getAction() == DISCONNECT_ACTION) { + DISCONNECT_ACTION.equals(getIntent().getAction())) { Log.d(TAG, "Got disconnect all request"); disconnectAll(); } @@ -422,18 +421,18 @@ public class HostListActivity extends ListActivity { public boolean onMenuItemClick(MenuItem item) { // prompt user to make sure they really want this new AlertDialog.Builder(HostListActivity.this) - .setMessage(getString(R.string.delete_message, host.getNickname())) - .setPositiveButton(R.string.delete_pos, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - // make sure we disconnect - if (bridge != null) - bridge.dispatchDisconnect(true); - - hostdb.deleteHost(host); - updateHandler.sendEmptyMessage(-1); - } - }) - .setNegativeButton(R.string.delete_neg, null).create().show(); + .setMessage(getString(R.string.delete_message, host.getNickname())) + .setPositiveButton(R.string.delete_pos, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + // make sure we disconnect + if (bridge != null) + bridge.dispatchDisconnect(true); + + hostdb.deleteHost(host); + updateHandler.sendEmptyMessage(-1); + } + }) + .setNegativeButton(R.string.delete_neg, null).create().show(); return true; } @@ -448,10 +447,8 @@ public class HostListActivity extends ListActivity { waitingForDisconnectAll = true; return; } - // Copy the bridges list because bridges are removed from the array when disconnected. - for (TerminalBridge bridge : new ArrayList(bound.bridges)) { - bridge.dispatchDisconnect(true); - } + + bound.disconnectAll(true, false); updateHandler.sendEmptyMessage(-1); waitingForDisconnectAll = false; diff --git a/app/src/main/java/org/connectbot/service/TerminalManager.java b/app/src/main/java/org/connectbot/service/TerminalManager.java index 1688d2a..b5d891d 100644 --- a/app/src/main/java/org/connectbot/service/TerminalManager.java +++ b/app/src/main/java/org/connectbot/service/TerminalManager.java @@ -199,7 +199,7 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen /** * Disconnect all currently connected bridges. */ - private void disconnectAll(final boolean immediate, final boolean excludeLocal) { + public void disconnectAll(final boolean immediate, final boolean excludeLocal) { TerminalBridge[] tmpBridges = null; synchronized (bridges) { -- cgit v1.2.3