1b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh/*
2b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * Copyright (c) 2008-2009, Motorola, Inc.
3b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh *
4b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * All rights reserved.
5b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh *
6b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * Redistribution and use in source and binary forms, with or without
7b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * modification, are permitted provided that the following conditions are met:
8b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh *
9b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * - Redistributions of source code must retain the above copyright notice,
10b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * this list of conditions and the following disclaimer.
11b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh *
12b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * - Redistributions in binary form must reproduce the above copyright notice,
13b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * this list of conditions and the following disclaimer in the documentation
14b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * and/or other materials provided with the distribution.
15b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh *
16b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * - Neither the name of the Motorola, Inc. nor the names of its contributors
17b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * may be used to endorse or promote products derived from this software
18b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * without specific prior written permission.
19b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh *
20b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh * POSSIBILITY OF SUCH DAMAGE.
31b9cd7fee03a41e56a0cea9aa2e1af3b28c5be590Jaikumar Ganesh */
322c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan
332c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fanpackage com.android.bluetooth.pbap;
342c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan
3541ef8d494511c040451f2f887cb31c3100746b61Nick Pellyimport android.bluetooth.BluetoothAdapter;
362c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fanimport android.content.BroadcastReceiver;
372c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fanimport android.content.Context;
382c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fanimport android.content.Intent;
392c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fanimport android.util.Log;
402c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan
412c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fanpublic class BluetoothPbapReceiver extends BroadcastReceiver {
422c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan
432c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan    private static final String TAG = "BluetoothPbapReceiver";
442c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan
454446eaa935994bc91d6d308303e8d27526b4590dLixin Yue    private static final boolean V = BluetoothPbapService.VERBOSE;
462c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan
472c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan    @Override
482c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan    public void onReceive(Context context, Intent intent) {
496654f5c903de510a70f9e72cd5ad7837b615d93ffredc        if (V) Log.v(TAG, "PbapReceiver onReceive ");
504446eaa935994bc91d6d308303e8d27526b4590dLixin Yue
51d896162ef022a5607b13bae5177637ff3f18b737Lixin Yue        Intent in = new Intent();
52d896162ef022a5607b13bae5177637ff3f18b737Lixin Yue        in.putExtras(intent);
53d896162ef022a5607b13bae5177637ff3f18b737Lixin Yue        in.setClass(context, BluetoothPbapService.class);
542c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan        String action = intent.getAction();
55d896162ef022a5607b13bae5177637ff3f18b737Lixin Yue        in.putExtra("action", action);
566654f5c903de510a70f9e72cd5ad7837b615d93ffredc        if (V) Log.v(TAG,"***********action = " + action);
576654f5c903de510a70f9e72cd5ad7837b615d93ffredc
584446eaa935994bc91d6d308303e8d27526b4590dLixin Yue        boolean startService = true;
59f4a286ee2aba5a75abe41881334bbdb5d0080105Nick Pelly        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
60613bae227d65f61903e196944a9c718b4394f25aNick Pelly            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
61f4a286ee2aba5a75abe41881334bbdb5d0080105Nick Pelly            in.putExtra(BluetoothAdapter.EXTRA_STATE, state);
626654f5c903de510a70f9e72cd5ad7837b615d93ffredc            if (V) Log.v(TAG,"***********state = " + state);
63f4a286ee2aba5a75abe41881334bbdb5d0080105Nick Pelly            if ((state == BluetoothAdapter.STATE_TURNING_ON)
6415d36984a79d6e35c659edb0efdf929f0b526bd5Fred                    || (state == BluetoothAdapter.STATE_OFF)) {
6515d36984a79d6e35c659edb0efdf929f0b526bd5Fred                //FIX: We turn on PBAP after BluetoothAdapter.STATE_ON,
6615d36984a79d6e35c659edb0efdf929f0b526bd5Fred                //but we turn off PBAP right after BluetoothAdapter.STATE_TURNING_OFF
674446eaa935994bc91d6d308303e8d27526b4590dLixin Yue                startService = false;
682c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan            }
697d6a3299b58c5b53de4016a2e199974b94ae90dfMathias Jeppsson        } else {
707d6a3299b58c5b53de4016a2e199974b94ae90dfMathias Jeppsson            // Don't forward intent unless device has bluetooth and bluetooth is enabled.
717d6a3299b58c5b53de4016a2e199974b94ae90dfMathias Jeppsson            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
727d6a3299b58c5b53de4016a2e199974b94ae90dfMathias Jeppsson            if (adapter == null || !adapter.isEnabled()) {
737d6a3299b58c5b53de4016a2e199974b94ae90dfMathias Jeppsson                startService = false;
747d6a3299b58c5b53de4016a2e199974b94ae90dfMathias Jeppsson            }
752c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan        }
764446eaa935994bc91d6d308303e8d27526b4590dLixin Yue        if (startService) {
776654f5c903de510a70f9e72cd5ad7837b615d93ffredc            if (V) Log.v(TAG,"***********Calling start service!!!! with action = " + in.getAction());
784446eaa935994bc91d6d308303e8d27526b4590dLixin Yue            context.startService(in);
792c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan        }
802c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan    }
812c282d5898ac0916470ebfa9ff26ba784cf4bb24Jackson Fan}
82