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.LinearLayout;
25 import android.widget.Switch;
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(R.layout.preference_widget_master_switch);
66         setIconSpaceReserved(true);
67     }
68 
69     @Override
onBindViewHolder(PreferenceViewHolder holder)70     public void onBindViewHolder(PreferenceViewHolder holder) {
71         super.onBindViewHolder(holder);
72 
73         final TypedValue outValue = new TypedValue();
74         getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
75                 outValue, true);
76 
77         final LinearLayout mainFrame = holder.itemView.findViewById(R.id.main_frame);
78         if (mainFrame != null) {
79             mainFrame.setOnClickListener(view -> callOnSettingsClicked());
80             mainFrame.setClickable(mSettingsEditable);
81             mainFrame.setFocusable(mSettingsEditable);
82             mainFrame.setBackgroundResource(
83                     mSettingsEditable ? outValue.resourceId : /* Remove background */ 0);
84         }
85 
86         Switch switchWidget = holder.itemView.findViewById(R.id.switchWidget);
87         if (switchWidget != null) {
88             // Consumes move events to ignore drag actions.
89             switchWidget.setOnTouchListener((v, event) -> {
90                 return event.getActionMasked() == MotionEvent.ACTION_MOVE;
91             });
92             switchWidget.setContentDescription(
93                     getContext().getText(R.string.accessibility_shortcut_settings));
94             switchWidget.setChecked(mChecked);
95             switchWidget.setOnClickListener(view -> callOnToggleClicked());
96             switchWidget.setClickable(mSettingsEditable);
97             switchWidget.setFocusable(mSettingsEditable);
98             switchWidget.setBackgroundResource(
99                     mSettingsEditable ? outValue.resourceId : /* Remove background */ 0);
100         }
101 
102         final View divider = holder.itemView.findViewById(R.id.divider);
103         if (divider != null) {
104             divider.setVisibility(mSettingsEditable ? View.VISIBLE : View.GONE);
105         }
106 
107         holder.itemView.setOnClickListener(view -> callOnToggleClicked());
108         holder.itemView.setClickable(!mSettingsEditable);
109         holder.itemView.setFocusable(!mSettingsEditable);
110     }
111 
112     /**
113      * Sets the shortcut toggle according to checked value.
114      *
115      * @param checked the state value of shortcut toggle
116      */
setChecked(boolean checked)117     public void setChecked(boolean checked) {
118         if (mChecked != checked) {
119             mChecked = checked;
120             notifyChanged();
121         }
122     }
123 
124     /**
125      * Gets the checked value of shortcut toggle.
126      *
127      * @return the checked value of shortcut toggle
128      */
isChecked()129     public boolean isChecked() {
130         return mChecked;
131     }
132 
133     /**
134      * Sets the editable state of Settings view. If the view cannot edited, it makes the settings
135      * and toggle be not touchable. The main ui handles touch event directly by {@link #onClick}.
136      */
setSettingsEditable(boolean enabled)137     public void setSettingsEditable(boolean enabled) {
138         if (mSettingsEditable != enabled) {
139             mSettingsEditable = enabled;
140             notifyChanged();
141         }
142     }
143 
isSettingsEditable()144     public boolean isSettingsEditable() {
145         return mSettingsEditable;
146     }
147 
148     /**
149      * Sets the callback to be invoked when this preference is clicked by the user.
150      *
151      * @param callback the callback to be invoked
152      */
setOnClickCallback(OnClickCallback callback)153     public void setOnClickCallback(OnClickCallback callback) {
154         mClickCallback = callback;
155     }
156 
callOnSettingsClicked()157     private void callOnSettingsClicked() {
158         if (mClickCallback != null) {
159             mClickCallback.onSettingsClicked(this);
160         }
161     }
162 
callOnToggleClicked()163     private void callOnToggleClicked() {
164         setChecked(!mChecked);
165         if (mClickCallback != null) {
166             mClickCallback.onToggleClicked(this);
167         }
168     }
169 }
170