PhonePolicy.java revision d617142a6e7567c6ae46fbbbebb58e14900cb3a1
186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal/*
286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal * Copyright (C) 2017 The Android Open Source Project
386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal *
486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal * Licensed under the Apache License, Version 2.0 (the "License");
586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal * you may not use this file except in compliance with the License.
686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal * You may obtain a copy of the License at
786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal *
886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal *      http://www.apache.org/licenses/LICENSE-2.0
986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal *
1086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal * Unless required by applicable law or agreed to in writing, software
1186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal * distributed under the License is distributed on an "AS IS" BASIS,
1286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal * See the License for the specific language governing permissions and
1486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal * limitations under the License.
1586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal */
1686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
1786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalpackage com.android.bluetooth.btservice;
1886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
1986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.bluetooth.BluetoothA2dp;
2086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.bluetooth.BluetoothAdapter;
2186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.bluetooth.BluetoothDevice;
2286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.bluetooth.BluetoothHeadset;
2386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.bluetooth.BluetoothProfile;
2486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.bluetooth.BluetoothUuid;
2586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.content.BroadcastReceiver;
2686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.content.Context;
2786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.content.Intent;
2886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.content.IntentFilter;
2986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.os.Handler;
3086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.os.Looper;
3186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.os.Message;
3286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.os.ParcelUuid;
33c4fbd756e2645147470c486ae96f2253f5e13a52Jack Heimport android.os.Parcelable;
34928f14463ee51192039abef856495ecce418fe01Jack Heimport android.support.annotation.VisibleForTesting;
3586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport android.util.Log;
3686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
3786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport com.android.bluetooth.a2dp.A2dpService;
3809e97f4cfcca3564c8daa864ede0eee172e9d5aaHansong Zhangimport com.android.bluetooth.hearingaid.HearingAidService;
3986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport com.android.bluetooth.hfp.HeadsetService;
40f0429c92669aa388785a97f00985d63a83e509c2Hansong Zhangimport com.android.bluetooth.hid.HidHostService;
4186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport com.android.bluetooth.pan.PanService;
4286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport com.android.internal.R;
4386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
442cc754651ee7245e62fa247b1ca85acb6889e79eJack Heimport java.util.HashSet;
4586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalimport java.util.List;
4686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
4786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// Describes the phone policy
4886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal//
4986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// The policy should be as decoupled from the stack as possible. In an ideal world we should not
5086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// need to have this policy talk with any non-public APIs and one way to enforce that would be to
5186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// keep this file outside the Bluetooth process. Unfortunately, keeping a separate process alive is
5286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// an expensive and a tedious task.
5386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal//
5486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// Best practices:
5586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// a) PhonePolicy should be ALL private methods
5686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal//    -- Use broadcasts which can be listened in on the BroadcastReceiver
5786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// b) NEVER call from the PhonePolicy into the Java stack, unless public APIs. It is OK to call into
5886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// the non public versions as long as public versions exist (so that a 3rd party policy can mimick)
5986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// us.
6086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal//
6186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// Policy description:
6286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal//
6386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// Policies are usually governed by outside events that may warrant an action. We talk about various
6486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// events and the resulting outcome from this policy:
6586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal//
6686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// 1. Adapter turned ON: At this point we will try to auto-connect the (device, profile) pairs which
6786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// have PRIORITY_AUTO_CONNECT. The fact that we *only* auto-connect Headset and A2DP is something
6886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// that is hardcoded and specific to phone policy (see autoConnect() function)
6986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// 2. When the profile connection-state changes: At this point if a new profile gets CONNECTED we
7086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// will try to connect other profiles on the same device. This is to avoid collision if devices
7186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal// somehow end up trying to connect at same time or general connection issues.
7286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwalclass PhonePolicy {
73dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private static final boolean DBG = true;
74dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private static final String TAG = "BluetoothPhonePolicy";
7586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
7686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    // Message types for the handler (internal messages generated by intents or timeouts)
77dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private static final int MESSAGE_PROFILE_CONNECTION_STATE_CHANGED = 1;
78dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private static final int MESSAGE_PROFILE_INIT_PRIORITIES = 2;
79dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private static final int MESSAGE_CONNECT_OTHER_PROFILES = 3;
80dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private static final int MESSAGE_ADAPTER_STATE_TURNED_ON = 4;
8186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
8286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    // Timeouts
83d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He    @VisibleForTesting
84d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He    static int sConnectOtherProfilesTimeoutMillis = 6000; // 6s
8586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
86dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private final AdapterService mAdapterService;
87dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private final ServiceFactory mFactory;
88dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private final Handler mHandler;
89dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private final HashSet<BluetoothDevice> mHeadsetRetrySet = new HashSet<>();
90dbf5c4e1c9e57fa6e5cc96ee9f442da8aa391a5dJack He    private final HashSet<BluetoothDevice> mA2dpRetrySet = new HashSet<>();
91d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He    private final HashSet<BluetoothDevice> mConnectOtherProfilesDeviceSet = new HashSet<>();
9286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
9386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    // Broadcast receiver for all changes to states of various profiles
9486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
9586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        @Override
9686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        public void onReceive(Context context, Intent intent) {
97d94f6ddab1f088340d197086949409c144ec2b0aJack He            String action = intent.getAction();
98d94f6ddab1f088340d197086949409c144ec2b0aJack He            if (action == null) {
99d94f6ddab1f088340d197086949409c144ec2b0aJack He                errorLog("Received intent with null action");
100d94f6ddab1f088340d197086949409c144ec2b0aJack He                return;
101d94f6ddab1f088340d197086949409c144ec2b0aJack He            }
102d94f6ddab1f088340d197086949409c144ec2b0aJack He            switch (action) {
103d94f6ddab1f088340d197086949409c144ec2b0aJack He                case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
104d94f6ddab1f088340d197086949409c144ec2b0aJack He                    mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED,
105c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                            BluetoothProfile.HEADSET, -1, // No-op argument
106c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                            intent).sendToTarget();
107d94f6ddab1f088340d197086949409c144ec2b0aJack He                    break;
108d94f6ddab1f088340d197086949409c144ec2b0aJack He                case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
109d94f6ddab1f088340d197086949409c144ec2b0aJack He                    mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED,
110c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                            BluetoothProfile.A2DP, -1, // No-op argument
111c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                            intent).sendToTarget();
112d94f6ddab1f088340d197086949409c144ec2b0aJack He                    break;
113d94f6ddab1f088340d197086949409c144ec2b0aJack He                case BluetoothAdapter.ACTION_STATE_CHANGED:
114d94f6ddab1f088340d197086949409c144ec2b0aJack He                    // Only pass the message on if the adapter has actually changed state from
115d94f6ddab1f088340d197086949409c144ec2b0aJack He                    // non-ON to ON. NOTE: ON is the state depicting BREDR ON and not just BLE ON.
116d94f6ddab1f088340d197086949409c144ec2b0aJack He                    int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
117d94f6ddab1f088340d197086949409c144ec2b0aJack He                    if (newState == BluetoothAdapter.STATE_ON) {
118d94f6ddab1f088340d197086949409c144ec2b0aJack He                        mHandler.obtainMessage(MESSAGE_ADAPTER_STATE_TURNED_ON).sendToTarget();
119d94f6ddab1f088340d197086949409c144ec2b0aJack He                    }
120d94f6ddab1f088340d197086949409c144ec2b0aJack He                    break;
121d94f6ddab1f088340d197086949409c144ec2b0aJack He                case BluetoothDevice.ACTION_UUID:
122d94f6ddab1f088340d197086949409c144ec2b0aJack He                    mHandler.obtainMessage(MESSAGE_PROFILE_INIT_PRIORITIES, intent).sendToTarget();
123d94f6ddab1f088340d197086949409c144ec2b0aJack He                    break;
124d94f6ddab1f088340d197086949409c144ec2b0aJack He                default:
125d94f6ddab1f088340d197086949409c144ec2b0aJack He                    Log.e(TAG, "Received unexpected intent, action=" + action);
126d94f6ddab1f088340d197086949409c144ec2b0aJack He                    break;
12786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            }
12886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
12986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    };
13086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
131928f14463ee51192039abef856495ecce418fe01Jack He    @VisibleForTesting
132928f14463ee51192039abef856495ecce418fe01Jack He    BroadcastReceiver getBroadcastReceiver() {
13386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        return mReceiver;
13486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
13586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
13686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    // Handler to handoff intents to class thread
13786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    class PhonePolicyHandler extends Handler {
13886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        PhonePolicyHandler(Looper looper) {
13986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            super(looper);
14086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
14186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
14286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        @Override
14386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        public void handleMessage(Message msg) {
14486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            switch (msg.what) {
14586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                case MESSAGE_PROFILE_INIT_PRIORITIES: {
146d94f6ddab1f088340d197086949409c144ec2b0aJack He                    Intent intent = (Intent) msg.obj;
14786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    BluetoothDevice device =
148d94f6ddab1f088340d197086949409c144ec2b0aJack He                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
149d94f6ddab1f088340d197086949409c144ec2b0aJack He                    Parcelable[] uuids = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
150d94f6ddab1f088340d197086949409c144ec2b0aJack He                    debugLog("Received ACTION_UUID for device " + device);
1510068bdad22ffd70c90de67b6e85c8a0dfa838d90Sanket Agarwal                    if (uuids != null) {
1520068bdad22ffd70c90de67b6e85c8a0dfa838d90Sanket Agarwal                        ParcelUuid[] uuidsToSend = new ParcelUuid[uuids.length];
1530068bdad22ffd70c90de67b6e85c8a0dfa838d90Sanket Agarwal                        for (int i = 0; i < uuidsToSend.length; i++) {
1540068bdad22ffd70c90de67b6e85c8a0dfa838d90Sanket Agarwal                            uuidsToSend[i] = (ParcelUuid) uuids[i];
155d94f6ddab1f088340d197086949409c144ec2b0aJack He                            debugLog("index=" + i + "uuid=" + uuidsToSend[i]);
1560068bdad22ffd70c90de67b6e85c8a0dfa838d90Sanket Agarwal                        }
1570068bdad22ffd70c90de67b6e85c8a0dfa838d90Sanket Agarwal                        processInitProfilePriorities(device, uuidsToSend);
1580068bdad22ffd70c90de67b6e85c8a0dfa838d90Sanket Agarwal                    }
159c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                }
160c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                break;
16186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
16286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                case MESSAGE_PROFILE_CONNECTION_STATE_CHANGED: {
16386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    Intent intent = (Intent) msg.obj;
16486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    BluetoothDevice device =
16586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
16686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    int prevState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);
16786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    int nextState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
16886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    processProfileStateChanged(device, msg.arg1, nextState, prevState);
169c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                }
170c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                break;
17186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
172d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He                case MESSAGE_CONNECT_OTHER_PROFILES: {
17386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    // Called when we try connect some profiles in processConnectOtherProfiles but
17486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    // we send a delayed message to try connecting the remaining profiles
175d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He                    BluetoothDevice device = (BluetoothDevice) msg.obj;
176d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He                    processConnectOtherProfiles(device);
177d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He                    mConnectOtherProfilesDeviceSet.remove(device);
17886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    break;
179d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He                }
18086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                case MESSAGE_ADAPTER_STATE_TURNED_ON:
18186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    // Call auto connect when adapter switches state to ON
1822cc754651ee7245e62fa247b1ca85acb6889e79eJack He                    resetStates();
18386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    autoConnect();
18486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    break;
18586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            }
18686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
187c4fbd756e2645147470c486ae96f2253f5e13a52Jack He    }
188c4fbd756e2645147470c486ae96f2253f5e13a52Jack He
189c4fbd756e2645147470c486ae96f2253f5e13a52Jack He    ;
19086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
19186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    // Policy API functions for lifecycle management (protected)
19286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    protected void start() {
19386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        IntentFilter filter = new IntentFilter();
19486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
19586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
19686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        filter.addAction(BluetoothDevice.ACTION_UUID);
19786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
19886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        mAdapterService.registerReceiver(mReceiver, filter);
19986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
200c4fbd756e2645147470c486ae96f2253f5e13a52Jack He
20186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    protected void cleanup() {
20286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        mAdapterService.unregisterReceiver(mReceiver);
2032cc754651ee7245e62fa247b1ca85acb6889e79eJack He        resetStates();
20486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
20586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
20686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    PhonePolicy(AdapterService service, ServiceFactory factory) {
20786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        mAdapterService = service;
20886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        mFactory = factory;
20986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        mHandler = new PhonePolicyHandler(service.getMainLooper());
21086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
21186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
21286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    // Policy implementation, all functions MUST be private
21386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    private void processInitProfilePriorities(BluetoothDevice device, ParcelUuid[] uuids) {
214d94f6ddab1f088340d197086949409c144ec2b0aJack He        debugLog("processInitProfilePriorities() - device " + device);
215f0429c92669aa388785a97f00985d63a83e509c2Hansong Zhang        HidHostService hidService = mFactory.getHidHostService();
21686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        A2dpService a2dpService = mFactory.getA2dpService();
21786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        HeadsetService headsetService = mFactory.getHeadsetService();
21886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        PanService panService = mFactory.getPanService();
21909e97f4cfcca3564c8daa864ede0eee172e9d5aaHansong Zhang        HearingAidService hearingAidService = mFactory.getHearingAidService();
22086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
22186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        // Set profile priorities only for the profiles discovered on the remote device.
22286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        // This avoids needless auto-connect attempts to profiles non-existent on the remote device
223c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        if ((hidService != null) && (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Hid)
224c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                || BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Hogp)) && (
225c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                hidService.getPriority(device) == BluetoothProfile.PRIORITY_UNDEFINED)) {
22686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            hidService.setPriority(device, BluetoothProfile.PRIORITY_ON);
22786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
22886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
22986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        // If we do not have a stored priority for HFP/A2DP (all roles) then default to on.
230c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        if ((headsetService != null) && ((BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HSP)
231c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                || BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree)) && (
232c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                headsetService.getPriority(device) == BluetoothProfile.PRIORITY_UNDEFINED))) {
23386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            headsetService.setPriority(device, BluetoothProfile.PRIORITY_ON);
23486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
23586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
236c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        if ((a2dpService != null) && (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AudioSink)
237c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                || BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AdvAudioDist)) && (
238c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                a2dpService.getPriority(device) == BluetoothProfile.PRIORITY_UNDEFINED)) {
23986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            a2dpService.setPriority(device, BluetoothProfile.PRIORITY_ON);
24086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
24186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
242c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        if ((panService != null) && (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.PANU) && (
243c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                panService.getPriority(device) == BluetoothProfile.PRIORITY_UNDEFINED)
244c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                && mAdapterService.getResources()
245c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                .getBoolean(R.bool.config_bluetooth_pan_enable_autoconnect))) {
24686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            panService.setPriority(device, BluetoothProfile.PRIORITY_ON);
24786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
24809e97f4cfcca3564c8daa864ede0eee172e9d5aaHansong Zhang
24909e97f4cfcca3564c8daa864ede0eee172e9d5aaHansong Zhang        if ((hearingAidService != null)
25009e97f4cfcca3564c8daa864ede0eee172e9d5aaHansong Zhang                && BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HearingAid)
25109e97f4cfcca3564c8daa864ede0eee172e9d5aaHansong Zhang                && (hearingAidService.getPriority(device) == BluetoothProfile.PRIORITY_UNDEFINED)) {
25209e97f4cfcca3564c8daa864ede0eee172e9d5aaHansong Zhang            debugLog("setting hearing aid profile priority for device " + device);
25309e97f4cfcca3564c8daa864ede0eee172e9d5aaHansong Zhang            hearingAidService.setPriority(device, BluetoothProfile.PRIORITY_ON);
25409e97f4cfcca3564c8daa864ede0eee172e9d5aaHansong Zhang        }
25586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
25686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
257c4fbd756e2645147470c486ae96f2253f5e13a52Jack He    private void processProfileStateChanged(BluetoothDevice device, int profileId, int nextState,
258c4fbd756e2645147470c486ae96f2253f5e13a52Jack He            int prevState) {
259d94f6ddab1f088340d197086949409c144ec2b0aJack He        debugLog("processProfileStateChanged, device=" + device + ", profile=" + profileId + ", "
260d94f6ddab1f088340d197086949409c144ec2b0aJack He                + prevState + " -> " + nextState);
261c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        if (((profileId == BluetoothProfile.A2DP) || (profileId == BluetoothProfile.HEADSET)) && (
262c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                nextState == BluetoothProfile.STATE_CONNECTED)) {
2632cc754651ee7245e62fa247b1ca85acb6889e79eJack He            switch (profileId) {
2642cc754651ee7245e62fa247b1ca85acb6889e79eJack He                case BluetoothProfile.A2DP:
2652cc754651ee7245e62fa247b1ca85acb6889e79eJack He                    mA2dpRetrySet.remove(device);
2662cc754651ee7245e62fa247b1ca85acb6889e79eJack He                    break;
2672cc754651ee7245e62fa247b1ca85acb6889e79eJack He                case BluetoothProfile.HEADSET:
2682cc754651ee7245e62fa247b1ca85acb6889e79eJack He                    mHeadsetRetrySet.remove(device);
2692cc754651ee7245e62fa247b1ca85acb6889e79eJack He                    break;
2702cc754651ee7245e62fa247b1ca85acb6889e79eJack He            }
27186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            connectOtherProfile(device);
27286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            setProfileAutoConnectionPriority(device, profileId);
27386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
27486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
27586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
2762cc754651ee7245e62fa247b1ca85acb6889e79eJack He    private void resetStates() {
2772cc754651ee7245e62fa247b1ca85acb6889e79eJack He        mHeadsetRetrySet.clear();
2782cc754651ee7245e62fa247b1ca85acb6889e79eJack He        mA2dpRetrySet.clear();
2792cc754651ee7245e62fa247b1ca85acb6889e79eJack He    }
2802cc754651ee7245e62fa247b1ca85acb6889e79eJack He
28186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    private void autoConnect() {
28286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (mAdapterService.getState() != BluetoothAdapter.STATE_ON) {
28386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            errorLog("autoConnect() - BT is not ON. Exiting autoConnect");
28486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            return;
28586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
28686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
287d94f6ddab1f088340d197086949409c144ec2b0aJack He        if (!mAdapterService.isQuietModeEnabled()) {
28886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            debugLog("autoConnect() - Initiate auto connection on BT on...");
28986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            // Phone profiles.
29086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            autoConnectHeadset();
29186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            autoConnectA2dp();
29286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        } else {
29386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            debugLog("autoConnect() - BT is in quiet mode. Not initiating auto connections");
29486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
29586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
29686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
29786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    private void autoConnectHeadset() {
298d94f6ddab1f088340d197086949409c144ec2b0aJack He        final HeadsetService hsService = mFactory.getHeadsetService();
29986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (hsService == null) {
300d94f6ddab1f088340d197086949409c144ec2b0aJack He            errorLog("autoConnectHeadset, service is null");
30186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            return;
30286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
3030612fada88b00a9896feeb9e7a16d5e3e41114f2Jack He        final BluetoothDevice[] bondedDevices = mAdapterService.getBondedDevices();
30486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (bondedDevices == null) {
305d94f6ddab1f088340d197086949409c144ec2b0aJack He            errorLog("autoConnectHeadset, bondedDevices are null");
30686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            return;
30786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
30886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        for (BluetoothDevice device : bondedDevices) {
3092bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov            int priority = hsService.getPriority(device);
3102bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov            debugLog("autoConnectHeadset, attempt auto-connect with device " + device
3112bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                     + " priority " + priority);
3122bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov            if (priority == BluetoothProfile.PRIORITY_AUTO_CONNECT) {
313d94f6ddab1f088340d197086949409c144ec2b0aJack He                debugLog("autoConnectHeadset, Connecting HFP with " + device);
31486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                hsService.connect(device);
3152bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov            } else {
3162bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                debugLog("autoConnectHeadset, skipped auto-connect with device " + device
3172bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                         + " priority " + priority);
31886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            }
31986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
32086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
32186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
32286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    private void autoConnectA2dp() {
323d94f6ddab1f088340d197086949409c144ec2b0aJack He        final A2dpService a2dpService = mFactory.getA2dpService();
324d94f6ddab1f088340d197086949409c144ec2b0aJack He        if (a2dpService == null) {
325d94f6ddab1f088340d197086949409c144ec2b0aJack He            errorLog("autoConnectA2dp, service is null");
326d94f6ddab1f088340d197086949409c144ec2b0aJack He            return;
327d94f6ddab1f088340d197086949409c144ec2b0aJack He        }
3280612fada88b00a9896feeb9e7a16d5e3e41114f2Jack He        final BluetoothDevice[] bondedDevices = mAdapterService.getBondedDevices();
329d94f6ddab1f088340d197086949409c144ec2b0aJack He        if (bondedDevices == null) {
330d94f6ddab1f088340d197086949409c144ec2b0aJack He            errorLog("autoConnectA2dp, bondedDevices are null");
33186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            return;
33286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
33386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        for (BluetoothDevice device : bondedDevices) {
3342bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov            int priority = a2dpService.getPriority(device);
3352bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov            debugLog("autoConnectA2dp, attempt auto-connect with device " + device
3362bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                     + " priority " + priority);
3372bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov            if (priority == BluetoothProfile.PRIORITY_AUTO_CONNECT) {
338d94f6ddab1f088340d197086949409c144ec2b0aJack He                debugLog("autoConnectA2dp, connecting A2DP with " + device);
339d94f6ddab1f088340d197086949409c144ec2b0aJack He                a2dpService.connect(device);
3402bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov            } else {
3412bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                debugLog("autoConnectA2dp, skipped auto-connect with device " + device
3422bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                         + " priority " + priority);
34386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            }
34486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
34586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
34686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
347d94f6ddab1f088340d197086949409c144ec2b0aJack He    private void connectOtherProfile(BluetoothDevice device) {
348d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He        if (mAdapterService.isQuietModeEnabled()) {
349d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He            debugLog("connectOtherProfile: in quiet mode, skip connect other profile " + device);
350d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He            return;
351d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He        }
352d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He        if (mConnectOtherProfilesDeviceSet.contains(device)) {
353d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He            debugLog("connectOtherProfile: already scheduled callback for " + device);
354d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He            return;
35586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
356d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He        mConnectOtherProfilesDeviceSet.add(device);
357d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He        Message m = mHandler.obtainMessage(MESSAGE_CONNECT_OTHER_PROFILES);
358d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He        m.obj = device;
359d617142a6e7567c6ae46fbbbebb58e14900cb3a1Jack He        mHandler.sendMessageDelayed(m, sConnectOtherProfilesTimeoutMillis);
36086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
36186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
36286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    // This function is called whenever a profile is connected.  This allows any other bluetooth
36386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    // profiles which are not already connected or in the process of connecting to attempt to
36486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    // connect to the device that initiated the connection.  In the event that this function is
36586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    // invoked and there are no current bluetooth connections no new profiles will be connected.
36686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    private void processConnectOtherProfiles(BluetoothDevice device) {
367d94f6ddab1f088340d197086949409c144ec2b0aJack He        debugLog("processConnectOtherProfiles, device=" + device);
36886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (mAdapterService.getState() != BluetoothAdapter.STATE_ON) {
369d94f6ddab1f088340d197086949409c144ec2b0aJack He            warnLog("processConnectOtherProfiles, adapter is not ON " + mAdapterService.getState());
37086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            return;
37186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
37286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        HeadsetService hsService = mFactory.getHeadsetService();
37386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        A2dpService a2dpService = mFactory.getA2dpService();
37486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        PanService panService = mFactory.getPanService();
37586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
37686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        boolean allProfilesEmpty = true;
37786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        List<BluetoothDevice> a2dpConnDevList = null;
37886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        List<BluetoothDevice> hsConnDevList = null;
37986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        List<BluetoothDevice> panConnDevList = null;
38086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
38186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (hsService != null) {
38286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            hsConnDevList = hsService.getConnectedDevices();
38386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            allProfilesEmpty = allProfilesEmpty && hsConnDevList.isEmpty();
38486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
38586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (a2dpService != null) {
38686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            a2dpConnDevList = a2dpService.getConnectedDevices();
38786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            allProfilesEmpty = allProfilesEmpty && a2dpConnDevList.isEmpty();
38886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
38986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (panService != null) {
39086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            panConnDevList = panService.getConnectedDevices();
39186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            allProfilesEmpty = allProfilesEmpty && panConnDevList.isEmpty();
39286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
39386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
39486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (allProfilesEmpty) {
3952cc754651ee7245e62fa247b1ca85acb6889e79eJack He            // considered as fully disconnected, don't bother connecting others.
396d94f6ddab1f088340d197086949409c144ec2b0aJack He            debugLog("processConnectOtherProfiles, all profiles disconnected for " + device);
3972cc754651ee7245e62fa247b1ca85acb6889e79eJack He            // reset retry status so that in the next round we can start retrying connections again
3982cc754651ee7245e62fa247b1ca85acb6889e79eJack He            resetStates();
39986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            return;
40086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
40186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
40286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (hsService != null) {
40344ef48a20624b290ae614af344c802d6e749eba2Pavlin Radoslavov            if (!mHeadsetRetrySet.contains(device) && (
404c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                    hsService.getPriority(device) >= BluetoothProfile.PRIORITY_ON) && (
405c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                    hsService.getConnectionState(device) == BluetoothProfile.STATE_DISCONNECTED)) {
406d94f6ddab1f088340d197086949409c144ec2b0aJack He                debugLog("Retrying connection to Headset with device " + device);
4072cc754651ee7245e62fa247b1ca85acb6889e79eJack He                mHeadsetRetrySet.add(device);
40886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                hsService.connect(device);
40986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            }
41086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
41186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (a2dpService != null) {
41244ef48a20624b290ae614af344c802d6e749eba2Pavlin Radoslavov            if (!mA2dpRetrySet.contains(device) && (
413c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                    a2dpService.getPriority(device) >= BluetoothProfile.PRIORITY_ON) && (
414c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                    a2dpService.getConnectionState(device)
415c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                            == BluetoothProfile.STATE_DISCONNECTED)) {
41686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                debugLog("Retrying connection to A2DP with device " + device);
4172cc754651ee7245e62fa247b1ca85acb6889e79eJack He                mA2dpRetrySet.add(device);
41886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                a2dpService.connect(device);
41986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            }
42086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
42186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        if (panService != null) {
42244ef48a20624b290ae614af344c802d6e749eba2Pavlin Radoslavov            // TODO: the panConnDevList.isEmpty() check below should be removed once
42344ef48a20624b290ae614af344c802d6e749eba2Pavlin Radoslavov            // Multi-PAN is supported.
424c4fbd756e2645147470c486ae96f2253f5e13a52Jack He            if (panConnDevList.isEmpty() && (panService.getPriority(device)
425c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                    >= BluetoothProfile.PRIORITY_ON) && (panService.getConnectionState(device)
426c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                    == BluetoothProfile.STATE_DISCONNECTED)) {
427d94f6ddab1f088340d197086949409c144ec2b0aJack He                debugLog("Retrying connection to PAN with device " + device);
42886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                panService.connect(device);
42986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            }
43086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
43186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
43286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
433d94f6ddab1f088340d197086949409c144ec2b0aJack He    private void setProfileAutoConnectionPriority(BluetoothDevice device, int profileId) {
43486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        switch (profileId) {
43586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            case BluetoothProfile.HEADSET:
43686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                HeadsetService hsService = mFactory.getHeadsetService();
437c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                if ((hsService != null) && (BluetoothProfile.PRIORITY_AUTO_CONNECT
438c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                        != hsService.getPriority(device))) {
439eb2e34735003388606ff66988634d012049cdcd0xutianguo                    List<BluetoothDevice> deviceList = hsService.getConnectedDevices();
44086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    adjustOtherHeadsetPriorities(hsService, deviceList);
44186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    hsService.setPriority(device, BluetoothProfile.PRIORITY_AUTO_CONNECT);
44286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                }
44386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                break;
44486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
44586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            case BluetoothProfile.A2DP:
44686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                A2dpService a2dpService = mFactory.getA2dpService();
44786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                if ((a2dpService != null) && (BluetoothProfile.PRIORITY_AUTO_CONNECT
448c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                        != a2dpService.getPriority(device))) {
4492bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                    List<BluetoothDevice> deviceList = a2dpService.getConnectedDevices();
4502bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                    adjustOtherSinkPriorities(a2dpService, deviceList);
45186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    a2dpService.setPriority(device, BluetoothProfile.PRIORITY_AUTO_CONNECT);
45286c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                }
45386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                break;
45486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
45586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            default:
456d94f6ddab1f088340d197086949409c144ec2b0aJack He                Log.w(TAG, "Tried to set AutoConnect priority on invalid profile " + profileId);
45786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                break;
45886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
45986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
46086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
461c4fbd756e2645147470c486ae96f2253f5e13a52Jack He    private void adjustOtherHeadsetPriorities(HeadsetService hsService,
462c4fbd756e2645147470c486ae96f2253f5e13a52Jack He            List<BluetoothDevice> connectedDeviceList) {
46386c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        for (BluetoothDevice device : mAdapterService.getBondedDevices()) {
46486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            if (hsService.getPriority(device) >= BluetoothProfile.PRIORITY_AUTO_CONNECT
46586c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                    && !connectedDeviceList.contains(device)) {
4662bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                debugLog("adjustOtherHeadsetPriorities, device " + device + " PRIORITY_ON");
46786c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                hsService.setPriority(device, BluetoothProfile.PRIORITY_ON);
46886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            }
46986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
47086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
47186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal
472c4fbd756e2645147470c486ae96f2253f5e13a52Jack He    private void adjustOtherSinkPriorities(A2dpService a2dpService,
4732bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov            List<BluetoothDevice> connectedDeviceList) {
47486c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        for (BluetoothDevice device : mAdapterService.getBondedDevices()) {
4752bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov            if (a2dpService.getPriority(device) >= BluetoothProfile.PRIORITY_AUTO_CONNECT
4762bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                    && !connectedDeviceList.contains(device)) {
4772bb921516fddb3947f8a3dc199d142a9690b063aPavlin Radoslavov                debugLog("adjustOtherSinkPriorities, device " + device + " PRIORITY_ON");
47886c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal                a2dpService.setPriority(device, BluetoothProfile.PRIORITY_ON);
47986c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal            }
48086c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal        }
48186c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal    }
482d94f6ddab1f088340d197086949409c144ec2b0aJack He
483d94f6ddab1f088340d197086949409c144ec2b0aJack He    private static void debugLog(String msg) {
484c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        if (DBG) {
48509e97f4cfcca3564c8daa864ede0eee172e9d5aaHansong Zhang            Log.i(TAG, msg);
486c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        }
487d94f6ddab1f088340d197086949409c144ec2b0aJack He    }
488d94f6ddab1f088340d197086949409c144ec2b0aJack He
489d94f6ddab1f088340d197086949409c144ec2b0aJack He    private static void warnLog(String msg) {
490d94f6ddab1f088340d197086949409c144ec2b0aJack He        Log.w(TAG, msg);
491d94f6ddab1f088340d197086949409c144ec2b0aJack He    }
492d94f6ddab1f088340d197086949409c144ec2b0aJack He
493d94f6ddab1f088340d197086949409c144ec2b0aJack He    private static void errorLog(String msg) {
494d94f6ddab1f088340d197086949409c144ec2b0aJack He        Log.e(TAG, msg);
495d94f6ddab1f088340d197086949409c144ec2b0aJack He    }
49686c29fe88456bdcfbd4334647b04ef81ff384a06Sanket Agarwal}
497