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_FORBIDDEN;
20 
21 import android.bluetooth.BluetoothAdapter;
22 import android.bluetooth.BluetoothClass;
23 import android.bluetooth.BluetoothDevice;
24 import android.bluetooth.BluetoothPbap;
25 import android.bluetooth.BluetoothProfile;
26 import android.bluetooth.BluetoothUuid;
27 import android.content.Context;
28 import android.os.ParcelUuid;
29 import android.util.Log;
30 
31 import com.android.internal.annotations.VisibleForTesting;
32 import com.android.settingslib.R;
33 
34 /**
35  * PBAPServer Profile
36  */
37 public class PbapServerProfile implements LocalBluetoothProfile {
38     private static final String TAG = "PbapServerProfile";
39 
40     private BluetoothPbap mService;
41     private boolean mIsProfileReady;
42 
43     @VisibleForTesting
44     public static final String NAME = "PBAP Server";
45 
46     // Order of this profile in device profiles list
47     private static final int ORDINAL = 6;
48 
49     // The UUIDs indicate that remote device might access pbap server
50     static final ParcelUuid[] PBAB_CLIENT_UUIDS = {
51         BluetoothUuid.HSP,
52         BluetoothUuid.HFP,
53         BluetoothUuid.PBAP_PCE
54     };
55 
56     // These callbacks run on the main thread.
57     private final class PbapServiceListener
58             implements BluetoothProfile.ServiceListener {
59 
60         @Override
onServiceConnected(int profile, BluetoothProfile proxy)61         public void onServiceConnected(int profile, BluetoothProfile proxy) {
62             mService = (BluetoothPbap) proxy;
63             mIsProfileReady=true;
64         }
65 
66         @Override
onServiceDisconnected(int profile)67         public void onServiceDisconnected(int profile) {
68             mIsProfileReady=false;
69         }
70     }
71 
isProfileReady()72     public boolean isProfileReady() {
73         return mIsProfileReady;
74     }
75 
76     @Override
getProfileId()77     public int getProfileId() {
78         return BluetoothProfile.PBAP;
79     }
80 
PbapServerProfile(Context context)81     PbapServerProfile(Context context) {
82         BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new PbapServiceListener(),
83                 BluetoothProfile.PBAP);
84     }
85 
accessProfileEnabled()86     public boolean accessProfileEnabled() {
87         return true;
88     }
89 
isAutoConnectable()90     public boolean isAutoConnectable() {
91         return false;
92     }
93 
getConnectionStatus(BluetoothDevice device)94     public int getConnectionStatus(BluetoothDevice device) {
95         if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
96         return mService.getConnectionState(device);
97     }
98 
99     @Override
isEnabled(BluetoothDevice device)100     public boolean isEnabled(BluetoothDevice device) {
101         return false;
102     }
103 
104     @Override
getConnectionPolicy(BluetoothDevice device)105     public int getConnectionPolicy(BluetoothDevice device) {
106         return -1;
107     }
108 
109     @Override
setEnabled(BluetoothDevice device, boolean enabled)110     public boolean setEnabled(BluetoothDevice device, boolean enabled) {
111         boolean isEnabled = false;
112         if (mService == null) {
113             return false;
114         }
115 
116         if (!enabled) {
117             isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
118         }
119 
120         return isEnabled;
121     }
122 
toString()123     public String toString() {
124         return NAME;
125     }
126 
getOrdinal()127     public int getOrdinal() {
128         return ORDINAL;
129     }
130 
getNameResource(BluetoothDevice device)131     public int getNameResource(BluetoothDevice device) {
132         return R.string.bluetooth_profile_pbap;
133     }
134 
getSummaryResourceForDevice(BluetoothDevice device)135     public int getSummaryResourceForDevice(BluetoothDevice device) {
136         return R.string.bluetooth_profile_pbap_summary;
137     }
138 
getDrawableResource(BluetoothClass btClass)139     public int getDrawableResource(BluetoothClass btClass) {
140         return com.android.internal.R.drawable.ic_phone;
141     }
142 
finalize()143     protected void finalize() {
144         Log.d(TAG, "finalize()");
145         if (mService != null) {
146             try {
147                 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.PBAP,
148                         mService);
149                 mService = null;
150             }catch (Throwable t) {
151                 Log.w(TAG, "Error cleaning up PBAP proxy", t);
152             }
153         }
154     }
155 }
156