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 LocalBluetoothManager mLocalManager; 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 onCreate(Bundle savedInstanceState)55 protected void onCreate(Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 58 if (parseIntent()) { 59 finish(); 60 return; 61 } 62 63 createDialog(); 64 } 65 createDialog()66 void createDialog() { 67 final AlertController.AlertParams p = mAlertParams; 68 p.mIconId = android.R.drawable.ic_dialog_info; 69 p.mTitle = getString(R.string.bluetooth_permission_request); 70 71 View view = getLayoutInflater().inflate(R.layout.bluetooth_discoverable, null); 72 p.mView = view; 73 TextView tv = (TextView) view.findViewById(R.id.message); 74 75 if (mEnableOnly) { 76 tv.setText(getString(R.string.bluetooth_ask_enablement)); 77 } else { 78 tv.setText(getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout)); 79 } 80 81 p.mPositiveButtonText = getString(R.string.yes); 82 p.mPositiveButtonListener = this; 83 p.mNegativeButtonText = getString(R.string.no); 84 p.mNegativeButtonListener = this; 85 86 setupAlert(); 87 } 88 onClick(DialogInterface dialog, int which)89 public void onClick(DialogInterface dialog, int which) { 90 int returnCode; 91 switch (which) { 92 case DialogInterface.BUTTON_POSITIVE: 93 int btState = 0; 94 95 try { 96 // TODO There's a better way. 97 int retryCount = 30; 98 do { 99 btState = mLocalManager.getBluetoothState(); 100 Thread.sleep(100); 101 } while (btState == BluetoothAdapter.STATE_TURNING_OFF && --retryCount > 0); 102 } catch (InterruptedException e) { 103 // don't care 104 } 105 106 if (btState == BluetoothAdapter.STATE_TURNING_ON 107 || btState == BluetoothAdapter.STATE_ON 108 || mLocalManager.getBluetoothAdapter().enable()) { 109 returnCode = RequestPermissionActivity.RESULT_BT_STARTING_OR_STARTED; 110 } else { 111 returnCode = Activity.RESULT_CANCELED; 112 } 113 break; 114 115 case DialogInterface.BUTTON_NEGATIVE: 116 returnCode = Activity.RESULT_CANCELED; 117 break; 118 default: 119 return; 120 } 121 setResult(returnCode); 122 } 123 parseIntent()124 private boolean parseIntent() { 125 Intent intent = getIntent(); 126 if (intent != null && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON)) { 127 mEnableOnly = true; 128 } else if (intent != null 129 && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE)) { 130 mEnableOnly = false; 131 // Value used for display purposes. Not range checking. 132 mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 133 BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT); 134 } else { 135 setResult(Activity.RESULT_CANCELED); 136 return true; 137 } 138 139 mLocalManager = LocalBluetoothManager.getInstance(this); 140 if (mLocalManager == null) { 141 Log.e(TAG, "Error: there's a problem starting bluetooth"); 142 setResult(Activity.RESULT_CANCELED); 143 return true; 144 } 145 146 return false; 147 } 148 149 @Override onBackPressed()150 public void onBackPressed() { 151 setResult(Activity.RESULT_CANCELED); 152 super.onBackPressed(); 153 } 154 } 155