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