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