1 /* 2 * Copyright (C) 2023 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.shortcuts; 18 19 import android.content.Context; 20 import android.os.UserHandle; 21 import android.view.accessibility.AccessibilityManager; 22 23 import androidx.annotation.NonNull; 24 import androidx.preference.Preference; 25 26 import com.android.internal.accessibility.common.ShortcutConstants; 27 import com.android.internal.accessibility.util.ShortcutUtils; 28 import com.android.internal.util.Preconditions; 29 import com.android.settings.core.BasePreferenceController; 30 31 import java.util.Collections; 32 import java.util.Set; 33 34 /** 35 * A base preference controller for {@link ShortcutOptionPreference} 36 */ 37 public abstract class ShortcutOptionPreferenceController extends BasePreferenceController 38 implements Preference.OnPreferenceChangeListener { 39 private Set<String> mShortcutTargets = Collections.emptySet(); 40 private boolean mIsInSetupWizard; 41 ShortcutOptionPreferenceController(Context context, String preferenceKey)42 public ShortcutOptionPreferenceController(Context context, String preferenceKey) { 43 super(context, preferenceKey); 44 } 45 46 @Override updateState(Preference preference)47 public void updateState(Preference preference) { 48 super.updateState(preference); 49 if (getPreferenceKey().equals(preference.getKey()) 50 && preference instanceof ShortcutOptionPreference) { 51 ((ShortcutOptionPreference) preference).setChecked(isChecked()); 52 } 53 } 54 55 @Override getAvailabilityStatus()56 public int getAvailabilityStatus() { 57 if (isShortcutAvailable()) { 58 return AVAILABLE_UNSEARCHABLE; 59 } 60 return CONDITIONALLY_UNAVAILABLE; 61 } 62 63 /** 64 * Set the targets (i.e. a11y features) to be configured with the a11y shortcut option. 65 * <p> 66 * Note: the shortcutTargets cannot be empty, since the edit a11y shortcut option 67 * is meant to configure the shortcut options for an a11y feature. 68 * </> 69 * 70 * @param shortcutTargets the a11y features, like color correction, Talkback, etc. 71 * @throws NullPointerException if the {@code shortcutTargets} was {@code null} 72 * @throws IllegalArgumentException if the {@code shortcutTargets} was empty 73 */ setShortcutTargets(Set<String> shortcutTargets)74 public void setShortcutTargets(Set<String> shortcutTargets) { 75 Preconditions.checkCollectionNotEmpty(shortcutTargets, /* valueName= */ "a11y targets"); 76 77 this.mShortcutTargets = shortcutTargets; 78 } 79 setInSetupWizard(boolean isInSetupWizard)80 public void setInSetupWizard(boolean isInSetupWizard) { 81 this.mIsInSetupWizard = isInSetupWizard; 82 } 83 getShortcutTargets()84 protected Set<String> getShortcutTargets() { 85 return mShortcutTargets; 86 } 87 isInSetupWizard()88 protected boolean isInSetupWizard() { 89 return mIsInSetupWizard; 90 } 91 92 @Override onPreferenceChange(@onNull Preference preference, Object newValue)93 public final boolean onPreferenceChange(@NonNull Preference preference, Object newValue) { 94 enableShortcutForTargets((Boolean) newValue); 95 return false; 96 } 97 98 @ShortcutConstants.UserShortcutType getShortcutType()99 protected int getShortcutType() { 100 return ShortcutConstants.UserShortcutType.DEFAULT; 101 } 102 103 /** 104 * Returns true if the shortcut is associated to the targets 105 */ isChecked()106 protected boolean isChecked() { 107 Set<String> targets = ShortcutUtils.getShortcutTargetsFromSettings( 108 mContext, getShortcutType(), UserHandle.myUserId()); 109 110 return !targets.isEmpty() && targets.containsAll(getShortcutTargets()); 111 } 112 113 114 /** 115 * Enable or disable the shortcut for the given accessibility features. 116 */ enableShortcutForTargets(boolean enable)117 protected void enableShortcutForTargets(boolean enable) { 118 Set<String> shortcutTargets = getShortcutTargets(); 119 @ShortcutConstants.UserShortcutType int shortcutType = getShortcutType(); 120 121 AccessibilityManager a11yManager = mContext.getSystemService( 122 AccessibilityManager.class); 123 if (a11yManager != null) { 124 a11yManager.enableShortcutsForTargets(enable, shortcutType, shortcutTargets, 125 UserHandle.myUserId()); 126 } 127 } 128 /** 129 * Returns true when the user can associate a shortcut to the targets 130 */ isShortcutAvailable()131 protected abstract boolean isShortcutAvailable(); 132 } 133