• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.hdmi;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.hardware.hdmi.HdmiControlManager;
24 import android.hardware.hdmi.HdmiPlaybackClient;
25 import android.os.Bundle;
26 import android.os.CountDownTimer;
27 import android.util.Slog;
28 import android.view.View;
29 import android.view.WindowManager;
30 import android.widget.Button;
31 import android.widget.ImageView;
32 import android.widget.TextView;
33 
34 import com.android.systemui.tv.TvBottomSheetActivity;
35 import com.android.systemui.tv.res.R;
36 
37 /**
38  * Confirmation dialog shown when the device loses active source. Gives the user the option to keep
39  * the device active, in case the loss of active source was due to a misleading CEC message from a
40  * faulty device.
41  */
42 public class HdmiCecActiveSourceLostActivity extends TvBottomSheetActivity
43         implements View.OnClickListener {
44     private static final String TAG = "HdmiCecActiveSourceLostActivity";
45     private HdmiControlManager mHdmiControlManager;
46     private static final int COUNTDOWN_GO_TO_SLEEP_MS = 30_000; // 30 seconds
47 
48     @Override
onCreate(Bundle b)49     public final void onCreate(Bundle b) {
50         super.onCreate(b);
51         mHdmiControlManager =
52                 (HdmiControlManager)
53                         getApplicationContext().getSystemService(Context.HDMI_CONTROL_SERVICE);
54         getWindow()
55                 .addPrivateFlags(
56                         WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
57 
58         OnActiveSourceRecoveredBroadcastReceiver onActiveSourceRecoveredBroadcastReceiver =
59                 new OnActiveSourceRecoveredBroadcastReceiver();
60         IntentFilter filter =
61                 new IntentFilter(HdmiControlManager.ACTION_ON_ACTIVE_SOURCE_RECOVERED_DISMISS_UI);
62         getApplicationContext()
63                 .registerReceiver(
64                         onActiveSourceRecoveredBroadcastReceiver,
65                         filter,
66                         Context.RECEIVER_EXPORTED);
67         initUI();
68     }
69 
70     @Override
onClick(View v)71     public void onClick(View v) {
72         if (v.getId() == R.id.bottom_sheet_negative_button) {
73             mHdmiControlManager.setPowerStateChangeOnActiveSourceLost(
74                     HdmiControlManager.POWER_STATE_CHANGE_ON_ACTIVE_SOURCE_LOST_NONE);
75         }
76         HdmiPlaybackClient playbackClient = mHdmiControlManager.getPlaybackClient();
77         if (playbackClient != null) {
78             playbackClient.oneTouchPlay(new HdmiPlaybackClient.OneTouchPlayCallback() {
79                 @Override
80                 public void onComplete(int result) {
81                     if (result != HdmiControlManager.RESULT_SUCCESS) {
82                         Slog.w(TAG, "One touch play failed: " + result);
83                     }
84                 }
85             });
86         }
87 
88         finish();
89     }
90 
initUI()91     private void initUI() {
92         TextView titleTextView = findViewById(R.id.bottom_sheet_title);
93         TextView contentTextView = findViewById(R.id.bottom_sheet_body);
94         ImageView icon = findViewById(R.id.bottom_sheet_icon);
95         ImageView secondIcon = findViewById(R.id.bottom_sheet_second_icon);
96         Button okButton = findViewById(R.id.bottom_sheet_positive_button);
97         Button cancelButton = findViewById(R.id.bottom_sheet_negative_button);
98 
99         titleTextView.setText(getString(R.string.hdmi_cec_on_active_source_lost_title));
100         contentTextView.setText(getString(R.string.hdmi_cec_on_active_source_lost_description));
101         icon.setImageResource(R.drawable.ic_input_switch);
102         secondIcon.setVisibility(View.GONE);
103 
104         new CountDownTimer(COUNTDOWN_GO_TO_SLEEP_MS, 1000) {
105             public void onTick(long millisUntilFinished) {
106                 // Start countdown from 30, not 29.
107                 okButton.setText(String.format(getResources()
108                                 .getString(R.string.hdmi_cec_on_active_source_lost_ok),
109                         millisUntilFinished / 1000 + 1));
110             }
111             public void onFinish() {
112                 okButton.setText(String.format(getResources()
113                                 .getString(R.string.hdmi_cec_on_active_source_lost_ok), 0));
114                 finish();
115             }
116         }.start();
117 
118 
119         okButton.setOnClickListener(this);
120         okButton.requestFocus();
121 
122         cancelButton.setText(R.string.hdmi_cec_on_active_source_lost_do_not_show);
123         cancelButton.setOnClickListener(this);
124     }
125 
126     private class OnActiveSourceRecoveredBroadcastReceiver extends BroadcastReceiver {
127         @Override
onReceive(Context context, Intent intent)128         public void onReceive(Context context, Intent intent) {
129             finish();
130         }
131     }
132 }
133