Unify Tick Preference from Physic keyboard and Regional preference

- Also change icon color from colorAccentPrimaryVarient to colorAccentPrimary

Bug: b/272398108
Bug: b/264476709
Test: Manual test.
Change-Id: Ide602c6fb9501b832df646692ec618be8a76e7b9
This commit is contained in:
tom hsu
2023-03-29 21:28:25 +08:00
parent e78b3d3f7e
commit 23359bc0e9
8 changed files with 42 additions and 102 deletions

View File

@@ -18,7 +18,7 @@
android:height="24dp" android:height="24dp"
android:viewportWidth="24.0" android:viewportWidth="24.0"
android:viewportHeight="24.0" android:viewportHeight="24.0"
android:tint="?androidprv:attr/colorAccentPrimaryVariant"> android:tint="?androidprv:attr/colorAccent">
<path <path
android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z" android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"
android:fillColor="@android:color/white"/> android:fillColor="@android:color/white"/>

View File

@@ -15,7 +15,7 @@
--> -->
<ImageView <ImageView
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard_check_icon" android:id="@+id/check_icon"
android:src="@drawable/ic_check_24dp" android:src="@drawable/ic_check_24dp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@@ -28,6 +28,7 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.widget.TickButtonPreference;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop; import com.android.settingslib.core.lifecycle.events.OnStop;
@@ -38,7 +39,7 @@ import java.util.Map;
public class NewKeyboardLayoutPickerController extends BasePreferenceController implements public class NewKeyboardLayoutPickerController extends BasePreferenceController implements
InputManager.InputDeviceListener, LifecycleObserver, OnStart, OnStop { InputManager.InputDeviceListener, LifecycleObserver, OnStart, OnStop {
private final InputManager mIm; private final InputManager mIm;
private final Map<KeyboardLayoutPreference, KeyboardLayout> mPreferenceMap; private final Map<TickButtonPreference, KeyboardLayout> mPreferenceMap;
private Fragment mParent; private Fragment mParent;
private int mInputDeviceId; private int mInputDeviceId;
@@ -102,16 +103,15 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
@Override @Override
public boolean handlePreferenceTreeClick(Preference preference) { public boolean handlePreferenceTreeClick(Preference preference) {
if (!(preference instanceof TickButtonPreference)) {
if (!(preference instanceof KeyboardLayoutPreference)) {
return false; return false;
} }
final KeyboardLayoutPreference pref = (KeyboardLayoutPreference) preference; final TickButtonPreference pref = (TickButtonPreference) preference;
pref.setCheckMark(true); pref.setSelected(true);
if (mPreviousSelection != null && !mPreviousSelection.equals(preference.getKey())) { if (mPreviousSelection != null && !mPreviousSelection.equals(preference.getKey())) {
KeyboardLayoutPreference preSelectedPref = mScreen.findPreference(mPreviousSelection); TickButtonPreference preSelectedPref = mScreen.findPreference(mPreviousSelection);
preSelectedPref.setCheckMark(false); preSelectedPref.setSelected(false);
} }
setLayout(pref); setLayout(pref);
mPreviousSelection = preference.getKey(); mPreviousSelection = preference.getKey();
@@ -140,12 +140,13 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
return; return;
} }
for (KeyboardLayout layout : mKeyboardLayouts) { for (KeyboardLayout layout : mKeyboardLayouts) {
final KeyboardLayoutPreference pref; final TickButtonPreference pref;
pref = new TickButtonPreference(mScreen.getContext());
pref.setTitle(layout.getLabel());
if (mLayout.equals(layout.getLabel())) { if (mLayout.equals(layout.getLabel())) {
pref = new KeyboardLayoutPreference(mScreen.getContext(), layout.getLabel(), true); pref.setSelected(true);
mPreviousSelection = layout.getDescriptor(); mPreviousSelection = layout.getDescriptor();
} else {
pref = new KeyboardLayoutPreference(mScreen.getContext(), layout.getLabel(), false);
} }
pref.setKey(layout.getDescriptor()); pref.setKey(layout.getDescriptor());
mScreen.addPreference(pref); mScreen.addPreference(pref);
@@ -153,7 +154,7 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
} }
} }
private void setLayout(KeyboardLayoutPreference preference) { private void setLayout(TickButtonPreference preference) {
mIm.setKeyboardLayoutForInputDevice( mIm.setKeyboardLayoutForInputDevice(
mInputDeviceIdentifier, mInputDeviceIdentifier,
mUserId, mUserId,

View File

@@ -34,6 +34,7 @@ import com.android.internal.app.LocaleStore;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.SubSettingLauncher; import com.android.settings.core.SubSettingLauncher;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.widget.TickButtonPreference;
import java.util.Locale; import java.util.Locale;
@@ -138,7 +139,7 @@ public class NumberingSystemItemController extends BasePreferenceController {
String key = supportedLocale.getUnicodeLocaleType( String key = supportedLocale.getUnicodeLocaleType(
ExtensionTypes.NUMBERING_SYSTEM); ExtensionTypes.NUMBERING_SYSTEM);
pref.setKey(key == null ? RegionalPreferencesDataUtils.DEFAULT_VALUE : key); pref.setKey(key == null ? RegionalPreferencesDataUtils.DEFAULT_VALUE : key);
pref.setTickEnable(isSameNumberingSystem(targetLocale, supportedLocale)); pref.setSelected(isSameNumberingSystem(targetLocale, supportedLocale));
screen.addPreference(pref); screen.addPreference(pref);
} }
} }
@@ -162,7 +163,7 @@ public class NumberingSystemItemController extends BasePreferenceController {
TickButtonPreference pref = (TickButtonPreference) mPreferenceScreen.getPreference(i); TickButtonPreference pref = (TickButtonPreference) mPreferenceScreen.getPreference(i);
Log.i(TAG, "[onPreferenceClick] key is " + pref.getKey()); Log.i(TAG, "[onPreferenceClick] key is " + pref.getKey());
if (pref.getKey().equals(preference.getKey())) { if (pref.getKey().equals(preference.getKey())) {
pref.setTickEnable(true); pref.setSelected(true);
Locale updatedLocale = Locale updatedLocale =
saveNumberingSystemToLocale( saveNumberingSystemToLocale(
Locale.forLanguageTag(mSelectedLanguage), pref.getKey()); Locale.forLanguageTag(mSelectedLanguage), pref.getKey());
@@ -176,7 +177,7 @@ public class NumberingSystemItemController extends BasePreferenceController {
mParentFragment.setArguments(bundle); mParentFragment.setArguments(bundle);
continue; continue;
} }
pref.setTickEnable(false); pref.setSelected(false);
} }
} }

