1e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan/* 2e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * Copyright (C) 2009 The Android Open Source Project 3e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * 4e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * Licensed under the Apache License, Version 2.0 (the "License"); 5e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * you may not use this file except in compliance with the License. 6e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * You may obtain a copy of the License at 7e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * 8e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * http://www.apache.org/licenses/LICENSE-2.0 9e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * 10e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * Unless required by applicable law or agreed to in writing, software 11e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * distributed under the License is distributed on an "AS IS" BASIS, 12e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * See the License for the specific language governing permissions and 14e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * limitations under the License. 15e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan */ 16e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 17e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanpackage com.android.settings.bluetooth; 18e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 19834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chanimport com.android.internal.app.AlertActivity; 20834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chanimport com.android.internal.app.AlertController; 21834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chanimport com.android.settings.R; 22834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chan 23834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chanimport android.app.Activity; 24e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.bluetooth.BluetoothAdapter; 25e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.content.DialogInterface; 26e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.content.Intent; 27e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.os.Bundle; 28e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.util.Log; 29e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.view.View; 30e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.widget.TextView; 31e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 32e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan/** 33e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * RequestPermissionHelperActivity asks the user whether to enable discovery. 34e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * This is usually started by RequestPermissionActivity. 35e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan */ 36e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanpublic class RequestPermissionHelperActivity extends AlertActivity implements 37e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan DialogInterface.OnClickListener { 38e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private static final String TAG = "RequestPermissionHelperActivity"; 39e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 40e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan public static final String ACTION_INTERNAL_REQUEST_BT_ON = 41e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON"; 42e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 43e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan public static final String ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE = 44e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE"; 45e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 46436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby private LocalBluetoothAdapter mLocalAdapter; 47e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 48e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private int mTimeout; 49e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 50e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // True if requesting BT to be turned on 51e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // False if requesting BT to be turned on + discoverable mode 52e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private boolean mEnableOnly; 53e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 54e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan @Override 55e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan protected void onCreate(Bundle savedInstanceState) { 56e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan super.onCreate(savedInstanceState); 57e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 58436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby // Note: initializes mLocalAdapter and returns true on error 59e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (parseIntent()) { 60e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan finish(); 61e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return; 62e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 63e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 64e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan createDialog(); 6531c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen 6631c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog) == true) { 6731c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen // dismiss dialog immediately if settings say so 6831c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen onClick(null, BUTTON_POSITIVE); 6931c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen dismiss(); 7031c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen } 71e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 72e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 73e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan void createDialog() { 74e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan final AlertController.AlertParams p = mAlertParams; 75e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 76e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (mEnableOnly) { 7795c1003f1c6ea957065fa752d89c9f64b599461cJake Hamby p.mMessage = getString(R.string.bluetooth_ask_enablement); 78e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 791b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) { 8095c1003f1c6ea957065fa752d89c9f64b599461cJake Hamby p.mMessage = getString(R.string.bluetooth_ask_enablement_and_lasting_discovery); 811b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren } else { 8295c1003f1c6ea957065fa752d89c9f64b599461cJake Hamby p.mMessage = getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout); 831b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren } 84e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 85e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 8695c1003f1c6ea957065fa752d89c9f64b599461cJake Hamby p.mPositiveButtonText = getString(R.string.allow); 87e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan p.mPositiveButtonListener = this; 8895c1003f1c6ea957065fa752d89c9f64b599461cJake Hamby p.mNegativeButtonText = getString(R.string.deny); 89e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan p.mNegativeButtonListener = this; 90e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 91e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan setupAlert(); 92e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 93e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 94e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan public void onClick(DialogInterface dialog, int which) { 95e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan int returnCode; 96436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby // FIXME: fix this ugly switch logic! 97e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan switch (which) { 98436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby case BUTTON_POSITIVE: 99e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan int btState = 0; 100e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 101e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan try { 102e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // TODO There's a better way. 103e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan int retryCount = 30; 104e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan do { 105436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby btState = mLocalAdapter.getBluetoothState(); 106e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan Thread.sleep(100); 107e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } while (btState == BluetoothAdapter.STATE_TURNING_OFF && --retryCount > 0); 108436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby } catch (InterruptedException ignored) { 109e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // don't care 110e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 111e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 112e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (btState == BluetoothAdapter.STATE_TURNING_ON 113e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan || btState == BluetoothAdapter.STATE_ON 114436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby || mLocalAdapter.enable()) { 115e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan returnCode = RequestPermissionActivity.RESULT_BT_STARTING_OR_STARTED; 116e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 117436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby returnCode = RESULT_CANCELED; 118e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 119e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan break; 120e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 121436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby case BUTTON_NEGATIVE: 122436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby returnCode = RESULT_CANCELED; 123e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan break; 124e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan default: 125e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return; 126e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 127e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan setResult(returnCode); 128e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 129e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 130436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby /** 131436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby * Parse the received Intent and initialize mLocalBluetoothAdapter. 132436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby * @return true if an error occurred; false otherwise 133436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby */ 134e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private boolean parseIntent() { 135e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan Intent intent = getIntent(); 136e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (intent != null && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON)) { 137e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mEnableOnly = true; 138e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else if (intent != null 139e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE)) { 140e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mEnableOnly = false; 141e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // Value used for display purposes. Not range checking. 142e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 143e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT); 144e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 145436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 146e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return true; 147e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 148e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 149436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby LocalBluetoothManager manager = LocalBluetoothManager.getInstance(this); 150436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby if (manager == null) { 151436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby Log.e(TAG, "Error: there's a problem starting Bluetooth"); 152436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 153e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return true; 154e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 155436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby mLocalAdapter = manager.getBluetoothAdapter(); 156e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 157e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return false; 158e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 159e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 160e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan @Override 161e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan public void onBackPressed() { 162436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 163e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan super.onBackPressed(); 164e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 165e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan} 166