Skip to content

Commit

Permalink
Moving the most of visible texts and labels to strings.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
twaik committed Jul 15, 2024
1 parent 033c14a commit 0cd36ad
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 391 deletions.
83 changes: 43 additions & 40 deletions app/src/main/java/com/termux/x11/LoriePreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceDataStore;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceGroup;

import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.PreferenceScreen;
import androidx.preference.SeekBarPreference;

import android.provider.Settings;
Expand Down Expand Up @@ -221,6 +221,12 @@ private void with(CharSequence key, Consumer<Preference> action) {
action.accept(p);
}

@SuppressLint("DiscouragedApi")
int findId(String name) {
//noinspection DataFlowIssue
return getResources().getIdentifier("pref_" + name, "string", getContext().getPackageName());
}

/** @noinspection DataFlowIssue*/
@Override @SuppressLint("ApplySharedPref")
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
Expand All @@ -230,17 +236,37 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable S
prefs.touchMode.put("1");

setPreferencesFromResource(R.xml.preferences, root == null ? "main" : root);

int id;
PreferenceScreen screen = getPreferenceScreen();
if ((id = findId(screen.getKey())) != 0)
getPreferenceScreen().setTitle(getResources().getString(id));
for (int i=0; i<getPreferenceScreen().getPreferenceCount(); i++) {
Preference p = screen.getPreference(i);
p.setOnPreferenceChangeListener(this);
p.setPreferenceDataStore(prefs);

if ((id = findId(p.getKey())) != 0)
p.setTitle(getResources().getString(id));

if ((id = findId(p.getKey() + "_summary")) != 0)
p.setSummary(getResources().getString(id));

if (p instanceof ListPreference) {
ListPreference list = (ListPreference) p;
list.setEntries(prefs.keys.get(p.getKey()).asList().getEntries());
list.setEntryValues(prefs.keys.get(p.getKey()).asList().getValues());
list.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance());
}
}

with("showAdditionalKbd", p -> p.setLayoutResource(R.layout.preference));
with("version", p -> p.setSummary(BuildConfig.VERSION_NAME));

String requiresExactOrCustom = "Requires \"display resolution mode\" to be \"exact\" or \"custom\"";
String requiresIntercepting = "Requires intercepting system shortcuts with Dex mode or with Accessibility service";
String requiresTrackpadAndNative = "Requires \"Touchscreen input mode\" to be \"Trackpad\" and \"Display resolution mode\" to be not \"native\"";

setSummary("displayStretch", requiresExactOrCustom);
setSummary("adjustResolution", requiresExactOrCustom);
setSummary("pauseKeyInterceptingWithEsc", requiresIntercepting);
setSummary("scaleTouchpad", requiresTrackpadAndNative);
setSummary("displayStretch", R.string.pref_summary_requiresExactOrCustom);
setSummary("adjustResolution", R.string.pref_summary_requiresExactOrCustom);
setSummary("pauseKeyInterceptingWithEsc", R.string.pref_summary_requiresIntercepting);
setSummary("scaleTouchpad", R.string.pref_summary_requiresTrackpadAndNative);

if (!SamsungDexUtils.available())
setVisible("dexMetaKeyCapture", false);
Expand All @@ -256,26 +282,18 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable S
setVisible("showStylusClickOverride", stylusAvailable);
setVisible("stylusIsMouse", stylusAvailable);
setVisible("stylusButtonContactModifierMode", stylusAvailable);

ListPreference p;
for (String key: prefs.keys.keySet()) {
if (!key.endsWith("Action") || (p = findPreference(key)) == null)
continue;

p.setEntries(prefs.keys.get(key).asList().getEntries());
p.setEntryValues(prefs.keys.get(key).asList().getValues());
}
setVisible("xrMode", XrActivity.isSupported());

setNoActionOptionText(findPreference("volumeDownAction"), "android volume control");
setNoActionOptionText(findPreference("volumeUpAction"), "android volume control");
}

