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