OSDN Git Service

For multisession support, we eventually want to keep the terminal
authorSteven Luo <steven+android@steven676.net>
Sat, 27 Aug 2011 19:20:44 +0000 (12:20 -0700)
committerJack Palevich <jack.palevich@gmail.com>
Sat, 27 Aug 2011 19:20:44 +0000 (12:20 -0700)
sessions in the service instead of tying them to the activity.  This
implies that we have to stop holding SharedPreferences objects in all of
the classes tied to the session.  To this end, introduce a new
TermSettings class, which is responsible for reading SharedPreferences
and holding the current value of config settings.

src/jackpal/androidterm/util/TermSettings.java [new file with mode: 0644]

diff --git a/src/jackpal/androidterm/util/TermSettings.java b/src/jackpal/androidterm/util/TermSettings.java
new file mode 100644 (file)
index 0000000..f366e7f
--- /dev/null
@@ -0,0 +1,170 @@
+/*
+ * Copyright (C) 2007 The Android Open Source Project
+ *
+ * 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 jackpal.androidterm.util;
+
+import android.content.SharedPreferences;
+import android.view.KeyEvent;
+
+/**
+ * Terminal emulator settings
+ */
+public class TermSettings {
+    private SharedPreferences mPrefs;
+
+    private int mStatusBar = 0;
+    private int mCursorStyle = 0;
+    private int mCursorBlink = 0;
+    private int mFontSize = 9;
+    private int mColorId = 2;
+    private int mControlKeyId = 5; // Default to Volume Down
+    private int mFnKeyId = 4; // Default to Volume Up
+    private int mUseCookedIME = 0;
+    private String mShell;
+    private String mInitialCommand;
+
+    private static final String STATUSBAR_KEY = "statusbar";
+    private static final String CURSORSTYLE_KEY = "cursorstyle";
+    private static final String CURSORBLINK_KEY = "cursorblink";
+    private static final String FONTSIZE_KEY = "fontsize";
+    private static final String COLOR_KEY = "color";
+    private static final String CONTROLKEY_KEY = "controlkey";
+    private static final String FNKEY_KEY = "fnkey";
+    private static final String IME_KEY = "ime";
+    private static final String SHELL_KEY = "shell";
+    private static final String INITIALCOMMAND_KEY = "initialcommand";
+
+    public static final int WHITE = 0xffffffff;
+    public static final int BLACK = 0xff000000;
+    public static final int BLUE =  0xff344ebd;
+    public static final int GREEN = 0xff00ff00;
+    public static final int AMBER = 0xffffb651;
+    public static final int RED =   0xffff0113;
+
+    public static final int[][] COLOR_SCHEMES = {{BLACK, WHITE}, {WHITE, BLACK}, {WHITE, BLUE}, {GREEN, BLACK}, {AMBER, BLACK}, {RED, BLACK}};
+
+    /** An integer not in the range of real key codes. */
+    public static final int KEYCODE_NONE = -1;
+
+    public static final int CONTROL_KEY_ID_NONE = 7;
+    public static final int[] CONTROL_KEY_SCHEMES = {
+        KeyEvent.KEYCODE_DPAD_CENTER,
+        KeyEvent.KEYCODE_AT,
+        KeyEvent.KEYCODE_ALT_LEFT,
+        KeyEvent.KEYCODE_ALT_RIGHT,
+        KeyEvent.KEYCODE_VOLUME_UP,
+        KeyEvent.KEYCODE_VOLUME_DOWN,
+        KeyEvent.KEYCODE_CAMERA,
+        KEYCODE_NONE
+    };
+
+    public static final int FN_KEY_ID_NONE = 7;
+    public static final int[] FN_KEY_SCHEMES = {
+        KeyEvent.KEYCODE_DPAD_CENTER,
+        KeyEvent.KEYCODE_AT,
+        KeyEvent.KEYCODE_ALT_LEFT,
+        KeyEvent.KEYCODE_ALT_RIGHT,
+        KeyEvent.KEYCODE_VOLUME_UP,
+        KeyEvent.KEYCODE_VOLUME_DOWN,
+        KeyEvent.KEYCODE_CAMERA,
+        KEYCODE_NONE
+    };
+
+    public TermSettings(SharedPreferences prefs) {
+        readPrefs(prefs);
+    }
+
+    public void readPrefs(SharedPreferences prefs) {
+        mPrefs = prefs;
+        mStatusBar = readIntPref(STATUSBAR_KEY, mStatusBar, 1);
+        // mCursorStyle = readIntPref(CURSORSTYLE_KEY, mCursorStyle, 2);
+        // mCursorBlink = readIntPref(CURSORBLINK_KEY, mCursorBlink, 1);
+        mFontSize = readIntPref(FONTSIZE_KEY, mFontSize, 20);
+        mColorId = readIntPref(COLOR_KEY, mColorId, COLOR_SCHEMES.length - 1);
+        mControlKeyId = readIntPref(CONTROLKEY_KEY, mControlKeyId,
+                CONTROL_KEY_SCHEMES.length - 1);
+        mFnKeyId = readIntPref(FNKEY_KEY, mFnKeyId,
+                FN_KEY_SCHEMES.length - 1);
+        mUseCookedIME = readIntPref(IME_KEY, mUseCookedIME, 1);
+        mShell = readStringPref(SHELL_KEY, mShell);
+        mInitialCommand = readStringPref(INITIALCOMMAND_KEY, mInitialCommand);
+        mPrefs = null;  // we leak a Context if we hold on to this
+    }
+
+    private int readIntPref(String key, int defaultValue, int maxValue) {
+        int val;
+        try {
+            val = Integer.parseInt(
+                mPrefs.getString(key, Integer.toString(defaultValue)));
+        } catch (NumberFormatException e) {
+            val = defaultValue;
+        }
+        val = Math.max(0, Math.min(val, maxValue));
+        return val;
+    }
+
+    private String readStringPref(String key, String defaultValue) {
+        return mPrefs.getString(key, defaultValue);
+    }
+
+    public boolean showStatusBar() {
+        return (mStatusBar != 0);
+    }
+
+    public int getCursorStyle() {
+        return mCursorStyle;
+    }
+
+    public int getCursorBlink() {
+        return mCursorBlink;
+    }
+
+    public int getFontSize() {
+        return mFontSize;
+    }
+
+    public int[] getColorScheme() {
+        return COLOR_SCHEMES[mColorId];
+    }
+
+    public int getControlKeyId() {
+        return mControlKeyId;
+    }
+
+    public int getFnKeyId() {
+        return mFnKeyId;
+    }
+
+    public int getControlKeyCode() {
+        return CONTROL_KEY_SCHEMES[mControlKeyId];
+    }
+
+    public int getFnKeyCode() {
+        return FN_KEY_SCHEMES[mFnKeyId];
+    }
+
+    public boolean useCookedIME() {
+        return (mUseCookedIME != 0);
+    }
+
+    public String getShell() {
+        return mShell;
+    }
+
+    public String getInitialCommand() {
+        return mInitialCommand;
+    }
+}