private void setSummary(CharSequence key, CharSequence disabled) {
private void setSummary(CharSequence key, int disabled) {
Preference pref = findPreference(key);
if (pref != null)
pref.setSummaryProvider(new Preference.SummaryProvider<>() {
@Nullable @Override public CharSequence provideSummary(@NonNull Preference p) {
return p.isEnabled() ? null : disabled;
return p.isEnabled() ? null : getResources().getString(disabled);
}
});
}
Expand All @@ -297,7 +315,11 @@ void updatePreferencesLayout() {
if (getContext() == null)
return;

reloadPrefs();
for (String key : prefs.keys.keySet()) {
Preference p = findPreference(key);
if (p != null)
onSetInitialValue(p);
}

String displayResMode = prefs.displayResolutionMode.get();
setVisible("displayScale", displayResMode.contentEquals("scaled"));
Expand Down Expand Up @@ -341,20 +363,9 @@ private void setNoActionOptionText(Preference preference, CharSequence text) {
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setListeners(getPreferenceScreen());
updatePreferencesLayout();
}

void setListeners(PreferenceGroup g) {
for (int i=0; i < g.getPreferenceCount(); i++) {
g.getPreference(i).setOnPreferenceChangeListener(this);
g.getPreference(i).setPreferenceDataStore(prefs);

if (g.getPreference(i) instanceof PreferenceGroup)
setListeners((PreferenceGroup) g.getPreference(i));
}
}

@Override
public boolean onPreferenceTreeClick(@NonNull Preference p) {
if (p.getKey() == null)
Expand All @@ -378,14 +389,6 @@ public boolean onPreferenceTreeClick(@NonNull Preference p) {
return super.onPreferenceTreeClick(p);
}

public void reloadPrefs() {
for (String key : prefs.keys.keySet()) {
Preference p = findPreference(key);
if (p != null)
onSetInitialValue(p);
}
}

@SuppressLint("ApplySharedPref")
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/termux/x11/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ Notification buildNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getNotificationChannel(mNotificationManager))
.setContentTitle("Termux:X11")
.setSmallIcon(R.drawable.ic_x11_icon)
.setContentText("Pull down to show options")
.setContentText(getResources().getText(R.string.notification_content_text))
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX)
.setSilent(true)
Expand Down
13 changes: 4 additions & 9 deletions app/src/main/java/com/termux/x11/input/TouchInputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,20 +516,15 @@ public PendingIntent extractIntentFromPreferences(Prefs p, String name, int requ
}
}

@SuppressLint("DiscouragedApi")
public String extractTitleFromPreferences(Prefs p, String name) {
LoriePreferences.PrefsProto.Preference pref = p.keys.get(name + "Action");
if (pref == null)
return null;

switch(pref.asList().get()) {
case "open preferences": return "Preferences";
case "exit": return "Exit";
case "restart activity": return "Restart";
case "toggle soft keyboard": return "Toggle IME";
case "toggle additional key bar": return "Toggle additional keyboard";
case "release pointer and keyboard capture": return "Release captures";
default: return null;
}
String key = pref.asList().get().replace(' ', '_');
int id = mActivity.getResources().getIdentifier("notification_" + key, "string", mActivity.getPackageName());
return id == 0 ? null : mActivity.getResources().getString(id);
}

