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

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

Merge "Implement method for get previously connected devices"

parents 4c3a6b68 c0c1163f
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
    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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:viewportWidth="24"
        android:viewportHeight="24"
        android:width="24dp"
        android:height="24dp"
        android:tint="?android:attr/colorControlNormal">
    <path
        android:fillColor="#00000000"
        android:pathData="M0 0h24v24H0z" />
    <path
        android:fillColor="#000000"
        android:pathData="M17 1.01L7 1c-1.1 0-2 0.9-2 2v18c0 1.1 0.9 2 2 2h10c1.1 0 2 -0.9 2-2V3c0-1.1 -0.9-1.99-2-1.99zM17 19H7V5h10v14z" />
</vector>
+2 −2
Original line number Diff line number Diff line
@@ -1153,6 +1153,6 @@
    <!-- The notice header of Third-party licenses. not translatable -->
    <string name="notice_header" translatable="false"></string>

    <!-- Name of the phone device [CHAR LIMIT=NONE] -->
    <string name="media_transfer_phone_device_name">Phone speaker</string>
    <!-- Name of the this device. [CHAR LIMIT=30] -->
    <string name="media_transfer_this_device_name">This device</string>
</resources>
+12 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package com.android.settingslib.media;

import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;

@@ -41,6 +42,11 @@ public class BluetoothMediaDevice extends MediaDevice {
        return mCachedDevice.getName();
    }

    @Override
    public String getSummary() {
        return mCachedDevice.getConnectionSummary();
    }

    @Override
    public int getIcon() {
        //TODO(b/117129183): This is not final icon for bluetooth device, just for demo.
@@ -86,4 +92,10 @@ public class BluetoothMediaDevice extends MediaDevice {
        }
        return false;
    }

    @Override
    public boolean isConnected() {
        return mCachedDevice.getBondState() == BluetoothDevice.BOND_BONDED
                && mCachedDevice.isConnected();
    }
}
+61 −28
Original line number Diff line number Diff line
@@ -41,6 +41,9 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall

    private static final String TAG = "BluetoothMediaManager";

    private final DeviceAttributeChangeCallback mDeviceAttributeChangeCallback =
            new DeviceAttributeChangeCallback();

    private LocalBluetoothManager mLocalBluetoothManager;
    private LocalBluetoothProfileManager mProfileManager;
    private CachedBluetoothDeviceManager mCachedBluetoothDeviceManager;
