1 /* 2 * Copyright (C) 2019 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.accessibility; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.util.TypedValue; 22 import android.view.MotionEvent; 23 import android.view.View; 24 import android.widget.CompoundButton; 25 import android.widget.LinearLayout; 26 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceViewHolder; 29 30 import com.android.settings.R; 31 32 /** 33 * Preference that can enable accessibility shortcut and let users choose which shortcut type they 34 * prefer to use. 35 */ 36 public class ShortcutPreference extends Preference { 37 38 /** 39 * Interface definition for a callback to be invoked when the toggle or settings has been 40 * clicked. 41 */ 42 public interface OnClickCallback { 43 /** 44 * Called when the settings view has been clicked. 45 * 46 * @param preference The clicked preference 47 */ onSettingsClicked(ShortcutPreference preference)48 void onSettingsClicked(ShortcutPreference preference); 49 50 /** 51 * Called when the toggle in ShortcutPreference has been clicked. 52 * 53 * @param preference The clicked preference 54 */ onToggleClicked(ShortcutPreference preference)55 void onToggleClicked(ShortcutPreference preference); 56 } 57 58 private OnClickCallback mClickCallback = null; 59 private boolean mChecked = false; 60 private boolean mSettingsEditable = true; 61 ShortcutPreference(Context context, AttributeSet attrs)62 ShortcutPreference(Context context, AttributeSet attrs) { 63 super(context, attrs); 64 setLayoutResource(R.layout.accessibility_shortcut_secondary_action); 65 setWidgetLayoutResource(androidx.preference.R.layout.preference_widget_switch_compat); 66 setIconSpaceReserved(false); 67 // Treat onSettingsClicked as this preference's click. 68 setOnPreferenceClickListener(preference -> { 69 callOnSettingsClicked(); 70 return true; 71 }); 72 } 73 74 @Override onBindViewHolder(PreferenceViewHolder holder)75 public void onBindViewHolder(PreferenceViewHolder holder) { 76 super.onBindViewHolder(holder); 77 78 final TypedValue outValue = new TypedValue(); 79 getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, 80 outValue, true); 81 82 final LinearLayout mainFrame = holder.itemView.findViewById(R.id.main_frame); 83 if (mainFrame != null) { 84 mainFrame.setOnClickListener(view -> callOnSettingsClicked()); 85 mainFrame.setClickable(mSettingsEditable); 86 mainFrame.setFocusable(mSettingsEditable); 87 mainFrame.setBackgroundResource( 88 mSettingsEditable ? outValue.resourceId : /* Remove background */ 0); 89 } 90 91 CompoundButton switchWidget = 92 holder.itemView.findViewById(androidx.preference.R.id.switchWidget); 93 if (switchWidget != null) { 94 // Consumes move events to ignore drag actions. 95 switchWidget.setOnTouchListener((v, event) -> { 96 return event.getActionMasked() == MotionEvent.ACTION_MOVE; 97 }); 98 switchWidget.setContentDescription( 99 getContext().getText(R.string.accessibility_shortcut_settings)); 100 switchWidget.setChecked(mChecked); 101 switchWidget.setOnClickListener(view -> callOnToggleClicked()); 102 switchWidget.setClickable(mSettingsEditable); 103 switchWidget.setFocusable(mSettingsEditable); 104 switchWidget.setBackgroundResource( 105 mSettingsEditable ? outValue.resourceId : /* Remove background */ 0); 106 } 107 108 final View divider = holder.itemView.findViewById(R.id.divider); 109 if (divider != null) { 110 divider.setVisibility(mSettingsEditable ? View.VISIBLE : View.GONE); 111 } 112 113 holder.itemView.setOnClickListener(view -> callOnToggleClicked()); 114 holder.itemView.setClickable(!mSettingsEditable); 115 holder.itemView.setFocusable(!mSettingsEditable); 116 } 117 118 /** 119 * Sets the shortcut toggle according to checked value. 120 * 121 * @param checked the state value of shortcut toggle 122 */ setChecked(boolean checked)123 public void setChecked(boolean checked) { 124 if (mChecked != checked) { 125 mChecked = checked; 126 notifyChanged(); 127 } 128 } 129 130 /** 131 * Gets the checked value of shortcut toggle. 132 * 133 * @return the checked value of shortcut toggle 134 */ isChecked()135 public boolean isChecked() { 136 return mChecked; 137 } 138 139 /** 140 * Sets the editable state of Settings view. If the view cannot edited, it makes the settings 141 * and toggle be not touchable. The main ui handles touch event directly by {@link #onClick}. 142 */ setSettingsEditable(boolean enabled)143 public void setSettingsEditable(boolean enabled) { 144 if (mSettingsEditable != enabled) { 145 mSettingsEditable = enabled; 146 notifyChanged(); 147 } 148 } 149 isSettingsEditable()150 public boolean isSettingsEditable() { 151 return mSettingsEditable; 152 } 153 154 /** 155 * Sets the callback to be invoked when this preference is clicked by the user. 156 * 157 * @param callback the callback to be invoked 158 */ setOnClickCallback(OnClickCallback callback)159 public void setOnClickCallback(OnClickCallback callback) { 160 mClickCallback = callback; 161 } 162 callOnSettingsClicked()163 private void callOnSettingsClicked() { 164 if (mClickCallback != null) { 165 mClickCallback.onSettingsClicked(this); 166 } 167 } 168 callOnToggleClicked()169 private void callOnToggleClicked() { 170 setChecked(!mChecked); 171 if (mClickCallback != null) { 172 mClickCallback.onToggleClicked(this); 173 } 174 } 175 } 176