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