View File

@@ -29,6 +29,7 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.widget.TickButtonPreference;
/** A fragment to include each kind of regional preferences. */ /** A fragment to include each kind of regional preferences. */
public class RegionalPreferencesFragment extends SettingsPreferenceFragment { public class RegionalPreferencesFragment extends SettingsPreferenceFragment {
@@ -63,7 +64,7 @@ public class RegionalPreferencesFragment extends SettingsPreferenceFragment {
TickButtonPreference pref = (TickButtonPreference) mPreferenceScreen.getPreference(i); TickButtonPreference pref = (TickButtonPreference) mPreferenceScreen.getPreference(i);
Log.i(TAG, "[onPreferenceClick] key is " + pref.getKey()); Log.i(TAG, "[onPreferenceClick] key is " + pref.getKey());
if (pref.getKey().equals(preference.getKey())) { if (pref.getKey().equals(preference.getKey())) {
pref.setTickEnable(true); pref.setSelected(true);
RegionalPreferencesDataUtils.savePreference( RegionalPreferencesDataUtils.savePreference(
getPrefContext(), getPrefContext(),
mType, mType,
@@ -72,7 +73,7 @@ public class RegionalPreferencesFragment extends SettingsPreferenceFragment {
? null : preference.getKey()); ? null : preference.getKey());
continue; continue;
} }
pref.setTickEnable(false); pref.setSelected(false);
} }
return true; return true;
} }
@@ -115,7 +116,7 @@ public class RegionalPreferencesFragment extends SettingsPreferenceFragment {
String value = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData( String value = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData(
getPrefContext(), mType); getPrefContext(), mType);
pref.setKey(item); pref.setKey(item);
pref.setTickEnable(!value.isEmpty() && item.equals(value)); pref.setSelected(!value.isEmpty() && item.equals(value));
mPreferenceScreen.addPreference(pref); mPreferenceScreen.addPreference(pref);
} }
return super.onCreateView(inflater, container, savedInstanceState); return super.onCreateView(inflater, container, savedInstanceState);

