1 /* 2 * Copyright (C) 2018 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 package com.android.settings.wifi; 17 18 import android.app.Activity; 19 import android.app.Dialog; 20 import android.app.settings.SettingsEnums; 21 import android.content.ActivityNotFoundException; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.content.Intent; 26 import android.os.Bundle; 27 import android.provider.Settings; 28 import android.text.TextUtils; 29 import android.util.Log; 30 import android.widget.Toast; 31 32 import androidx.annotation.VisibleForTesting; 33 import androidx.appcompat.app.AlertDialog; 34 35 import com.android.settings.R; 36 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 37 import com.android.settingslib.HelpUtils; 38 39 public class WifiScanningRequiredFragment extends InstrumentedDialogFragment implements 40 DialogInterface.OnClickListener { 41 42 private static final String TAG = "WifiScanReqFrag"; 43 newInstance()44 public static WifiScanningRequiredFragment newInstance() { 45 WifiScanningRequiredFragment fragment = new WifiScanningRequiredFragment(); 46 return fragment; 47 } 48 49 @Override onCreateDialog(Bundle savedInstanceState)50 public Dialog onCreateDialog(Bundle savedInstanceState) { 51 AlertDialog.Builder builder = new AlertDialog.Builder(getContext()) 52 .setTitle(R.string.wifi_settings_scanning_required_title) 53 .setView(R.layout.wifi_settings_scanning_required_view) 54 .setPositiveButton(R.string.wifi_settings_scanning_required_turn_on, this) 55 .setNegativeButton(R.string.cancel, null); 56 addButtonIfNeeded(builder); 57 58 return builder.create(); 59 } 60 61 @Override getMetricsCategory()62 public int getMetricsCategory() { 63 return SettingsEnums.WIFI_SCANNING_NEEDED_DIALOG; 64 } 65 66 @Override onClick(DialogInterface dialog, int which)67 public void onClick(DialogInterface dialog, int which) { 68 Context context = getContext(); 69 ContentResolver contentResolver = context.getContentResolver(); 70 switch(which) { 71 case DialogInterface.BUTTON_POSITIVE: 72 Settings.Global.putInt(contentResolver, 73 Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1); 74 Toast.makeText( 75 context, 76 context.getString(R.string.wifi_settings_scanning_required_enabled), 77 Toast.LENGTH_SHORT).show(); 78 getTargetFragment().onActivityResult( 79 getTargetRequestCode(), 80 Activity.RESULT_OK, 81 null); 82 break; 83 case DialogInterface.BUTTON_NEUTRAL: 84 openHelpPage(); 85 break; 86 case DialogInterface.BUTTON_NEGATIVE: 87 default: 88 // do nothing 89 } 90 } 91 addButtonIfNeeded(AlertDialog.Builder builder)92 void addButtonIfNeeded(AlertDialog.Builder builder) { 93 // Only show "learn more" if there is a help page to show 94 if (!TextUtils.isEmpty(getContext().getString(R.string.help_uri_wifi_scanning_required))) { 95 builder.setNeutralButton(R.string.learn_more, this); 96 } 97 } 98 openHelpPage()99 private void openHelpPage() { 100 Intent intent = getHelpIntent(getContext()); 101 if (intent != null) { 102 try { 103 startActivity(intent); 104 } catch (ActivityNotFoundException e) { 105 Log.e(TAG, "Activity was not found for intent, " + intent.toString()); 106 } 107 } 108 } 109 110 @VisibleForTesting getHelpIntent(Context context)111 Intent getHelpIntent(Context context) { 112 return HelpUtils.getHelpIntent( 113 context, 114 context.getString(R.string.help_uri_wifi_scanning_required), 115 context.getClass().getName()); 116 } 117 } 118