• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_primary_switch);
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         Switch switchWidget = holder.itemView.findViewById(R.id.switchWidget);
92         if (switchWidget != null) {
93             // Consumes move events to ignore drag actions.
94             switchWidget.setOnTouchListener((v, event) -> {
95                 return event.getActionMasked() == MotionEvent.ACTION_MOVE;
96             });
97             switchWidget.setContentDescription(
98                     getContext().getText(R.string.accessibility_shortcut_settings));
99             switchWidget.setChecked(mChecked);
100             switchWidget.setOnClickListener(view -> callOnToggleClicked());
101             switchWidget.setClickable(mSettingsEditable);
102             switchWidget.setFocusable(mSettingsEditable);
103             switchWidget.setBackgroundResource(
104                     mSettingsEditable ? outValue.resourceId : /* Remove background */ 0);
105         }
106 
107         final View divider = holder.itemView.findViewById(R.id.divider);
108         if (divider != null) {
109             divider.setVisibility(mSettingsEditable ? View.VISIBLE : View.GONE);
110         }
111 
112         holder.itemView.setOnClickListener(view -> callOnToggleClicked());
113         holder.itemView.setClickable(!mSettingsEditable);
114         holder.itemView.setFocusable(!mSettingsEditable);
115     }
116 
117     /**
118      * Sets the shortcut toggle according to checked value.
119      *
120      * @param checked the state value of shortcut toggle
121      */
setChecked(boolean checked)122     public void setChecked(boolean checked) {
123         if (mChecked != checked) {
124             mChecked = checked;
125             notifyChanged();
126         }
127     }
128 
129     /**
130      * Gets the checked value of shortcut toggle.
131      *
132      * @return the checked value of shortcut toggle
133      */
isChecked()134     public boolean isChecked() {
135         return mChecked;
136     }
137 
138     /**
139      * Sets the editable state of Settings view. If the view cannot edited, it makes the settings
140      * and toggle be not touchable. The main ui handles touch event directly by {@link #onClick}.
141      */
setSettingsEditable(boolean enabled)142     public void setSettingsEditable(boolean enabled) {
143         if (mSettingsEditable != enabled) {
144             mSettingsEditable = enabled;
145             notifyChanged();
146         }
147     }
148 
isSettingsEditable()149     public boolean isSettingsEditable() {
150         return mSettingsEditable;
151     }
152 
153     /**
154      * Sets the callback to be invoked when this preference is clicked by the user.
155      *
156      * @param callback the callback to be invoked
157      */
setOnClickCallback(OnClickCallback callback)158     public void setOnClickCallback(OnClickCallback callback) {
159         mClickCallback = callback;
160     }
161 
callOnSettingsClicked()162     private void callOnSettingsClicked() {
163         if (mClickCallback != null) {
164             mClickCallback.onSettingsClicked(this);
165         }
166     }
167 
callOnToggleClicked()168     private void callOnToggleClicked() {
169         setChecked(!mChecked);
170         if (mClickCallback != null) {
171             mClickCallback.onToggleClicked(this);
172         }
173     }
174 }
175