Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ab1bc299 authored by hughchen's avatar hughchen
Browse files

Use BluetoothDevice.ACCESS_* instead of CachedBluetoothDevice.ACCESS_*

1. Use BluetoothDevice.ACCESS_* instead of CachedBluetoothDevice.ACCESS_*
2. Use BluetoothDevice.setPhonebookAccessPermission() directly.
3. Use BluetoothDevice.setMessageAccessPermission() directly.
4. Use BluetoothDevice.getPhonebookAccessPermission() directly.
5. Use BluetoothDevice.getMessageAccessPermission() directly.
6. Use BluetoothDevice.getSimAccessPermission() directly.

Bug: 112517004
Test: make -j42 RunSettingsRoboTests
Change-Id: Ibe6b207b891b9bd2b328a2e2c7264a9a78cb498f
parent 6fdf8c22
Loading
Loading
Loading
Loading
+21 −21
Original line number Diff line number Diff line
@@ -98,11 +98,11 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
        BluetoothDevice device = mCachedDevice.getDevice();
        profilePref.setEnabled(!mCachedDevice.isBusy());
        if (profile instanceof MapProfile) {
            profilePref.setChecked(mCachedDevice.getMessagePermissionChoice()
                    == CachedBluetoothDevice.ACCESS_ALLOWED);
            profilePref.setChecked(device.getMessageAccessPermission()
                    == BluetoothDevice.ACCESS_ALLOWED);
        } else if (profile instanceof PbapServerProfile) {
            profilePref.setChecked(mCachedDevice.getPhonebookPermissionChoice()
                    == CachedBluetoothDevice.ACCESS_ALLOWED);
            profilePref.setChecked(device.getPhonebookAccessPermission()
                    == BluetoothDevice.ACCESS_ALLOWED);
        } else if (profile instanceof PanProfile) {
            profilePref.setChecked(profile.getConnectionStatus(device) ==
                    BluetoothProfile.STATE_CONNECTED);
@@ -130,31 +130,31 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
    /**
     * Helper method to enable a profile for a device.
     */
    private void enableProfile(LocalBluetoothProfile profile, BluetoothDevice device,
            SwitchPreference profilePref) {
    private void enableProfile(LocalBluetoothProfile profile) {
        final BluetoothDevice bluetoothDevice = mCachedDevice.getDevice();
        if (profile instanceof PbapServerProfile) {
            mCachedDevice.setPhonebookPermissionChoice(CachedBluetoothDevice.ACCESS_ALLOWED);
            bluetoothDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
            // We don't need to do the additional steps below for this profile.
            return;
        }
        if (profile instanceof MapProfile) {
            mCachedDevice.setMessagePermissionChoice(BluetoothDevice.ACCESS_ALLOWED);
            bluetoothDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
        }
        profile.setPreferred(device, true);
        profile.setPreferred(bluetoothDevice, true);
        mCachedDevice.connectProfile(profile);
    }

    /**
     * Helper method to disable a profile for a device
     */
    private void disableProfile(LocalBluetoothProfile profile, BluetoothDevice device,
            SwitchPreference profilePref) {
    private void disableProfile(LocalBluetoothProfile profile) {
        final BluetoothDevice bluetoothDevice = mCachedDevice.getDevice();
        mCachedDevice.disconnect(profile);
        profile.setPreferred(device, false);
        profile.setPreferred(bluetoothDevice, false);
        if (profile instanceof MapProfile) {
            mCachedDevice.setMessagePermissionChoice(BluetoothDevice.ACCESS_REJECTED);
            bluetoothDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_REJECTED);
        } else if (profile instanceof PbapServerProfile) {
            mCachedDevice.setPhonebookPermissionChoice(CachedBluetoothDevice.ACCESS_REJECTED);
            bluetoothDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED);
        }
    }

@@ -175,11 +175,10 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
            }
        }
        SwitchPreference profilePref = (SwitchPreference) preference;
        BluetoothDevice device = mCachedDevice.getDevice();
        if (profilePref.isChecked()) {
            enableProfile(profile, device, profilePref);
            enableProfile(profile);
        } else {
            disableProfile(profile, device, profilePref);
            disableProfile(profile);
        }
        refreshProfilePreference(profilePref, profile);
        return true;
