Add configurable back button behavior

Allow the back button to (1) close all windows (the current behavior);
(2) close the current window only; (3) close the activity, but leave the
terminal sessions running; or (4) send ESC to the terminal.

Signed-off-by: Jack Palevich <jackpal@google.com>
This commit is contained in:
Steven Luo 2011-11-18 02:24:40 -08:00 committed by Jack Palevich
parent 73590196f5
commit d702d2c7fe
6 changed files with 105 additions and 3 deletions

View File

@ -97,6 +97,21 @@
<item>5</item>
</string-array>
<string-array name="entries_backaction_preference">
<item>Closes all terminal windows</item>
<item>Closes this terminal window only</item>
<item>Closes activity, leaving sessions running</item>
<item>Sends ESC to terminal</item>
</string-array>
<!-- Do not localize entryvalues -->
<string-array name="entryvalues_backaction_preference">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="entries_controlkey_preference">
<item>Jog ball</item>
<item>\@ key</item>

View File

@ -71,6 +71,10 @@
<string name="keyboard_preferences">Keyboard</string>
<string name="title_backaction_preference">Back button behavior</string>
<string name="summary_backaction_preference">Choose what pressing the back button does.</string>
<string name="dialog_title_backaction_preference">Back button behavior</string>
<string name="title_controlkey_preference">Control key</string>
<string name="summary_controlkey_preference">Choose control key.</string>
<string name="dialog_title_controlkey_preference">Control key</string>
@ -98,6 +102,7 @@
<string name="default_value_cursorblink_preference">0</string>
<string name="default_value_fontsize_preference">10</string>
<string name="default_value_color_preference">2</string>
<string name="default_value_backaction_preference">0</string>
<string name="default_value_controlkey_preference">5</string>
<string name="default_value_fnkey_preference">4</string>
<string name="default_value_ime_preference">0</string>

View File

@ -84,6 +84,15 @@
<PreferenceCategory
android:title="@string/keyboard_preferences">
<ListPreference
android:key="backaction"
android:defaultValue="@string/default_value_backaction_preference"
android:title="@string/title_backaction_preference"
android:summary="@string/summary_backaction_preference"
android:entries="@array/entries_backaction_preference"
android:entryValues="@array/entryvalues_backaction_preference"
android:dialogTitle="@string/dialog_title_backaction_preference" />
<ListPreference
android:key="controlkey"
android:defaultValue="@string/default_value_controlkey_preference"

View File

