The dialog should persist after rotation.
1. Use DialogFragment 2. Override onSaveInstanceState 3. Refresh the UI after key remapping Demo: https://screencast.googleplex.com/cast/NjMzMzcxNjc2Mzc3MDg4MHxlMjM0M2FiMi0zOA Bug: 277148566 Test: manual Change-Id: I0dc6678bfa45a4f84f38bf810433dd1e4432ba4a
This commit is contained in:
committed by
Daniel Huang
parent
3f16621b86
commit
94d5d00a1a
@@ -18,6 +18,7 @@ package com.android.settings.inputmethod;
|
|||||||
|
|
||||||
import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
|
import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -52,11 +53,13 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class ModifierKeysPickerDialogFragment extends DialogFragment {
|
public class ModifierKeysPickerDialogFragment extends DialogFragment {
|
||||||
|
|
||||||
|
static final String DEFAULT_KEY = "default_key";
|
||||||
|
static final String SELECTION_KEY = "delection_key";
|
||||||
|
|
||||||
private Preference mPreference;
|
private Preference mPreference;
|
||||||
private String mKeyDefaultName;
|
private String mKeyDefaultName;
|
||||||
private String mKeyFocus;
|
private String mKeyFocus;
|
||||||
private Context mContext;
|
private Activity mActivity;
|
||||||
private InputManager mIm;
|
|
||||||
|
|
||||||
private List<int[]> mRemappableKeyList =
|
private List<int[]> mRemappableKeyList =
|
||||||
new ArrayList<>(Arrays.asList(
|
new ArrayList<>(Arrays.asList(
|
||||||
@@ -67,36 +70,41 @@ public class ModifierKeysPickerDialogFragment extends DialogFragment {
|
|||||||
|
|
||||||
private Map<String, int[]> mRemappableKeyMap = new HashMap<>();
|
private Map<String, int[]> mRemappableKeyMap = new HashMap<>();
|
||||||
|
|
||||||
public ModifierKeysPickerDialogFragment() {
|
public ModifierKeysPickerDialogFragment() {}
|
||||||
}
|
|
||||||
|
|
||||||
public ModifierKeysPickerDialogFragment(Preference preference, InputManager inputManager) {
|
@Override
|
||||||
mPreference = preference;
|
public void onSaveInstanceState(Bundle savedInstanceState) {
|
||||||
mKeyDefaultName = preference.getTitle().toString();
|
savedInstanceState.putString(SELECTION_KEY, mKeyFocus);
|
||||||
mKeyFocus = preference.getSummary().toString();
|
super.onSaveInstanceState(savedInstanceState);
|
||||||
mIm = inputManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
super.onCreateDialog(savedInstanceState);
|
super.onCreateDialog(savedInstanceState);
|
||||||
mContext = getActivity();
|
|
||||||
|
mActivity = getActivity();
|
||||||
|
InputManager inputManager = mActivity.getSystemService(InputManager.class);
|
||||||
|
mKeyDefaultName = getArguments().getString(DEFAULT_KEY);
|
||||||
|
mKeyFocus = getArguments().getString(SELECTION_KEY);
|
||||||
|
if (savedInstanceState != null) {
|
||||||
|
mKeyFocus = savedInstanceState.getString(SELECTION_KEY);
|
||||||
|
}
|
||||||
List<String> modifierKeys = new ArrayList<String>(Arrays.asList(
|
List<String> modifierKeys = new ArrayList<String>(Arrays.asList(
|
||||||
mContext.getString(R.string.modifier_keys_caps_lock),
|
mActivity.getString(R.string.modifier_keys_caps_lock),
|
||||||
mContext.getString(R.string.modifier_keys_ctrl),
|
mActivity.getString(R.string.modifier_keys_ctrl),
|
||||||
mContext.getString(R.string.modifier_keys_meta),
|
mActivity.getString(R.string.modifier_keys_meta),
|
||||||
mContext.getString(R.string.modifier_keys_alt)));
|
mActivity.getString(R.string.modifier_keys_alt)));
|
||||||
for (int i = 0; i < modifierKeys.size(); i++) {
|
for (int i = 0; i < modifierKeys.size(); i++) {
|
||||||
mRemappableKeyMap.put(modifierKeys.get(i), mRemappableKeyList.get(i));
|
mRemappableKeyMap.put(modifierKeys.get(i), mRemappableKeyList.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
View dialoglayout =
|
View dialoglayout =
|
||||||
LayoutInflater.from(mContext).inflate(R.layout.modifier_key_picker_dialog, null);
|
LayoutInflater.from(mActivity).inflate(R.layout.modifier_key_picker_dialog, null);
|
||||||
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mActivity);
|
||||||
dialogBuilder.setView(dialoglayout);
|
dialogBuilder.setView(dialoglayout);
|
||||||
|
|
||||||
TextView summary = dialoglayout.findViewById(R.id.modifier_key_picker_summary);
|
TextView summary = dialoglayout.findViewById(R.id.modifier_key_picker_summary);
|
||||||
CharSequence summaryText = mContext.getString(
|
CharSequence summaryText = mActivity.getString(
|
||||||
R.string.modifier_keys_picker_summary, mKeyDefaultName);
|
R.string.modifier_keys_picker_summary, mKeyDefaultName);
|
||||||
summary.setText(summaryText);
|
summary.setText(summaryText);
|
||||||
|
|
||||||
@@ -119,14 +127,14 @@ public class ModifierKeysPickerDialogFragment extends DialogFragment {
|
|||||||
Spannable itemSummary;
|
Spannable itemSummary;
|
||||||
if (selectedItem.equals(mKeyDefaultName)) {
|
if (selectedItem.equals(mKeyDefaultName)) {
|
||||||
itemSummary = new SpannableString(
|
itemSummary = new SpannableString(
|
||||||
mContext.getString(R.string.modifier_keys_default_summary));
|
mActivity.getString(R.string.modifier_keys_default_summary));
|
||||||
itemSummary.setSpan(
|
itemSummary.setSpan(
|
||||||
new ForegroundColorSpan(getColorOfTextColorSecondary()),
|
new ForegroundColorSpan(getColorOfTextColorSecondary()),
|
||||||
0, itemSummary.length(), 0);
|
0, itemSummary.length(), 0);
|
||||||
// Set keys to default.
|
// Set keys to default.
|
||||||
int[] keys = mRemappableKeyMap.get(mKeyDefaultName);
|
int[] keys = mRemappableKeyMap.get(mKeyDefaultName);
|
||||||
for (int i = 0; i < keys.length; i++) {
|
for (int i = 0; i < keys.length; i++) {
|
||||||
mIm.remapModifierKey(keys[i], keys[i]);
|
inputManager.remapModifierKey(keys[i], keys[i]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
itemSummary = new SpannableString(selectedItem);
|
itemSummary = new SpannableString(selectedItem);
|
||||||
@@ -136,29 +144,29 @@ public class ModifierKeysPickerDialogFragment extends DialogFragment {
|
|||||||
int[] fromKeys = mRemappableKeyMap.get(mKeyDefaultName);
|
int[] fromKeys = mRemappableKeyMap.get(mKeyDefaultName);
|
||||||
int[] toKeys = mRemappableKeyMap.get(selectedItem);
|
int[] toKeys = mRemappableKeyMap.get(selectedItem);
|
||||||
// CAPS_LOCK only one key, so always choose the left key for remapping.
|
// CAPS_LOCK only one key, so always choose the left key for remapping.
|
||||||
if (isKeyCapsLock(mContext, mKeyDefaultName)) {
|
if (isKeyCapsLock(mActivity, mKeyDefaultName)) {
|
||||||
mIm.remapModifierKey(fromKeys[0], toKeys[0]);
|
inputManager.remapModifierKey(fromKeys[0], toKeys[0]);
|
||||||
}
|
}
|
||||||
// Remap KEY_LEFT and KEY_RIGHT to CAPS_LOCK.
|
// Remap KEY_LEFT and KEY_RIGHT to CAPS_LOCK.
|
||||||
if (!isKeyCapsLock(mContext, mKeyDefaultName)
|
if (!isKeyCapsLock(mActivity, mKeyDefaultName)
|
||||||
&& isKeyCapsLock(mContext, selectedItem)) {
|
&& isKeyCapsLock(mActivity, selectedItem)) {
|
||||||
mIm.remapModifierKey(fromKeys[0], toKeys[0]);
|
inputManager.remapModifierKey(fromKeys[0], toKeys[0]);
|
||||||
mIm.remapModifierKey(fromKeys[1], toKeys[0]);
|
inputManager.remapModifierKey(fromKeys[1], toKeys[0]);
|
||||||
}
|
}
|
||||||
// Auto handle left and right keys remapping.
|
// Auto handle left and right keys remapping.
|
||||||
if (!isKeyCapsLock(mContext, mKeyDefaultName)
|
if (!isKeyCapsLock(mActivity, mKeyDefaultName)
|
||||||
&& !isKeyCapsLock(mContext, selectedItem)) {
|
&& !isKeyCapsLock(mActivity, selectedItem)) {
|
||||||
mIm.remapModifierKey(fromKeys[0], toKeys[0]);
|
inputManager.remapModifierKey(fromKeys[0], toKeys[0]);
|
||||||
mIm.remapModifierKey(fromKeys[1], toKeys[1]);
|
inputManager.remapModifierKey(fromKeys[1], toKeys[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mPreference.setSummary(itemSummary);
|
dismiss();
|
||||||
modifierKeyDialog.dismiss();
|
mActivity.recreate();
|
||||||
});
|
});
|
||||||
|
|
||||||
Button cancelButton = dialoglayout.findViewById(R.id.modifier_key_cancel_button);
|
Button cancelButton = dialoglayout.findViewById(R.id.modifier_key_cancel_button);
|
||||||
cancelButton.setOnClickListener(v -> {
|
cancelButton.setOnClickListener(v -> {
|
||||||
modifierKeyDialog.dismiss();
|
dismiss();
|
||||||
});
|
});
|
||||||
|
|
||||||
final Window window = modifierKeyDialog.getWindow();
|
final Window window = modifierKeyDialog.getWindow();
|
||||||
@@ -207,16 +215,17 @@ public class ModifierKeysPickerDialogFragment extends DialogFragment {
|
|||||||
@Override
|
@Override
|
||||||
public View getView(int i, View view, ViewGroup viewGroup) {
|
public View getView(int i, View view, ViewGroup viewGroup) {
|
||||||
if (view == null) {
|
if (view == null) {
|
||||||
view = LayoutInflater.from(mContext).inflate(R.layout.modifier_key_item, null);
|
view = LayoutInflater.from(mActivity).inflate(R.layout.modifier_key_item, null);
|
||||||
}
|
}
|
||||||
TextView textView = view.findViewById(R.id.modifier_key_text);
|
TextView textView = view.findViewById(R.id.modifier_key_text);
|
||||||
ImageView checkIcon = view.findViewById(R.id.modifier_key_check_icon);
|
ImageView checkIcon = view.findViewById(R.id.modifier_key_check_icon);
|
||||||
textView.setText(mList.get(i));
|
textView.setText(mList.get(i));
|
||||||
if (mCurrentItem == i) {
|
if (mCurrentItem == i) {
|
||||||
|
mKeyFocus = mList.get(i);
|
||||||
textView.setTextColor(getColorOfColorAccentPrimaryVariant());
|
textView.setTextColor(getColorOfColorAccentPrimaryVariant());
|
||||||
checkIcon.setImageAlpha(255);
|
checkIcon.setImageAlpha(255);
|
||||||
view.setBackground(
|
view.setBackground(
|
||||||
mContext.getDrawable(R.drawable.modifier_key_lisetview_background));
|
mActivity.getDrawable(R.drawable.modifier_key_lisetview_background));
|
||||||
} else {
|
} else {
|
||||||
textView.setTextColor(getColorOfTextColorPrimary());
|
textView.setTextColor(getColorOfTextColorPrimary());
|
||||||
checkIcon.setImageAlpha(0);
|
checkIcon.setImageAlpha(0);
|
||||||
@@ -235,15 +244,15 @@ public class ModifierKeysPickerDialogFragment extends DialogFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int getColorOfTextColorPrimary() {
|
private int getColorOfTextColorPrimary() {
|
||||||
return Utils.getColorAttrDefaultColor(mContext, android.R.attr.textColorPrimary);
|
return Utils.getColorAttrDefaultColor(mActivity, android.R.attr.textColorPrimary);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getColorOfTextColorSecondary() {
|
private int getColorOfTextColorSecondary() {
|
||||||
return Utils.getColorAttrDefaultColor(mContext, android.R.attr.textColorSecondary);
|
return Utils.getColorAttrDefaultColor(mActivity, android.R.attr.textColorSecondary);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getColorOfColorAccentPrimaryVariant() {
|
private int getColorOfColorAccentPrimaryVariant() {
|
||||||
return Utils.getColorAttrDefaultColor(
|
return Utils.getColorAttrDefaultColor(
|
||||||
mContext, com.android.internal.R.attr.materialColorPrimaryContainer);
|
mActivity, com.android.internal.R.attr.materialColorPrimaryContainer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,12 +18,12 @@ package com.android.settings.inputmethod;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.input.InputManager;
|
import android.hardware.input.InputManager;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.text.Spannable;
|
import android.text.Spannable;
|
||||||
import android.text.SpannableString;
|
import android.text.SpannableString;
|
||||||
import android.text.style.ForegroundColorSpan;
|
import android.text.style.ForegroundColorSpan;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
|
|
||||||
import androidx.fragment.app.DialogFragment;
|
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
@@ -41,8 +41,8 @@ import java.util.Objects;
|
|||||||
|
|
||||||
public class ModifierKeysPreferenceController extends BasePreferenceController {
|
public class ModifierKeysPreferenceController extends BasePreferenceController {
|
||||||
|
|
||||||
private static String KEY_TAG = "modifier_keys_dialog_tag";
|
private static final String KEY_TAG = "modifier_keys_dialog_tag";
|
||||||
private static String KEY_RESTORE_PREFERENCE = "modifier_keys_restore";
|
private static final String KEY_RESTORE_PREFERENCE = "modifier_keys_restore";
|
||||||
|
|
||||||
private static final String KEY_PREFERENCE_CAPS_LOCK = "modifier_keys_caps_lock";
|
private static final String KEY_PREFERENCE_CAPS_LOCK = "modifier_keys_caps_lock";
|
||||||
private static final String KEY_PREFERENCE_CTRL = "modifier_keys_ctrl";
|
private static final String KEY_PREFERENCE_CTRL = "modifier_keys_ctrl";
|
||||||
@@ -52,6 +52,7 @@ public class ModifierKeysPreferenceController extends BasePreferenceController {
|
|||||||
private Fragment mParent;
|
private Fragment mParent;
|
||||||
private FragmentManager mFragmentManager;
|
private FragmentManager mFragmentManager;
|
||||||
private final InputManager mIm;
|
private final InputManager mIm;
|
||||||
|
private PreferenceScreen mScreen;
|
||||||
|
|
||||||
private final List<Integer> mRemappableKeys = new ArrayList<>(
|
private final List<Integer> mRemappableKeys = new ArrayList<>(
|
||||||
Arrays.asList(
|
Arrays.asList(
|
||||||
@@ -82,40 +83,39 @@ public class ModifierKeysPreferenceController extends BasePreferenceController {
|
|||||||
@Override
|
@Override
|
||||||
public void displayPreference(PreferenceScreen screen) {
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
super.displayPreference(screen);
|
super.displayPreference(screen);
|
||||||
|
|
||||||
if (mParent == null) {
|
if (mParent == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
mScreen = screen;
|
||||||
|
refreshUi();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void refreshUi() {
|
||||||
for (Map.Entry<Integer, Integer> entry : mIm.getModifierKeyRemapping().entrySet()) {
|
for (Map.Entry<Integer, Integer> entry : mIm.getModifierKeyRemapping().entrySet()) {
|
||||||
int fromKey = entry.getKey();
|
int fromKey = entry.getKey();
|
||||||
int toKey = entry.getValue();
|
int toKey = entry.getValue();
|
||||||
int index = mRemappableKeys.indexOf(toKey);
|
int index = mRemappableKeys.indexOf(toKey);
|
||||||
|
|
||||||
if (isCtrl(fromKey) && mRemappableKeys.contains(toKey)) {
|
if (isCtrl(fromKey) && mRemappableKeys.contains(toKey)) {
|
||||||
Preference preference = screen.findPreference(KEY_PREFERENCE_CTRL);
|
Preference preference = mScreen.findPreference(KEY_PREFERENCE_CTRL);
|
||||||
preference.setSummary(changeSummaryColor(mKeyNames[index]));
|
preference.setSummary(changeSummaryColor(mKeyNames[index]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isMeta(fromKey) && mRemappableKeys.contains(toKey)) {
|
if (isMeta(fromKey) && mRemappableKeys.contains(toKey)) {
|
||||||
Preference preference = screen.findPreference(KEY_PREFERENCE_META);
|
Preference preference = mScreen.findPreference(KEY_PREFERENCE_META);
|
||||||
preference.setSummary(changeSummaryColor(mKeyNames[index]));
|
preference.setSummary(changeSummaryColor(mKeyNames[index]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isAlt(fromKey) && mRemappableKeys.contains(toKey)) {
|
if (isAlt(fromKey) && mRemappableKeys.contains(toKey)) {
|
||||||
Preference preference = screen.findPreference(KEY_PREFERENCE_ALT);
|
Preference preference = mScreen.findPreference(KEY_PREFERENCE_ALT);
|
||||||
preference.setSummary(changeSummaryColor(mKeyNames[index]));
|
preference.setSummary(changeSummaryColor(mKeyNames[index]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isCapLock(fromKey) && mRemappableKeys.contains(toKey)) {
|
if (isCapLock(fromKey) && mRemappableKeys.contains(toKey)) {
|
||||||
Preference preference = screen.findPreference(KEY_PREFERENCE_CAPS_LOCK);
|
Preference preference = mScreen.findPreference(KEY_PREFERENCE_CAPS_LOCK);
|
||||||
preference.setSummary(changeSummaryColor(mKeyNames[index]));
|
preference.setSummary(changeSummaryColor(mKeyNames[index]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The dialog screen depends on the previous selected key's fragment.
|
|
||||||
// In the rotation scenario, we should remove the previous dialog screen first.
|
|
||||||
clearPreviousDialog();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -133,19 +133,18 @@ public class ModifierKeysPreferenceController extends BasePreferenceController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void showModifierKeysDialog(Preference preference) {
|
private void showModifierKeysDialog(Preference preference) {
|
||||||
ModifierKeysPickerDialogFragment fragment =
|
|
||||||
new ModifierKeysPickerDialogFragment(preference, mIm);
|
|
||||||
fragment.setTargetFragment(mParent, 0);
|
|
||||||
fragment.show(mFragmentManager, KEY_TAG);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void clearPreviousDialog() {
|
|
||||||
mFragmentManager = mParent.getFragmentManager();
|
mFragmentManager = mParent.getFragmentManager();
|
||||||
DialogFragment preKeysDialogFragment =
|
ModifierKeysPickerDialogFragment fragment = new ModifierKeysPickerDialogFragment();
|
||||||
(DialogFragment) mFragmentManager.findFragmentByTag(KEY_TAG);
|
fragment.setTargetFragment(mParent, 0);
|
||||||
if (preKeysDialogFragment != null) {
|
Bundle bundle = new Bundle();
|
||||||
preKeysDialogFragment.dismiss();
|
bundle.putString(
|
||||||
}
|
ModifierKeysPickerDialogFragment.DEFAULT_KEY,
|
||||||
|
preference.getTitle().toString());
|
||||||
|
bundle.putString(
|
||||||
|
ModifierKeysPickerDialogFragment.SELECTION_KEY,
|
||||||
|
preference.getSummary().toString());
|
||||||
|
fragment.setArguments(bundle);
|
||||||
|
fragment.show(mFragmentManager, KEY_TAG);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Spannable changeSummaryColor(String summary) {
|
private Spannable changeSummaryColor(String summary) {
|
||||||
|
@@ -18,25 +18,19 @@ package com.android.settings.inputmethod;
|
|||||||
|
|
||||||
import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
|
import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.Context;
|
|
||||||
import android.hardware.input.InputManager;
|
import android.hardware.input.InputManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.Spannable;
|
|
||||||
import android.text.SpannableString;
|
|
||||||
import android.text.style.ForegroundColorSpan;
|
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
|
||||||
import androidx.fragment.app.DialogFragment;
|
import androidx.fragment.app.DialogFragment;
|
||||||
import androidx.preference.Preference;
|
|
||||||
import androidx.preference.PreferenceScreen;
|
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settingslib.Utils;
|
|
||||||
|
|
||||||
public class ModifierKeysResetDialogFragment extends DialogFragment {
|
public class ModifierKeysResetDialogFragment extends DialogFragment {
|
||||||
private static final String MODIFIER_KEYS_CAPS_LOCK = "modifier_keys_caps_lock";
|
private static final String MODIFIER_KEYS_CAPS_LOCK = "modifier_keys_caps_lock";
|
||||||
@@ -44,41 +38,36 @@ public class ModifierKeysResetDialogFragment extends DialogFragment {
|
|||||||
private static final String MODIFIER_KEYS_META = "modifier_keys_meta";
|
private static final String MODIFIER_KEYS_META = "modifier_keys_meta";
|
||||||
private static final String MODIFIER_KEYS_ALT = "modifier_keys_alt";
|
private static final String MODIFIER_KEYS_ALT = "modifier_keys_alt";
|
||||||
|
|
||||||
private PreferenceScreen mScreen;
|
|
||||||
private InputManager mIm;
|
|
||||||
private String[] mKeys = {
|
private String[] mKeys = {
|
||||||
MODIFIER_KEYS_CAPS_LOCK,
|
MODIFIER_KEYS_CAPS_LOCK,
|
||||||
MODIFIER_KEYS_CTRL,
|
MODIFIER_KEYS_CTRL,
|
||||||
MODIFIER_KEYS_META,
|
MODIFIER_KEYS_META,
|
||||||
MODIFIER_KEYS_ALT};
|
MODIFIER_KEYS_ALT};
|
||||||
|
|
||||||
public ModifierKeysResetDialogFragment() {
|
public ModifierKeysResetDialogFragment() {}
|
||||||
}
|
|
||||||
|
|
||||||
public ModifierKeysResetDialogFragment(PreferenceScreen screen, InputManager inputManager) {
|
|
||||||
mScreen = screen;
|
|
||||||
mIm = inputManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
super.onCreateDialog(savedInstanceState);
|
super.onCreateDialog(savedInstanceState);
|
||||||
Context mContext = getActivity();
|
|
||||||
|
Activity activity = getActivity();
|
||||||
|
InputManager inputManager = activity.getSystemService(InputManager.class);
|
||||||
View dialoglayout =
|
View dialoglayout =
|
||||||
LayoutInflater.from(mContext).inflate(R.layout.modifier_key_reset_dialog, null);
|
LayoutInflater.from(activity).inflate(R.layout.modifier_key_reset_dialog, null);
|
||||||
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
|
||||||
dialogBuilder.setView(dialoglayout);
|
dialogBuilder.setView(dialoglayout);
|
||||||
AlertDialog modifierKeyResetDialog = dialogBuilder.create();
|
AlertDialog modifierKeyResetDialog = dialogBuilder.create();
|
||||||
|
|
||||||
Button restoreButton = dialoglayout.findViewById(R.id.modifier_key_reset_restore_button);
|
Button restoreButton = dialoglayout.findViewById(R.id.modifier_key_reset_restore_button);
|
||||||
restoreButton.setOnClickListener(v -> {
|
restoreButton.setOnClickListener(v -> {
|
||||||
resetToDefault();
|
inputManager.clearAllModifierKeyRemappings();
|
||||||
modifierKeyResetDialog.dismiss();
|
dismiss();
|
||||||
|
activity.recreate();
|
||||||
});
|
});
|
||||||
|
|
||||||
Button cancelButton = dialoglayout.findViewById(R.id.modifier_key_reset_cancel_button);
|
Button cancelButton = dialoglayout.findViewById(R.id.modifier_key_reset_cancel_button);
|
||||||
cancelButton.setOnClickListener(v -> {
|
cancelButton.setOnClickListener(v -> {
|
||||||
modifierKeyResetDialog.dismiss();
|
dismiss();
|
||||||
});
|
});
|
||||||
|
|
||||||
final Window window = modifierKeyResetDialog.getWindow();
|
final Window window = modifierKeyResetDialog.getWindow();
|
||||||
@@ -86,25 +75,4 @@ public class ModifierKeysResetDialogFragment extends DialogFragment {
|
|||||||
|
|
||||||
return modifierKeyResetDialog;
|
return modifierKeyResetDialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetToDefault() {
|
|
||||||
Context mContext = getActivity();
|
|
||||||
for (int i = 0; i < mKeys.length; i++) {
|
|
||||||
Preference preference = mScreen.findPreference(mKeys[i]);
|
|
||||||
Spannable title = new SpannableString(
|
|
||||||
mContext.getString(R.string.modifier_keys_default_summary));
|
|
||||||
title.setSpan(
|
|
||||||
new ForegroundColorSpan(getColorOfTextColorSecondary()),
|
|
||||||
0, title.length(), 0);
|
|
||||||
preference.setSummary(title);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mIm != null) {
|
|
||||||
mIm.clearAllModifierKeyRemappings();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getColorOfTextColorSecondary() {
|
|
||||||
return Utils.getColorAttrDefaultColor(getActivity(), android.R.attr.textColorSecondary);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -17,12 +17,10 @@
|
|||||||
package com.android.settings.inputmethod;
|
package com.android.settings.inputmethod;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.input.InputManager;
|
|
||||||
import android.text.Spannable;
|
import android.text.Spannable;
|
||||||
import android.text.SpannableString;
|
import android.text.SpannableString;
|
||||||
import android.text.style.ForegroundColorSpan;
|
import android.text.style.ForegroundColorSpan;
|
||||||
|
|
||||||
import androidx.fragment.app.DialogFragment;
|
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
@@ -34,16 +32,14 @@ import com.android.settingslib.Utils;
|
|||||||
|
|
||||||
public class ModifierKeysRestorePreferenceController extends BasePreferenceController {
|
public class ModifierKeysRestorePreferenceController extends BasePreferenceController {
|
||||||
|
|
||||||
private static String KEY_TAG = "modifier_keys_dialog_tag";
|
private static final String KEY_TAG = "modifier_keys_restore_dialog_tag";
|
||||||
|
|
||||||
private Fragment mParent;
|
private Fragment mParent;
|
||||||
private FragmentManager mFragmentManager;
|
private FragmentManager mFragmentManager;
|
||||||
private PreferenceScreen mScreen;
|
private PreferenceScreen mScreen;
|
||||||
private final InputManager mIm;
|
|
||||||
|
|
||||||
public ModifierKeysRestorePreferenceController(Context context, String key) {
|
public ModifierKeysRestorePreferenceController(Context context, String key) {
|
||||||
super(context, key);
|
super(context, key);
|
||||||
mIm = context.getSystemService(InputManager.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFragment(Fragment parent) {
|
public void setFragment(Fragment parent) {
|
||||||
@@ -57,9 +53,6 @@ public class ModifierKeysRestorePreferenceController extends BasePreferenceContr
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mScreen = screen;
|
mScreen = screen;
|
||||||
// The dialog screen depends on the previous selected key's fragment.
|
|
||||||
// In the rotation scenario, we should remove the previous dialog first.
|
|
||||||
clearPreviousDialog();
|
|
||||||
setResetKeyColor();
|
setResetKeyColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,8 +71,8 @@ public class ModifierKeysRestorePreferenceController extends BasePreferenceContr
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void showResetDialog() {
|
private void showResetDialog() {
|
||||||
ModifierKeysResetDialogFragment fragment =
|
mFragmentManager = mParent.getFragmentManager();
|
||||||
new ModifierKeysResetDialogFragment(mScreen, mIm);
|
ModifierKeysResetDialogFragment fragment = new ModifierKeysResetDialogFragment();
|
||||||
fragment.setTargetFragment(mParent, 0);
|
fragment.setTargetFragment(mParent, 0);
|
||||||
fragment.show(mFragmentManager, KEY_TAG);
|
fragment.show(mFragmentManager, KEY_TAG);
|
||||||
}
|
}
|
||||||
@@ -98,13 +91,4 @@ public class ModifierKeysRestorePreferenceController extends BasePreferenceContr
|
|||||||
return Utils.getColorAttrDefaultColor(
|
return Utils.getColorAttrDefaultColor(
|
||||||
mParent.getActivity(), com.android.internal.R.attr.materialColorPrimaryContainer);
|
mParent.getActivity(), com.android.internal.R.attr.materialColorPrimaryContainer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearPreviousDialog() {
|
|
||||||
mFragmentManager = mParent.getFragmentManager();
|
|
||||||
DialogFragment preResetDialogFragment =
|
|
||||||
(DialogFragment) mFragmentManager.findFragmentByTag(KEY_TAG);
|
|
||||||
if (preResetDialogFragment != null) {
|
|
||||||
preResetDialogFragment.dismiss();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user