public NotificationCompat.Builder setupNotification(Prefs prefs, NotificationCompat.Builder builder) {
Expand Down
102 changes: 94 additions & 8 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,99 @@ E.g. <b>META</b> for <i>META key</i>, \n
<b>MOUSE_HELPER</b> for <i>mouse helper toggling key</i>, \n
<b>STYLUS_HELPER</b> for <i>stylus click override overlay toggling key</i>, \n
<b>EXIT</b> for <i>application exit key</i>.</string>
<string name="notification_content_text">Pull down to show options</string>
<string name="notification_open_preferences">Preferences</string>
<string name="notification_exit">Exit</string>
<string name="notification_restart_activity">Restart</string>
<string name="notification_toggle_soft_keyboard">Toggle IME</string>
<string name="notification_toggle_additional_key_bar">Toggle additional keyboard</string>
<string name="notification_release_pointer_and_keyboard_capture">Release captures</string>

<!-- Log Level -->
<string name="log_level_title">Log Level</string>
<string name="log_level_off">Off</string>
<string name="log_level_normal">Normal</string>
<string name="log_level_debug">Debug</string>
<string name="log_level_verbose">Verbose</string>
<string name="log_level_unknown">*Unknown*</string>
<string name="log_level_value">Logcat log level set to \"%1$s\"</string>
<string name="pref_main">Preferences</string>
<string name="pref_output">Output</string>
<string name="pref_pointer">Pointer</string>
<string name="pref_kbd">Keyboard</string>
<string name="pref_other">Other</string>
<string name="pref_version">Version</string>
<string name="pref_ekbar">Extra key bar preferences</string>
<string name="pref_userActions">Response to user actions</string>

<string name="pref_displayResolutionMode">Display resolution mode</string>
<string name="pref_displayScale">Display scale, %</string>
<string name="pref_displayResolutionExact">Display resolution</string>
<string name="pref_displayResolutionCustom">Display resolution</string>
<string name="pref_adjustResolution">Adjust the set resolution to fit screen orientation</string>
<string name="pref_displayStretch">Stretch to fit display</string>
<string name="pref_Reseed">Reseed screen while soft keyboard is open</string>
<string name="pref_Reseed_summary">The screen size will be adjusted while Soft Keyboard is open.</string>
<string name="pref_PIP">PIP mode</string>
<string name="pref_PIP_summary">Show app in picture-in-picture mode when home button or recents button is pressed</string>
<string name="pref_fullscreen">Fullscreen</string>
<string name="pref_fullscreen_summary">Toggle immersive mode in your device display</string>
<string name="pref_forceOrientation">Force screen orientation</string>
<string name="pref_hideCutout">Hide display cutout (if any)</string>
<string name="pref_keepScreenOn">Keep Screen On</string>

<string name="pref_touchMode">Touchscreen input mode</string>
<string name="pref_scaleTouchpad">Apply display scale factor to touchpad</string>
<string name="pref_showStylusClickOverride">Show stylus click options</string>
<string name="pref_showStylusClickOverride_summary">Stylus touch override, left, middle or right click (stylus only)</string>
<string name="pref_stylusIsMouse">Enable stylus mouse mode</string>
<string name="pref_stylusIsMouse_summary">Make your stylus work like a mouse. When activated, stylus will only move the cursor and send mouse clicks, ignoring pressure, angle, and tilt.</string>
<string name="pref_stylusButtonContactModifierMode">Stylus button contact modifier mode</string>
<string name="pref_stylusButtonContactModifierMode_summary">Enable this option to send right or middle mouse buttons only when stylus touches the screen while the button is pressed.</string>
<string name="pref_showMouseHelper">Show mouse click helper overlay</string>
<string name="pref_showMouseHelper_summary">Onscreen mouse buttons usable with touchpad</string>
<string name="pref_pointerCapture">Capture external pointer devices when possible</string>
<string name="pref_pointerCapture_summary">Intercept all hardware pointer events. Pointer is back to Android after pressing Escape key.</string>
<string name="pref_transformCapturedPointer">Transform captured pointer movements</string>
<string name="pref_capturedPointerSpeedFactor">Captured pointer speed factor, %</string>
<string name="pref_tapToMove">Enable tap-to-move for touchpads</string>

<string name="pref_showAdditionalKbd">Show additional keyboard</string>
<string name="pref_showAdditionalKbd_summary">Show keyboard with additional keys.</string>
<string name="pref_showIMEWhileExternalConnected">Show IME with external keyboard</string>
<string name="pref_showIMEWhileExternalConnected_summary">Show software keyboard while hardware keyboard is connected.</string>
<string name="pref_preferScancodes">Prefer scancodes when possible</string>
<string name="pref_preferScancodes_summary">Let X server handle hardware keyboard layout (with DE settings or setxkbmap).</string>
<string name="pref_hardwareKbdScancodesWorkaround">Hardware keyboard scancodes workaround</string>
<string name="pref_hardwareKbdScancodesWorkaround_summary">Fixes scancodes on some devices. Might cause problems with multiple layouts.</string>
<string name="pref_dexMetaKeyCapture">Intercept system shortcuts</string>
<string name="pref_dexMetaKeyCapture_summary">Samsung Dex only. Allows interception of &quot;Alt+F4&quot;, &quot;Meta+D&quot;, &quot;Meta+E&quot;, etc.</string>
<string name="pref_enableAccessibilityService">Enable Accessibility service for intercepting system shortcuts manually.</string>
<string name="pref_enableAccessibilityService_summary">Open Accessibility settings.</string>
<string name="pref_enableAccessibilityServiceAutomatically">Enable Accessibility service for intercepting system shortcuts automatically.</string>
<string name="pref_enableAccessibilityServiceAutomatically_summary">Requires WRITE_SECURE_SETTINGS permission.</string>
<string name="pref_pauseKeyInterceptingWithEsc">Pause key intercepting with Esc key</string>
<string name="pref_filterOutWinkey">Filter out intercepted Win (Meta/Mod4) key.</string>
<string name="pref_filterOutWinkey_summary">Allows you to use Dex shortcuts while intercepting. Requires Accessibility service to work.</string>
<string name="pref_enableGboardCJK">Workaround to enable CJK Gboard</string>
<string name="pref_enableGboardCJK_summary">May require Android 14 and Gboard 14</string>

<string name="pref_clipboardEnable">Clipboard sharing</string>
<string name="pref_requestNotificationPermission">Request notification permission</string>
<string name="pref_xrMode">Meta Oculus XR mode</string>
<string name="pref_configureResponseToUserActions">Configure response to user actions</string>
<string name="pref_storeSecondaryDisplayPreferencesSeparately">Store preferences for secondary displays separately</string>
<string name="pref_storeSecondaryDisplayPreferencesSeparately_summary">Open this screen on display you want to configure</string>

<string name="pref_adjustHeightForEK">Adjust display height for extra keys bar</string>
<string name="pref_adjustHeightForEK_summary">May cause screen flickering during toggling EK bar.</string>
<string name="pref_useTermuxEKBarBehaviour">Deactivate special keys on additional key bar after each keypress</string>
<string name="pref_useTermuxEKBarBehaviour_summary">Use long-tap to lock special keys</string>
<string name="pref_opacityEKBar">Opacity of extra keys bar, %</string>
<string name="pref_extra_keys_config">Extra keys config</string>

<string name="pref_swipeUpAction">Three finger swipe up</string>
<string name="pref_swipeDownAction">Three finger swipe down</string>
<string name="pref_volumeUpAction">Volume up</string>
<string name="pref_volumeDownAction">Volume down</string>
<string name="pref_backButtonAction">Back button</string>
<string name="pref_notificationTapAction">Notification tap</string>
<string name="pref_notificationButton0Action">Notification first button</string>
<string name="pref_notificationButton1Action">Notification second button</string>

<string name="pref_summary_requiresExactOrCustom">Requires "display resolution mode" to be "exact" or "custom"</string>
<string name="pref_summary_requiresIntercepting">Requires intercepting system shortcuts with Dex mode or with Accessibility service</string>
<string name="pref_summary_requiresTrackpadAndNative">Requires "Touchscreen input mode" to be "Trackpad" and "Display resolution mode" to be not "native"</string>
</resources>
Loading

0 comments on commit 0cd36ad

Please sign in to comment.