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 android.app.Activity; 20 import android.app.admin.DevicePolicyManager; 21 import android.bluetooth.BluetoothAdapter; 22 import android.content.DialogInterface; 23 import android.content.Intent; 24 import android.content.pm.ApplicationInfo; 25 import android.content.pm.PackageManager; 26 import android.os.Bundle; 27 import android.os.UserManager; 28 import android.support.annotation.NonNull; 29 import android.text.TextUtils; 30 import android.util.Log; 31 32 import com.android.internal.app.AlertActivity; 33 import com.android.internal.app.AlertController; 34 import com.android.internal.util.ArrayUtils; 35 import com.android.internal.util.CharSequences; 36 import com.android.settings.R; 37 import com.android.settingslib.bluetooth.LocalBluetoothAdapter; 38 import com.android.settingslib.bluetooth.LocalBluetoothManager; 39 40 /** 41 * RequestPermissionHelperActivity asks the user whether to toggle Bluetooth. 42 * 43 * TODO: This activity isn't needed - this should be folded in RequestPermissionActivity 44 */ 45 public class RequestPermissionHelperActivity extends AlertActivity implements 46 DialogInterface.OnClickListener { 47 private static final String TAG = "RequestPermissionHelperActivity"; 48 49 public static final String ACTION_INTERNAL_REQUEST_BT_ON = 50 "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON"; 51 52 public static final String ACTION_INTERNAL_REQUEST_BT_OFF = 53 "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_OFF"; 54 55 public static final String EXTRA_APP_LABEL = 56 "com.android.settings.bluetooth.extra.APP_LABEL"; 57 58 private LocalBluetoothAdapter mLocalAdapter; 59 60 private CharSequence mAppLabel; 61 62 private int mTimeout = -1; 63 64 private int mRequest; 65 66 @Override onCreate(Bundle savedInstanceState)67 protected void onCreate(Bundle savedInstanceState) { 68 super.onCreate(savedInstanceState); 69 70 setResult(RESULT_CANCELED); 71 72 // Note: initializes mLocalAdapter and returns true on error 73 if (!parseIntent()) { 74 finish(); 75 return; 76 } 77 78 if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog)) { 79 // Don't even show the dialog if configured this way 80 onClick(null, BUTTON_POSITIVE); 81 dismiss(); 82 } 83 84 createDialog(); 85 } 86 createDialog()87 void createDialog() { 88 final AlertController.AlertParams p = mAlertParams; 89 90 switch (mRequest) { 91 case RequestPermissionActivity.REQUEST_ENABLE: { 92 if (mTimeout < 0) { 93 p.mMessage = mAppLabel != null 94 ? getString(R.string.bluetooth_ask_enablement, mAppLabel) 95 : getString(R.string.bluetooth_ask_enablement_no_name); 96 } else if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) { 97 p.mMessage = mAppLabel != null 98 ? getString( 99 R.string.bluetooth_ask_enablement_and_lasting_discovery, 100 mAppLabel) 101 : getString( 102 R.string.bluetooth_ask_enablement_and_lasting_discovery_no_name); 103 } else { 104 p.mMessage = mAppLabel != null 105 ? getString(R.string.bluetooth_ask_enablement_and_discovery, 106 mAppLabel, mTimeout) 107 : getString(R.string.bluetooth_ask_enablement_and_discovery_no_name, 108 mTimeout); 109 } 110 } break; 111 112 case RequestPermissionActivity.REQUEST_DISABLE: { 113 p.mMessage = mAppLabel != null 114 ? getString(R.string.bluetooth_ask_disablement, mAppLabel) 115 : getString(R.string.bluetooth_ask_disablement_no_name); 116 } break; 117 } 118 119 p.mPositiveButtonText = getString(R.string.allow); 120 p.mPositiveButtonListener = this; 121 p.mNegativeButtonText = getString(R.string.deny); 122 123 setupAlert(); 124 } 125 onClick(DialogInterface dialog, int which)126 public void onClick(DialogInterface dialog, int which) { 127 switch (mRequest) { 128 case RequestPermissionActivity.REQUEST_ENABLE: 129 case RequestPermissionActivity.REQUEST_ENABLE_DISCOVERABLE: { 130 UserManager userManager = getSystemService(UserManager.class); 131 if (userManager.hasUserRestriction(UserManager.DISALLOW_BLUETOOTH)) { 132 // If Bluetooth is disallowed, don't try to enable it, show policy transparency 133 // message instead. 134 DevicePolicyManager dpm = getSystemService(DevicePolicyManager.class); 135 Intent intent = dpm.createAdminSupportIntent(UserManager.DISALLOW_BLUETOOTH); 136 if (intent != null) { 137 startActivity(intent); 138 } 139 } else { 140 mLocalAdapter.enable(); 141 setResult(Activity.RESULT_OK); 142 } 143 } break; 144 145 case RequestPermissionActivity.REQUEST_DISABLE: { 146 mLocalAdapter.disable(); 147 setResult(Activity.RESULT_OK); 148 } break; 149 } 150 } 151 152 /** 153 * Parse the received Intent and initialize mLocalBluetoothAdapter. 154 * @return true if an error occurred; false otherwise 155 */ parseIntent()156 private boolean parseIntent() { 157 Intent intent = getIntent(); 158 if (intent == null) { 159 return false; 160 } 161 162 String action = intent.getAction(); 163 if (ACTION_INTERNAL_REQUEST_BT_ON.equals(action)) { 164 mRequest = RequestPermissionActivity.REQUEST_ENABLE; 165 if (intent.hasExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION)) { 166 // Value used for display purposes. Not range checking. 167 mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 168 BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT); 169 } 170 } else if (ACTION_INTERNAL_REQUEST_BT_OFF.equals(action)) { 171 mRequest = RequestPermissionActivity.REQUEST_DISABLE; 172 } else { 173 return false; 174 } 175 176 LocalBluetoothManager manager = Utils.getLocalBtManager(this); 177 if (manager == null) { 178 Log.e(TAG, "Error: there's a problem starting Bluetooth"); 179 return false; 180 } 181 182 mAppLabel = getIntent().getCharSequenceExtra(EXTRA_APP_LABEL); 183 184 mLocalAdapter = manager.getBluetoothAdapter(); 185 186 return true; 187 } 188 } 189