@@ -65,7 +68,10 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
        mLocalBluetoothManager.getEventManager().registerCallback(this);
        buildBluetoothDeviceList();
        dispatchDeviceListAdded();
        addServiceListenerIfNecessary();
    }

    private void addServiceListenerIfNecessary() {
        // The profile may not ready when calling startScan().
        // Device status are all disconnected since profiles are not ready to connected.
        // In this case, we observe onServiceConnected() in LocalBluetoothProfileManager.
@@ -78,18 +84,18 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall

    private void buildBluetoothDeviceList() {
        mMediaDevices.clear();
        addConnectedA2dpDevices();
        addConnectedHearingAidDevices();
        addConnectableA2dpDevices();
        addConnectableHearingAidDevices();
    }

    private void addConnectedA2dpDevices() {
    private void addConnectableA2dpDevices() {
        final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
        if (a2dpProfile == null) {
            Log.w(TAG, "addConnectedA2dpDevices() a2dp profile is null!");
            Log.w(TAG, "addConnectableA2dpDevices() a2dp profile is null!");
            return;
        }

        final List<BluetoothDevice> devices = a2dpProfile.getConnectedDevices();
        final List<BluetoothDevice> devices = a2dpProfile.getConnectableDevices();

        for (BluetoothDevice device : devices) {
            final CachedBluetoothDevice cachedDevice =
@@ -100,10 +106,12 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
                continue;
            }

            Log.d(TAG, "addConnectedA2dpDevices() device : " + cachedDevice.getName()
                    + ", is connected : " + cachedDevice.isConnected());
            Log.d(TAG, "addConnectableA2dpDevices() device : " + cachedDevice.getName()
                    + ", is connected : " + cachedDevice.isConnected()
                    + ", is preferred : " + a2dpProfile.isPreferred(device));

            if (cachedDevice.isConnected()) {
            if (a2dpProfile.isPreferred(device)
                    && BluetoothDevice.BOND_BONDED == cachedDevice.getBondState()) {
                addMediaDevice(cachedDevice);
            }
        }
@@ -111,15 +119,15 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
        mIsA2dpProfileReady = a2dpProfile.isProfileReady();
    }

    private void addConnectedHearingAidDevices() {
    private void addConnectableHearingAidDevices() {
        final HearingAidProfile hapProfile = mProfileManager.getHearingAidProfile();
        if (hapProfile == null) {
            Log.w(TAG, "addConnectedA2dpDevices() hap profile is null!");
            Log.w(TAG, "addConnectableHearingAidDevices() hap profile is null!");
            return;
        }

        final List<Long> devicesHiSyncIds = new ArrayList<>();
        final List<BluetoothDevice> devices = hapProfile.getConnectedDevices();
        final List<BluetoothDevice> devices = hapProfile.getConnectableDevices();

        for (BluetoothDevice device : devices) {
            final CachedBluetoothDevice cachedDevice =
@@ -130,13 +138,16 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
                continue;
            }

            Log.d(TAG, "addConnectedHearingAidDevices() device : " + cachedDevice.getName()
                    + ", is connected : " + cachedDevice.isConnected());
            Log.d(TAG, "addConnectableHearingAidDevices() device : " + cachedDevice.getName()
                    + ", is connected : " + cachedDevice.isConnected()
                    + ", is preferred : " + hapProfile.isPreferred(device));

            final long hiSyncId = hapProfile.getHiSyncId(device);

            // device with same hiSyncId should not be shown in the UI.
            // So do not add it into connectedDevices.
            if (!devicesHiSyncIds.contains(hiSyncId) && cachedDevice.isConnected()) {
            if (!devicesHiSyncIds.contains(hiSyncId) && hapProfile.isPreferred(device)
                    && BluetoothDevice.BOND_BONDED == cachedDevice.getBondState()) {
                devicesHiSyncIds.add(hiSyncId);
                addMediaDevice(cachedDevice);
            }
@@ -149,6 +160,7 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
        MediaDevice mediaDevice = findMediaDevice(MediaDeviceUtils.getId(cachedDevice));
        if (mediaDevice == null) {
            mediaDevice = new BluetoothMediaDevice(mContext, cachedDevice);
            cachedDevice.registerCallback(mDeviceAttributeChangeCallback);
            mLastAddedDevice = mediaDevice;
            mMediaDevices.add(mediaDevice);
        }
@@ -157,6 +169,14 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
    @Override
    public void stopScan() {
        mLocalBluetoothManager.getEventManager().unregisterCallback(this);
        unregisterDeviceAttributeChangeCallback();
    }

    private void unregisterDeviceAttributeChangeCallback() {
        for (MediaDevice device : mMediaDevices) {
            ((BluetoothMediaDevice) device).getCachedDevice()
                    .unregisterCallback(mDeviceAttributeChangeCallback);
        }
    }

    @Override
@@ -164,13 +184,14 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
        if (BluetoothAdapter.STATE_ON == bluetoothState) {
            buildBluetoothDeviceList();
            dispatchDeviceListAdded();
            addServiceListenerIfNecessary();
        } else if (BluetoothAdapter.STATE_OFF == bluetoothState) {
            final List<MediaDevice> removeDevicesList = new ArrayList<>();
            for (MediaDevice device : mMediaDevices) {
                if (device instanceof BluetoothMediaDevice) {
                ((BluetoothMediaDevice) device).getCachedDevice()
                        .unregisterCallback(mDeviceAttributeChangeCallback);
                removeDevicesList.add(device);
            }
            }
            mMediaDevices.removeAll(removeDevicesList);
            dispatchDeviceListRemoved(removeDevicesList);
        }
@@ -212,6 +233,7 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
    private void removeMediaDevice(CachedBluetoothDevice cachedDevice) {
        final MediaDevice mediaDevice = findMediaDevice(MediaDeviceUtils.getId(cachedDevice));
        if (mediaDevice != null) {
            cachedDevice.unregisterCallback(mDeviceAttributeChangeCallback);
            mLastRemovedDevice = mediaDevice;
            mMediaDevices.remove(mediaDevice);
        }
@@ -230,12 +252,17 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
        Log.d(TAG, "onProfileConnectionStateChanged() device: " + cachedDevice
                + ", state: " + state + ", bluetoothProfile: " + bluetoothProfile);

        if (isCachedDeviceConnected(cachedDevice)) {
            addMediaDevice(cachedDevice);
            dispatchDeviceAdded(cachedDevice);
        } else {
        updateMediaDeviceListIfNecessary(cachedDevice);
    }

    private void updateMediaDeviceListIfNecessary(CachedBluetoothDevice cachedDevice) {
        if (BluetoothDevice.BOND_NONE == cachedDevice.getBondState()) {
            removeMediaDevice(cachedDevice);
            dispatchDeviceRemoved(cachedDevice);
        } else {
            if (findMediaDevice(MediaDeviceUtils.getId(cachedDevice)) != null) {
                dispatchDataChanged();
            }
        }
    }

@@ -243,13 +270,7 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
    public void onAclConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
        Log.d(TAG, "onAclConnectionStateChanged() device: " + cachedDevice + ", state: " + state);

        if (isCachedDeviceConnected(cachedDevice)) {
            addMediaDevice(cachedDevice);
            dispatchDeviceAdded(cachedDevice);
        } else {
            removeMediaDevice(cachedDevice);
            dispatchDeviceRemoved(cachedDevice);
        }
        updateMediaDeviceListIfNecessary(cachedDevice);
    }

    @Override
@@ -281,4 +302,16 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
    public void onServiceDisconnected() {

    }

    /**
     * This callback is for update {@link BluetoothMediaDevice} summary when
     * {@link CachedBluetoothDevice} connection state is changed.
     */
    private class DeviceAttributeChangeCallback implements CachedBluetoothDevice.Callback {

        @Override
        public void onDeviceAttributesChanged() {
            dispatchDataChanged();
        }
    }
}
+10 −0
Original line number Diff line number Diff line
@@ -40,6 +40,11 @@ public class InfoMediaDevice extends MediaDevice {
        return mRouteInfo.getName();
    }

    @Override
    public String getSummary() {
        return null;
    }

    @Override
    public int getIcon() {
        //TODO(b/121083246): This is not final icon for cast device, just for demo.
@@ -63,4 +68,9 @@ public class InfoMediaDevice extends MediaDevice {
    public void disconnect() {
        //TODO(b/121083246): disconnected last select device
    }

    @Override
    public boolean isConnected() {
        return true;
    }
}
Loading