1 /*
2  * Copyright (C) 2023 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.tv.usb;
18 
19 import android.os.Bundle;
20 import android.util.Log;
21 import android.view.View;
22 import android.view.WindowManager;
23 import android.widget.Button;
24 import android.widget.ImageView;
25 import android.widget.TextView;
26 
27 import com.android.systemui.tv.TvBottomSheetActivity;
28 import com.android.systemui.tv.res.R;
29 import com.android.systemui.usb.UsbDialogHelper;
30 
31 abstract class TvUsbDialogActivity extends TvBottomSheetActivity implements View.OnClickListener {
32     private static final String TAG = TvUsbDialogActivity.class.getSimpleName();
33     UsbDialogHelper mDialogHelper;
34 
35     @Override
onCreate(Bundle b)36     public final void onCreate(Bundle b) {
37         super.onCreate(b);
38         getWindow().addPrivateFlags(
39                 WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
40         try {
41             mDialogHelper = new UsbDialogHelper(getApplicationContext(), getIntent());
42         } catch (IllegalStateException e) {
43             Log.e(TAG, "unable to initialize", e);
44             finish();
45         }
46     }
47 
48     @Override
onResume()49     protected void onResume() {
50         super.onResume();
51         mDialogHelper.registerUsbDisconnectedReceiver(this);
52     }
53 
54     @Override
onPause()55     protected void onPause() {
56         if (mDialogHelper != null) {
57             mDialogHelper.unregisterUsbDisconnectedReceiver(this);
58         }
59         super.onPause();
60     }
61 
62     @Override
onClick(View v)63     public void onClick(View v) {
64         if (v.getId() == R.id.bottom_sheet_positive_button) {
65             onConfirm();
66         } else {
67             finish();
68         }
69     }
70 
71     /**
72      * Called when the ok button is clicked.
73      */
onConfirm()74     abstract void onConfirm();
75 
initUI(CharSequence title, CharSequence text)76     void initUI(CharSequence title, CharSequence text) {
77         TextView titleTextView = findViewById(R.id.bottom_sheet_title);
78         TextView contentTextView = findViewById(R.id.bottom_sheet_body);
79         ImageView icon = findViewById(R.id.bottom_sheet_icon);
80         ImageView secondIcon = findViewById(R.id.bottom_sheet_second_icon);
81         Button okButton = findViewById(R.id.bottom_sheet_positive_button);
82         Button cancelButton = findViewById(R.id.bottom_sheet_negative_button);
83 
84         titleTextView.setText(title);
85         contentTextView.setText(text);
86         icon.setImageResource(com.android.internal.R.drawable.ic_usb_48dp);
87         secondIcon.setVisibility(View.GONE);
88         okButton.setText(android.R.string.ok);
89         okButton.setOnClickListener(this);
90 
91         cancelButton.setText(android.R.string.cancel);
92         cancelButton.setOnClickListener(this);
93         cancelButton.requestFocus();
94     }
95 }
96