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
17package com.android.settings.bluetooth;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothDevice;
21import android.bluetooth.BluetoothDevicePicker;
22import android.content.Intent;
23import android.os.Bundle;
24
25import com.android.settings.R;
26
27/**
28 * BluetoothSettings is the Settings screen for Bluetooth configuration and
29 * connection management.
30 */
31public final class DevicePickerFragment extends DeviceListPreferenceFragment {
32
33    private boolean mNeedAuth;
34    private String mLaunchPackage;
35    private String mLaunchClass;
36    private boolean mStartScanOnResume;
37
38    @Override
39    void addPreferencesForActivity() {
40        addPreferencesFromResource(R.xml.device_picker);
41
42        Intent intent = getActivity().getIntent();
43        mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
44        setFilter(intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
45                BluetoothDevicePicker.FILTER_TYPE_ALL));
46        mLaunchPackage = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE);
47        mLaunchClass = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS);
48    }
49
50    @Override
51    public void onCreate(Bundle savedInstanceState) {
52        super.onCreate(savedInstanceState);
53        getActivity().setTitle(getString(R.string.device_picker));
54        mStartScanOnResume = (savedInstanceState == null);  // don't start scan after rotation
55    }
56
57    @Override
58    public void onResume() {
59        super.onResume();
60        addCachedDevices();
61        if (mStartScanOnResume) {
62            mLocalAdapter.startScanning(true);
63            mStartScanOnResume = false;
64        }
65    }
66
67    @Override
68    void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
69        mLocalAdapter.stopScanning();
70        LocalBluetoothPreferences.persistSelectedDeviceInPicker(
71                getActivity(), mSelectedDevice.getAddress());
72        if ((btPreference.getCachedDevice().getBondState() ==
73                BluetoothDevice.BOND_BONDED) || !mNeedAuth) {
74            sendDevicePickedIntent(mSelectedDevice);
75            finish();
76        } else {
77            super.onDevicePreferenceClick(btPreference);
78        }
79    }
80
81    public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice,
82            int bondState) {
83        if (bondState == BluetoothDevice.BOND_BONDED) {
84            BluetoothDevice device = cachedDevice.getDevice();
85            if (device.equals(mSelectedDevice)) {
86                sendDevicePickedIntent(device);
87                finish();
88            }
89        }
90    }
91
92    @Override
93    public void onBluetoothStateChanged(int bluetoothState) {
94        super.onBluetoothStateChanged(bluetoothState);
95
96        if (bluetoothState == BluetoothAdapter.STATE_ON) {
97            mLocalAdapter.startScanning(false);
98        }
99    }
100
101    private void sendDevicePickedIntent(BluetoothDevice device) {
102        Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
103        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
104        if (mLaunchPackage != null && mLaunchClass != null) {
105            intent.setClassName(mLaunchPackage, mLaunchClass);
106        }
107        getActivity().sendBroadcast(intent);
108    }
109}
110