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