17ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk/*
27ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * Copyright (C) 2011 The Android Open Source Project
37ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk *
47ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * Licensed under the Apache License, Version 2.0 (the "License");
57ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * you may not use this file except in compliance with the License.
67ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * You may obtain a copy of the License at
77ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk *
87ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
97ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk *
107ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * Unless required by applicable law or agreed to in writing, software
117ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * distributed under the License is distributed on an "AS IS" BASIS,
127ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * See the License for the specific language governing permissions and
147ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * limitations under the License.
157ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk */
167ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
177ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkpackage com.android.settingslib.bluetooth;
187ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
197ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.bluetooth.BluetoothClass;
207ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.bluetooth.BluetoothDevice;
217ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.bluetooth.BluetoothPbap;
227ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.bluetooth.BluetoothProfile;
237ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.bluetooth.BluetoothUuid;
247ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.content.Context;
257ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.os.ParcelUuid;
267ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.util.Log;
277ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
287ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport com.android.settingslib.R;
297ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
307ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk/**
317ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * PBAPServer Profile
327ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk */
337ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkpublic final class PbapServerProfile implements LocalBluetoothProfile {
347ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private static final String TAG = "PbapServerProfile";
357ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private static boolean V = true;
367ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
377ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private BluetoothPbap mService;
387ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private boolean mIsProfileReady;
397ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
407ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    static final String NAME = "PBAP Server";
417ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
427ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    // Order of this profile in device profiles list
437ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private static final int ORDINAL = 6;
447ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
457ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    // The UUIDs indicate that remote device might access pbap server
467ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    static final ParcelUuid[] PBAB_CLIENT_UUIDS = {
477ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        BluetoothUuid.HSP,
487ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        BluetoothUuid.Handsfree,
497ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        BluetoothUuid.PBAP_PCE
507ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    };
517ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
527ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    // These callbacks run on the main thread.
537ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private final class PbapServiceListener
547ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            implements BluetoothPbap.ServiceListener {
557ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
567ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        public void onServiceConnected(BluetoothPbap proxy) {
577ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            if (V) Log.d(TAG,"Bluetooth service connected");
587ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            mService = (BluetoothPbap) proxy;
597ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            mIsProfileReady=true;
607ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        }
617ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
627ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        public void onServiceDisconnected() {
637ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            if (V) Log.d(TAG,"Bluetooth service disconnected");
647ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            mIsProfileReady=false;
657ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        }
667ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
677ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
687ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public boolean isProfileReady() {
697ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return mIsProfileReady;
707ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
717ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
727ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    PbapServerProfile(Context context) {
737ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        BluetoothPbap pbap = new BluetoothPbap(context, new PbapServiceListener());
747ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
757ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
767ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public boolean isConnectable() {
777ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return true;
787ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
797ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
807ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public boolean isAutoConnectable() {
817ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return false;
827ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
837ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
847ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public boolean connect(BluetoothDevice device) {
857ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        /*Can't connect from server */
867ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return false;
877ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
887ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
897ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
907ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public boolean disconnect(BluetoothDevice device) {
917ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        if (mService == null) return false;
927ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return mService.disconnect();
937ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
947ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
957ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public int getConnectionStatus(BluetoothDevice device) {
967ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        if (mService == null) {
977ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            return BluetoothProfile.STATE_DISCONNECTED;
987ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        }
997ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        if (mService.isConnected(device))
1007ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            return BluetoothProfile.STATE_CONNECTED;
1017ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        else
1027ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            return BluetoothProfile.STATE_DISCONNECTED;
1037ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1047ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1057ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public boolean isPreferred(BluetoothDevice device) {
1067ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return false;
1077ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1087ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1097ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public int getPreferred(BluetoothDevice device) {
1107ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return -1;
1117ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1127ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1137ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public void setPreferred(BluetoothDevice device, boolean preferred) {
1147ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        // ignore: isPreferred is always true for PBAP
1157ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1167ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1177ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public String toString() {
1187ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return NAME;
1197ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1207ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1217ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public int getOrdinal() {
1227ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return ORDINAL;
1237ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1247ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1257ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public int getNameResource(BluetoothDevice device) {
1267ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return R.string.bluetooth_profile_pbap;
1277ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1287ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1297ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public int getSummaryResourceForDevice(BluetoothDevice device) {
1307ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return R.string.bluetooth_profile_pbap_summary;
1317ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1327ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1337ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public int getDrawableResource(BluetoothClass btClass) {
1347ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return R.drawable.ic_bt_cellphone;
1357ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1367ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1377ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    protected void finalize() {
1387ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        if (V) Log.d(TAG, "finalize()");
1397ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        if (mService != null) {
1407ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            try {
1417ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk                mService.close();
1427ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk                mService = null;
1437ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            }catch (Throwable t) {
1447ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk                Log.w(TAG, "Error cleaning up PBAP proxy", t);
1457ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            }
1467ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        }
1477ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1487ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk}
149