Skip to content

Commit 93b506a

Browse files
Move Termux Preferences to com.termux.app.settings.preferences package
The termux preferences handling was mixed in with termux properties before an earlier commit. They are now moved out of into a separate sub package, the following classes are added: - `TermuxPreferenceConstants` class that defines shared constants of the preferences used by Termux app and its plugins. This class should be imported by other termux plugin apps instead of copying and defining their own constants. - `TermuxSharedPreferences` class that acts as manager for handling termux preferences.
1 parent ebf2e47 commit 93b506a

File tree

5 files changed

+215
-111
lines changed

5 files changed

+215
-111
lines changed

app/src/main/java/com/termux/app/TermuxActivity.java

+24-14
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
import com.termux.R;
5050
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
51+
import com.termux.app.settings.preferences.TermuxSharedPreferences;
5152
import com.termux.app.terminal.BellHandler;
5253
import com.termux.app.terminal.TermuxViewClient;
5354
import com.termux.app.terminal.extrakeys.ExtraKeysView;
@@ -119,9 +120,9 @@ public final class TermuxActivity extends Activity implements ServiceConnection
119120

120121
ExtraKeysView mExtraKeysView;
121122

122-
TermuxPreferences mSettings;
123+
private TermuxSharedPreferences mPreferences;
123124

124-
TermuxSharedProperties mProperties;
125+
private TermuxSharedProperties mProperties;
125126

