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