1 /*
2  * Copyright (C) 2020 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.development;
18 
19 import android.content.Context;
20 import android.content.DialogInterface;
21 import android.os.Bundle;
22 import android.view.View;
23 import android.widget.Button;
24 
25 import androidx.appcompat.app.AlertDialog;
26 
27 import com.android.settings.R;
28 
29 /**
30  * Class to show a variety of dialogs for the Wireless debugging
31  * fragment.
32  */
33 public class AdbWirelessDialog extends AlertDialog implements
34         AdbWirelessDialogUiBase,
35         DialogInterface.OnClickListener {
36 
37     /**
38      * Interface for subscribers to implement in order to listen
39      * to AdbWirelessDialog events.
40      */
41     public interface AdbWirelessDialogListener {
42         /**
43          * Called when the dialog was closed by clicking a negative button.
44          */
onCancel()45         default void onCancel() {
46         }
47 
48         /**
49          * Called when the dialog was closed by clicking a positive button.
50          *
51          * @param dialog the dialog that was closed.
52          */
onSubmit(AdbWirelessDialog dialog)53         default void onSubmit(AdbWirelessDialog dialog) {
54         }
55 
56         /**
57          * Called when the dialog was dismissed.
58          */
onDismiss()59         default void onDismiss() {
60         }
61     }
62 
63     private static final String TAG = "AdbWirelessDialog";
64 
65     private static final int BUTTON_CANCEL = DialogInterface.BUTTON_NEGATIVE;
66     private static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
67 
68     private final AdbWirelessDialogListener mListener;
69     private final int mMode;
70 
71     private View mView;
72     private AdbWirelessDialogController mController;
73 
74     /**
75      * Creates a AdbWirelessDialog with no additional style. It displays as a dialog above the
76      * current view.
77      */
createModal( Context context, AdbWirelessDialogListener listener, int mode)78     public static AdbWirelessDialog createModal(
79             Context context,
80             AdbWirelessDialogListener listener,
81             int mode) {
82         return new AdbWirelessDialog(context, listener, mode);
83     }
84 
AdbWirelessDialog(Context context, AdbWirelessDialogListener listener, int mode)85     AdbWirelessDialog(Context context, AdbWirelessDialogListener listener, int mode) {
86         super(context);
87         mListener = listener;
88         mMode = mode;
89     }
90 
91     @Override
onCreate(Bundle savedInstanceState)92     protected void onCreate(Bundle savedInstanceState) {
93         mView = getLayoutInflater().inflate(R.layout.adb_wireless_dialog, null);
94         setView(mView);
95         mController = new AdbWirelessDialogController(this, mView, mMode);
96         super.onCreate(savedInstanceState);
97     }
98 
99     @Override
onStop()100     protected void onStop() {
101         super.onStop();
102 
103         dismiss();
104         if (mListener != null) {
105             mListener.onDismiss();
106         }
107     }
108 
109     @Override
onClick(DialogInterface dialogInterface, int id)110     public void onClick(DialogInterface dialogInterface, int id) {
111         if (mListener != null) {
112             switch (id) {
113                 case BUTTON_CANCEL:
114                     mListener.onCancel();
115                     break;
116             }
117         }
118     }
119 
120     @Override
getController()121     public AdbWirelessDialogController getController() {
122         return mController;
123     }
124 
125     @Override
dispatchSubmit()126     public void dispatchSubmit() {
127         if (mListener != null) {
128             mListener.onSubmit(this);
129         }
130         dismiss();
131     }
132 
133     @Override
getMode()134     public int getMode() {
135         return mMode;
136     }
137 
138     @Override
getSubmitButton()139     public Button getSubmitButton() {
140         return getButton(BUTTON_SUBMIT);
141     }
142 
143     @Override
getCancelButton()144     public Button getCancelButton() {
145         return getButton(BUTTON_NEGATIVE);
146     }
147 
148     @Override
setSubmitButton(CharSequence text)149     public void setSubmitButton(CharSequence text) {
150         setButton(BUTTON_SUBMIT, text, this);
151     }
152 
153     @Override
setCancelButton(CharSequence text)154     public void setCancelButton(CharSequence text) {
155         setButton(BUTTON_NEGATIVE, text, this);
156     }
157 }
158