1 /* 2 * Copyright (C) 2019 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.car.settings.wifi.preferences; 18 19 import android.app.Dialog; 20 import android.content.ActivityNotFoundException; 21 import android.content.DialogInterface; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.text.TextUtils; 25 26 import com.android.car.settings.R; 27 import com.android.car.settings.common.Logger; 28 import com.android.car.ui.AlertDialogBuilder; 29 import com.android.car.ui.preference.CarUiDialogFragment; 30 import com.android.settingslib.HelpUtils; 31 32 /** Dialog to request enabling of wifi scanning when user tries to enable auto wifi wakeup. */ 33 public class ConfirmEnableWifiScanningDialogFragment extends CarUiDialogFragment { 34 35 public static final String TAG = "ConfirmEnableWifiScanningDialogFragment"; 36 private static final Logger LOG = new Logger(ConfirmEnableWifiScanningDialogFragment.class); 37 38 private WifiScanningEnabledListener mListener; 39 40 /** Sets the wifi scanning enabled listener. */ setWifiScanningEnabledListener(WifiScanningEnabledListener listener)41 public void setWifiScanningEnabledListener(WifiScanningEnabledListener listener) { 42 mListener = listener; 43 } 44 45 @Override onCreateDialog(Bundle savedInstanceState)46 public Dialog onCreateDialog(Bundle savedInstanceState) { 47 AlertDialogBuilder builder = new AlertDialogBuilder(getContext()) 48 .setTitle(R.string.wifi_settings_scanning_required_title) 49 .setPositiveButton(R.string.wifi_settings_scanning_required_turn_on, this) 50 .setNegativeButton(R.string.cancel, null); 51 52 // Only show "learn more" if there is a help page to show 53 if (!TextUtils.isEmpty(getContext().getString(R.string.help_uri_wifi_scanning_required))) { 54 builder.setNeutralButton(R.string.learn_more, this); 55 } 56 57 return builder.create(); 58 } 59 60 @Override onClick(DialogInterface dialog, int which)61 public void onClick(DialogInterface dialog, int which) { 62 switch (which) { 63 case DialogInterface.BUTTON_POSITIVE: 64 if (mListener != null) { 65 mListener.onWifiScanningEnabled(); 66 } 67 break; 68 case DialogInterface.BUTTON_NEUTRAL: 69 openHelpPage(); 70 break; 71 case DialogInterface.BUTTON_NEGATIVE: 72 default: 73 // do nothing 74 } 75 } 76 openHelpPage()77 private void openHelpPage() { 78 Intent intent = HelpUtils.getHelpIntent(getContext(), 79 getContext().getString(R.string.help_uri_wifi_scanning_required), 80 getContext().getClass().getName()); 81 if (intent != null) { 82 try { 83 startActivity(intent); 84 } catch (ActivityNotFoundException e) { 85 LOG.e("Activity was not found for intent, " + intent.toString()); 86 } 87 } 88 } 89 90 @Override onDialogClosed(boolean positiveResult)91 protected void onDialogClosed(boolean positiveResult) { 92 } 93 94 /** Listener for when the dialog is confirmed and the wifi scanning is enabled. */ 95 public interface WifiScanningEnabledListener { 96 /** Actions to take when wifi scanning is enabled. */ onWifiScanningEnabled()97 void onWifiScanningEnabled(); 98 } 99 } 100