1 /* 2 * Copyright (C) 2017 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.tv.settings.inputmethod; 18 19 import android.annotation.UserIdInt; 20 import android.content.Context; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.view.inputmethod.InputMethodInfo; 24 import android.widget.CompoundButton; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceViewHolder; 27 import com.android.settingslib.inputmethod.InputMethodPreference; 28 import com.android.tv.settings.R; 29 import com.android.tv.settings.overlay.FlavorUtils; 30 import com.google.android.material.materialswitch.MaterialSwitch; 31 32 /** 33 * Input method preference for Android TV. 34 * 35 * <p>This preference handle the switch logic for TV. 36 */ 37 public class TVInputMethodPreference extends InputMethodPreference { TVInputMethodPreference( final Context prefContext, final InputMethodInfo imi, final boolean isAllowedByOrganization, final InputMethodPreference.OnSavePreferenceListener onSaveListener, final @UserIdInt int userId)38 public TVInputMethodPreference( 39 final Context prefContext, 40 final InputMethodInfo imi, 41 final boolean isAllowedByOrganization, 42 final InputMethodPreference.OnSavePreferenceListener onSaveListener, 43 final @UserIdInt int userId) { 44 super(prefContext, imi, isAllowedByOrganization, onSaveListener, userId); 45 if (FlavorUtils.isTwoPanel(getContext())) { 46 setWidgetLayoutResource(R.layout.preference_switch_input); 47 } 48 } 49 50 @Override onPreferenceClick(final Preference preference)51 public boolean onPreferenceClick(final Preference preference) { 52 final CompoundButton switchWidget = getSwitch(); 53 if (!switchWidget.isEnabled()) { 54 return true; 55 } 56 final boolean newValue = !isChecked(); 57 switchWidget.setChecked(isChecked()); 58 callChangeListener(newValue); 59 return true; 60 } 61 62 @Override onBindViewHolder(PreferenceViewHolder holder)63 public void onBindViewHolder(PreferenceViewHolder holder) { 64 super.onBindViewHolder(holder); 65 if (FlavorUtils.isTwoPanel(getContext())) { 66 setOnFocusChangeListenerTintChanges(holder); 67 } 68 } 69 70 /** 71 * This method sets an OnFocusChangeListener to change the statelist drawable manually because we 72 * cannot use duplicateParentState here, because it's possible for the Preference row to have 73 * state "enabled" and the switch to be "disabled" 74 */ setOnFocusChangeListenerTintChanges(PreferenceViewHolder holder)75 private void setOnFocusChangeListenerTintChanges(PreferenceViewHolder holder) { 76 ViewGroup widgetFrame = (ViewGroup) holder.findViewById(android.R.id.widget_frame); 77 View container = (View) widgetFrame.getParent(); 78 MaterialSwitch switchWidget = (MaterialSwitch) widgetFrame.findViewById(R.id.switchWidget); 79 switchWidget.setTrackTintList(getContext().getColorStateList(R.color.track_tint_selector)); 80 switchWidget.setTrackDecorationTintList( 81 getContext().getColorStateList(R.color.track_decoration_tint_selector)); 82 switchWidget.setThumbTintList(getContext().getColorStateList(R.color.thumb_tint_selector)); 83 container.setOnFocusChangeListener( 84 (v, hasFocus) -> { 85 if (hasFocus) { 86 switchWidget.setTrackTintList( 87 getContext().getColorStateList(R.color.track_tint_selector_focused)); 88 switchWidget.setTrackDecorationTintList( 89 getContext().getColorStateList(R.color.track_decoration_tint_selector_focused)); 90 switchWidget.setThumbTintList( 91 getContext().getColorStateList(R.color.thumb_tint_selector_focused)); 92 } else { 93 switchWidget.setTrackTintList( 94 getContext().getColorStateList(R.color.track_tint_selector)); 95 switchWidget.setTrackDecorationTintList( 96 getContext().getColorStateList(R.color.track_decoration_tint_selector)); 97 switchWidget.setThumbTintList( 98 getContext().getColorStateList(R.color.thumb_tint_selector)); 99 } 100 }); 101 } 102 } 103