View File

@@ -1,69 +0,0 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.regionalpreferences;
import android.content.Context;
import android.view.View;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import com.android.settingslib.widget.TwoTargetPreference;
/** A preference with tick button */
public class TickButtonPreference extends TwoTargetPreference {
private static final String TAG = TickButtonPreference.class.getSimpleName();
private boolean mIsTickEnabled;
private View mWidgetFrame;
private View mDivider;
public TickButtonPreference(Context context) {
super(context, null);
}
/** Set this preference to be selected. */
public void setTickEnable(boolean isEnable) {
mIsTickEnabled = isEnable;
if (mWidgetFrame != null) {
mWidgetFrame.setVisibility(isEnable ? View.VISIBLE : View.INVISIBLE);
}
}
/** Check if this preference is selected. */
public boolean isTickEnabled() {
return mIsTickEnabled;
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
mDivider = holder.findViewById(R.id.two_target_divider);
mWidgetFrame = holder.findViewById(android.R.id.widget_frame);
if (mDivider != null) {
mDivider.setVisibility(View.GONE);
}
if (mWidgetFrame != null) {
mWidgetFrame.setVisibility(mIsTickEnabled ? View.VISIBLE : View.INVISIBLE);
}
}
@Override
protected int getSecondTargetResId() {
super.getSecondTargetResId();
return R.layout.preference_widget_tick;
}
}

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2022 The Android Open Source Project * Copyright (C) 2023 The Android Open Source Project
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.android.settings.inputmethod; package com.android.settings.widget;
import android.content.Context; import android.content.Context;
import android.view.View; import android.view.View;
@@ -25,29 +25,33 @@ import androidx.preference.PreferenceViewHolder;
import com.android.settings.R; import com.android.settings.R;
public class KeyboardLayoutPreference extends Preference { /** A preference with tick icon. */
public class TickButtonPreference extends Preference {
private ImageView mCheckIcon; private ImageView mCheckIcon;
private boolean mIsMark; private boolean mIsSelected = false;
public KeyboardLayoutPreference(Context context, String layoutName, boolean defaultMark) { public TickButtonPreference(Context context) {
super(context); super(context);
setWidgetLayoutResource(R.layout.preference_check_icon); setWidgetLayoutResource(R.layout.preference_check_icon);
setTitle(layoutName);
mIsMark = defaultMark;
} }
@Override @Override
public void onBindViewHolder(PreferenceViewHolder holder) { public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder); super.onBindViewHolder(holder);
mCheckIcon = (ImageView) holder.findViewById(R.id.keyboard_check_icon); mCheckIcon = (ImageView) holder.findViewById(R.id.check_icon);
setCheckMark(mIsMark); setSelected(mIsSelected);
} }
public void setCheckMark(boolean isMark) { /** Set icon state.*/
public void setSelected(boolean isSelected) {
if (mCheckIcon != null) { if (mCheckIcon != null) {
mCheckIcon.setVisibility(isMark ? View.VISIBLE : View.INVISIBLE); mCheckIcon.setVisibility(isSelected ? View.VISIBLE : View.INVISIBLE);
mIsMark = isMark;
} }
mIsSelected = isSelected;
}
/** Return state of presenting icon. */
public boolean isSelected() {
return mIsSelected;
} }
} }

View File

@@ -33,6 +33,8 @@ import androidx.preference.PreferenceScreen;
import androidx.test.annotation.UiThreadTest; import androidx.test.annotation.UiThreadTest;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import com.android.settings.widget.TickButtonPreference;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;