blob: e077a6763755ebefda566905235435b6119a4e18 [file] [log] [blame]
Jason Monk7ce96b92015-02-02 11:27:58 -05001/*
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
17package com.android.settingslib.bluetooth;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothClass;
21import android.bluetooth.BluetoothDevice;
22import android.bluetooth.BluetoothPan;
23import android.bluetooth.BluetoothProfile;
24import android.content.Context;
25import android.util.Log;
26
27import com.android.settingslib.R;
28
29import java.util.HashMap;
30import java.util.List;
31
32/**
33 * PanProfile handles Bluetooth PAN profile (NAP and PANU).
34 */
Hansong Zhang1c1bc252017-10-30 16:38:16 -070035public class PanProfile implements LocalBluetoothProfile {
Jason Monk7ce96b92015-02-02 11:27:58 -050036 private static final String TAG = "PanProfile";
37 private static boolean V = true;
38
39 private BluetoothPan mService;
40 private boolean mIsProfileReady;
Hansong Zhang490d0202018-04-10 16:06:19 -070041 private final LocalBluetoothAdapter mLocalAdapter;
Jason Monk7ce96b92015-02-02 11:27:58 -050042
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
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
62 public void onServiceDisconnected(int profile) {
63 if (V) Log.d(TAG,"Bluetooth service disconnected");
64 mIsProfileReady=false;
65 }
66 }
67
68 public boolean isProfileReady() {
69 return mIsProfileReady;
70 }
71
Hansong Zhang490d0202018-04-10 16:06:19 -070072 PanProfile(Context context, LocalBluetoothAdapter adapter) {
73 mLocalAdapter = adapter;
74 mLocalAdapter.getProfileProxy(context, new PanServiceListener(),
75 BluetoothProfile.PAN);
Jason Monk7ce96b92015-02-02 11:27:58 -050076 }
77
78 public boolean isConnectable() {
79 return true;
80 }
81
82 public boolean isAutoConnectable() {
83 return false;
84 }
85
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
97 public boolean disconnect(BluetoothDevice device) {
98 if (mService == null) return false;
99 return mService.disconnect(device);
100 }
101
102 public int getConnectionStatus(BluetoothDevice device) {
103 if (mService == null) {
104 return BluetoothProfile.STATE_DISCONNECTED;
105 }
106 return mService.getConnectionState(device);
107 }
108
109 public boolean isPreferred(BluetoothDevice device) {
Andre Eisenbach60598072015-06-02 01:38:03 -0700110 return true;
Jason Monk7ce96b92015-02-02 11:27:58 -0500111 }
112
113 public int getPreferred(BluetoothDevice device) {
114 return -1;
115 }
116
117 public void setPreferred(BluetoothDevice device, boolean preferred) {
118 // ignore: isPreferred is always true for PAN
119 }
120
121 public String toString() {
122 return NAME;
123 }
124
125 public int getOrdinal() {
126 return ORDINAL;
127 }
128
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
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 Utils.getConnectionStateSummary(state);
152 }
153 }
154
155 public int getDrawableResource(BluetoothClass btClass) {
156 return R.drawable.ic_bt_network_pan;
157 }
158
159 // Tethering direction determines UI strings.
160 void setLocalRole(BluetoothDevice device, int role) {
161 mDeviceRoleMap.put(device, role);
162 }
163
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
172 protected void finalize() {
173 if (V) 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}