2019-12-31 18:19:35 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
2023-10-17 13:44:01 +02:00
|
|
|
* modified
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only
|
2019-12-31 18:19:35 +01:00
|
|
|
*/
|
|
|
|
|
2024-01-31 18:32:43 +01:00
|
|
|
package helium314.keyboard.latin.settings;
|
2019-12-31 18:19:35 +01:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.res.Resources;
|
2022-01-29 15:56:03 +01:00
|
|
|
import android.media.AudioManager;
|
2019-12-31 18:19:35 +01:00
|
|
|
import android.os.Bundle;
|
2023-11-27 15:50:06 +01:00
|
|
|
import android.view.inputmethod.InputMethodSubtype;
|
2023-09-01 18:20:09 +02:00
|
|
|
|
2023-10-16 20:42:48 +02:00
|
|
|
import androidx.preference.Preference;
|
|
|
|
|
2024-01-31 18:32:43 +01:00
|
|
|
import helium314.keyboard.keyboard.KeyboardLayoutSet;
|
|
|
|
import helium314.keyboard.keyboard.KeyboardSwitcher;
|
|
|
|
import helium314.keyboard.latin.AudioAndHapticFeedbackManager;
|
|
|
|
import helium314.keyboard.latin.R;
|
|
|
|
import helium314.keyboard.latin.RichInputMethodManager;
|
2024-04-06 11:17:56 +02:00
|
|
|
import helium314.keyboard.latin.utils.DialogUtilsKt;
|
2024-02-05 09:33:06 +01:00
|
|
|
import helium314.keyboard.latin.utils.PopupKeysUtilsKt;
|
2024-01-31 18:32:43 +01:00
|
|
|
import helium314.keyboard.latin.utils.SubtypeSettingsKt;
|
2024-02-01 19:46:44 +01:00
|
|
|
import helium314.keyboard.latin.utils.SubtypeUtilsKt;
|
2024-01-31 18:32:43 +01:00
|
|
|
import helium314.keyboard.latin.utils.ToolbarUtilsKt;
|
2019-12-31 18:19:35 +01:00
|
|
|
|
2023-11-27 15:50:06 +01:00
|
|
|
import kotlin.collections.ArraysKt;
|
|
|
|
|
2019-12-31 18:19:35 +01:00
|
|
|
public final class PreferencesSettingsFragment extends SubScreenFragment {
|
|
|
|
|
2023-10-20 17:49:47 +02:00
|
|
|
private boolean mReloadKeyboard = false;
|
|
|
|
|
2019-12-31 18:19:35 +01:00
|
|
|
@Override
|
|
|
|
public void onCreate(final Bundle icicle) {
|
|
|
|
super.onCreate(icicle);
|
|
|
|
addPreferencesFromResource(R.xml.prefs_screen_preferences);
|
|
|
|
|
|
|
|
final Resources res = getResources();
|
|
|
|
final Context context = getActivity();
|
|
|
|
|
|
|
|
// When we are called from the Settings application but we are not already running, some
|
|
|
|
// singleton and utility classes may not have been initialized. We have to call
|
|
|
|
// initialization method of these classes here. See {@link LatinIME#onCreate()}.
|
|
|
|
RichInputMethodManager.init(context);
|
|
|
|
|
|
|
|
if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) {
|
|
|
|
removePreference(Settings.PREF_VIBRATE_ON);
|
2022-01-29 15:56:03 +01:00
|
|
|
removePreference(Settings.PREF_VIBRATION_DURATION_SETTINGS);
|
2019-12-31 18:19:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-29 15:56:03 +01:00
|
|
|
setupKeypressVibrationDurationSettings();
|
|
|
|
setupKeypressSoundVolumeSettings();
|
2022-02-18 21:08:29 +01:00
|
|
|
setupHistoryRetentionTimeSettings();
|
|
|
|
refreshEnablingsOfKeypressSoundAndVibrationAndHistRetentionSettings();
|
2023-11-27 15:50:06 +01:00
|
|
|
setLocalizedNumberRowVisibility();
|
2024-01-31 16:54:31 +01:00
|
|
|
findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setVisible(getSharedPreferences().getBoolean(Settings.PREF_SHOW_HINTS, false));
|
|
|
|
findPreference(Settings.PREF_POPUP_KEYS_ORDER).setOnPreferenceClickListener((pref) -> {
|
2024-04-06 12:22:14 +02:00
|
|
|
DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_POPUP_KEYS_ORDER,
|
|
|
|
PopupKeysUtilsKt.POPUP_KEYS_ORDER_DEFAULT, R.string.popup_order, (x) -> null);
|
2023-12-25 09:20:15 +01:00
|
|
|
return true;
|
|
|
|
});
|
2024-01-31 16:54:31 +01:00
|
|
|
findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setOnPreferenceClickListener((pref) -> {
|
2024-04-06 12:22:14 +02:00
|
|
|
DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_POPUP_KEYS_LABELS_ORDER,
|
|
|
|
PopupKeysUtilsKt.POPUP_KEYS_LABEL_DEFAULT, R.string.hint_source, (x) -> null);
|
2023-12-25 09:20:15 +01:00
|
|
|
return true;
|
|
|
|
});
|
2023-12-28 22:47:31 +01:00
|
|
|
findPreference(Settings.PREF_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> {
|
2024-04-06 12:22:14 +02:00
|
|
|
DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultToolbarPref(),
|
|
|
|
R.string.toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
|
2023-12-28 22:47:31 +01:00
|
|
|
return true;
|
|
|
|
});
|
2024-05-27 20:34:26 +02:00
|
|
|
findPreference(Settings.PREF_PINNED_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> {
|
|
|
|
DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_PINNED_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultPinnedToolbarPref(),
|
|
|
|
R.string.pinned_toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
|
|
|
|
return true;
|
|
|
|
});
|
2024-05-01 23:41:11 +02:00
|
|
|
findPreference(Settings.PREF_CLIPBOARD_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> {
|
|
|
|
DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultClipboardToolbarPref(),
|
2024-05-27 20:34:26 +02:00
|
|
|
R.string.clipboard_toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
|
2024-05-01 23:41:11 +02:00
|
|
|
return true;
|
|
|
|
});
|
2019-12-31 18:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
|
2022-02-18 21:08:29 +01:00
|
|
|
refreshEnablingsOfKeypressSoundAndVibrationAndHistRetentionSettings();
|
2023-12-25 09:20:15 +01:00
|
|
|
if (key == null) return;
|
|
|
|
switch (key) {
|
2024-01-31 16:54:31 +01:00
|
|
|
case Settings.PREF_POPUP_KEYS_ORDER, Settings.PREF_SHOW_POPUP_HINTS, Settings.PREF_SHOW_NUMBER_ROW,
|
2024-05-27 20:34:26 +02:00
|
|
|
Settings.PREF_POPUP_KEYS_LABELS_ORDER, Settings.PREF_TOOLBAR_KEYS, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS,
|
|
|
|
Settings.PREF_PINNED_TOOLBAR_KEYS, Settings.PREF_QUICK_PIN_TOOLBAR_KEYS
|
2023-12-25 09:20:15 +01:00
|
|
|
-> mReloadKeyboard = true;
|
|
|
|
case Settings.PREF_LOCALIZED_NUMBER_ROW -> KeyboardLayoutSet.onSystemLocaleChanged();
|
|
|
|
case Settings.PREF_SHOW_HINTS
|
2024-01-31 16:54:31 +01:00
|
|
|
-> findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setVisible(prefs.getBoolean(Settings.PREF_SHOW_HINTS, false));
|
2023-12-10 18:31:03 +01:00
|
|
|
}
|
2023-10-20 17:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
if (mReloadKeyboard)
|
|
|
|
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext());
|
|
|
|
mReloadKeyboard = false;
|
2019-12-31 18:19:35 +01:00
|
|
|
}
|
|
|
|
|
2023-11-27 15:50:06 +01:00
|
|
|
private void setLocalizedNumberRowVisibility() {
|
|
|
|
final Preference pref = findPreference(Settings.PREF_LOCALIZED_NUMBER_ROW);
|
|
|
|
if (pref == null) return;
|
2023-11-30 10:58:53 +01:00
|
|
|
// locales that have a number row defined (not good to have it hardcoded, but reading a bunch of files may be noticeably slow)
|
2024-02-10 19:44:20 +01:00
|
|
|
final String[] numberRowLocales = new String[] { "ar", "bn", "fa", "gu", "hi", "kn", "mr", "ne", "ur" };
|
2023-11-27 15:50:06 +01:00
|
|
|
for (final InputMethodSubtype subtype : SubtypeSettingsKt.getEnabledSubtypes(getSharedPreferences(), true)) {
|
2024-02-01 19:46:44 +01:00
|
|
|
if (ArraysKt.any(numberRowLocales, (l) -> l.equals(SubtypeUtilsKt.locale(subtype).getLanguage()))) {
|
2023-11-27 15:50:06 +01:00
|
|
|
pref.setVisible(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pref.setVisible(false);
|
|
|
|
}
|
|
|
|
|
2022-02-18 21:08:29 +01:00
|
|
|
private void refreshEnablingsOfKeypressSoundAndVibrationAndHistRetentionSettings() {
|
2019-12-31 18:19:35 +01:00
|
|
|
final SharedPreferences prefs = getSharedPreferences();
|
|
|
|
final Resources res = getResources();
|
2023-09-07 13:29:21 +02:00
|
|
|
setPreferenceVisible(Settings.PREF_VIBRATION_DURATION_SETTINGS,
|
2019-12-31 18:19:35 +01:00
|
|
|
Settings.readVibrationEnabled(prefs, res));
|
2023-09-07 13:29:21 +02:00
|
|
|
setPreferenceVisible(Settings.PREF_KEYPRESS_SOUND_VOLUME,
|
2019-12-31 18:19:35 +01:00
|
|
|
Settings.readKeypressSoundEnabled(prefs, res));
|
2023-09-07 13:29:21 +02:00
|
|
|
setPreferenceVisible(Settings.PREF_CLIPBOARD_HISTORY_RETENTION_TIME,
|
2022-02-18 21:08:29 +01:00
|
|
|
Settings.readClipboardHistoryEnabled(prefs));
|
2019-12-31 18:19:35 +01:00
|
|
|
}
|
2022-01-29 15:56:03 +01:00
|
|
|
|
|
|
|
private void setupKeypressVibrationDurationSettings() {
|
2023-09-09 08:29:06 +02:00
|
|
|
final SeekBarDialogPreference pref = findPreference(
|
2022-01-29 15:56:03 +01:00
|
|
|
Settings.PREF_VIBRATION_DURATION_SETTINGS);
|
|
|
|
if (pref == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final SharedPreferences prefs = getSharedPreferences();
|
|
|
|
final Resources res = getResources();
|
|
|
|
pref.setInterface(new SeekBarDialogPreference.ValueProxy() {
|
|
|
|
@Override
|
|
|
|
public void writeValue(final int value, final String key) {
|
|
|
|
prefs.edit().putInt(key, value).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeDefaultValue(final String key) {
|
|
|
|
prefs.edit().remove(key).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int readValue(final String key) {
|
2024-05-26 20:25:13 +02:00
|
|
|
return Settings.readKeypressVibrationDuration(prefs);
|
2022-01-29 15:56:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int readDefaultValue(final String key) {
|
2024-05-26 20:25:13 +02:00
|
|
|
return -1;
|
2022-01-29 15:56:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void feedbackValue(final int value) {
|
|
|
|
AudioAndHapticFeedbackManager.getInstance().vibrate(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getValueText(final int value) {
|
|
|
|
if (value < 0) {
|
|
|
|
return res.getString(R.string.settings_system_default);
|
|
|
|
}
|
2023-09-07 15:54:08 +02:00
|
|
|
return res.getString(R.string.abbreviation_unit_milliseconds, Integer.toString(value));
|
2022-01-29 15:56:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setupKeypressSoundVolumeSettings() {
|
2023-09-09 08:29:06 +02:00
|
|
|
final SeekBarDialogPreference pref = findPreference(
|
2022-01-29 15:56:03 +01:00
|
|
|
Settings.PREF_KEYPRESS_SOUND_VOLUME);
|
|
|
|
if (pref == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final SharedPreferences prefs = getSharedPreferences();
|
|
|
|
final Resources res = getResources();
|
2023-09-07 15:54:08 +02:00
|
|
|
final AudioManager am = (AudioManager) requireContext().getSystemService(Context.AUDIO_SERVICE);
|
2022-01-29 15:56:03 +01:00
|
|
|
pref.setInterface(new SeekBarDialogPreference.ValueProxy() {
|
|
|
|
private static final float PERCENTAGE_FLOAT = 100.0f;
|
|
|
|
|
|
|
|
private float getValueFromPercentage(final int percentage) {
|
|
|
|
return percentage / PERCENTAGE_FLOAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getPercentageFromValue(final float floatValue) {
|
|
|
|
return (int)(floatValue * PERCENTAGE_FLOAT);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeValue(final int value, final String key) {
|
|
|
|
prefs.edit().putFloat(key, getValueFromPercentage(value)).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeDefaultValue(final String key) {
|
|
|
|
prefs.edit().remove(key).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int readValue(final String key) {
|
2024-05-26 20:25:13 +02:00
|
|
|
return getPercentageFromValue(Settings.readKeypressSoundVolume(prefs));
|
2022-01-29 15:56:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int readDefaultValue(final String key) {
|
2024-05-26 20:25:13 +02:00
|
|
|
return getPercentageFromValue(-1f);
|
2022-01-29 15:56:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getValueText(final int value) {
|
|
|
|
if (value < 0) {
|
|
|
|
return res.getString(R.string.settings_system_default);
|
|
|
|
}
|
|
|
|
return Integer.toString(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void feedbackValue(final int value) {
|
|
|
|
am.playSoundEffect(
|
|
|
|
AudioManager.FX_KEYPRESS_STANDARD, getValueFromPercentage(value));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-18 21:08:29 +01:00
|
|
|
private void setupHistoryRetentionTimeSettings() {
|
2022-01-29 15:56:03 +01:00
|
|
|
final SharedPreferences prefs = getSharedPreferences();
|
|
|
|
final Resources res = getResources();
|
2023-09-09 08:29:06 +02:00
|
|
|
final SeekBarDialogPreference pref = findPreference(
|
2022-02-18 21:08:29 +01:00
|
|
|
Settings.PREF_CLIPBOARD_HISTORY_RETENTION_TIME);
|
2022-01-29 15:56:03 +01:00
|
|
|
if (pref == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pref.setInterface(new SeekBarDialogPreference.ValueProxy() {
|
|
|
|
@Override
|
|
|
|
public void writeValue(final int value, final String key) {
|
|
|
|
prefs.edit().putInt(key, value).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeDefaultValue(final String key) {
|
|
|
|
prefs.edit().remove(key).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int readValue(final String key) {
|
2022-02-18 21:08:29 +01:00
|
|
|
return Settings.readClipboardHistoryRetentionTime(prefs, res);
|
2022-01-29 15:56:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int readDefaultValue(final String key) {
|
2022-02-18 21:08:29 +01:00
|
|
|
return Settings.readDefaultClipboardHistoryRetentionTime(res);
|
2022-01-29 15:56:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getValueText(final int value) {
|
2022-02-18 21:08:29 +01:00
|
|
|
if (value <= 0) {
|
|
|
|
return res.getString(R.string.settings_no_limit);
|
|
|
|
}
|
2023-09-07 15:54:08 +02:00
|
|
|
return res.getString(R.string.abbreviation_unit_minutes, Integer.toString(value));
|
2022-01-29 15:56:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void feedbackValue(final int value) {}
|
|
|
|
});
|
|
|
|
}
|
2019-12-31 18:19:35 +01:00
|
|
|
}
|