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.res.Resources;
21 import android.view.View;
22 import android.widget.TextView;
23 
24 import com.android.settings.R;
25 
26 /**
27  * The class for allowing UIs like {@link AdbWirelessDialog} and {@link AdbWirelessDialogUiBase} to
28  * share the logic for controlling buttons, text fields, etc.
29  */
30 public class AdbWirelessDialogController {
31     private static final String TAG = "AdbWirelessDialogCtrl";
32 
33     private final AdbWirelessDialogUiBase mUi;
34     private final View mView;
35 
36     private int mMode;
37 
38     // The dialog for showing the six-digit code
39     private TextView mPairingCodeTitle;
40     private TextView mSixDigitCode;
41     private TextView mIpAddr;
42 
43     // The dialog for showing pairing failed message
44     private TextView mFailedMsg;
45 
46     private Context mContext;
47 
AdbWirelessDialogController(AdbWirelessDialogUiBase parent, View view, int mode)48     public AdbWirelessDialogController(AdbWirelessDialogUiBase parent, View view,
49             int mode) {
50         mUi = parent;
51         mView = view;
52         mMode = mode;
53 
54         mContext = mUi.getContext();
55         final Resources res = mContext.getResources();
56 
57         mSixDigitCode = mView.findViewById(R.id.pairing_code);
58         mIpAddr = mView.findViewById(R.id.ip_addr);
59 
60         switch (mMode) {
61             case AdbWirelessDialogUiBase.MODE_PAIRING:
62                 String title = res.getString(R.string.adb_pairing_device_dialog_title);
63                 mUi.setTitle(title);
64                 mView.findViewById(R.id.l_pairing_six_digit).setVisibility(View.VISIBLE);
65                 mUi.setCancelButton(res.getString(R.string.cancel));
66                 mUi.setCanceledOnTouchOutside(false);
67                 break;
68             case AdbWirelessDialogUiBase.MODE_PAIRING_FAILED:
69                 String msg = res.getString(R.string.adb_pairing_device_dialog_failed_msg);
70                 mUi.setTitle(R.string.adb_pairing_device_dialog_failed_title);
71                 mView.findViewById(R.id.l_pairing_failed).setVisibility(View.VISIBLE);
72                 mFailedMsg = (TextView) mView.findViewById(R.id.pairing_failed_label);
73                 mFailedMsg.setText(msg);
74                 mUi.setSubmitButton(res.getString(R.string.okay));
75                 break;
76             case AdbWirelessDialogUiBase.MODE_QRCODE_FAILED:
77                 mUi.setTitle(R.string.adb_pairing_device_dialog_failed_title);
78                 mView.findViewById(R.id.l_qrcode_pairing_failed).setVisibility(View.VISIBLE);
79                 mUi.setSubmitButton(res.getString(R.string.okay));
80                 break;
81         }
82 
83         // After done view show and hide, request focus from parent view
84         mView.findViewById(R.id.l_adbwirelessdialog).requestFocus();
85     }
86 
87     /**
88      * Set the pairing code UI text field to code.
89      *
90      * @param code the pairing code string
91      */
setPairingCode(String code)92     public void setPairingCode(String code) {
93         mSixDigitCode.setText(code);
94     }
95 
96     /**
97      * Set the Ip address UI text field to ipAddr.
98      *
99      * @param ipAddr the ip address string
100      */
setIpAddr(String ipAddr)101     public void setIpAddr(String ipAddr) {
102         mIpAddr.setText(ipAddr);
103     }
104 }
105