@ -812,7 +812,9 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
return true;
} else if (isSystemKey(keyCode, event)) {
// Don't intercept the system keys
return super.onKeyDown(keyCode, event);
if (keyCode != KeyEvent.KEYCODE_BACK || mSettings.getBackKeyAction() != TermSettings.BACK_KEY_SENDS_ESC) {
return super.onKeyDown(keyCode, event);
}
}
// Translate the keyCode into an ASCII character.
@ -836,7 +838,9 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
return true;
} else if (isSystemKey(keyCode, event)) {
// Don't intercept the system keys
return super.onKeyUp(keyCode, event);
if (keyCode != KeyEvent.KEYCODE_BACK || mSettings.getBackKeyAction() != TermSettings.BACK_KEY_SENDS_ESC) {
return super.onKeyUp(keyCode, event);
}
}
mKeyListener.keyUp(keyCode);
@ -1955,6 +1959,10 @@ class TermKeyListener {
}
break;
case KeyEvent.KEYCODE_BACK:
result = 27; // ESC
break;
default: {
result = event.getUnicodeChar(
(mCapKey.isActive() || mCapsLock ? KeyEvent.META_SHIFT_ON : 0) |

View File

@ -40,6 +40,7 @@ import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
@ -78,6 +79,7 @@ public class Term extends Activity {
private final static int PASTE_ID = 2;
private boolean mAlreadyStarted = false;
private boolean mStopServiceOnFinish = false;
private Intent TSIntent;
@ -88,6 +90,8 @@ public class Term extends Activity {
private PowerManager.WakeLock mWakeLock;
private WifiManager.WifiLock mWifiLock;
private boolean mBackKeyPressed;
private TermService mTermService;
private ServiceConnection mTSConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
@ -150,7 +154,9 @@ public class Term extends Activity {
super.onDestroy();
mViewFlipper.removeAllViews();
unbindService(mTSConnection);
stopService(TSIntent);
if (mStopServiceOnFinish) {
stopService(TSIntent);
}
mTermService = null;
mTSConnection = null;
if (mWakeLock.isHeld()) {
@ -359,6 +365,7 @@ public class Term extends Activity {
session.finish();
mViewFlipper.removeView(view);
if (mTermSessions.size() == 0) {
mStopServiceOnFinish = true;
finish();
} else {
mViewFlipper.showNext();
@ -380,6 +387,7 @@ public class Term extends Activity {
} else {
// Close the activity if user closed all sessions
if (mTermSessions.size() == 0) {
mStopServiceOnFinish = true;
finish();
}
}
@ -434,6 +442,50 @@ public class Term extends Activity {
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
/* The pre-Eclair default implementation of onKeyDown() would prevent
our handling of the Back key in onKeyUp() from taking effect, so
ignore it here */
if (keyCode == KeyEvent.KEYCODE_BACK) {
/* Android pre-Eclair has no key event tracking, and a back key
down event delivered to an activity above us in the back stack
could be succeeded by a back key up event to us, so we need to
keep track of our own back key presses */
mBackKeyPressed = true;
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (!mBackKeyPressed) {
/* This key up event might correspond to a key down delivered
to another activity -- ignore */
return false;
}
mBackKeyPressed = false;
switch (mSettings.getBackKeyAction()) {
case TermSettings.BACK_KEY_STOPS_SERVICE:
mStopServiceOnFinish = true;
case TermSettings.BACK_KEY_CLOSES_ACTIVITY:
finish();
return true;
case TermSettings.BACK_KEY_CLOSES_WINDOW:
doCloseWindow();
return true;
default:
return false;
}
default:
return super.onKeyUp(keyCode, event);
}
}
private boolean canPaste() {
ClipboardManager clip = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
if (clip.hasText()) {

View File

@ -36,6 +36,7 @@ public class TermSettings {
private String mShell;
private String mInitialCommand;
private boolean mUTF8ByDefault = false;
private int mBackKeyAction = 0; // Default to closing activity
private static final String STATUSBAR_KEY = "statusbar";
private static final String CURSORSTYLE_KEY = "cursorstyle";
@ -48,6 +49,7 @@ public class TermSettings {
private static final String SHELL_KEY = "shell";
private static final String INITIALCOMMAND_KEY = "initialcommand";
private static final String UTF8_KEY = "utf8_by_default";
private static final String BACKACTION_KEY = "backaction";
public static final int WHITE = 0xffffffff;
public static final int BLACK = 0xff000000;
@ -85,6 +87,12 @@ public class TermSettings {
KEYCODE_NONE
};
public static final int BACK_KEY_STOPS_SERVICE = 0;
public static final int BACK_KEY_CLOSES_WINDOW = 1;
public static final int BACK_KEY_CLOSES_ACTIVITY = 2;
public static final int BACK_KEY_SENDS_ESC = 3;
private static final int BACK_KEY_MAX = 3;
public TermSettings(SharedPreferences prefs) {
readPrefs(prefs);
}
@ -104,6 +112,7 @@ public class TermSettings {
mShell = readStringPref(SHELL_KEY, mShell);
mInitialCommand = readStringPref(INITIALCOMMAND_KEY, mInitialCommand);
mUTF8ByDefault = readBooleanPref(UTF8_KEY, false);
mBackKeyAction = readIntPref(BACKACTION_KEY, mBackKeyAction, BACK_KEY_MAX);
mPrefs = null; // we leak a Context if we hold on to this
}
@ -178,4 +187,8 @@ public class TermSettings {
public boolean defaultToUTF8Mode() {
return mUTF8ByDefault;
}
public int getBackKeyAction() {
return mBackKeyAction;
}
}