2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package jackpal.androidterm.util;
19 import android.content.res.Resources;
20 import android.content.SharedPreferences;
21 import android.view.KeyEvent;
23 import jackpal.androidterm.R;
26 * Terminal emulator settings
28 public class TermSettings {
29 private SharedPreferences mPrefs;
31 private int mStatusBar;
32 private int mCursorStyle;
33 private int mCursorBlink;
34 private int mFontSize;
36 private int mControlKeyId;
38 private int mUseCookedIME;
39 private String mShell;
40 private String mInitialCommand;
41 private boolean mUTF8ByDefault;
42 private int mBackKeyAction;
43 private String mTermType;
44 private boolean mCloseOnExit;
46 private static final String STATUSBAR_KEY = "statusbar";
47 private static final String CURSORSTYLE_KEY = "cursorstyle";
48 private static final String CURSORBLINK_KEY = "cursorblink";
49 private static final String FONTSIZE_KEY = "fontsize";
50 private static final String COLOR_KEY = "color";
51 private static final String CONTROLKEY_KEY = "controlkey";
52 private static final String FNKEY_KEY = "fnkey";
53 private static final String IME_KEY = "ime";
54 private static final String SHELL_KEY = "shell";
55 private static final String INITIALCOMMAND_KEY = "initialcommand";
56 private static final String UTF8_KEY = "utf8_by_default";
57 private static final String BACKACTION_KEY = "backaction";
58 private static final String TERMTYPE_KEY = "termtype";
59 private static final String CLOSEONEXIT_KEY = "close_window_on_process_exit";
61 public static final int WHITE = 0xffffffff;
62 public static final int BLACK = 0xff000000;
63 public static final int BLUE = 0xff344ebd;
64 public static final int GREEN = 0xff00ff00;
65 public static final int AMBER = 0xffffb651;
66 public static final int RED = 0xffff0113;
68 public static final int[][] COLOR_SCHEMES = {{0, BLACK, 7, WHITE}, {7, WHITE, 0, BLACK}, {7, WHITE, 4, BLUE}, {2, GREEN, 0, BLACK}, {3, AMBER, 0, BLACK}, {1, RED, 0, BLACK}};
70 /** An integer not in the range of real key codes. */
71 public static final int KEYCODE_NONE = -1;
73 public static final int CONTROL_KEY_ID_NONE = 7;
74 public static final int[] CONTROL_KEY_SCHEMES = {
75 KeyEvent.KEYCODE_DPAD_CENTER,
77 KeyEvent.KEYCODE_ALT_LEFT,
78 KeyEvent.KEYCODE_ALT_RIGHT,
79 KeyEvent.KEYCODE_VOLUME_UP,
80 KeyEvent.KEYCODE_VOLUME_DOWN,
81 KeyEvent.KEYCODE_CAMERA,
85 public static final int FN_KEY_ID_NONE = 7;
86 public static final int[] FN_KEY_SCHEMES = {
87 KeyEvent.KEYCODE_DPAD_CENTER,
89 KeyEvent.KEYCODE_ALT_LEFT,
90 KeyEvent.KEYCODE_ALT_RIGHT,
91 KeyEvent.KEYCODE_VOLUME_UP,
92 KeyEvent.KEYCODE_VOLUME_DOWN,
93 KeyEvent.KEYCODE_CAMERA,
97 public static final int BACK_KEY_STOPS_SERVICE = 0;
98 public static final int BACK_KEY_CLOSES_WINDOW = 1;
99 public static final int BACK_KEY_CLOSES_ACTIVITY = 2;
100 public static final int BACK_KEY_SENDS_ESC = 3;
101 public static final int BACK_KEY_SENDS_TAB = 4;
102 private static final int BACK_KEY_MAX = 4;
104 public TermSettings(Resources res, SharedPreferences prefs) {
105 readDefaultPrefs(res);
109 private void readDefaultPrefs(Resources res) {
110 mStatusBar = Integer.parseInt(res.getString(R.string.pref_statusbar_default));
111 mCursorStyle = Integer.parseInt(res.getString(R.string.pref_cursorstyle_default));
112 mCursorBlink = Integer.parseInt(res.getString(R.string.pref_cursorblink_default));
113 mFontSize = Integer.parseInt(res.getString(R.string.pref_fontsize_default));
114 mColorId = Integer.parseInt(res.getString(R.string.pref_color_default));
115 mUTF8ByDefault = res.getBoolean(R.bool.pref_utf8_by_default_default);
116 mBackKeyAction = Integer.parseInt(res.getString(R.string.pref_backaction_default));
117 mControlKeyId = Integer.parseInt(res.getString(R.string.pref_controlkey_default));
118 mFnKeyId = Integer.parseInt(res.getString(R.string.pref_fnkey_default));
119 mUseCookedIME = Integer.parseInt(res.getString(R.string.pref_ime_default));
120 mShell = res.getString(R.string.pref_shell_default);
121 mInitialCommand = res.getString(R.string.pref_initialcommand_default);
122 mTermType = res.getString(R.string.pref_termtype_default);
123 mCloseOnExit = res.getBoolean(R.bool.pref_close_window_on_process_exit_default);
126 public void readPrefs(SharedPreferences prefs) {
128 mStatusBar = readIntPref(STATUSBAR_KEY, mStatusBar, 1);
129 // mCursorStyle = readIntPref(CURSORSTYLE_KEY, mCursorStyle, 2);
130 // mCursorBlink = readIntPref(CURSORBLINK_KEY, mCursorBlink, 1);
131 mFontSize = readIntPref(FONTSIZE_KEY, mFontSize, 20);
132 mColorId = readIntPref(COLOR_KEY, mColorId, COLOR_SCHEMES.length - 1);
133 mControlKeyId = readIntPref(CONTROLKEY_KEY, mControlKeyId,
134 CONTROL_KEY_SCHEMES.length - 1);
135 mFnKeyId = readIntPref(FNKEY_KEY, mFnKeyId,
136 FN_KEY_SCHEMES.length - 1);
137 mUseCookedIME = readIntPref(IME_KEY, mUseCookedIME, 1);
138 mShell = readStringPref(SHELL_KEY, mShell);
139 mInitialCommand = readStringPref(INITIALCOMMAND_KEY, mInitialCommand);
140 mUTF8ByDefault = readBooleanPref(UTF8_KEY, mUTF8ByDefault);
141 mBackKeyAction = readIntPref(BACKACTION_KEY, mBackKeyAction, BACK_KEY_MAX);
142 mTermType = readStringPref(TERMTYPE_KEY, mTermType);
143 mCloseOnExit = readBooleanPref(CLOSEONEXIT_KEY, mCloseOnExit);
144 mPrefs = null; // we leak a Context if we hold on to this
147 private int readIntPref(String key, int defaultValue, int maxValue) {
150 val = Integer.parseInt(
151 mPrefs.getString(key, Integer.toString(defaultValue)));
152 } catch (NumberFormatException e) {
155 val = Math.max(0, Math.min(val, maxValue));
159 private String readStringPref(String key, String defaultValue) {
160 return mPrefs.getString(key, defaultValue);
163 private boolean readBooleanPref(String key, boolean defaultValue) {
164 return mPrefs.getBoolean(key, defaultValue);
167 public boolean showStatusBar() {
168 return (mStatusBar != 0);
171 public int getCursorStyle() {
175 public int getCursorBlink() {
179 public int getFontSize() {
183 public int[] getColorScheme() {
184 return COLOR_SCHEMES[mColorId];
187 public int getControlKeyId() {
188 return mControlKeyId;
191 public int getFnKeyId() {
195 public int getControlKeyCode() {
196 return CONTROL_KEY_SCHEMES[mControlKeyId];
199 public int getFnKeyCode() {
200 return FN_KEY_SCHEMES[mFnKeyId];
203 public boolean useCookedIME() {
204 return (mUseCookedIME != 0);
207 public String getShell() {
211 public String getInitialCommand() {
212 return mInitialCommand;
215 public boolean defaultToUTF8Mode() {
216 return mUTF8ByDefault;
219 public int getBackKeyAction() {
220 return mBackKeyAction;
223 public boolean backKeySendsCharacter() {
224 return mBackKeyAction >= BACK_KEY_SENDS_ESC;
226 public int getBackKeyCharacter() {
227 switch (mBackKeyAction) {
228 case BACK_KEY_SENDS_ESC: return 27;
229 case BACK_KEY_SENDS_TAB: return 9;
234 public String getTermType() {
238 public boolean closeWindowOnProcessExit() {