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