• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.wifi;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager;
27 import android.net.wifi.WifiManager;
28 import android.os.Bundle;
29 import android.provider.Settings;
30 
31 import com.android.internal.logging.nano.MetricsProto;
32 import com.android.settings.R;
33 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
34 
35 /**
36  * This activity requests users permission to allow scanning even when Wi-Fi is turned off
37  */
38 public class WifiScanModeActivity extends Activity {
39     private DialogFragment mDialog;
40     private String mApp;
41 
42     @Override
onCreate(Bundle savedInstanceState)43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         Intent intent = getIntent();
46         if (savedInstanceState == null) {
47             if (intent != null && intent.getAction()
48                     .equals(WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE)) {
49                 ApplicationInfo ai;
50                 mApp = getCallingPackage();
51                 try {
52                     PackageManager pm = getPackageManager();
53                     ai = pm.getApplicationInfo(mApp, 0);
54                     mApp = (String)pm.getApplicationLabel(ai);
55                 } catch (PackageManager.NameNotFoundException e) { }
56             } else {
57                 finish();
58                 return;
59             }
60         } else {
61             mApp = savedInstanceState.getString("app");
62         }
63         createDialog();
64     }
65 
createDialog()66     private void createDialog() {
67         if (mDialog == null) {
68             mDialog = AlertDialogFragment.newInstance(mApp);
69             mDialog.show(getFragmentManager(), "dialog");
70         }
71     }
72 
dismissDialog()73     private void dismissDialog() {
74         if (mDialog != null) {
75             mDialog.dismiss();
76             mDialog = null;
77         }
78     }
79 
doPositiveClick()80     private void doPositiveClick() {
81         Settings.Global.putInt(getContentResolver(),
82                 Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1);
83         setResult(RESULT_OK);
84         finish();
85     }
86 
doNegativeClick()87     private void doNegativeClick() {
88         setResult(RESULT_CANCELED);
89         finish();
90     }
91 
92     @Override
onSaveInstanceState(Bundle outState)93     public void onSaveInstanceState(Bundle outState) {
94         super.onSaveInstanceState(outState);
95         outState.putString("app", mApp);
96     }
97 
98     @Override
onPause()99     public void onPause() {
100         super.onPause();
101         dismissDialog();
102     }
103 
onResume()104     public void onResume() {
105         super.onResume();
106         createDialog();
107     }
108 
109     public static class AlertDialogFragment extends InstrumentedDialogFragment {
newInstance(String app)110         static AlertDialogFragment newInstance(String app) {
111             AlertDialogFragment frag = new AlertDialogFragment(app);
112             return frag;
113         }
114 
115         private final String mApp;
AlertDialogFragment(String app)116         public AlertDialogFragment(String app) {
117             super();
118             mApp = app;
119         }
120 
AlertDialogFragment()121         public AlertDialogFragment() {
122             super();
123             mApp = null;
124         }
125 
126         @Override
getMetricsCategory()127         public int getMetricsCategory() {
128             return MetricsProto.MetricsEvent.DIALOG_WIFI_SCAN_MODE;
129         }
130 
131         @Override
onCreateDialog(Bundle savedInstanceState)132         public Dialog onCreateDialog(Bundle savedInstanceState) {
133             return new AlertDialog.Builder(getActivity())
134                     .setMessage(getString(R.string.wifi_scan_always_turnon_message, mApp))
135                     .setPositiveButton(R.string.wifi_scan_always_confirm_allow,
136                             new DialogInterface.OnClickListener() {
137                                 public void onClick(DialogInterface dialog, int whichButton) {
138                                     ((WifiScanModeActivity) getActivity()).doPositiveClick();
139                                 }
140                             }
141                     )
142                     .setNegativeButton(R.string.wifi_scan_always_confirm_deny,
143                             new DialogInterface.OnClickListener() {
144                                 public void onClick(DialogInterface dialog, int whichButton) {
145                                     ((WifiScanModeActivity) getActivity()).doNegativeClick();
146                                 }
147                             }
148                     )
149                     .create();
150         }
151         @Override
onCancel(DialogInterface dialog)152         public void onCancel(DialogInterface dialog) {
153             ((WifiScanModeActivity) getActivity()).doNegativeClick();
154         }
155     }
156 }
157