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         p.mIconId = android.R.drawable.ic_dialog_info;
     76         p.mTitle = getString(R.string.bluetooth_permission_request);
     77 
     78         View view = getLayoutInflater().inflate(R.layout.bluetooth_discoverable, null);
     79         p.mView = view;
     80         TextView tv = (TextView) view.findViewById(R.id.message);
     81 
     82         if (mEnableOnly) {
     83             tv.setText(getString(R.string.bluetooth_ask_enablement));
     84         } else {
     85             if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
     86                 tv.setText(getString(R.string.bluetooth_ask_enablement_and_lasting_discovery));
     87             } else {
     88                 tv.setText(getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout));
     89             }
     90         }
     91 
     92         p.mPositiveButtonText = getString(R.string.yes);
     93         p.mPositiveButtonListener = this;
     94         p.mNegativeButtonText = getString(R.string.no);
     95         p.mNegativeButtonListener = this;
     96 
     97         setupAlert();
     98     }
     99 
    100     public void onClick(DialogInterface dialog, int which) {
    101         int returnCode;
    102         // FIXME: fix this ugly switch logic!
    103         switch (which) {
    104             case BUTTON_POSITIVE:
    105                 int btState = 0;
    106 
    107                 try {
    108                     // TODO There's a better way.
    109                     int retryCount = 30;
    110                     do {
    111                         btState = mLocalAdapter.getBluetoothState();
    112                         Thread.sleep(100);
    113                     } while (btState == BluetoothAdapter.STATE_TURNING_OFF && --retryCount > 0);
    114                 } catch (InterruptedException ignored) {
    115                     // don't care
    116                 }
    117 
    118                 if (btState == BluetoothAdapter.STATE_TURNING_ON
    119                         || btState == BluetoothAdapter.STATE_ON
    120                         || mLocalAdapter.enable()) {
    121                     returnCode = RequestPermissionActivity.RESULT_BT_STARTING_OR_STARTED;
    122                 } else {
    123                     returnCode = RESULT_CANCELED;
    124                 }
    125                 break;
    126 
    127             case BUTTON_NEGATIVE:
    128                 returnCode = RESULT_CANCELED;
    129                 break;
    130             default:
    131                 return;
    132         }
    133         setResult(returnCode);
    134     }
    135 
    136     /**
    137      * Parse the received Intent and initialize mLocalBluetoothAdapter.
    138      * @return true if an error occurred; false otherwise
    139      */
    140     private boolean parseIntent() {
    141         Intent intent = getIntent();
    142         if (intent != null && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON)) {
    143             mEnableOnly = true;
    144         } else if (intent != null
    145                 && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE)) {
    146             mEnableOnly = false;
    147             // Value used for display purposes. Not range checking.
    148             mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
    149                     BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
    150         } else {
    151             setResult(RESULT_CANCELED);
    152             return true;
    153         }
    154 
    155         LocalBluetoothManager manager = LocalBluetoothManager.getInstance(this);
    156         if (manager == null) {
    157             Log.e(TAG, "Error: there's a problem starting Bluetooth");
    158             setResult(RESULT_CANCELED);
    159             return true;
    160         }
    161         mLocalAdapter = manager.getBluetoothAdapter();
    162 
    163         return false;
    164     }
    165 
    166     @Override
    167     public void onBackPressed() {
    168         setResult(RESULT_CANCELED);
    169         super.onBackPressed();
    170     }
    171 }
    172