diff --git a/res/xml/notification_access_permission_details.xml b/res/xml/notification_access_permission_details.xml index bf61882d0bc..0b764301153 100644 --- a/res/xml/notification_access_permission_details.xml +++ b/res/xml/notification_access_permission_details.xml @@ -35,22 +35,22 @@ android:key="type_filter_ongoing" android:title="@string/notif_type_ongoing" android:icon="@drawable/ic_ongoing_notification" - settings:controller="com.android.settings.applications.specialaccess.notificationaccess.OngoingTypeFilterPreferenceController"/>/> + settings:controller="com.android.settings.applications.specialaccess.notificationaccess.OngoingTypeFilterPreferenceController"/> /> + settings:controller="com.android.settings.applications.specialaccess.notificationaccess.ConversationTypeFilterPreferenceController"/> /> + settings:controller="com.android.settings.applications.specialaccess.notificationaccess.AlertingTypeFilterPreferenceController"/> /> + settings:controller="com.android.settings.applications.specialaccess.notificationaccess.SilentTypeFilterPreferenceController"/> associatedMacAddrs = + mCdm.getAssociations(mCn.getPackageName(), mUserId); + if (associatedMacAddrs != null) { + for (String assocMac : associatedMacAddrs) { + final Collection cachedDevices = + mBm.getCachedDeviceManager().getCachedDevicesCopy(); + for (CachedBluetoothDevice cachedBluetoothDevice : cachedDevices) { + if (Objects.equals(assocMac, cachedBluetoothDevice.getAddress())) { + if (multiple) { + sb.append(", "); + } else { + multiple = true; + } + sb.append(cachedBluetoothDevice.getName()); + } + } + } + } + } catch (RemoteException e) { + Log.w(TAG, "Error calling CDM", e); + } + return sb.toString(); + } } diff --git a/src/com/android/settings/applications/specialaccess/notificationaccess/NotificationAccessDetails.java b/src/com/android/settings/applications/specialaccess/notificationaccess/NotificationAccessDetails.java index a205b57433b..16aa07883e6 100644 --- a/src/com/android/settings/applications/specialaccess/notificationaccess/NotificationAccessDetails.java +++ b/src/com/android/settings/applications/specialaccess/notificationaccess/NotificationAccessDetails.java @@ -21,7 +21,7 @@ import static com.android.settings.applications.AppInfoBase.ARG_PACKAGE_NAME; import android.app.Activity; import android.app.NotificationManager; import android.app.settings.SettingsEnums; -import android.bluetooth.BluetoothAdapter; +import android.companion.ICompanionDeviceManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; @@ -30,6 +30,7 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.pm.ServiceInfo; import android.os.Bundle; +import android.os.ServiceManager; import android.os.UserHandle; import android.os.UserManager; import android.provider.Settings; @@ -45,14 +46,13 @@ import com.android.settings.R; import com.android.settings.SettingsActivity; import com.android.settings.applications.AppInfoBase; import com.android.settings.applications.manageapplications.ManageApplications; +import com.android.settings.bluetooth.Utils; import com.android.settings.core.SubSettingLauncher; import com.android.settings.dashboard.DashboardFragment; import com.android.settings.notification.NotificationBackend; import com.android.settingslib.RestrictedLockUtils; import com.android.settingslib.RestrictedLockUtilsInternal; -import com.android.settingslib.bluetooth.CachedBluetoothDevice; -import java.util.Collection; import java.util.List; import java.util.Objects; @@ -99,7 +99,12 @@ public class NotificationAccessDetails extends DashboardFragment { .setFragment(this) .setPackageInfo(mPackageInfo) .setPm(context.getPackageManager()) - .setServiceName(mServiceName); + .setServiceName(mServiceName) + .setBluetoothManager(Utils.getLocalBtManager(context)) + .setCdm(ICompanionDeviceManager.Stub.asInterface( + ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE))) + .setCn(mComponentName) + .setUserId(mUserId); getPreferenceControllers().forEach(controllers -> { controllers.forEach(controller -> { if (controller instanceof TypeFilterPreferenceController) { diff --git a/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/HeaderPreferenceControllerTest.java b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/HeaderPreferenceControllerTest.java new file mode 100644 index 00000000000..40ad1e08b17 --- /dev/null +++ b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/HeaderPreferenceControllerTest.java @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2021 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.applications.specialaccess.notificationaccess; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import android.bluetooth.BluetoothAdapter; +import android.companion.ICompanionDeviceManager; +import android.content.ComponentName; +import android.content.Context; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.android.settingslib.bluetooth.CachedBluetoothDevice; +import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; +import com.android.settingslib.bluetooth.LocalBluetoothManager; + +import com.google.common.collect.ImmutableList; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +@RunWith(AndroidJUnit4.class) +public class HeaderPreferenceControllerTest { + + private Context mContext; + private HeaderPreferenceController mController; + @Mock + LocalBluetoothManager mBm; + @Mock + ICompanionDeviceManager mCdm; + @Mock + CachedBluetoothDeviceManager mCbm; + ComponentName mCn = new ComponentName("a", "b"); + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = ApplicationProvider.getApplicationContext(); + + when(mBm.getCachedDeviceManager()).thenReturn(mCbm); + + mController = new HeaderPreferenceController(mContext, "key"); + mController.setCn(mCn); + mController.setUserId(0); + mController.setBluetoothManager(mBm); + mController.setCdm(mCdm); + } + + @Test + public void getDeviceList_noAssociations() throws Exception { + when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(null); + + Collection cachedDevices = new ArrayList<>(); + CachedBluetoothDevice cbd1 = mock(CachedBluetoothDevice.class); + when(cbd1.getAddress()).thenReturn("00:00:00:00:00:10"); + when(cbd1.getName()).thenReturn("Device 1"); + cachedDevices.add(cbd1); + when(mCbm.getCachedDevicesCopy()).thenReturn(cachedDevices); + + BluetoothAdapter.getDefaultAdapter().enable(); + + assertThat(mController.getDeviceList().toString()).isEmpty(); + } + + @Test + public void getDeviceList_associationsButNoDevice() throws Exception { + List macs = ImmutableList.of("00:00:00:00:00:10", "00:00:00:00:00:20"); + when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(macs); + + when(mCbm.getCachedDevicesCopy()).thenReturn(new ArrayList<>()); + + assertThat(mController.getDeviceList().toString()).isEmpty(); + } + + @Test + public void getDeviceList_singleDevice() throws Exception { + List macs = ImmutableList.of("00:00:00:00:00:10", "00:00:00:00:00:20"); + when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(macs); + + Collection cachedDevices = new ArrayList<>(); + CachedBluetoothDevice cbd1 = mock(CachedBluetoothDevice.class); + when(cbd1.getAddress()).thenReturn(macs.get(0)); + when(cbd1.getName()).thenReturn("Device 1"); + cachedDevices.add(cbd1); + when(mCbm.getCachedDevicesCopy()).thenReturn(cachedDevices); + + assertThat(mController.getDeviceList().toString()).isEqualTo("Device 1"); + } + + @Test + public void getDeviceList_multipleDevices() throws Exception { + List macs = ImmutableList.of("00:00:00:00:00:10", "00:00:00:00:00:20"); + when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(macs); + + Collection cachedDevices = new ArrayList<>(); + CachedBluetoothDevice cbd1 = mock(CachedBluetoothDevice.class); + when(cbd1.getAddress()).thenReturn(macs.get(0)); + when(cbd1.getName()).thenReturn("Device 1"); + cachedDevices.add(cbd1); + + CachedBluetoothDevice cbd2 = mock(CachedBluetoothDevice.class); + when(cbd2.getAddress()).thenReturn(macs.get(1)); + when(cbd2.getName()).thenReturn("Device 2"); + cachedDevices.add(cbd2); + when(mCbm.getCachedDevicesCopy()).thenReturn(cachedDevices); + + assertThat(mController.getDeviceList().toString()).isEqualTo("Device 1, Device 2"); + } +}