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