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.Context;
23import android.content.Intent;
24import android.os.Bundle;
25import android.os.UserManager;
26import android.view.Menu;
27import android.view.MenuInflater;
28import android.view.MenuItem;
29
30import com.android.internal.logging.MetricsProto.MetricsEvent;
31import com.android.settings.R;
32import com.android.settingslib.bluetooth.CachedBluetoothDevice;
33
34import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
35
36/**
37 * BluetoothSettings is the Settings screen for Bluetooth configuration and
38 * connection management.
39 */
40public final class DevicePickerFragment extends DeviceListPreferenceFragment {
41    private static final int MENU_ID_REFRESH = Menu.FIRST;
42
43    public DevicePickerFragment() {
44        super(null /* Not tied to any user restrictions. */);
45    }
46
47    private boolean mNeedAuth;
48    private String mLaunchPackage;
49    private String mLaunchClass;
50    private boolean mStartScanOnResume;
51
52    @Override
53    void addPreferencesForActivity() {
54        addPreferencesFromResource(R.xml.device_picker);
55
56        Intent intent = getActivity().getIntent();
57        mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
58        setFilter(intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
59                BluetoothDevicePicker.FILTER_TYPE_ALL));
60        mLaunchPackage = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE);
61        mLaunchClass = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS);
62    }
63
64    @Override
65    void initDevicePreference(BluetoothDevicePreference preference) {
66        preference.setWidgetLayoutResource(R.layout.preference_empty_list);
67    }
68
69    @Override
70    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
71        menu.add(Menu.NONE, MENU_ID_REFRESH, 0, R.string.bluetooth_search_for_devices)
72                .setEnabled(true)
73                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
74        super.onCreateOptionsMenu(menu, inflater);
75    }
76
77    @Override
78    public boolean onOptionsItemSelected(MenuItem item) {
79        switch (item.getItemId()) {
80            case MENU_ID_REFRESH:
81                mLocalAdapter.startScanning(true);
82                return true;
83        }
84        return super.onOptionsItemSelected(item);
85    }
86
87    @Override
88    protected int getMetricsCategory() {
89        return MetricsEvent.BLUETOOTH_DEVICE_PICKER;
90    }
91
92    @Override
93    public void onCreate(Bundle savedInstanceState) {
94        super.onCreate(savedInstanceState);
95        getActivity().setTitle(getString(R.string.device_picker));
96        UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
97        mStartScanOnResume = !um.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)
98                && (savedInstanceState == null);  // don't start scan after rotation
99        setHasOptionsMenu(true);
100    }
101
102    @Override
103    public void onResume() {
104        super.onResume();
105        addCachedDevices();
106        if (mStartScanOnResume) {
107            mLocalAdapter.startScanning(true);
108            mStartScanOnResume = false;
109        }
110    }
111
112    @Override
113    void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
114        mLocalAdapter.stopScanning();
115        LocalBluetoothPreferences.persistSelectedDeviceInPicker(
116                getActivity(), mSelectedDevice.getAddress());
117        if ((btPreference.getCachedDevice().getBondState() ==
118                BluetoothDevice.BOND_BONDED) || !mNeedAuth) {
119            sendDevicePickedIntent(mSelectedDevice);
120            finish();
121        } else {
122            super.onDevicePreferenceClick(btPreference);
123        }
124    }
125
126    public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice,
127            int bondState) {
128        if (bondState == BluetoothDevice.BOND_BONDED) {
129            BluetoothDevice device = cachedDevice.getDevice();
130            if (device.equals(mSelectedDevice)) {
131                sendDevicePickedIntent(device);
132                finish();
133            }
134        }
135    }
136
137    @Override
138    public void onBluetoothStateChanged(int bluetoothState) {
139        super.onBluetoothStateChanged(bluetoothState);
140
141        if (bluetoothState == BluetoothAdapter.STATE_ON) {
142            mLocalAdapter.startScanning(false);
143        }
144    }
145
146    private void sendDevicePickedIntent(BluetoothDevice device) {
147        Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
148        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
149        if (mLaunchPackage != null && mLaunchClass != null) {
150            intent.setClassName(mLaunchPackage, mLaunchClass);
151        }
152        getActivity().sendBroadcast(intent);
153    }
154}
155