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

Commit adc64e47 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add SapProfile to LocalBluetoothProfileManager (1/2)"

parents f322456b 5315c522
Loading
Loading
Loading
Loading
+14 −0
Original line number Original line Diff line number Diff line
@@ -30,6 +30,7 @@ import android.bluetooth.BluetoothMapClient;
import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothPbap;
import android.bluetooth.BluetoothPbap;
import android.bluetooth.BluetoothPbapClient;
import android.bluetooth.BluetoothPbapClient;
import android.bluetooth.BluetoothSap;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.BluetoothUuid;
import android.content.Context;
import android.content.Context;
@@ -98,6 +99,7 @@ public class LocalBluetoothProfileManager {
    private PbapClientProfile mPbapClientProfile;
    private PbapClientProfile mPbapClientProfile;
    private PbapServerProfile mPbapProfile;
    private PbapServerProfile mPbapProfile;
    private HearingAidProfile mHearingAidProfile;
    private HearingAidProfile mHearingAidProfile;
    private SapProfile mSapProfile;


    /**
    /**
     * Mapping from profile name, e.g. "HEADSET" to profile object.
     * Mapping from profile name, e.g. "HEADSET" to profile object.
@@ -210,6 +212,13 @@ public class LocalBluetoothProfileManager {
            addProfile(mPbapClientProfile, PbapClientProfile.NAME,
            addProfile(mPbapClientProfile, PbapClientProfile.NAME,
                    BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED);
                    BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED);
        }
        }
        if (mSapProfile == null && supportedList.contains(BluetoothProfile.SAP)) {
            if (DEBUG) {
                Log.d(TAG, "Adding local SAP profile");
            }
            mSapProfile = new SapProfile(mContext, mDeviceManager, this);
            addProfile(mSapProfile, SapProfile.NAME, BluetoothSap.ACTION_CONNECTION_STATE_CHANGED);
        }
        mEventManager.registerProfileIntentReceiver();
        mEventManager.registerProfileIntentReceiver();
    }
    }


@@ -550,6 +559,11 @@ public class LocalBluetoothProfileManager {
            removedProfiles.remove(mHearingAidProfile);
            removedProfiles.remove(mHearingAidProfile);
        }
        }


        if (mSapProfile != null && BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.SAP)) {
            profiles.add(mSapProfile);
            removedProfiles.remove(mSapProfile);
        }

        if (DEBUG) {
        if (DEBUG) {
            Log.d(TAG,"New Profiles" + profiles.toString());
            Log.d(TAG,"New Profiles" + profiles.toString());
        }
        }
+29 −34
Original line number Original line Diff line number Diff line
@@ -36,12 +36,10 @@ import java.util.List;
 */
 */
final class SapProfile implements LocalBluetoothProfile {
final class SapProfile implements LocalBluetoothProfile {
    private static final String TAG = "SapProfile";
    private static final String TAG = "SapProfile";
    private static boolean V = true;


    private BluetoothSap mService;
    private BluetoothSap mService;
    private boolean mIsProfileReady;
    private boolean mIsProfileReady;


    private final LocalBluetoothAdapter mLocalAdapter;
    private final CachedBluetoothDeviceManager mDeviceManager;
    private final CachedBluetoothDeviceManager mDeviceManager;
    private final LocalBluetoothProfileManager mProfileManager;
    private final LocalBluetoothProfileManager mProfileManager;


@@ -59,7 +57,7 @@ final class SapProfile implements LocalBluetoothProfile {
            implements BluetoothProfile.ServiceListener {
            implements BluetoothProfile.ServiceListener {


        public void onServiceConnected(int profile, BluetoothProfile proxy) {
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (V) Log.d(TAG,"Bluetooth service connected");
            Log.d(TAG, "Bluetooth service connected, profile:" + profile);
            mService = (BluetoothSap) proxy;
            mService = (BluetoothSap) proxy;
            // We just bound to the service, so refresh the UI for any connected SAP devices.
            // We just bound to the service, so refresh the UI for any connected SAP devices.
            List<BluetoothDevice> deviceList = mService.getConnectedDevices();
            List<BluetoothDevice> deviceList = mService.getConnectedDevices();
@@ -81,7 +79,7 @@ final class SapProfile implements LocalBluetoothProfile {
        }
        }


        public void onServiceDisconnected(int profile) {
        public void onServiceDisconnected(int profile) {
            if (V) Log.d(TAG,"Bluetooth service disconnected");
            Log.d(TAG, "Bluetooth service disconnected, profile:" + profile);
            mProfileManager.callServiceDisconnectedListeners();
            mProfileManager.callServiceDisconnectedListeners();
            mIsProfileReady=false;
            mIsProfileReady=false;
        }
        }
@@ -96,13 +94,11 @@ final class SapProfile implements LocalBluetoothProfile {
        return BluetoothProfile.SAP;
        return BluetoothProfile.SAP;
    }
    }


    SapProfile(Context context, LocalBluetoothAdapter adapter,
    SapProfile(Context context, CachedBluetoothDeviceManager deviceManager,
            CachedBluetoothDeviceManager deviceManager,
            LocalBluetoothProfileManager profileManager) {
            LocalBluetoothProfileManager profileManager) {
        mLocalAdapter = adapter;
        mDeviceManager = deviceManager;
        mDeviceManager = deviceManager;
        mProfileManager = profileManager;
        mProfileManager = profileManager;
        mLocalAdapter.getProfileProxy(context, new SapServiceListener(),
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new SapServiceListener(),
                BluetoothProfile.SAP);
                BluetoothProfile.SAP);
    }
    }


@@ -115,50 +111,47 @@ final class SapProfile implements LocalBluetoothProfile {
    }
    }


    public boolean connect(BluetoothDevice device) {
    public boolean connect(BluetoothDevice device) {
        if (mService == null) return false;
        if (mService == null) {
        List<BluetoothDevice> sinks = mService.getConnectedDevices();
            return false;
        if (sinks != null) {
            for (BluetoothDevice sink : sinks) {
                mService.disconnect(sink);
            }
        }
        }
        return mService.connect(device);
        return mService.connect(device);
    }
    }


    public boolean disconnect(BluetoothDevice device) {
    public boolean disconnect(BluetoothDevice device) {
        if (mService == null) return false;
        if (mService == null) {
        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
            return false;
        if (!deviceList.isEmpty() && deviceList.get(0).equals(device)) {
        }
        if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
        if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
            mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
            mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
        }
        }
        return mService.disconnect(device);
        return mService.disconnect(device);
        } else {
            return false;
        }
    }
    }


    public int getConnectionStatus(BluetoothDevice device) {
    public int getConnectionStatus(BluetoothDevice device) {
        if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
        if (mService == null) {
        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
            return BluetoothProfile.STATE_DISCONNECTED;

        }
        return !deviceList.isEmpty() && deviceList.get(0).equals(device)
        return mService.getConnectionState(device);
                ? mService.getConnectionState(device)
                : BluetoothProfile.STATE_DISCONNECTED;
    }
    }


    public boolean isPreferred(BluetoothDevice device) {
    public boolean isPreferred(BluetoothDevice device) {
        if (mService == null) return false;
        if (mService == null) {
            return false;
        }
        return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
        return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
    }
    }


    public int getPreferred(BluetoothDevice device) {
    public int getPreferred(BluetoothDevice device) {
        if (mService == null) return BluetoothProfile.PRIORITY_OFF;
        if (mService == null) {
            return BluetoothProfile.PRIORITY_OFF;
        }
        return mService.getPriority(device);
        return mService.getPriority(device);
    }
    }


    public void setPreferred(BluetoothDevice device, boolean preferred) {
    public void setPreferred(BluetoothDevice device, boolean preferred) {
        if (mService == null) return;
        if (mService == null) {
            return;
        }
        if (preferred) {
        if (preferred) {
            if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
            if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
@@ -169,7 +162,9 @@ final class SapProfile implements LocalBluetoothProfile {
    }
    }


    public List<BluetoothDevice> getConnectedDevices() {
    public List<BluetoothDevice> getConnectedDevices() {
        if (mService == null) return new ArrayList<BluetoothDevice>(0);
        if (mService == null) {
            return new ArrayList<BluetoothDevice>(0);
        }
        return mService.getDevicesMatchingConnectionStates(
        return mService.getDevicesMatchingConnectionStates(
              new int[] {BluetoothProfile.STATE_CONNECTED,
              new int[] {BluetoothProfile.STATE_CONNECTED,
                         BluetoothProfile.STATE_CONNECTING,
                         BluetoothProfile.STATE_CONNECTING,
@@ -207,7 +202,7 @@ final class SapProfile implements LocalBluetoothProfile {
    }
    }


    protected void finalize() {
    protected void finalize() {
        if (V) Log.d(TAG, "finalize()");
        Log.d(TAG, "finalize()");
        if (mService != null) {
        if (mService != null) {
            try {
            try {
                BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.SAP,
                BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.SAP,
+90 −0
Original line number Original line 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.settingslib.bluetooth;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSap;
import android.bluetooth.BluetoothProfile;

import com.android.settingslib.SettingsLibRobolectricTestRunner;
import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadow.api.Shadow;

@RunWith(SettingsLibRobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothAdapter.class})
public class SapProfileTest {

    @Mock
    private CachedBluetoothDeviceManager mDeviceManager;
    @Mock
    private LocalBluetoothProfileManager mProfileManager;
    @Mock
    private BluetoothSap mService;
    @Mock
    private CachedBluetoothDevice mCachedBluetoothDevice;
    @Mock
    private BluetoothDevice mBluetoothDevice;
    private BluetoothProfile.ServiceListener mServiceListener;
    private SapProfile mProfile;
    private ShadowBluetoothAdapter mShadowBluetoothAdapter;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
        mProfile = new SapProfile(RuntimeEnvironment.application, mDeviceManager, mProfileManager);
        mServiceListener = mShadowBluetoothAdapter.getServiceListener();
        mServiceListener.onServiceConnected(BluetoothProfile.SAP, mService);
    }

    @Test
    public void connect_shouldConnectBluetoothSap() {
        mProfile.connect(mBluetoothDevice);
        verify(mService).connect(mBluetoothDevice);
    }

    @Test
    public void disconnect_shouldDisconnectBluetoothSap() {
        mProfile.disconnect(mBluetoothDevice);
        verify(mService).disconnect(mBluetoothDevice);
    }

    @Test
    public void getConnectionStatus_shouldReturnConnectionState() {
        when(mService.getConnectionState(mBluetoothDevice)).
                thenReturn(BluetoothProfile.STATE_CONNECTED);
        assertThat(mProfile.getConnectionStatus(mBluetoothDevice)).
                isEqualTo(BluetoothProfile.STATE_CONNECTED);
    }
}
 No newline at end of file