Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright (C) 2009 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 com.android.internal.app.AlertActivity;
     20 import com.android.internal.app.AlertController;
     21 import com.android.settings.R;
     22 
     23 import android.app.Activity;
     24 import android.bluetooth.BluetoothAdapter;
     25 import android.content.DialogInterface;
     26 import android.content.Intent;
     27 import android.os.Bundle;
     28 import android.util.Log;
     29 import android.view.View;
     30 import android.widget.TextView;
     31 
     32 /**
     33  * RequestPermissionHelperActivity asks the user whether to enable discovery.
     34  * This is usually started by RequestPermissionActivity.
     35  */
     36 public class RequestPermissionHelperActivity extends AlertActivity implements
     37         DialogInterface.OnClickListener {
     38     private static final String TAG = "RequestPermissionHelperActivity";
     39 
     40     public static final String ACTION_INTERNAL_REQUEST_BT_ON =
     41         "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON";
     42 
     43     public static final String ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE =
     44         "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE";
     45 
     46     private LocalBluetoothAdapter mLocalAdapter;
     47 
     48     private int mTimeout;
     49 
     50     // True if requesting BT to be turned on
     51     // False if requesting BT to be turned on + discoverable mode
     52     private boolean mEnableOnly;
     53 
     54     @Override
     55     protected void onCreate(Bundle savedInstanceState) {
     56         super.onCreate(savedInstanceState);
     57 
     58         // Note: initializes mLocalAdapter and returns true on error
     59         if (parseIntent()) {
     60             finish();
     61             return;
     62         }
     63 
     64         createDialog();
     65 
     66         if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog) == true) {
     67             // dismiss dialog immediately if settings say so
     68             onClick(null, BUTTON_POSITIVE);
     69             dismiss();
     70         }
     71     }
     72 
     73     void createDialog() {
     74         final AlertController.AlertParams p = mAlertParams;
     75 
     76         if (mEnableOnly) {
     77             p.mMessage = getString(R.string.bluetooth_ask_enablement);
     78         } else {
     79             if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
     80                 p.mMessage = getString(R.string.bluetooth_ask_enablement_and_lasting_discovery);
     81             } else {
     82                 p.mMessage = getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout);
     83             }
     84         }
     85 
     86         p.mPositiveButtonText = getString(R.string.allow);
     87         p.mPositiveButtonListener = this;
     88         p.mNegativeButtonText = getString(R.string.deny);
     89         p.mNegativeButtonListener = this;
     90 
     91         setupAlert();
     92     }
     93 
     94     public void onClick(DialogInterface dialog, int which) {
     95         int returnCode;
     96         // FIXME: fix this ugly switch logic!
     97         switch (which) {
     98             case BUTTON_POSITIVE:
     99                 int btState = 0;
    100 
    101                 try {
    102                     // TODO There's a better way.
    103                     int retryCount = 30;
    104                     do {
    105                         btState = mLocalAdapter.getBluetoothState();
    106                         Thread.sleep(100);
    107                     } while (btState == BluetoothAdapter.STATE_TURNING_OFF && --retryCount > 0);
    108                 } catch (InterruptedException ignored) {
    109                     // don't care
    110                 }
    111 
    112                 if (btState == BluetoothAdapter.STATE_TURNING_ON
    113                         || btState == BluetoothAdapter.STATE_ON
    114                         || mLocalAdapter.enable()) {
    115                     returnCode = RequestPermissionActivity.RESULT_BT_STARTING_OR_STARTED;
    116                 } else {
    117                     returnCode = RESULT_CANCELED;
    118                 }
    119                 break;
    120 
    121             case BUTTON_NEGATIVE:
    122                 returnCode = RESULT_CANCELED;
    123                 break;
    124             default:
    125                 return;
    126         }
    127         setResult(returnCode);
    128     }
    129 
    130     /**
    131      * Parse the received Intent and initialize mLocalBluetoothAdapter.
    132      * @return true if an error occurred; false otherwise
    133      */
    134     private boolean parseIntent() {
    135         Intent intent = getIntent();
    136         if (intent != null && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON)) {
    137             mEnableOnly = true;
    138         } else if (intent != null
    139                 && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE)) {
    140             mEnableOnly = false;
    141             // Value used for display purposes. Not range checking.
    142             mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
    143                     BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
    144         } else {
    145             setResult(RESULT_CANCELED);
    146             return true;
    147         }
    148 
    149         LocalBluetoothManager manager = LocalBluetoothManager.getInstance(this);
    150         if (manager == null) {
    151             Log.e(TAG, "Error: there's a problem starting Bluetooth");
    152             setResult(RESULT_CANCELED);
    153             return true;
    154         }
    155         mLocalAdapter = manager.getBluetoothAdapter();
    156 
    157         return false;
    158     }
    159 
    160     @Override
    161     public void onBackPressed() {
    162         setResult(RESULT_CANCELED);
    163         super.onBackPressed();
    164     }
    165 }
    166