Add mechanism to add accessibility service or activity into bluetooth 'Related' category am: 02b373522a am: 99e5fe760a

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/17472403

Change-Id: I2674a27be6bade64b028fdd2f1cc9118ed544c85
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
jasonwshsu
2022-05-15 07:48:50 +00:00
committed by Automerger Merge Worker
10 changed files with 199 additions and 21 deletions

View File

@@ -186,7 +186,7 @@ public class BluetoothDetailsCompanionAppsController extends BluetoothDetailsCon
String address, PreferenceCategory container) { String address, PreferenceCategory container) {
// If the device is FastPair, remove CDM companion apps. // If the device is FastPair, remove CDM companion apps.
final BluetoothFeatureProvider bluetoothFeatureProvider = FeatureFactory.getFactory(context) final BluetoothFeatureProvider bluetoothFeatureProvider = FeatureFactory.getFactory(context)
.getBluetoothFeatureProvider(context); .getBluetoothFeatureProvider();
final boolean sliceEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI, final boolean sliceEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI,
SettingsUIDeviceConfig.BT_SLICE_SETTINGS_ENABLED, true); SettingsUIDeviceConfig.BT_SLICE_SETTINGS_ENABLED, true);
final Uri settingsUri = bluetoothFeatureProvider.getBluetoothDeviceSettingsUri( final Uri settingsUri = bluetoothFeatureProvider.getBluetoothDeviceSettingsUri(

View File

@@ -17,22 +17,40 @@
package com.android.settings.bluetooth; package com.android.settings.bluetooth;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.accessibilityservice.AccessibilityShortcutInfo;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.os.UserHandle;
import android.view.accessibility.AccessibilityManager;
import androidx.annotation.NonNull;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.net.module.util.CollectionUtils;
import com.android.settings.accessibility.RestrictedPreferenceHelper;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** /**
* This class adds related tools preference. * This class adds related tools preference.
*/ */
public class BluetoothDetailsRelatedToolsController extends BluetoothDetailsController{ public class BluetoothDetailsRelatedToolsController extends BluetoothDetailsController{
private static final String KEY_RELATED_TOOLS_GROUP = "bluetooth_related_tools"; private static final String KEY_RELATED_TOOLS_GROUP = "bluetooth_related_tools";
private static final String KEY_LIVE_CAPTION = "live_caption"; private static final String KEY_LIVE_CAPTION = "live_caption";
private static final int ORDINAL = 99;
private PreferenceCategory mPreferenceCategory;
public BluetoothDetailsRelatedToolsController(Context context, public BluetoothDetailsRelatedToolsController(Context context,
PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle) { PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle) {
@@ -51,14 +69,20 @@ public class BluetoothDetailsRelatedToolsController extends BluetoothDetailsCont
return; return;
} }
final PreferenceCategory preferenceCategory = screen.findPreference(getPreferenceKey()); mPreferenceCategory = screen.findPreference(getPreferenceKey());
final Preference liveCaptionPreference = screen.findPreference(KEY_LIVE_CAPTION); final Preference liveCaptionPreference = screen.findPreference(KEY_LIVE_CAPTION);
if (!liveCaptionPreference.isVisible()) { if (!liveCaptionPreference.isVisible()) {
preferenceCategory.removePreference(liveCaptionPreference); mPreferenceCategory.removePreference(liveCaptionPreference);
} }
if (preferenceCategory.getPreferenceCount() == 0) { final List<ComponentName> relatedToolsList = FeatureFactory.getFactory(
screen.removePreference(preferenceCategory); mContext).getBluetoothFeatureProvider().getRelatedTools();
if (!CollectionUtils.isEmpty(relatedToolsList)) {
addAccessibilityInstalledRelatedPreference(relatedToolsList);
}
if (mPreferenceCategory.getPreferenceCount() == 0) {
screen.removePreference(mPreferenceCategory);
} }
} }
@@ -69,4 +93,32 @@ public class BluetoothDetailsRelatedToolsController extends BluetoothDetailsCont
public String getPreferenceKey() { public String getPreferenceKey() {
return KEY_RELATED_TOOLS_GROUP; return KEY_RELATED_TOOLS_GROUP;
} }
private void addAccessibilityInstalledRelatedPreference(
@NonNull List<ComponentName> componentNameList) {
final AccessibilityManager a11yManager = AccessibilityManager.getInstance(mContext);
final RestrictedPreferenceHelper preferenceHelper = new RestrictedPreferenceHelper(
mContext);
final List<AccessibilityServiceInfo> a11yServiceInfoList =
a11yManager.getInstalledAccessibilityServiceList().stream()
.filter(info -> componentNameList.contains(info.getComponentName()))
.collect(Collectors.toList());
final List<AccessibilityShortcutInfo> a11yShortcutInfoList =
a11yManager.getInstalledAccessibilityShortcutListAsUser(mContext,
UserHandle.myUserId()).stream()
.filter(info -> componentNameList.contains(info.getComponentName()))
.collect(Collectors.toList());
final List<RestrictedPreference> preferences = Stream.of(
preferenceHelper.createAccessibilityServicePreferenceList(a11yServiceInfoList),
preferenceHelper.createAccessibilityActivityPreferenceList(a11yShortcutInfoList))
.flatMap(Collection::stream)
.collect(Collectors.toList());
for (RestrictedPreference preference : preferences) {
preference.setOrder(ORDINAL);
mPreferenceCategory.addPreference(preference);
}
}
} }

