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