blob: aa95be27ea52704fe604e759ddde628a1543a9b3 [file] [log] [blame]
Joseph Pirozzo563c7002016-03-21 15:49:48 -07001/*
2 * Copyright (C) 2016 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
17package com.android.settingslib.bluetooth;
18
19import android.bluetooth.BluetoothPbapClient;
20import android.bluetooth.BluetoothAdapter;
21import android.bluetooth.BluetoothClass;
22import android.bluetooth.BluetoothDevice;
23import android.bluetooth.BluetoothProfile;
24import android.bluetooth.BluetoothUuid;
25import android.content.Context;
26import android.os.ParcelUuid;
27import android.util.Log;
28
29import com.android.settingslib.R;
30
31import java.util.ArrayList;
32import java.util.Collection;
33import java.util.List;
34
35final class PbapClientProfile implements LocalBluetoothProfile {
36 private static final String TAG = "PbapClientProfile";
37 private static boolean V = false;
38
39 private BluetoothPbapClient mService;
40 private boolean mIsProfileReady;
41
42 private final LocalBluetoothAdapter mLocalAdapter;
43 private final CachedBluetoothDeviceManager mDeviceManager;
44
45 static final ParcelUuid[] SRC_UUIDS = {
46 BluetoothUuid.PBAP_PSE,
47 };
48
49 static final String NAME = "PbapClient";
50 private final LocalBluetoothProfileManager mProfileManager;
51
52 // Order of this profile in device profiles list
53 private static final int ORDINAL = 6;
54
55 // These callbacks run on the main thread.
56 private final class PbapClientServiceListener
57 implements BluetoothProfile.ServiceListener {
58
59 public void onServiceConnected(int profile, BluetoothProfile proxy) {
60 if (V) {
61 Log.d(TAG,"Bluetooth service connected");
62 }
63 mService = (BluetoothPbapClient) proxy;
64 // We just bound to the service, so refresh the UI for any connected PBAP devices.
65 List<BluetoothDevice> deviceList = mService.getConnectedDevices();
66 while (!deviceList.isEmpty()) {
67 BluetoothDevice nextDevice = deviceList.remove(0);
68 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
69 // we may add a new device here, but generally this should not happen
70 if (device == null) {
71 Log.w(TAG, "PbapClientProfile found new device: " + nextDevice);
72 device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, nextDevice);
73 }
74 device.onProfileStateChanged(PbapClientProfile.this, BluetoothProfile.STATE_CONNECTED);
75 device.refresh();
76 }
77 mIsProfileReady = true;
78 }
79
80 public void onServiceDisconnected(int profile) {
81 if (V) {
82 Log.d(TAG,"Bluetooth service disconnected");
83 }
84 mIsProfileReady = false;
85 }
86 }
87
88 private void refreshProfiles() {
89 Collection<CachedBluetoothDevice> cachedDevices = mDeviceManager.getCachedDevicesCopy();
90 for (CachedBluetoothDevice device : cachedDevices) {
91 device.onUuidChanged();
92 }
93 }
94
95 public boolean pbapClientExists() {
96 return (mService != null);
97 }
98
99 public boolean isProfileReady() {
100 return mIsProfileReady;
101 }
102
103 PbapClientProfile(Context context, LocalBluetoothAdapter adapter,
104 CachedBluetoothDeviceManager deviceManager,
105 LocalBluetoothProfileManager profileManager) {
106 mLocalAdapter = adapter;
107 mDeviceManager = deviceManager;
108 mProfileManager = profileManager;
109 mLocalAdapter.getProfileProxy(context, new PbapClientServiceListener(),
110 BluetoothProfile.PBAP_CLIENT);
111 }
112
113 public boolean isConnectable() {
114 return true;
115 }
116
117 public boolean isAutoConnectable() {
118 return true;
119 }
120
121 public List<BluetoothDevice> getConnectedDevices() {
122 if (mService == null) {
123 return new ArrayList<BluetoothDevice>(0);
124 }
125 return mService.getDevicesMatchingConnectionStates(
126 new int[] {BluetoothProfile.STATE_CONNECTED,
127 BluetoothProfile.STATE_CONNECTING,
128 BluetoothProfile.STATE_DISCONNECTING});
129 }
130
131 public boolean connect(BluetoothDevice device) {
132 if (V) {
133 Log.d(TAG,"PBAPClientProfile got connect request");
134 }
135 if (mService == null) {
136 return false;
137 }
138 List<BluetoothDevice> srcs = getConnectedDevices();
139 if (srcs != null) {
140 for (BluetoothDevice src : srcs) {
141 if (src.equals(device)) {
142 // Connect to same device, Ignore it
143 Log.d(TAG,"Ignoring Connect");
144 return true;
145 }
146 }
147 mService.disconnect(device);
148 }
149 Log.d(TAG,"PBAPClientProfile attempting to connect to " + device.getAddress());
150
151 return mService.connect(device);
152 }
153
154 public boolean disconnect(BluetoothDevice device) {
155 if (V) {
156 Log.d(TAG,"PBAPClientProfile got disconnect request");
157 }
158 if (mService == null) {
159 return false;
160 }
161 return mService.disconnect(device);
162 }
163
164 public int getConnectionStatus(BluetoothDevice device) {
165 if (mService == null) {
166 return BluetoothProfile.STATE_DISCONNECTED;
167 }
168 return mService.getConnectionState(device);
169 }
170
171 public boolean isPreferred(BluetoothDevice device) {
172 if (mService == null) {
173 return false;
174 }
175 return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
176 }
177
178 public int getPreferred(BluetoothDevice device) {
179 if (mService == null) {
180 return BluetoothProfile.PRIORITY_OFF;
181 }
182 return mService.getPriority(device);
183 }
184
185 public void setPreferred(BluetoothDevice device, boolean preferred) {
186 if (mService == null) {
187 return;
188 }
189 if (preferred) {
190 if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
191 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
192 }
193 } else {
194 mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
195 }
196 }
197
198 public String toString() {
199 return NAME;
200 }
201
202 public int getOrdinal() {
203 return ORDINAL;
204 }
205
206 public int getNameResource(BluetoothDevice device) {
207 // we need to have same string in UI as the server side.
208 return R.string.bluetooth_profile_pbap;
209 }
210
211 public int getSummaryResourceForDevice(BluetoothDevice device) {
212 return R.string.bluetooth_profile_pbap_summary;
213 }
214
215 public int getDrawableResource(BluetoothClass btClass) {
216 return R.drawable.ic_bt_cellphone;
217 }
218
219 protected void finalize() {
220 if (V) {
221 Log.d(TAG, "finalize()");
222 }
223 if (mService != null) {
224 try {
225 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(
226 BluetoothProfile.PBAP_CLIENT,mService);
227 mService = null;
228 } catch (Throwable t) {
229 Log.w(TAG, "Error cleaning up PBAP Client proxy", t);
230 }
231 }
232 }
233}