View File

@@ -143,7 +143,7 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
use(LeAudioBluetoothDetailsHeaderController.class).init(mCachedDevice, mManager); use(LeAudioBluetoothDetailsHeaderController.class).init(mCachedDevice, mManager);
final BluetoothFeatureProvider featureProvider = FeatureFactory.getFactory( final BluetoothFeatureProvider featureProvider = FeatureFactory.getFactory(
context).getBluetoothFeatureProvider(context); context).getBluetoothFeatureProvider();
final boolean sliceEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI, final boolean sliceEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI,
SettingsUIDeviceConfig.BT_SLICE_SETTINGS_ENABLED, true); SettingsUIDeviceConfig.BT_SLICE_SETTINGS_ENABLED, true);
@@ -155,7 +155,7 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
private void updateExtraControlUri(int viewWidth) { private void updateExtraControlUri(int viewWidth) {
BluetoothFeatureProvider featureProvider = FeatureFactory.getFactory( BluetoothFeatureProvider featureProvider = FeatureFactory.getFactory(
getContext()).getBluetoothFeatureProvider(getContext()); getContext()).getBluetoothFeatureProvider();
boolean sliceEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI, boolean sliceEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI,
SettingsUIDeviceConfig.BT_SLICE_SETTINGS_ENABLED, true); SettingsUIDeviceConfig.BT_SLICE_SETTINGS_ENABLED, true);
Uri controlUri = null; Uri controlUri = null;

View File

@@ -17,15 +17,18 @@
package com.android.settings.bluetooth; package com.android.settings.bluetooth;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.net.Uri; import android.net.Uri;
import java.util.List;
/** /**
* Provider for bluetooth related feature * Provider for bluetooth related features.
*/ */
public interface BluetoothFeatureProvider { public interface BluetoothFeatureProvider {
/** /**
* Get the {@link Uri} that represents extra settings for a specific bluetooth device * Gets the {@link Uri} that represents extra settings for a specific bluetooth device
* *
* @param bluetoothDevice bluetooth device * @param bluetoothDevice bluetooth device
* @return {@link Uri} for extra settings * @return {@link Uri} for extra settings
@@ -33,10 +36,18 @@ public interface BluetoothFeatureProvider {
Uri getBluetoothDeviceSettingsUri(BluetoothDevice bluetoothDevice); Uri getBluetoothDeviceSettingsUri(BluetoothDevice bluetoothDevice);
/** /**
* Get the {@link Uri} that represents extra control for a specific bluetooth device * Gets the {@link Uri} that represents extra control for a specific bluetooth device
* *
* @param bluetoothDevice bluetooth device * @param bluetoothDevice bluetooth device
* @return {@link String} uri string for extra control * @return {@link String} uri string for extra control
*/ */
String getBluetoothDeviceControlUri(BluetoothDevice bluetoothDevice); String getBluetoothDeviceControlUri(BluetoothDevice bluetoothDevice);
/**
* Gets the {@link ComponentName} of services or activities that need to be shown in related
* tools.
*
* @return list of {@link ComponentName}
*/
List<ComponentName> getRelatedTools();
} }

View File

@@ -17,21 +17,20 @@
package com.android.settings.bluetooth; package com.android.settings.bluetooth;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.BluetoothUtils;
import java.util.List;
/** /**
* Impl of {@link BluetoothFeatureProvider} * Impl of {@link BluetoothFeatureProvider}
*/ */
public class BluetoothFeatureProviderImpl implements BluetoothFeatureProvider { public class BluetoothFeatureProviderImpl implements BluetoothFeatureProvider {
private Context mContext; public BluetoothFeatureProviderImpl(Context context) {}
public BluetoothFeatureProviderImpl(Context context) {
mContext = context;
}
@Override @Override
public Uri getBluetoothDeviceSettingsUri(BluetoothDevice bluetoothDevice) { public Uri getBluetoothDeviceSettingsUri(BluetoothDevice bluetoothDevice) {
@@ -44,4 +43,9 @@ public class BluetoothFeatureProviderImpl implements BluetoothFeatureProvider {
public String getBluetoothDeviceControlUri(BluetoothDevice bluetoothDevice) { public String getBluetoothDeviceControlUri(BluetoothDevice bluetoothDevice) {
return BluetoothUtils.getControlUriMetaData(bluetoothDevice); return BluetoothUtils.getControlUriMetaData(bluetoothDevice);
} }
@Override
public List<ComponentName> getRelatedTools() {
return null;
}
} }

View File

@@ -149,7 +149,10 @@ public abstract class FeatureFactory {
public abstract ContextualCardFeatureProvider getContextualCardFeatureProvider(Context context); public abstract ContextualCardFeatureProvider getContextualCardFeatureProvider(Context context);
public abstract BluetoothFeatureProvider getBluetoothFeatureProvider(Context context); /**
* Retrieves implementation for Bluetooth feature.
*/
public abstract BluetoothFeatureProvider getBluetoothFeatureProvider();
public abstract AwareFeatureProvider getAwareFeatureProvider(); public abstract AwareFeatureProvider getAwareFeatureProvider();

View File

@@ -283,10 +283,9 @@ public class FeatureFactoryImpl extends FeatureFactory {
} }
@Override @Override
public BluetoothFeatureProvider getBluetoothFeatureProvider(Context context) { public BluetoothFeatureProvider getBluetoothFeatureProvider() {
if (mBluetoothFeatureProvider == null) { if (mBluetoothFeatureProvider == null) {
mBluetoothFeatureProvider = new BluetoothFeatureProviderImpl( mBluetoothFeatureProvider = new BluetoothFeatureProviderImpl(getAppContext());
context.getApplicationContext());
} }
return mBluetoothFeatureProvider; return mBluetoothFeatureProvider;
} }

View File

@@ -20,20 +20,65 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.ComponentName;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.view.accessibility.AccessibilityManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowAccessibilityManager;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.List;
/** Tests for {@link BluetoothDetailsRelatedToolsController}. */ /** Tests for {@link BluetoothDetailsRelatedToolsController}. */
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class BluetoothDetailsRelatedToolsControllerTest extends BluetoothDetailsControllerTestBase { public class BluetoothDetailsRelatedToolsControllerTest extends BluetoothDetailsControllerTestBase {
@Rule
public final MockitoRule mockito = MockitoJUnit.rule();
private static final String PACKAGE_NAME = "com.android.test";
private static final String PACKAGE_NAME2 = "com.android.test2";
private static final String CLASS_NAME = PACKAGE_NAME + ".test_a11y_service";
private static final String KEY_RELATED_TOOLS_GROUP = "bluetooth_related_tools";
private static final String KEY_LIVE_CAPTION = "live_caption";
private BluetoothDetailsRelatedToolsController mController; private BluetoothDetailsRelatedToolsController mController;
private BluetoothFeatureProvider mFeatureProvider;
private ShadowAccessibilityManager mShadowAccessibilityManager;
private PreferenceCategory mPreferenceCategory;
@Override @Override
public void setUp() { public void setUp() {
super.setUp(); super.setUp();
final FakeFeatureFactory fakeFeatureFactory = FakeFeatureFactory.setupForTest();
mFeatureProvider = fakeFeatureFactory.getBluetoothFeatureProvider();
mShadowAccessibilityManager = Shadow.extract(AccessibilityManager.getInstance(mContext));
final Preference preference = new Preference(mContext);
preference.setKey(KEY_LIVE_CAPTION);
mPreferenceCategory = new PreferenceCategory(mContext);
mPreferenceCategory.setKey(KEY_RELATED_TOOLS_GROUP);
mScreen.addPreference(mPreferenceCategory);
mScreen.addPreference(preference);
mController = new BluetoothDetailsRelatedToolsController(mContext, mFragment, mCachedDevice, mController = new BluetoothDetailsRelatedToolsController(mContext, mFragment, mCachedDevice,
mLifecycle); mLifecycle);
mController.init(mScreen);
} }
@Test @Test
@@ -49,4 +94,68 @@ public class BluetoothDetailsRelatedToolsControllerTest extends BluetoothDetails
assertThat(mController.isAvailable()).isFalse(); assertThat(mController.isAvailable()).isFalse();
} }
@Test
public void displayPreference_oneRelatedToolsMatchA11yService_showOnePreference() {
when(mCachedDevice.isHearingAidDevice()).thenReturn(true);
mShadowAccessibilityManager.setInstalledAccessibilityServiceList(
List.of(getMockAccessibilityServiceInfo(PACKAGE_NAME, CLASS_NAME)));
when(mFeatureProvider.getRelatedTools()).thenReturn(
List.of(new ComponentName(PACKAGE_NAME, CLASS_NAME)));
mController.displayPreference(mScreen);
assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(1);
}
@Test
public void displayPreference_oneRelatedToolsNotMatchA11yService_showNoPreference() {
when(mCachedDevice.isHearingAidDevice()).thenReturn(true);
mShadowAccessibilityManager.setInstalledAccessibilityServiceList(
List.of(getMockAccessibilityServiceInfo(PACKAGE_NAME, CLASS_NAME)));
when(mFeatureProvider.getRelatedTools()).thenReturn(
List.of(new ComponentName(PACKAGE_NAME2, CLASS_NAME)));
mController.displayPreference(mScreen);
assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(0);
}
@Test
public void displayPreference_noRelatedTools_showNoPreference() {
when(mCachedDevice.isHearingAidDevice()).thenReturn(true);
mShadowAccessibilityManager.setInstalledAccessibilityServiceList(
List.of(getMockAccessibilityServiceInfo(PACKAGE_NAME, CLASS_NAME)));
when(mFeatureProvider.getRelatedTools()).thenReturn(null);
mController.displayPreference(mScreen);
assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(0);
}
private AccessibilityServiceInfo getMockAccessibilityServiceInfo(String packageName,
String className) {
final ApplicationInfo applicationInfo = new ApplicationInfo();
final ServiceInfo serviceInfo = new ServiceInfo();
applicationInfo.packageName = packageName;
serviceInfo.packageName = packageName;
serviceInfo.name = className;
serviceInfo.applicationInfo = applicationInfo;
final ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.serviceInfo = serviceInfo;
try {
final AccessibilityServiceInfo info = new AccessibilityServiceInfo(resolveInfo,
mContext);
ComponentName componentName = ComponentName.unflattenFromString(
packageName + "/" + className);
info.setComponentName(componentName);
return info;
} catch (XmlPullParserException | IOException e) {
// Do nothing
}
return null;
}
} }

View File

@@ -242,7 +242,7 @@ public class FakeFeatureFactory extends FeatureFactory {
} }
@Override @Override
public BluetoothFeatureProvider getBluetoothFeatureProvider(Context context) { public BluetoothFeatureProvider getBluetoothFeatureProvider() {
return mBluetoothFeatureProvider; return mBluetoothFeatureProvider;
} }

View File

@@ -228,7 +228,7 @@ public class FakeFeatureFactory extends FeatureFactory {
} }
@Override @Override
public BluetoothFeatureProvider getBluetoothFeatureProvider(Context context) { public BluetoothFeatureProvider getBluetoothFeatureProvider() {
return mBluetoothFeatureProvider; return mBluetoothFeatureProvider;
} }