1 /* 2 * Copyright (C) 2011 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.BluetoothPan; 23 import android.bluetooth.BluetoothProfile; 24 import android.content.Context; 25 import android.util.Log; 26 27 import com.android.settingslib.R; 28 29 import java.util.HashMap; 30 import java.util.List; 31 32 /** 33 * PanProfile handles Bluetooth PAN profile (NAP and PANU). 34 */ 35 public class PanProfile implements LocalBluetoothProfile { 36 private static final String TAG = "PanProfile"; 37 private static boolean V = true; 38 39 private BluetoothPan mService; 40 private boolean mIsProfileReady; 41 private final LocalBluetoothAdapter mLocalAdapter; 42 43 // Tethering direction for each device 44 private final HashMap<BluetoothDevice, Integer> mDeviceRoleMap = 45 new HashMap<BluetoothDevice, Integer>(); 46 47 static final String NAME = "PAN"; 48 49 // Order of this profile in device profiles list 50 private static final int ORDINAL = 4; 51 52 // These callbacks run on the main thread. 53 private final class PanServiceListener 54 implements BluetoothProfile.ServiceListener { 55 onServiceConnected(int profile, BluetoothProfile proxy)56 public void onServiceConnected(int profile, BluetoothProfile proxy) { 57 if (V) Log.d(TAG,"Bluetooth service connected"); 58 mService = (BluetoothPan) proxy; 59 mIsProfileReady=true; 60 } 61 onServiceDisconnected(int profile)62 public void onServiceDisconnected(int profile) { 63 if (V) Log.d(TAG,"Bluetooth service disconnected"); 64 mIsProfileReady=false; 65 } 66 } 67 isProfileReady()68 public boolean isProfileReady() { 69 return mIsProfileReady; 70 } 71 72 @Override getProfileId()73 public int getProfileId() { 74 return BluetoothProfile.PAN; 75 } 76 PanProfile(Context context, LocalBluetoothAdapter adapter)77 PanProfile(Context context, LocalBluetoothAdapter adapter) { 78 mLocalAdapter = adapter; 79 mLocalAdapter.getProfileProxy(context, new PanServiceListener(), 80 BluetoothProfile.PAN); 81 } 82 isConnectable()83 public boolean isConnectable() { 84 return true; 85 } 86 isAutoConnectable()87 public boolean isAutoConnectable() { 88 return false; 89 } 90 connect(BluetoothDevice device)91 public boolean connect(BluetoothDevice device) { 92 if (mService == null) return false; 93 List<BluetoothDevice> sinks = mService.getConnectedDevices(); 94 if (sinks != null) { 95 for (BluetoothDevice sink : sinks) { 96 mService.disconnect(sink); 97 } 98 } 99 return mService.connect(device); 100 } 101 disconnect(BluetoothDevice device)102 public boolean disconnect(BluetoothDevice device) { 103 if (mService == null) return false; 104 return mService.disconnect(device); 105 } 106 getConnectionStatus(BluetoothDevice device)107 public int getConnectionStatus(BluetoothDevice device) { 108 if (mService == null) { 109 return BluetoothProfile.STATE_DISCONNECTED; 110 } 111 return mService.getConnectionState(device); 112 } 113 isPreferred(BluetoothDevice device)114 public boolean isPreferred(BluetoothDevice device) { 115 return true; 116 } 117 getPreferred(BluetoothDevice device)118 public int getPreferred(BluetoothDevice device) { 119 return -1; 120 } 121 setPreferred(BluetoothDevice device, boolean preferred)122 public void setPreferred(BluetoothDevice device, boolean preferred) { 123 // ignore: isPreferred is always true for PAN 124 } 125 toString()126 public String toString() { 127 return NAME; 128 } 129 getOrdinal()130 public int getOrdinal() { 131 return ORDINAL; 132 } 133 getNameResource(BluetoothDevice device)134 public int getNameResource(BluetoothDevice device) { 135 if (isLocalRoleNap(device)) { 136 return R.string.bluetooth_profile_pan_nap; 137 } else { 138 return R.string.bluetooth_profile_pan; 139 } 140 } 141 getSummaryResourceForDevice(BluetoothDevice device)142 public int getSummaryResourceForDevice(BluetoothDevice device) { 143 int state = getConnectionStatus(device); 144 switch (state) { 145 case BluetoothProfile.STATE_DISCONNECTED: 146 return R.string.bluetooth_pan_profile_summary_use_for; 147 148 case BluetoothProfile.STATE_CONNECTED: 149 if (isLocalRoleNap(device)) { 150 return R.string.bluetooth_pan_nap_profile_summary_connected; 151 } else { 152 return R.string.bluetooth_pan_user_profile_summary_connected; 153 } 154 155 default: 156 return Utils.getConnectionStateSummary(state); 157 } 158 } 159 getDrawableResource(BluetoothClass btClass)160 public int getDrawableResource(BluetoothClass btClass) { 161 return R.drawable.ic_bt_network_pan; 162 } 163 164 // Tethering direction determines UI strings. setLocalRole(BluetoothDevice device, int role)165 void setLocalRole(BluetoothDevice device, int role) { 166 mDeviceRoleMap.put(device, role); 167 } 168 isLocalRoleNap(BluetoothDevice device)169 boolean isLocalRoleNap(BluetoothDevice device) { 170 if (mDeviceRoleMap.containsKey(device)) { 171 return mDeviceRoleMap.get(device) == BluetoothPan.LOCAL_NAP_ROLE; 172 } else { 173 return false; 174 } 175 } 176 finalize()177 protected void finalize() { 178 if (V) Log.d(TAG, "finalize()"); 179 if (mService != null) { 180 try { 181 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.PAN, mService); 182 mService = null; 183 }catch (Throwable t) { 184 Log.w(TAG, "Error cleaning up PAN proxy", t); 185 } 186 } 187 } 188 } 189