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 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.os.Bundle; 23 24 import androidx.annotation.NonNull; 25 import androidx.appcompat.app.AlertDialog; 26 27 import com.android.settings.R; 28 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 29 30 /** 31 * The dialog shows an error message when requesting network {@link NetworkRequestDialogFragment}. 32 * Contains multi-error types in {@code ERROR_DIALOG_TYPE}. 33 */ 34 public class NetworkRequestErrorDialogFragment extends InstrumentedDialogFragment { 35 36 public static final String DIALOG_TYPE = "DIALOG_ERROR_TYPE"; 37 38 public enum ERROR_DIALOG_TYPE {TIME_OUT, ABORT} 39 newInstance()40 public static NetworkRequestErrorDialogFragment newInstance() { 41 return new NetworkRequestErrorDialogFragment(); 42 } 43 NetworkRequestErrorDialogFragment()44 private NetworkRequestErrorDialogFragment() { 45 super(); 46 } 47 48 @Override onCancel(@onNull DialogInterface dialog)49 public void onCancel(@NonNull DialogInterface dialog) { 50 super.onCancel(dialog); 51 // Wants to finish the activity when user clicks back key or outside of the dialog. 52 getActivity().finish(); 53 } 54 55 @Override onCreateDialog(Bundle savedInstanceState)56 public Dialog onCreateDialog(Bundle savedInstanceState) { 57 // Gets error type to construct dialog. Default is TIME_OUT dialog. 58 ERROR_DIALOG_TYPE msgType = ERROR_DIALOG_TYPE.TIME_OUT; 59 if (getArguments() != null) { 60 msgType = (ERROR_DIALOG_TYPE) getArguments().getSerializable(DIALOG_TYPE); 61 } 62 63 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 64 if (msgType == ERROR_DIALOG_TYPE.TIME_OUT) { 65 builder.setMessage(R.string.network_connection_timeout_dialog_message) 66 .setPositiveButton(R.string.network_connection_timeout_dialog_ok, 67 (dialog, which) -> startScanningDialog()) 68 .setNegativeButton(R.string.cancel, (dialog, which) -> getActivity().finish()); 69 } else { 70 builder.setMessage(R.string.network_connection_errorstate_dialog_message) 71 .setPositiveButton(R.string.okay, (dialog, which) -> getActivity().finish()); 72 } 73 return builder.create(); 74 } 75 76 @Override getMetricsCategory()77 public int getMetricsCategory() { 78 return SettingsEnums.WIFI_SCANNING_NEEDED_DIALOG; 79 } 80 startScanningDialog()81 protected void startScanningDialog() { 82 final NetworkRequestDialogFragment fragment = NetworkRequestDialogFragment.newInstance(); 83 fragment.show(getActivity().getSupportFragmentManager(), 84 NetworkRequestErrorDialogFragment.class.getSimpleName()); 85 } 86 } 87