1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.inputmethod; 18 19 import android.content.Context; 20 import android.text.Spannable; 21 import android.text.SpannableString; 22 import android.text.style.ForegroundColorSpan; 23 24 import androidx.fragment.app.Fragment; 25 import androidx.fragment.app.FragmentManager; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.settings.R; 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settingslib.Utils; 32 33 public class ModifierKeysRestorePreferenceController extends BasePreferenceController { 34 35 private static final String KEY_TAG = "modifier_keys_restore_dialog_tag"; 36 37 private Fragment mParent; 38 private FragmentManager mFragmentManager; 39 private PreferenceScreen mScreen; 40 ModifierKeysRestorePreferenceController(Context context, String key)41 public ModifierKeysRestorePreferenceController(Context context, String key) { 42 super(context, key); 43 } 44 setFragment(Fragment parent)45 public void setFragment(Fragment parent) { 46 mParent = parent; 47 } 48 49 @Override displayPreference(PreferenceScreen screen)50 public void displayPreference(PreferenceScreen screen) { 51 super.displayPreference(screen); 52 if (mParent == null) { 53 return; 54 } 55 mScreen = screen; 56 setResetKeyColor(); 57 } 58 59 @Override handlePreferenceTreeClick(Preference preference)60 public boolean handlePreferenceTreeClick(Preference preference) { 61 if (!preference.getKey().equals(getPreferenceKey())) { 62 return false; 63 } 64 showResetDialog(); 65 return true; 66 } 67 68 @Override getAvailabilityStatus()69 public int getAvailabilityStatus() { 70 return AVAILABLE; 71 } 72 showResetDialog()73 private void showResetDialog() { 74 mFragmentManager = mParent.getFragmentManager(); 75 ModifierKeysResetDialogFragment fragment = new ModifierKeysResetDialogFragment(); 76 fragment.setTargetFragment(mParent, 0); 77 fragment.show(mFragmentManager, KEY_TAG); 78 } 79 setResetKeyColor()80 private void setResetKeyColor() { 81 Preference preference = mScreen.findPreference(getPreferenceKey()); 82 Spannable title = new SpannableString( 83 mParent.getActivity().getString(R.string.modifier_keys_reset_title)); 84 title.setSpan( 85 new ForegroundColorSpan(getColorOfMaterialColorPrimary()), 86 0, title.length(), 0); 87 preference.setTitle(title); 88 } 89 getColorOfMaterialColorPrimary()90 private int getColorOfMaterialColorPrimary() { 91 return Utils.getColorAttrDefaultColor( 92 mParent.getActivity(), com.android.internal.R.attr.materialColorPrimary); 93 } 94 } 95