Accessibility Service & Shortcut Redesign - Remove “volume key shortcut” setting (1/n)

Bug: 142529032
Test: Manual test
Change-Id: Icfff02b25b1c83598774dd1708e145c0cf9447d7
This commit is contained in:
menghanli
2020-02-11 22:03:29 +08:00
committed by Menghan Li
parent 23b7ca5576
commit 40f9e47bb7
5 changed files with 3 additions and 268 deletions

View File

@@ -24,8 +24,7 @@
android:fragment="com.android.settings.accessibility.AccessibilityShortcutPreferenceFragment"
android:key="accessibility_shortcut_preference"
android:persistent="false"
android:title="@string/accessibility_global_gesture_preference_title"
settings:controller="com.android.settings.accessibility.AccessibilityShortcutPreferenceController"/>
android:title="@string/accessibility_global_gesture_preference_title" />
<PreferenceCategory
android:key="user_installed_services_category"

View File

@@ -17,11 +17,6 @@
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/accessibility_global_gesture_preference_title">
<Preference
android:fragment="com.android.settings.accessibility.ShortcutServicePickerFragment"
android:key="accessibility_shortcut_service"
android:title="@string/accessibility_shortcut_service_title" />
<SwitchPreference
android:key="accessibility_shortcut_on_lock_screen"
android:title="@string/accessibility_shortcut_service_on_lock_screen_title" />

View File

@@ -1,52 +0,0 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import android.content.Context;
import android.os.UserHandle;
import android.view.accessibility.AccessibilityManager;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.accessibility.AccessibilityUtils;
public class AccessibilityShortcutPreferenceController extends BasePreferenceController {
public AccessibilityShortcutPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return AccessibilityManager
.getInstance(mContext).getInstalledAccessibilityServiceList().isEmpty()
? DISABLED_DEPENDENT_SETTING : AVAILABLE;
}
@Override
public CharSequence getSummary() {
if (AccessibilityManager.getInstance(mContext)
.getInstalledAccessibilityServiceList().isEmpty()) {
return mContext.getString(R.string.accessibility_no_services_installed);
} else {
final boolean shortcutEnabled =
AccessibilityUtils.isShortcutEnabled(mContext, UserHandle.myUserId());
return shortcutEnabled
? AccessibilityShortcutPreferenceFragment.getServiceName(mContext)
: mContext.getString(R.string.accessibility_feature_state_off);
}
}
}

View File

