1 /*
2  * Copyright (C) 2021 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.systemui.usb;
18 
19 import android.app.AlertDialog;
20 import android.content.DialogInterface;
21 import android.os.Bundle;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.WindowManager;
26 import android.widget.CheckBox;
27 import android.widget.CompoundButton;
28 import android.widget.TextView;
29 
30 import com.android.internal.app.AlertActivity;
31 import com.android.internal.app.AlertController;
32 import com.android.systemui.res.R;
33 
34 abstract class UsbDialogActivity extends AlertActivity
35         implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
36 
37     private static final String TAG = UsbDialogActivity.class.getSimpleName();
38 
39     UsbDialogHelper mDialogHelper;
40     private CheckBox mAlwaysUse;
41     private TextView mClearDefaultHint;
42 
43     @Override
onCreate(Bundle savedInstanceState)44     protected void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         getWindow().addSystemFlags(
47                 WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
48         try {
49             mDialogHelper = new UsbDialogHelper(getApplicationContext(), getIntent());
50         } catch (IllegalStateException e) {
51             Log.e(TAG, "unable to initialize", e);
52             finish();
53         }
54     }
55 
56     @Override
onResume()57     protected void onResume() {
58         super.onResume();
59         mDialogHelper.registerUsbDisconnectedReceiver(this);
60     }
61 
62     @Override
onPause()63     protected void onPause() {
64         if (mDialogHelper != null) {
65             mDialogHelper.unregisterUsbDisconnectedReceiver(this);
66         }
67         super.onPause();
68     }
69 
70     @Override
onClick(DialogInterface dialog, int which)71     public void onClick(DialogInterface dialog, int which) {
72         if (which == AlertDialog.BUTTON_POSITIVE) {
73             onConfirm();
74         } else {
75             finish();
76         }
77     }
78 
79     @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)80     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
81         if (mClearDefaultHint == null) return;
82 
83         if (isChecked) {
84             mClearDefaultHint.setVisibility(View.VISIBLE);
85         } else {
86             mClearDefaultHint.setVisibility(View.GONE);
87         }
88     }
89 
setAlertParams(String title, String message)90     void setAlertParams(String title, String message) {
91         final AlertController.AlertParams ap = mAlertParams;
92         ap.mTitle = title;
93         ap.mMessage = message;
94         ap.mPositiveButtonText = getString(android.R.string.ok);
95         ap.mNegativeButtonText = getString(android.R.string.cancel);
96         ap.mPositiveButtonListener = this;
97         ap.mNegativeButtonListener = this;
98     }
99 
addAlwaysUseCheckbox()100     void addAlwaysUseCheckbox() {
101         final AlertController.AlertParams ap = mAlertParams;
102         LayoutInflater inflater = getSystemService(LayoutInflater.class);
103         ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
104         mAlwaysUse = ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
105         if (mDialogHelper.isUsbAccessory()) {
106             mAlwaysUse.setText(getString(R.string.always_use_accessory, mDialogHelper.getAppName(),
107                     mDialogHelper.getDeviceDescription()));
108         } else {
109             // UsbDevice case
110             mAlwaysUse.setText(getString(R.string.always_use_device, mDialogHelper.getAppName(),
111                     mDialogHelper.getDeviceDescription()));
112         }
113         mAlwaysUse.setOnCheckedChangeListener(this);
114         mClearDefaultHint = ap.mView.findViewById(com.android.internal.R.id.clearDefaultHint);
115         mClearDefaultHint.setVisibility(View.GONE);
116     }
117 
isAlwaysUseChecked()118     boolean isAlwaysUseChecked() {
119         return mAlwaysUse != null && mAlwaysUse.isChecked();
120     }
121 
122     /**
123      * Called when the dialog is confirmed.
124      */
onConfirm()125     abstract void onConfirm();
126 }
127