Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settingslib.bluetooth; |
| 18 | |
| 19 | import android.bluetooth.BluetoothAdapter; |
| 20 | import android.bluetooth.BluetoothClass; |
| 21 | import android.bluetooth.BluetoothDevice; |
| 22 | import android.bluetooth.BluetoothMapClient; |
| 23 | import android.bluetooth.BluetoothProfile; |
| 24 | import android.bluetooth.BluetoothUuid; |
| 25 | import android.content.Context; |
| 26 | import android.os.ParcelUuid; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | import com.android.settingslib.R; |
| 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| 33 | |
| 34 | /** |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 35 | * MapClientProfile handles the Bluetooth MAP MCE role. |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 36 | */ |
| 37 | public final class MapClientProfile implements LocalBluetoothProfile { |
| 38 | private static final String TAG = "MapClientProfile"; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 39 | |
| 40 | private BluetoothMapClient mService; |
| 41 | private boolean mIsProfileReady; |
| 42 | |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 43 | private final CachedBluetoothDeviceManager mDeviceManager; |
| 44 | private final LocalBluetoothProfileManager mProfileManager; |
| 45 | |
| 46 | static final ParcelUuid[] UUIDS = { |
| 47 | BluetoothUuid.MAP, |
| 48 | BluetoothUuid.MNS, |
| 49 | BluetoothUuid.MAS, |
| 50 | }; |
| 51 | |
| 52 | static final String NAME = "MAP Client"; |
| 53 | |
| 54 | // Order of this profile in device profiles list |
| 55 | private static final int ORDINAL = 0; |
| 56 | |
| 57 | // These callbacks run on the main thread. |
| 58 | private final class MapClientServiceListener |
| 59 | implements BluetoothProfile.ServiceListener { |
| 60 | |
| 61 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 62 | mService = (BluetoothMapClient) proxy; |
| 63 | // We just bound to the service, so refresh the UI for any connected MAP devices. |
| 64 | List<BluetoothDevice> deviceList = mService.getConnectedDevices(); |
| 65 | while (!deviceList.isEmpty()) { |
| 66 | BluetoothDevice nextDevice = deviceList.remove(0); |
| 67 | CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice); |
| 68 | // we may add a new device here, but generally this should not happen |
| 69 | if (device == null) { |
| 70 | Log.w(TAG, "MapProfile found new device: " + nextDevice); |
timhypeng | f64f690 | 2018-07-31 15:40:15 +0800 | [diff] [blame] | 71 | device = mDeviceManager.addDevice(nextDevice); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 72 | } |
| 73 | device.onProfileStateChanged(MapClientProfile.this, |
| 74 | BluetoothProfile.STATE_CONNECTED); |
| 75 | device.refresh(); |
| 76 | } |
| 77 | |
| 78 | mProfileManager.callServiceConnectedListeners(); |
| 79 | mIsProfileReady=true; |
| 80 | } |
| 81 | |
| 82 | public void onServiceDisconnected(int profile) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 83 | mProfileManager.callServiceDisconnectedListeners(); |
| 84 | mIsProfileReady=false; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public boolean isProfileReady() { |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 89 | Log.d(TAG, "isProfileReady(): "+ mIsProfileReady); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 90 | return mIsProfileReady; |
| 91 | } |
| 92 | |
ryanywlin | 44de3a0 | 2018-05-02 15:15:37 +0800 | [diff] [blame] | 93 | @Override |
| 94 | public int getProfileId() { |
| 95 | return BluetoothProfile.MAP_CLIENT; |
| 96 | } |
| 97 | |
timhypeng | f64f690 | 2018-07-31 15:40:15 +0800 | [diff] [blame] | 98 | MapClientProfile(Context context, CachedBluetoothDeviceManager deviceManager, |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 99 | LocalBluetoothProfileManager profileManager) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 100 | mDeviceManager = deviceManager; |
| 101 | mProfileManager = profileManager; |
timhypeng | f64f690 | 2018-07-31 15:40:15 +0800 | [diff] [blame] | 102 | BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, |
| 103 | new MapClientServiceListener(), BluetoothProfile.MAP_CLIENT); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Leon Liao | 3b5e6bd | 2018-09-18 12:45:45 +0800 | [diff] [blame] | 106 | public boolean accessProfileEnabled() { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
| 110 | public boolean isAutoConnectable() { |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | public boolean connect(BluetoothDevice device) { |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 115 | if (mService == null) { |
| 116 | return false; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 117 | } |
| 118 | return mService.connect(device); |
| 119 | } |
| 120 | |
| 121 | public boolean disconnect(BluetoothDevice device) { |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 122 | if (mService == null) { |
| 123 | return false; |
| 124 | } |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 125 | // Downgrade priority as user is disconnecting. |
hughchen | 0f0d83e | 2019-12-10 14:37:09 +0800 | [diff] [blame] | 126 | if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) { |
| 127 | mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 128 | } |
| 129 | return mService.disconnect(device); |
| 130 | } |
| 131 | |
| 132 | public int getConnectionStatus(BluetoothDevice device) { |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 133 | if (mService == null) { |
| 134 | return BluetoothProfile.STATE_DISCONNECTED; |
| 135 | } |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 136 | return mService.getConnectionState(device); |
| 137 | } |
| 138 | |
| 139 | public boolean isPreferred(BluetoothDevice device) { |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 140 | if (mService == null) { |
| 141 | return false; |
| 142 | } |
hughchen | 0f0d83e | 2019-12-10 14:37:09 +0800 | [diff] [blame] | 143 | return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | public int getPreferred(BluetoothDevice device) { |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 147 | if (mService == null) { |
hughchen | 0f0d83e | 2019-12-10 14:37:09 +0800 | [diff] [blame] | 148 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 149 | } |
hughchen | 0f0d83e | 2019-12-10 14:37:09 +0800 | [diff] [blame] | 150 | return mService.getConnectionPolicy(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | public void setPreferred(BluetoothDevice device, boolean preferred) { |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 154 | if (mService == null) { |
| 155 | return; |
| 156 | } |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 157 | if (preferred) { |
hughchen | 0f0d83e | 2019-12-10 14:37:09 +0800 | [diff] [blame] | 158 | if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { |
| 159 | mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 160 | } |
| 161 | } else { |
hughchen | 0f0d83e | 2019-12-10 14:37:09 +0800 | [diff] [blame] | 162 | mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | public List<BluetoothDevice> getConnectedDevices() { |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 167 | if (mService == null) { |
| 168 | return new ArrayList<BluetoothDevice>(0); |
| 169 | } |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 170 | return mService.getDevicesMatchingConnectionStates( |
| 171 | new int[] {BluetoothProfile.STATE_CONNECTED, |
| 172 | BluetoothProfile.STATE_CONNECTING, |
| 173 | BluetoothProfile.STATE_DISCONNECTING}); |
| 174 | } |
| 175 | |
| 176 | public String toString() { |
| 177 | return NAME; |
| 178 | } |
| 179 | |
| 180 | public int getOrdinal() { |
| 181 | return ORDINAL; |
| 182 | } |
| 183 | |
| 184 | public int getNameResource(BluetoothDevice device) { |
| 185 | return R.string.bluetooth_profile_map; |
| 186 | } |
| 187 | |
| 188 | public int getSummaryResourceForDevice(BluetoothDevice device) { |
| 189 | int state = getConnectionStatus(device); |
| 190 | switch (state) { |
| 191 | case BluetoothProfile.STATE_DISCONNECTED: |
| 192 | return R.string.bluetooth_map_profile_summary_use_for; |
| 193 | |
| 194 | case BluetoothProfile.STATE_CONNECTED: |
| 195 | return R.string.bluetooth_map_profile_summary_connected; |
| 196 | |
| 197 | default: |
Kunhung Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 198 | return BluetoothUtils.getConnectionStateSummary(state); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | public int getDrawableResource(BluetoothClass btClass) { |
Amin Shaikh | f3a9427 | 2019-01-29 18:22:14 -0500 | [diff] [blame] | 203 | return com.android.internal.R.drawable.ic_phone; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | protected void finalize() { |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 207 | Log.d(TAG, "finalize()"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 208 | if (mService != null) { |
| 209 | try { |
| 210 | BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.MAP_CLIENT, |
Chienyuan | 8862bfb | 2018-09-17 10:54:37 +0800 | [diff] [blame] | 211 | mService); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 212 | mService = null; |
| 213 | }catch (Throwable t) { |
| 214 | Log.w(TAG, "Error cleaning up MAP Client proxy", t); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |