diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java index f5df773d..ed12ec61 100644 --- a/app/src/main/java/com/termux/app/TermuxActivity.java +++ b/app/src/main/java/com/termux/app/TermuxActivity.java @@ -48,6 +48,7 @@ import android.widget.Toast; import com.termux.R; import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY; +import com.termux.app.settings.preferences.TermuxSharedPreferences; import com.termux.app.terminal.BellHandler; import com.termux.app.terminal.TermuxViewClient; import com.termux.app.terminal.extrakeys.ExtraKeysView; @@ -119,9 +120,9 @@ public final class TermuxActivity extends Activity implements ServiceConnection ExtraKeysView mExtraKeysView; - TermuxPreferences mSettings; + private TermuxSharedPreferences mPreferences; - TermuxSharedProperties mProperties; + private TermuxSharedProperties mProperties; /** Initialized in {@link #onServiceConnected(ComponentName, IBinder)}. */ ArrayAdapter mListViewAdapter; @@ -212,7 +213,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection @Override public void onCreate(Bundle bundle) { - mSettings = new TermuxPreferences(this); + mPreferences = new TermuxSharedPreferences(this); mProperties = new TermuxSharedProperties(this); mIsUsingBlackUI = mProperties.isUsingBlackUI(); @@ -246,12 +247,12 @@ public final class TermuxActivity extends Activity implements ServiceConnection mTerminalView = findViewById(R.id.terminal_view); mTerminalView.setOnKeyListener(new TermuxViewClient(this)); - mTerminalView.setTextSize(mSettings.getFontSize()); - mTerminalView.setKeepScreenOn(mSettings.isScreenAlwaysOn()); + mTerminalView.setTextSize(mPreferences.getFontSize()); + mTerminalView.setKeepScreenOn(mPreferences.getKeepScreenOn()); mTerminalView.requestFocus(); final ViewPager viewPager = findViewById(R.id.viewpager); - if (mSettings.mShowExtraKeys) viewPager.setVisibility(View.VISIBLE); + if (mPreferences.getShowExtraKeys()) viewPager.setVisibility(View.VISIBLE); ViewGroup.LayoutParams layoutParams = viewPager.getLayoutParams(); @@ -382,7 +383,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection public void toggleShowExtraKeys() { final ViewPager viewPager = findViewById(R.id.viewpager); - final boolean showNow = mSettings.toggleShowExtraKeys(TermuxActivity.this); + final boolean showNow = mPreferences.toggleShowExtraKeys(); viewPager.setVisibility(showNow ? View.VISIBLE : View.GONE); if (showNow && viewPager.getCurrentItem() == 1) { // Focus the text input view if just revealed. @@ -636,7 +637,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection super.onStop(); mIsVisible = false; TerminalSession currentSession = getCurrentTermSession(); - if (currentSession != null) TermuxPreferences.storeCurrentSession(this, currentSession); + if (currentSession != null) mPreferences.setCurrentSession(currentSession.mHandle); unregisterReceiver(mBroadcastReceiever); getDrawer().closeDrawers(); } @@ -739,7 +740,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection menu.add(Menu.NONE, CONTEXTMENU_RESET_TERMINAL_ID, Menu.NONE, R.string.reset_terminal); menu.add(Menu.NONE, CONTEXTMENU_KILL_PROCESS_ID, Menu.NONE, getResources().getString(R.string.kill_process, getCurrentTermSession().getPid())).setEnabled(currentSession.isRunning()); menu.add(Menu.NONE, CONTEXTMENU_STYLING_ID, Menu.NONE, R.string.style_terminal); - menu.add(Menu.NONE, CONTEXTMENU_TOGGLE_KEEP_SCREEN_ON, Menu.NONE, R.string.toggle_keep_screen_on).setCheckable(true).setChecked(mSettings.isScreenAlwaysOn()); + menu.add(Menu.NONE, CONTEXTMENU_TOGGLE_KEEP_SCREEN_ON, Menu.NONE, R.string.toggle_keep_screen_on).setCheckable(true).setChecked(mPreferences.getKeepScreenOn()); menu.add(Menu.NONE, CONTEXTMENU_HELP_ID, Menu.NONE, R.string.help); } @@ -950,10 +951,10 @@ public final class TermuxActivity extends Activity implements ServiceConnection case CONTEXTMENU_TOGGLE_KEEP_SCREEN_ON: { if(mTerminalView.getKeepScreenOn()) { mTerminalView.setKeepScreenOn(false); - mSettings.setScreenAlwaysOn(this, false); + mPreferences.setKeepScreenOn(false); } else { mTerminalView.setKeepScreenOn(true); - mSettings.setScreenAlwaysOn(this, true); + mPreferences.setKeepScreenOn(true); } return true; } @@ -978,8 +979,8 @@ public final class TermuxActivity extends Activity implements ServiceConnection } public void changeFontSize(boolean increase) { - mSettings.changeFontSize(this, increase); - mTerminalView.setTextSize(mSettings.getFontSize()); + mPreferences.changeFontSize(this, increase); + mTerminalView.setTextSize(mPreferences.getFontSize()); } public void doPaste() { @@ -995,12 +996,21 @@ public final class TermuxActivity extends Activity implements ServiceConnection /** The current session as stored or the last one if that does not exist. */ public TerminalSession getStoredCurrentSessionOrLast() { - TerminalSession stored = TermuxPreferences.getCurrentSession(this); + TerminalSession stored = getCurrentSession(this); if (stored != null) return stored; List sessions = mTermService.getSessions(); return sessions.isEmpty() ? null : sessions.get(sessions.size() - 1); } + private TerminalSession getCurrentSession(TermuxActivity context) { + String sessionHandle = mPreferences.getCurrentSession(); + for (int i = 0, len = context.getTermService().getSessions().size(); i < len; i++) { + TerminalSession session = context.getTermService().getSessions().get(i); + if (session.mHandle.equals(sessionHandle)) return session; + } + return null; + } + /** Show a toast and dismiss the last one if still visible. */ void showToast(String text, boolean longDuration) { if (mLastToast != null) mLastToast.cancel(); diff --git a/app/src/main/java/com/termux/app/TermuxPreferences.java b/app/src/main/java/com/termux/app/TermuxPreferences.java deleted file mode 100644 index 5567439d..00000000 --- a/app/src/main/java/com/termux/app/TermuxPreferences.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.termux.app; - -import android.content.Context; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.util.TypedValue; - -import com.termux.terminal.TerminalSession; - -final class TermuxPreferences { - - private final int MIN_FONTSIZE; - private static final int MAX_FONTSIZE = 256; - - private static final String SHOW_EXTRA_KEYS_KEY = "show_extra_keys"; - private static final String FONTSIZE_KEY = "fontsize"; - private static final String CURRENT_SESSION_KEY = "current_session"; - private static final String SCREEN_ALWAYS_ON_KEY = "screen_always_on"; - - private boolean mScreenAlwaysOn; - private int mFontSize; - boolean mShowExtraKeys; - - TermuxPreferences(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - - float dipInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, context.getResources().getDisplayMetrics()); - - // This is a bit arbitrary and sub-optimal. We want to give a sensible default for minimum font size - // to prevent invisible text due to zoom be mistake: - MIN_FONTSIZE = (int) (4f * dipInPixels); - - mShowExtraKeys = prefs.getBoolean(SHOW_EXTRA_KEYS_KEY, true); - mScreenAlwaysOn = prefs.getBoolean(SCREEN_ALWAYS_ON_KEY, false); - - // http://www.google.com/design/spec/style/typography.html#typography-line-height - int defaultFontSize = Math.round(12 * dipInPixels); - // Make it divisible by 2 since that is the minimal adjustment step: - if (defaultFontSize % 2 == 1) defaultFontSize--; - - try { - mFontSize = Integer.parseInt(prefs.getString(FONTSIZE_KEY, Integer.toString(defaultFontSize))); - } catch (NumberFormatException | ClassCastException e) { - mFontSize = defaultFontSize; - } - mFontSize = clamp(mFontSize, MIN_FONTSIZE, MAX_FONTSIZE); - } - - boolean toggleShowExtraKeys(Context context) { - mShowExtraKeys = !mShowExtraKeys; - PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(SHOW_EXTRA_KEYS_KEY, mShowExtraKeys).apply(); - return mShowExtraKeys; - } - - int getFontSize() { - return mFontSize; - } - - void changeFontSize(Context context, boolean increase) { - mFontSize += (increase ? 1 : -1) * 2; - mFontSize = Math.max(MIN_FONTSIZE, Math.min(mFontSize, MAX_FONTSIZE)); - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - prefs.edit().putString(FONTSIZE_KEY, Integer.toString(mFontSize)).apply(); - } - - boolean isScreenAlwaysOn() { - return mScreenAlwaysOn; - } - - void setScreenAlwaysOn(Context context, boolean newValue) { - mScreenAlwaysOn = newValue; - PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(SCREEN_ALWAYS_ON_KEY, newValue).apply(); - } - - static void storeCurrentSession(Context context, TerminalSession session) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putString(TermuxPreferences.CURRENT_SESSION_KEY, session.mHandle).apply(); - } - - static TerminalSession getCurrentSession(TermuxActivity context) { - String sessionHandle = PreferenceManager.getDefaultSharedPreferences(context).getString(TermuxPreferences.CURRENT_SESSION_KEY, ""); - for (int i = 0, len = context.mTermService.getSessions().size(); i < len; i++) { - TerminalSession session = context.mTermService.getSessions().get(i); - if (session.mHandle.equals(sessionHandle)) return session; - } - return null; - } - - /** - * If value is not in the range [min, max], set it to either min or max. - */ - static int clamp(int value, int min, int max) { - return Math.min(Math.max(value, min), max); - } - -} diff --git a/app/src/main/java/com/termux/app/TermuxService.java b/app/src/main/java/com/termux/app/TermuxService.java index 0f4dd465..3301b30a 100644 --- a/app/src/main/java/com/termux/app/TermuxService.java +++ b/app/src/main/java/com/termux/app/TermuxService.java @@ -24,6 +24,7 @@ import android.widget.ArrayAdapter; import com.termux.R; import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY; import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE; +import com.termux.app.settings.preferences.TermuxSharedPreferences; import com.termux.terminal.EmulatorDebug; import com.termux.terminal.TerminalSession; import com.termux.terminal.TerminalSession.SessionChangedCallback; @@ -148,7 +149,8 @@ public final class TermuxService extends Service implements SessionChangedCallba } // Make the newly created session the current one to be displayed: - TermuxPreferences.storeCurrentSession(this, newSession); + TermuxSharedPreferences preferences = new TermuxSharedPreferences(this); + preferences.setCurrentSession(newSession.mHandle); // Launch the main Termux app, which will now show the current session: startActivity(new Intent(this, TermuxActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); diff --git a/app/src/main/java/com/termux/app/settings/preferences/TermuxPreferenceConstants.java b/app/src/main/java/com/termux/app/settings/preferences/TermuxPreferenceConstants.java new file mode 100644 index 00000000..f9ba174d --- /dev/null +++ b/app/src/main/java/com/termux/app/settings/preferences/TermuxPreferenceConstants.java @@ -0,0 +1,50 @@ +package com.termux.app.settings.preferences; + +import com.termux.app.TermuxConstants; + +/* + * Version: v0.1.0 + * + * Changelog + * + * - 0.1.0 (2021-03-12) + * - Initial Release. + * + */ + +/** + * A class that defines shared constants of the Shared preferences used by Termux app and its plugins. + * This class will be hosted by termux-app and should be imported by other termux plugin apps as is + * instead of copying constants to random classes. The 3rd party apps can also import it for + * interacting with termux apps. If changes are made to this file, increment the version number + * and add an entry in the Changelog section above. + * + * The properties are loaded from the first file found at + * {@link TermuxConstants#TERMUX_PROPERTIES_PRIMARY_FILE_PATH} or + * {@link TermuxConstants#TERMUX_PROPERTIES_SECONDARY_FILE_PATH} + */ +public final class TermuxPreferenceConstants { + + /** Defines the key for whether to show extra keys in termux terminal view */ + public static final String KEY_SHOW_EXTRA_KEYS = "show_extra_keys"; + public static final boolean DEFAULT_VALUE_SHOW_EXTRA_KEYS = true; + + + + /** Defines the key for whether to always keep screen on */ + public static final String KEY_KEEP_SCREEN_ON = "screen_always_on"; + public static final boolean DEFAULT_VALUE_KEEP_SCREEN_ON = true; + + + + /** Defines the key for font size of termux terminal view */ + public static final String KEY_FONTSIZE = "fontsize"; + + + + /** Defines the key for current termux terminal session */ + public static final String KEY_CURRENT_SESSION = "current_session"; + + + +} diff --git a/app/src/main/java/com/termux/app/settings/preferences/TermuxSharedPreferences.java b/app/src/main/java/com/termux/app/settings/preferences/TermuxSharedPreferences.java new file mode 100644 index 00000000..65fe9679 --- /dev/null +++ b/app/src/main/java/com/termux/app/settings/preferences/TermuxSharedPreferences.java @@ -0,0 +1,138 @@ +package com.termux.app.settings.preferences; + +import android.content.Context; +import android.content.SharedPreferences; +import android.util.Log; +import android.util.TypedValue; + +import com.termux.app.TermuxConstants; +import com.termux.terminal.EmulatorDebug; + +import javax.annotation.Nonnull; + +public class TermuxSharedPreferences { + + private final Context mContext; + private final SharedPreferences mSharedPreferences; + + + private int MIN_FONTSIZE; + private int MAX_FONTSIZE; + private int DEFAULT_FONTSIZE; + + public TermuxSharedPreferences(@Nonnull Context context) { + Context mTempContext; + + try { + mTempContext = context.createPackageContext(TermuxConstants.TERMUX_PACKAGE_NAME, Context.CONTEXT_RESTRICTED); + } catch (Exception e) { + Log.e(EmulatorDebug.LOG_TAG, "Failed to get \"" + TermuxConstants.TERMUX_PACKAGE_NAME + "\" package context", e); + Log.e(EmulatorDebug.LOG_TAG, "Force using current context"); + mTempContext = context; + } + + mContext = mTempContext; + mSharedPreferences = getSharedPreferences(mContext); + + setFontVariables(context); + } + + private static SharedPreferences getSharedPreferences(Context context) { + return context.getSharedPreferences(TermuxConstants.TERMUX_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION, Context.MODE_PRIVATE); + } + + + + public boolean getShowExtraKeys() { + return mSharedPreferences.getBoolean(TermuxPreferenceConstants.KEY_SHOW_EXTRA_KEYS, TermuxPreferenceConstants.DEFAULT_VALUE_SHOW_EXTRA_KEYS); + } + + public void setShowExtraKeys(boolean value) { + mSharedPreferences.edit().putBoolean(TermuxPreferenceConstants.KEY_SHOW_EXTRA_KEYS, value).apply(); + } + + public boolean toggleShowExtraKeys() { + boolean currentValue = getShowExtraKeys(); + setShowExtraKeys(!currentValue); + return !currentValue; + } + + + + public boolean getKeepScreenOn() { + return mSharedPreferences.getBoolean(TermuxPreferenceConstants.KEY_KEEP_SCREEN_ON, TermuxPreferenceConstants.DEFAULT_VALUE_KEEP_SCREEN_ON); + } + + public void setKeepScreenOn(boolean value) { + mSharedPreferences.edit().putBoolean(TermuxPreferenceConstants.KEY_KEEP_SCREEN_ON, value).apply(); + } + + + + private void setFontVariables(Context context) { + float dipInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, context.getResources().getDisplayMetrics()); + + // This is a bit arbitrary and sub-optimal. We want to give a sensible default for minimum font size + // to prevent invisible text due to zoom be mistake: + MIN_FONTSIZE = (int) (4f * dipInPixels); + + // http://www.google.com/design/spec/style/typography.html#typography-line-height + int defaultFontSize = Math.round(12 * dipInPixels); + // Make it divisible by 2 since that is the minimal adjustment step: + if (defaultFontSize % 2 == 1) defaultFontSize--; + + DEFAULT_FONTSIZE = defaultFontSize; + + MAX_FONTSIZE = 256; + } + + public int getFontSize() { + int fontSize; + String fontString; + + try { + fontString = mSharedPreferences.getString(TermuxPreferenceConstants.KEY_FONTSIZE, Integer.toString(DEFAULT_FONTSIZE)); + if(fontString != null) + fontSize = Integer.parseInt(fontString); + else + fontSize = DEFAULT_FONTSIZE; + } catch (NumberFormatException | ClassCastException e) { + fontSize = DEFAULT_FONTSIZE; + } + fontSize = clamp(fontSize, MIN_FONTSIZE, MAX_FONTSIZE); + + return fontSize; + } + + public void setFontSize(String value) { + mSharedPreferences.edit().putString(TermuxPreferenceConstants.KEY_FONTSIZE, value).apply(); + } + + public void changeFontSize(Context context, boolean increase) { + + int fontSize = getFontSize(); + + fontSize += (increase ? 1 : -1) * 2; + fontSize = Math.max(MIN_FONTSIZE, Math.min(fontSize, MAX_FONTSIZE)); + + setFontSize(Integer.toString(fontSize)); + } + + /** + * If value is not in the range [min, max], set it to either min or max. + */ + static int clamp(int value, int min, int max) { + return Math.min(Math.max(value, min), max); + } + + + + public String getCurrentSession() { + return mSharedPreferences.getString(TermuxPreferenceConstants.KEY_CURRENT_SESSION, ""); + } + + public void setCurrentSession(String value) { + mSharedPreferences.edit().putString(TermuxPreferenceConstants.KEY_CURRENT_SESSION, value).apply(); + } + +}