@@ -50,10 +50,8 @@ import com.android.settingslib.search.SearchIndexable;
public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePreferenceFragment
implements Indexable {
public static final String SHORTCUT_SERVICE_KEY = "accessibility_shortcut_service";
public static final String ON_LOCK_SCREEN_KEY = "accessibility_shortcut_on_lock_screen";
private Preference mServicePreference;
private SwitchPreference mOnLockScreenSwitchPreference;
private final ContentObserver mContentObserver = new ContentObserver(new Handler()) {
@Override
@@ -75,8 +73,6 @@ public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePrefer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mServicePreference = findPreference(SHORTCUT_SERVICE_KEY);
mOnLockScreenSwitchPreference = (SwitchPreference) findPreference(ON_LOCK_SCREEN_KEY);
mOnLockScreenSwitchPreference.setOnPreferenceChangeListener((Preference p, Object o) -> {
Settings.Secure.putInt(getContentResolver(),
@@ -94,7 +90,7 @@ public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePrefer
preferenceScreen.findPreference(KEY_GENERAL_CATEGORY).setVisible(false);
preferenceScreen.setOrderingAsAdded(false);
mToggleServiceDividerSwitchPreference.setOrder(mServicePreference.getOrder() - 1);
mToggleServiceDividerSwitchPreference.setVisible(false);
}
@Override
@@ -117,57 +113,14 @@ public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePrefer
return R.xml.accessibility_shortcut_settings;
}
@Override
protected void onRemoveSwitchPreferenceToggleSwitch() {
super.onRemoveSwitchPreferenceToggleSwitch();
mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener(null);
}
@Override
protected void onInstallSwitchPreferenceToggleSwitch() {
super.onInstallSwitchPreferenceToggleSwitch();
mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener((preference) -> {
boolean enabled = ((SwitchPreference) preference).isChecked();
Context context = getContext();
if (enabled && !shortcutFeatureAvailable(context)) {
// If no service is configured, we'll disable the shortcut shortly. Give the user
// a chance to select a service. We'll update the preferences when we resume.
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, ON);
mServicePreference.setEnabled(true);
mServicePreference.performClick();
} else {
onPreferenceToggled(Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, enabled);
}
return true;
});
}
@Override
protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
Settings.Secure.putInt(getContentResolver(), preferenceKey, enabled ? ON : OFF);
updatePreferences();
}
@Override
protected void updateToggleServiceTitle(SwitchPreference switchPreference) {
final String switchBarText = getString(R.string.accessibility_service_master_switch_title,
getString(R.string.accessibility_global_gesture_preference_title));
switchPreference.setTitle(switchBarText);
}
private void updatePreferences() {
ContentResolver cr = getContentResolver();
Context context = getContext();
mServicePreference.setSummary(getServiceName(context));
if (!shortcutFeatureAvailable(context)) {
// If no service is configured, make sure the overall shortcut is turned off
Settings.Secure.putInt(
getContentResolver(), Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, OFF);
}
boolean isEnabled = Settings.Secure
.getInt(cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, ON) == ON;
mToggleServiceDividerSwitchPreference.setChecked(isEnabled);
// The shortcut is enabled by default on the lock screen as long as the user has
// enabled the shortcut with the warning dialog
final int dialogShown = Settings.Secure.getInt(
@@ -175,9 +128,6 @@ public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePrefer
final boolean enabledFromLockScreen = Settings.Secure.getInt(
cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, dialogShown) == ON;
mOnLockScreenSwitchPreference.setChecked(enabledFromLockScreen);
// Only enable changing the service and lock screen behavior if the shortcut is on
mServicePreference.setEnabled(mToggleServiceDividerSwitchPreference.isChecked());
mOnLockScreenSwitchPreference.setEnabled(mToggleServiceDividerSwitchPreference.isChecked());
}
/**
@@ -214,8 +164,7 @@ public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePrefer
return getServiceInfo(context) != null;
}
private static @Nullable
ComponentName getShortcutComponent(Context context) {
private static @Nullable ComponentName getShortcutComponent(Context context) {
String componentNameString = AccessibilityUtils.getShortcutTargetServiceComponentNameString(
context, UserHandle.myUserId());
if (componentNameString == null) return null;

View File

@@ -1,156 +0,0 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.provider.Settings;
import android.view.accessibility.AccessibilityManager;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowAccessibilityManager;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class AccessibilityShortcutPreferenceControllerTest {
private final static String PACKAGE_NAME = "com.foo.bar";
private final static String CLASS_NAME = PACKAGE_NAME + ".fake_a11y_service";
private final static String COMPONENT_NAME = PACKAGE_NAME + "/" + CLASS_NAME;
private final static String SERVICE_NAME = "fake_a11y_service";
private final static int ON = 1;
private final static int OFF = 0;
private Context mContext;
private AccessibilityShortcutPreferenceController mController;
private ShadowAccessibilityManager mShadowAccessibilityManager;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mController = new AccessibilityShortcutPreferenceController(mContext, "shortcut_key");
mShadowAccessibilityManager = Shadow.extract(AccessibilityManager.getInstance(mContext));
mShadowAccessibilityManager.setInstalledAccessibilityServiceList(getMockServiceList());
}
@Test
public void getAvailabilityStatus_hasInstalledA11yServices_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_noInstalledServices_shouldReturnDisabledDependentSetting() {
mShadowAccessibilityManager.setInstalledAccessibilityServiceList(new ArrayList<>());
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.DISABLED_DEPENDENT_SETTING);
}
@Test
@Config(shadows = {ShadowAccessibilityShortcutPreferenceFragment.class})
public void getSummary_enabledAndSelectedA11yServices_shouldReturnSelectedServiceName() {
ShadowAccessibilityShortcutPreferenceFragment.setServiceName(SERVICE_NAME);
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, ON);
assertThat(mController.getSummary()).isEqualTo(SERVICE_NAME);
}
@Test
public void getSummary_enabledAndNoA11yServices_shouldReturnNoServiceInstalled() {
mShadowAccessibilityManager.setInstalledAccessibilityServiceList(new ArrayList<>());
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, ON);
assertThat(mController.getSummary())
.isEqualTo(mContext.getString(R.string.accessibility_no_services_installed));
}
@Test
public void getSummary_disabledShortcut_shouldReturnOffSummary() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, OFF);
assertThat(mController.getSummary())
.isEqualTo(mContext.getString(R.string.accessibility_feature_state_off));
}
@Implements(AccessibilityShortcutPreferenceFragment.class)
private static class ShadowAccessibilityShortcutPreferenceFragment {
private static String sSelectedServiceName;
public static void setServiceName(String selectedServiceName) {
sSelectedServiceName = selectedServiceName;
}
@Implementation
protected static CharSequence getServiceName(Context context) {
return sSelectedServiceName;
}
}
private AccessibilityServiceInfo getMockAccessibilityServiceInfo() {
final ApplicationInfo applicationInfo = new ApplicationInfo();
final ServiceInfo serviceInfo = new ServiceInfo();
applicationInfo.packageName = PACKAGE_NAME;
serviceInfo.packageName = PACKAGE_NAME;
serviceInfo.name = CLASS_NAME;
serviceInfo.applicationInfo = applicationInfo;
final ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.serviceInfo = serviceInfo;
try {
final AccessibilityServiceInfo info = new AccessibilityServiceInfo(resolveInfo,
mContext);
ComponentName componentName = ComponentName.unflattenFromString(COMPONENT_NAME);
info.setComponentName(componentName);
return info;
} catch (XmlPullParserException | IOException e) {
// Do nothing
}
return null;
}
private List<AccessibilityServiceInfo> getMockServiceList() {
final List<AccessibilityServiceInfo> infoList = new ArrayList<>();
infoList.add(getMockAccessibilityServiceInfo());
return infoList;
}
}