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.net.wifi.WifiManager.NetworkRequestUserSelectionCallback;
23 import android.os.Bundle;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.appcompat.app.AlertDialog;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
31 
32 /**
33  * The dialog shows an error message when requesting network {@link NetworkRequestDialogFragment}.
34  * Contains multi-error types in {@code ERROR_DIALOG_TYPE}.
35  */
36 public class NetworkRequestErrorDialogFragment extends InstrumentedDialogFragment {
37 
38     public static final String DIALOG_TYPE = "DIALOG_ERROR_TYPE";
39 
40     public enum ERROR_DIALOG_TYPE {TIME_OUT, ABORT}
41     @Nullable
42     private NetworkRequestUserSelectionCallback mRejectCallback;
43 
newInstance()44     public static NetworkRequestErrorDialogFragment newInstance() {
45         return new NetworkRequestErrorDialogFragment();
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         rejectNetworkRequestAndFinish();
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) -> onRescanClick())
68                     .setNegativeButton(R.string.cancel,
69                             (dialog, which) -> rejectNetworkRequestAndFinish());
70         } else {
71             builder.setMessage(R.string.network_connection_errorstate_dialog_message)
72                     .setPositiveButton(R.string.okay,
73                             (dialog, which) -> rejectNetworkRequestAndFinish());
74         }
75         return builder.create();
76     }
77 
78     @Override
getMetricsCategory()79     public int getMetricsCategory() {
80         return SettingsEnums.WIFI_SCANNING_NEEDED_DIALOG;
81     }
82 
83     // Sets the callback for fragment to reject this request.
setRejectCallback(NetworkRequestUserSelectionCallback rejectCallback)84     public void setRejectCallback(NetworkRequestUserSelectionCallback rejectCallback) {
85         mRejectCallback = rejectCallback;
86     }
87 
onRescanClick()88     protected void onRescanClick() {
89         if (getActivity() != null) {
90             dismiss();
91             ((NetworkRequestDialogActivity)getActivity()).onClickRescanButton();
92         }
93     }
94 
rejectNetworkRequestAndFinish()95     private void rejectNetworkRequestAndFinish() {
96         if (getActivity() != null) {
97             if (mRejectCallback != null) {
98                 mRejectCallback.reject();
99             }
100             getActivity().finish();
101         }
102     }
103 }
104