126127
/** Initialized in {@link #onServiceConnected(ComponentName, IBinder)}. */
127128
ArrayAdapter<TerminalSession> mListViewAdapter;
@@ -212,7 +213,7 @@ public boolean ensureStoragePermissionGranted() {
212213

213214
@Override
214215
public void onCreate(Bundle bundle) {
215-
mSettings = new TermuxPreferences(this);
216+
mPreferences = new TermuxSharedPreferences(this);
216217
mProperties = new TermuxSharedProperties(this);
217218

218219
mIsUsingBlackUI = mProperties.isUsingBlackUI();
@@ -246,12 +247,12 @@ public void onCreate(Bundle bundle) {
246247
mTerminalView = findViewById(R.id.terminal_view);
247248
mTerminalView.setOnKeyListener(new TermuxViewClient(this));
248249

249-
mTerminalView.setTextSize(mSettings.getFontSize());
250-
mTerminalView.setKeepScreenOn(mSettings.isScreenAlwaysOn());
250+
mTerminalView.setTextSize(mPreferences.getFontSize());
251+
mTerminalView.setKeepScreenOn(mPreferences.getKeepScreenOn());
251252
mTerminalView.requestFocus();
252253

253254
final ViewPager viewPager = findViewById(R.id.viewpager);
254-
if (mSettings.mShowExtraKeys) viewPager.setVisibility(View.VISIBLE);
255+
if (mPreferences.getShowExtraKeys()) viewPager.setVisibility(View.VISIBLE);
255256

256257

257258
ViewGroup.LayoutParams layoutParams = viewPager.getLayoutParams();
@@ -382,7 +383,7 @@ void sendOpenedBroadcast() {
382383

383384
public void toggleShowExtraKeys() {
384385
final ViewPager viewPager = findViewById(R.id.viewpager);
385-
final boolean showNow = mSettings.toggleShowExtraKeys(TermuxActivity.this);
386+
final boolean showNow = mPreferences.toggleShowExtraKeys();
386387
viewPager.setVisibility(showNow ? View.VISIBLE : View.GONE);
387388
if (showNow && viewPager.getCurrentItem() == 1) {
388389
// Focus the text input view if just revealed.
@@ -636,7 +637,7 @@ protected void onStop() {
636637
super.onStop();
637638
mIsVisible = false;
638639
TerminalSession currentSession = getCurrentTermSession();
639-
if (currentSession != null) TermuxPreferences.storeCurrentSession(this, currentSession);
640+
if (currentSession != null) mPreferences.setCurrentSession(currentSession.mHandle);
640641
unregisterReceiver(mBroadcastReceiever);
641642
getDrawer().closeDrawers();
642643
}
@@ -739,7 +740,7 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuIn
739740
menu.add(Menu.NONE, CONTEXTMENU_RESET_TERMINAL_ID, Menu.NONE, R.string.reset_terminal);
740741
menu.add(Menu.NONE, CONTEXTMENU_KILL_PROCESS_ID, Menu.NONE, getResources().getString(R.string.kill_process, getCurrentTermSession().getPid())).setEnabled(currentSession.isRunning());
741742
menu.add(Menu.NONE, CONTEXTMENU_STYLING_ID, Menu.NONE, R.string.style_terminal);
742-
menu.add(Menu.NONE, CONTEXTMENU_TOGGLE_KEEP_SCREEN_ON, Menu.NONE, R.string.toggle_keep_screen_on).setCheckable(true).setChecked(mSettings.isScreenAlwaysOn());
743+
menu.add(Menu.NONE, CONTEXTMENU_TOGGLE_KEEP_SCREEN_ON, Menu.NONE, R.string.toggle_keep_screen_on).setCheckable(true).setChecked(mPreferences.getKeepScreenOn());
743744
menu.add(Menu.NONE, CONTEXTMENU_HELP_ID, Menu.NONE, R.string.help);
744745
}
745746

@@ -950,10 +951,10 @@ public boolean onContextItemSelected(MenuItem item) {
950951
case CONTEXTMENU_TOGGLE_KEEP_SCREEN_ON: {
951952
if(mTerminalView.getKeepScreenOn()) {
952953
mTerminalView.setKeepScreenOn(false);
953-
mSettings.setScreenAlwaysOn(this, false);
954+
mPreferences.setKeepScreenOn(false);
954955
} else {
955956
mTerminalView.setKeepScreenOn(true);
956-
mSettings.setScreenAlwaysOn(this, true);
957+
mPreferences.setKeepScreenOn(true);
957958
}
958959
return true;
959960
}
@@ -978,8 +979,8 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
978979
}
979980

980981
public void changeFontSize(boolean increase) {
981-
mSettings.changeFontSize(this, increase);
982-
mTerminalView.setTextSize(mSettings.getFontSize());
982+
mPreferences.changeFontSize(this, increase);
983+
mTerminalView.setTextSize(mPreferences.getFontSize());
983984
}
984985

985986
public void doPaste() {
@@ -995,12 +996,21 @@ public void doPaste() {
995996

996997
/** The current session as stored or the last one if that does not exist. */
997998
public TerminalSession getStoredCurrentSessionOrLast() {
998-
TerminalSession stored = TermuxPreferences.getCurrentSession(this);
999+
TerminalSession stored = getCurrentSession(this);
9991000
if (stored != null) return stored;
10001001
List<TerminalSession> sessions = mTermService.getSessions();
10011002
return sessions.isEmpty() ? null : sessions.get(sessions.size() - 1);
10021003
}
10031004

1005+
private TerminalSession getCurrentSession(TermuxActivity context) {
1006+
String sessionHandle = mPreferences.getCurrentSession();
1007+
for (int i = 0, len = context.getTermService().getSessions().size(); i < len; i++) {
1008+
TerminalSession session = context.getTermService().getSessions().get(i);
1009+
if (session.mHandle.equals(sessionHandle)) return session;
1010+
}
1011+
return null;
1012+
}
1013+
10041014
/** Show a toast and dismiss the last one if still visible. */
10051015
void showToast(String text, boolean longDuration) {
10061016
if (mLastToast != null) mLastToast.cancel();

app/src/main/java/com/termux/app/TermuxPreferences.java

-96
This file was deleted.

app/src/main/java/com/termux/app/TermuxService.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.termux.R;
2525
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
2626
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
27+
import com.termux.app.settings.preferences.TermuxSharedPreferences;
2728
import com.termux.terminal.EmulatorDebug;
2829
import com.termux.terminal.TerminalSession;
2930
import com.termux.terminal.TerminalSession.SessionChangedCallback;
@@ -148,7 +149,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
148149
}
149150

150151
// Make the newly created session the current one to be displayed:
151-
TermuxPreferences.storeCurrentSession(this, newSession);
152+
TermuxSharedPreferences preferences = new TermuxSharedPreferences(this);
153+
preferences.setCurrentSession(newSession.mHandle);
152154

153155
// Launch the main Termux app, which will now show the current session:
154156
startActivity(new Intent(this, TermuxActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.termux.app.settings.preferences;
2+
3+
import com.termux.app.TermuxConstants;
4+
5+
/*
6+
* Version: v0.1.0
7+
*
8+
* Changelog
9+
*
10+
* - 0.1.0 (2021-03-12)
11+
* - Initial Release.
12+
*
13+
*/
14+
15+
/**
16+
* A class that defines shared constants of the Shared preferences used by Termux app and its plugins.
17+
* This class will be hosted by termux-app and should be imported by other termux plugin apps as is
18+
* instead of copying constants to random classes. The 3rd party apps can also import it for
19+
* interacting with termux apps. If changes are made to this file, increment the version number
20+
* and add an entry in the Changelog section above.
21+
*
22+
* The properties are loaded from the first file found at
23+
* {@link TermuxConstants#TERMUX_PROPERTIES_PRIMARY_FILE_PATH} or
24+
* {@link TermuxConstants#TERMUX_PROPERTIES_SECONDARY_FILE_PATH}
25+
*/
26+
public final class TermuxPreferenceConstants {
27+
28+
/** Defines the key for whether to show extra keys in termux terminal view */
29+
public static final String KEY_SHOW_EXTRA_KEYS = "show_extra_keys";
30+
public static final boolean DEFAULT_VALUE_SHOW_EXTRA_KEYS = true;
31+
32+
33+
34+
/** Defines the key for whether to always keep screen on */
35+
public static final String KEY_KEEP_SCREEN_ON = "screen_always_on";
36+
public static final boolean DEFAULT_VALUE_KEEP_SCREEN_ON = true;
37+
38+
39+
40+
/** Defines the key for font size of termux terminal view */
41+
public static final String KEY_FONTSIZE = "fontsize";
42+
43+
44+
45+
/** Defines the key for current termux terminal session */
46+
public static final String KEY_CURRENT_SESSION = "current_session";
47+
48+
49+
50+
}

0 commit comments

Comments
 (0)