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