1/*
2 * Copyright (C) 2015 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.BluetoothSap;
23import android.bluetooth.BluetoothProfile;
24import android.bluetooth.BluetoothUuid;
25import android.content.Context;
26import android.os.ParcelUuid;
27import android.util.Log;
28
29import com.android.settingslib.R;
30
31import java.util.ArrayList;
32import java.util.List;
33
34/**
35 * SapProfile handles Bluetooth SAP profile.
36 */
37final class SapProfile implements LocalBluetoothProfile {
38    private static final String TAG = "SapProfile";
39    private static boolean V = true;
40
41    private BluetoothSap mService;
42    private boolean mIsProfileReady;
43
44    private final LocalBluetoothAdapter mLocalAdapter;
45    private final CachedBluetoothDeviceManager mDeviceManager;
46    private final LocalBluetoothProfileManager mProfileManager;
47
48    static final ParcelUuid[] UUIDS = {
49        BluetoothUuid.SAP,
50    };
51
52    static final String NAME = "SAP";
53
54    // Order of this profile in device profiles list
55    private static final int ORDINAL = 10;
56
57    // These callbacks run on the main thread.
58    private final class SapServiceListener
59            implements BluetoothProfile.ServiceListener {
60
61        public void onServiceConnected(int profile, BluetoothProfile proxy) {
62            if (V) Log.d(TAG,"Bluetooth service connected");
63            mService = (BluetoothSap) proxy;
64            // We just bound to the service, so refresh the UI for any connected SAP devices.
65            List<BluetoothDevice> deviceList = mService.getConnectedDevices();
66            while (!deviceList.isEmpty()) {
67                BluetoothDevice nextDevice = deviceList.remove(0);
68                CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
69                // we may add a new device here, but generally this should not happen
70                if (device == null) {
71                    Log.w(TAG, "SapProfile found new device: " + nextDevice);
72                    device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, nextDevice);
73                }
74                device.onProfileStateChanged(SapProfile.this,
75                        BluetoothProfile.STATE_CONNECTED);
76                device.refresh();
77            }
78
79            mProfileManager.callServiceConnectedListeners();
80            mIsProfileReady=true;
81        }
82
83        public void onServiceDisconnected(int profile) {
84            if (V) Log.d(TAG,"Bluetooth service disconnected");
85            mProfileManager.callServiceDisconnectedListeners();
86            mIsProfileReady=false;
87        }
88    }
89
90    public boolean isProfileReady() {
91        return mIsProfileReady;
92    }
93
94    @Override
95    public int getProfileId() {
96        return BluetoothProfile.SAP;
97    }
98
99    SapProfile(Context context, LocalBluetoothAdapter adapter,
100            CachedBluetoothDeviceManager deviceManager,
101            LocalBluetoothProfileManager profileManager) {
102        mLocalAdapter = adapter;
103        mDeviceManager = deviceManager;
104        mProfileManager = profileManager;
105        mLocalAdapter.getProfileProxy(context, new SapServiceListener(),
106                BluetoothProfile.SAP);
107    }
108
109    public boolean isConnectable() {
110        return true;
111    }
112
113    public boolean isAutoConnectable() {
114        return true;
115    }
116
117    public boolean connect(BluetoothDevice device) {
118        if (mService == null) return false;
119        List<BluetoothDevice> sinks = mService.getConnectedDevices();
120        if (sinks != null) {
121            for (BluetoothDevice sink : sinks) {
122                mService.disconnect(sink);
123            }
124        }
125        return mService.connect(device);
126    }
127
128    public boolean disconnect(BluetoothDevice device) {
129        if (mService == null) return false;
130        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
131        if (!deviceList.isEmpty() && deviceList.get(0).equals(device)) {
132            if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
133                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
134            }
135            return mService.disconnect(device);
136        } else {
137            return false;
138        }
139    }
140
141    public int getConnectionStatus(BluetoothDevice device) {
142        if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
143        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
144
145        return !deviceList.isEmpty() && deviceList.get(0).equals(device)
146                ? mService.getConnectionState(device)
147                : BluetoothProfile.STATE_DISCONNECTED;
148    }
149
150    public boolean isPreferred(BluetoothDevice device) {
151        if (mService == null) return false;
152        return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
153    }
154
155    public int getPreferred(BluetoothDevice device) {
156        if (mService == null) return BluetoothProfile.PRIORITY_OFF;
157        return mService.getPriority(device);
158    }
159
160    public void setPreferred(BluetoothDevice device, boolean preferred) {
161        if (mService == null) return;
162        if (preferred) {
163            if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
164                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
165            }
166        } else {
167            mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
168        }
169    }
170
171    public List<BluetoothDevice> getConnectedDevices() {
172        if (mService == null) return new ArrayList<BluetoothDevice>(0);
173        return mService.getDevicesMatchingConnectionStates(
174              new int[] {BluetoothProfile.STATE_CONNECTED,
175                         BluetoothProfile.STATE_CONNECTING,
176                         BluetoothProfile.STATE_DISCONNECTING});
177    }
178
179    public String toString() {
180        return NAME;
181    }
182
183    public int getOrdinal() {
184        return ORDINAL;
185    }
186
187    public int getNameResource(BluetoothDevice device) {
188        return R.string.bluetooth_profile_sap;
189    }
190
191    public int getSummaryResourceForDevice(BluetoothDevice device) {
192        int state = getConnectionStatus(device);
193        switch (state) {
194            case BluetoothProfile.STATE_DISCONNECTED:
195                return R.string.bluetooth_sap_profile_summary_use_for;
196
197            case BluetoothProfile.STATE_CONNECTED:
198                return R.string.bluetooth_sap_profile_summary_connected;
199
200            default:
201                return Utils.getConnectionStateSummary(state);
202        }
203    }
204
205    public int getDrawableResource(BluetoothClass btClass) {
206        return R.drawable.ic_bt_cellphone;
207    }
208
209    protected void finalize() {
210        if (V) Log.d(TAG, "finalize()");
211        if (mService != null) {
212            try {
213                BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.SAP,
214                                                                       mService);
215                mService = null;
216            }catch (Throwable t) {
217                Log.w(TAG, "Error cleaning up SAP proxy", t);
218            }
219        }
220    }
221}
222