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 17package com.android.settings.wifi; 18 19import android.app.Activity; 20import android.app.AlertDialog; 21import android.app.Dialog; 22import android.app.DialogFragment; 23import android.content.DialogInterface; 24import android.content.Intent; 25import android.content.pm.ApplicationInfo; 26import android.content.pm.PackageManager; 27import android.net.wifi.WifiManager; 28import android.os.Bundle; 29import android.provider.Settings; 30 31import com.android.internal.logging.nano.MetricsProto; 32import com.android.settings.R; 33import 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 */ 38public class WifiScanModeActivity extends Activity { 39 private DialogFragment mDialog; 40 private String mApp; 41 42 @Override 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 66 private void createDialog() { 67 if (mDialog == null) { 68 mDialog = AlertDialogFragment.newInstance(mApp); 69 mDialog.show(getFragmentManager(), "dialog"); 70 } 71 } 72 73 private void dismissDialog() { 74 if (mDialog != null) { 75 mDialog.dismiss(); 76 mDialog = null; 77 } 78 } 79 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 87 private void doNegativeClick() { 88 setResult(RESULT_CANCELED); 89 finish(); 90 } 91 92 @Override 93 public void onSaveInstanceState(Bundle outState) { 94 super.onSaveInstanceState(outState); 95 outState.putString("app", mApp); 96 } 97 98 @Override 99 public void onPause() { 100 super.onPause(); 101 dismissDialog(); 102 } 103 104 public void onResume() { 105 super.onResume(); 106 createDialog(); 107 } 108 109 public static class AlertDialogFragment extends InstrumentedDialogFragment { 110 static AlertDialogFragment newInstance(String app) { 111 AlertDialogFragment frag = new AlertDialogFragment(app); 112 return frag; 113 } 114 115 private final String mApp; 116 public AlertDialogFragment(String app) { 117 super(); 118 mApp = app; 119 } 120 121 public AlertDialogFragment() { 122 super(); 123 mApp = null; 124 } 125 126 @Override 127 public int getMetricsCategory() { 128 return MetricsProto.MetricsEvent.DIALOG_WIFI_SCAN_MODE; 129 } 130 131 @Override 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 152 public void onCancel(DialogInterface dialog) { 153 ((WifiScanModeActivity) getActivity()).doNegativeClick(); 154 } 155 } 156} 157