• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.BluetoothMap;
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 /**
35  * MapProfile handles Bluetooth MAP profile.
36  */
37 public class MapProfile implements LocalBluetoothProfile {
38     private static final String TAG = "MapProfile";
39     private static boolean V = true;
40 
41     private BluetoothMap mService;
42     private boolean mIsProfileReady;
43 
44     private final LocalBluetoothAdapter mLocalAdapter;
45     private final CachedBluetoothDeviceManager mDeviceManager;
46     private final LocalBluetoothProfileManager mProfileManager;
47 
48     static final ParcelUuid[] UUIDS = {
49         BluetoothUuid.MAP,
50         BluetoothUuid.MNS,
51         BluetoothUuid.MAS,
52     };
53 
54     static final String NAME = "MAP";
55 
56     // Order of this profile in device profiles list
57 
58     // These callbacks run on the main thread.
59     private final class MapServiceListener
60             implements BluetoothProfile.ServiceListener {
61 
onServiceConnected(int profile, BluetoothProfile proxy)62         public void onServiceConnected(int profile, BluetoothProfile proxy) {
63             if (V) Log.d(TAG,"Bluetooth service connected");
64             mService = (BluetoothMap) proxy;
65             // We just bound to the service, so refresh the UI for any connected MAP devices.
66             List<BluetoothDevice> deviceList = mService.getConnectedDevices();
67             while (!deviceList.isEmpty()) {
68                 BluetoothDevice nextDevice = deviceList.remove(0);
69                 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
70                 // we may add a new device here, but generally this should not happen
71                 if (device == null) {
72                     Log.w(TAG, "MapProfile found new device: " + nextDevice);
73                     device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, nextDevice);
74                 }
75                 device.onProfileStateChanged(MapProfile.this,
76                         BluetoothProfile.STATE_CONNECTED);
77                 device.refresh();
78             }
79 
80             mProfileManager.callServiceConnectedListeners();
81             mIsProfileReady=true;
82         }
83 
onServiceDisconnected(int profile)84         public void onServiceDisconnected(int profile) {
85             if (V) Log.d(TAG,"Bluetooth service disconnected");
86             mProfileManager.callServiceDisconnectedListeners();
87             mIsProfileReady=false;
88         }
89     }
90 
isProfileReady()91     public boolean isProfileReady() {
92         if(V) Log.d(TAG,"isProfileReady(): "+ mIsProfileReady);
93         return mIsProfileReady;
94     }
95 
96     @Override
getProfileId()97     public int getProfileId() {
98         return BluetoothProfile.MAP;
99     }
100 
MapProfile(Context context, LocalBluetoothAdapter adapter, CachedBluetoothDeviceManager deviceManager, LocalBluetoothProfileManager profileManager)101     MapProfile(Context context, LocalBluetoothAdapter adapter,
102             CachedBluetoothDeviceManager deviceManager,
103             LocalBluetoothProfileManager profileManager) {
104         mLocalAdapter = adapter;
105         mDeviceManager = deviceManager;
106         mProfileManager = profileManager;
107         mLocalAdapter.getProfileProxy(context, new MapServiceListener(),
108                 BluetoothProfile.MAP);
109     }
110 
isConnectable()111     public boolean isConnectable() {
112         return true;
113     }
114 
isAutoConnectable()115     public boolean isAutoConnectable() {
116         return true;
117     }
118 
connect(BluetoothDevice device)119     public boolean connect(BluetoothDevice device) {
120         if(V)Log.d(TAG,"connect() - should not get called");
121         return false; // MAP never connects out
122     }
123 
disconnect(BluetoothDevice device)124     public boolean disconnect(BluetoothDevice device) {
125         if (mService == null) return false;
126         List<BluetoothDevice> deviceList = mService.getConnectedDevices();
127         if (!deviceList.isEmpty() && deviceList.get(0).equals(device)) {
128             if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
129                 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
130             }
131             return mService.disconnect(device);
132         } else {
133             return false;
134         }
135     }
136 
getConnectionStatus(BluetoothDevice device)137     public int getConnectionStatus(BluetoothDevice device) {
138         if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
139         List<BluetoothDevice> deviceList = mService.getConnectedDevices();
140         if(V) Log.d(TAG,"getConnectionStatus: status is: "+ mService.getConnectionState(device));
141 
142         return !deviceList.isEmpty() && deviceList.get(0).equals(device)
143                 ? mService.getConnectionState(device)
144                 : BluetoothProfile.STATE_DISCONNECTED;
145     }
146 
isPreferred(BluetoothDevice device)147     public boolean isPreferred(BluetoothDevice device) {
148         if (mService == null) return false;
149         return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
150     }
151 
getPreferred(BluetoothDevice device)152     public int getPreferred(BluetoothDevice device) {
153         if (mService == null) return BluetoothProfile.PRIORITY_OFF;
154         return mService.getPriority(device);
155     }
156 
setPreferred(BluetoothDevice device, boolean preferred)157     public void setPreferred(BluetoothDevice device, boolean preferred) {
158         if (mService == null) return;
159         if (preferred) {
160             if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
161                 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
162             }
163         } else {
164             mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
165         }
166     }
167 
getConnectedDevices()168     public List<BluetoothDevice> getConnectedDevices() {
169         if (mService == null) return new ArrayList<BluetoothDevice>(0);
170         return mService.getDevicesMatchingConnectionStates(
171               new int[] {BluetoothProfile.STATE_CONNECTED,
172                          BluetoothProfile.STATE_CONNECTING,
173                          BluetoothProfile.STATE_DISCONNECTING});
174     }
175 
toString()176     public String toString() {
177         return NAME;
178     }
179 
getOrdinal()180     public int getOrdinal() {
181         return BluetoothProfile.MAP;
182     }
183 
getNameResource(BluetoothDevice device)184     public int getNameResource(BluetoothDevice device) {
185         return R.string.bluetooth_profile_map;
186     }
187 
getSummaryResourceForDevice(BluetoothDevice device)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:
198                 return Utils.getConnectionStateSummary(state);
199         }
200     }
201 
getDrawableResource(BluetoothClass btClass)202     public int getDrawableResource(BluetoothClass btClass) {
203         return R.drawable.ic_bt_cellphone;
204     }
205 
finalize()206     protected void finalize() {
207         if (V) Log.d(TAG, "finalize()");
208         if (mService != null) {
209             try {
210                 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.MAP,
211                                                                        mService);
212                 mService = null;
213             }catch (Throwable t) {
214                 Log.w(TAG, "Error cleaning up MAP proxy", t);
215             }
216         }
217     }
218 }
219