@@ -191,17 +190,18 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
     */
    private List<LocalBluetoothProfile> getProfiles() {
        List<LocalBluetoothProfile> result = mCachedDevice.getConnectableProfiles();
        final BluetoothDevice device = mCachedDevice.getDevice();

        final int pbapPermission = mCachedDevice.getPhonebookPermissionChoice();
        final int pbapPermission = device.getPhonebookAccessPermission();
        // Only provide PBAP cabability if the client device has requested PBAP.
        if (pbapPermission != CachedBluetoothDevice.ACCESS_UNKNOWN) {
        if (pbapPermission != BluetoothDevice.ACCESS_UNKNOWN) {
            final PbapServerProfile psp = mManager.getProfileManager().getPbapProfile();
            result.add(psp);
        }

        final MapProfile mapProfile = mManager.getProfileManager().getMapProfile();
        final int mapPermission = mCachedDevice.getMessagePermissionChoice();
        if (mapPermission != CachedBluetoothDevice.ACCESS_UNKNOWN) {
        final int mapPermission = device.getMessageAccessPermission();
        if (mapPermission != BluetoothDevice.ACCESS_UNKNOWN) {
            result.add(mapProfile);
        }

+12 −12
Original line number Diff line number Diff line
@@ -237,42 +237,42 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
        String intentName = BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY;

        if (mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
            int phonebookPermission = cachedDevice.getPhonebookPermissionChoice();
            int phonebookPermission = mDevice.getPhonebookAccessPermission();

            if (phonebookPermission == CachedBluetoothDevice.ACCESS_UNKNOWN) {
            if (phonebookPermission == BluetoothDevice.ACCESS_UNKNOWN) {
                // Leave 'processed' as false.
            } else if (phonebookPermission == CachedBluetoothDevice.ACCESS_ALLOWED) {
            } else if (phonebookPermission == BluetoothDevice.ACCESS_ALLOWED) {
                sendReplyIntentToReceiver(true);
                processed = true;
            } else if (phonebookPermission == CachedBluetoothDevice.ACCESS_REJECTED) {
            } else if (phonebookPermission == BluetoothDevice.ACCESS_REJECTED) {
                sendReplyIntentToReceiver(false);
                processed = true;
            } else {
                Log.e(TAG, "Bad phonebookPermission: " + phonebookPermission);
            }
        } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
            int messagePermission = cachedDevice.getMessagePermissionChoice();
            int messagePermission = mDevice.getMessageAccessPermission();

            if (messagePermission == CachedBluetoothDevice.ACCESS_UNKNOWN) {
            if (messagePermission == BluetoothDevice.ACCESS_UNKNOWN) {
                // Leave 'processed' as false.
            } else if (messagePermission == CachedBluetoothDevice.ACCESS_ALLOWED) {
            } else if (messagePermission == BluetoothDevice.ACCESS_ALLOWED) {
                sendReplyIntentToReceiver(true);
                processed = true;
            } else if (messagePermission == CachedBluetoothDevice.ACCESS_REJECTED) {
            } else if (messagePermission == BluetoothDevice.ACCESS_REJECTED) {
                sendReplyIntentToReceiver(false);
                processed = true;
            } else {
                Log.e(TAG, "Bad messagePermission: " + messagePermission);
            }
        } else if(mRequestType == BluetoothDevice.REQUEST_TYPE_SIM_ACCESS) {
            int simPermission = cachedDevice.getSimPermissionChoice();
            int simPermission = mDevice.getSimAccessPermission();

            if (simPermission == CachedBluetoothDevice.ACCESS_UNKNOWN) {
            if (simPermission == BluetoothDevice.ACCESS_UNKNOWN) {
                // Leave 'processed' as false.
            } else if (simPermission == CachedBluetoothDevice.ACCESS_ALLOWED) {
            } else if (simPermission == BluetoothDevice.ACCESS_ALLOWED) {
                sendReplyIntentToReceiver(true);
                processed = true;
            } else if (simPermission == CachedBluetoothDevice.ACCESS_REJECTED) {
            } else if (simPermission == BluetoothDevice.ACCESS_REJECTED) {
                sendReplyIntentToReceiver(false);
                processed = true;
            } else {
+10 −11
Original line number Diff line number Diff line
@@ -31,8 +31,8 @@ import android.content.Context;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowBluetoothDevice;
import com.android.settings.testutils.shadow.ShadowBluetoothDevice;
import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfile;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
@@ -55,7 +55,7 @@ import androidx.preference.PreferenceCategory;
import androidx.preference.SwitchPreference;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = SettingsShadowBluetoothDevice.class)
@Config(shadows = {SettingsShadowBluetoothDevice.class, ShadowBluetoothDevice.class})
public class BluetoothDetailsProfilesControllerTest extends BluetoothDetailsControllerTestBase {

    private BluetoothDetailsProfilesController mController;
@@ -290,8 +290,7 @@ public class BluetoothDetailsProfilesControllerTest extends BluetoothDetailsCont
    @Test
    public void pbapProfileStartsEnabled() {
        setupDevice(makeDefaultDeviceConfig());
        when(mCachedDevice.getPhonebookPermissionChoice())
            .thenReturn(CachedBluetoothDevice.ACCESS_ALLOWED);
        mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
        PbapServerProfile psp = mock(PbapServerProfile.class);
        when(psp.getNameResource(mDevice)).thenReturn(R.string.bluetooth_profile_pbap);
        when(psp.toString()).thenReturn(PbapServerProfile.NAME);
@@ -306,14 +305,14 @@ public class BluetoothDetailsProfilesControllerTest extends BluetoothDetailsCont

        pref.performClick();
        assertThat(mProfiles.getPreferenceCount()).isEqualTo(1);
        verify(mCachedDevice).setPhonebookPermissionChoice(CachedBluetoothDevice.ACCESS_REJECTED);
        assertThat(mDevice.getPhonebookAccessPermission())
                .isEqualTo(BluetoothDevice.ACCESS_REJECTED);
    }

    @Test
    public void pbapProfileStartsDisabled() {
        setupDevice(makeDefaultDeviceConfig());
        when(mCachedDevice.getPhonebookPermissionChoice())
            .thenReturn(CachedBluetoothDevice.ACCESS_REJECTED);
        mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED);
        PbapServerProfile psp = mock(PbapServerProfile.class);
        when(psp.getNameResource(mDevice)).thenReturn(R.string.bluetooth_profile_pbap);
        when(psp.toString()).thenReturn(PbapServerProfile.NAME);
@@ -328,7 +327,8 @@ public class BluetoothDetailsProfilesControllerTest extends BluetoothDetailsCont

        pref.performClick();
        assertThat(mProfiles.getPreferenceCount()).isEqualTo(1);
        verify(mCachedDevice).setPhonebookPermissionChoice(CachedBluetoothDevice.ACCESS_ALLOWED);
        assertThat(mDevice.getPhonebookAccessPermission())
                .isEqualTo(BluetoothDevice.ACCESS_ALLOWED);
    }

    @Test
@@ -338,8 +338,7 @@ public class BluetoothDetailsProfilesControllerTest extends BluetoothDetailsCont
        when(mapProfile.getNameResource(mDevice)).thenReturn(R.string.bluetooth_profile_map);
        when(mProfileManager.getMapProfile()).thenReturn(mapProfile);
        when(mProfileManager.getProfileByName(eq(mapProfile.toString()))).thenReturn(mapProfile);
        when(mCachedDevice.getMessagePermissionChoice())
            .thenReturn(CachedBluetoothDevice.ACCESS_REJECTED);
        mDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_REJECTED);
        showScreen(mController);
        List<SwitchPreference> switches = getProfileSwitches(false);
        assertThat(switches.size()).isEqualTo(1);
@@ -349,7 +348,7 @@ public class BluetoothDetailsProfilesControllerTest extends BluetoothDetailsCont

        pref.performClick();
        assertThat(mProfiles.getPreferenceCount()).isEqualTo(1);
        verify(mCachedDevice).setMessagePermissionChoice(BluetoothDevice.ACCESS_ALLOWED);
        assertThat(mDevice.getMessageAccessPermission()).isEqualTo(BluetoothDevice.ACCESS_ALLOWED);
    }

    private A2dpProfile addMockA2dpProfile(boolean preferred, boolean supportsHighQualityAudio,
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.testutils.shadow;

import android.bluetooth.BluetoothDevice;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(value = BluetoothDevice.class, inheritImplementationMethods = true)
public class ShadowBluetoothDevice extends org.robolectric.shadows.ShadowBluetoothDevice {

    private int mMessageAccessPermission = BluetoothDevice.ACCESS_UNKNOWN;
    private int mPhonebookAccessPermission = BluetoothDevice.ACCESS_UNKNOWN;
    private int mSimAccessPermission = BluetoothDevice.ACCESS_UNKNOWN;

    @Implementation
    public void setMessageAccessPermission(int value) {
        mMessageAccessPermission = value;
    }

    @Implementation
    public int getMessageAccessPermission() {
        return mMessageAccessPermission;
    }

    @Implementation
    public void setPhonebookAccessPermission(int value) {
        mPhonebookAccessPermission = value;
    }

    @Implementation
    public int getPhonebookAccessPermission() {
        return mPhonebookAccessPermission;
    }

    @Implementation
    public void setSimAccessPermission(int value) {
        mSimAccessPermission = value;
    }

    @Implementation
    public int getSimAccessPermission() {
        return mSimAccessPermission;
    }
}