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