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