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.car.qc;
18 
19 import android.car.hardware.power.CarPowerManager;
20 import android.content.Context;
21 import android.content.Intent;
22 
23 import com.android.systemui.car.CarServiceProvider;
24 import com.android.systemui.car.systembar.element.CarSystemBarElementController;
25 import com.android.systemui.car.systembar.element.CarSystemBarElementStateController;
26 import com.android.systemui.car.systembar.element.CarSystemBarElementStatusBarDisableController;
27 import com.android.systemui.settings.UserTracker;
28 
29 import dagger.assisted.Assisted;
30 import dagger.assisted.AssistedFactory;
31 import dagger.assisted.AssistedInject;
32 
33 /**
34  * One of {@link QCFooterView} for quick control panels, which turns off the screen.
35  */
36 
37 public class QCScreenOffButtonController extends QCFooterViewController {
38     private final Context mContext;
39     private final UserTracker mUserTracker;
40     private final CarServiceProvider mCarServiceProvider;
41     private CarPowerManager mCarPowerManager;
42 
43     private final CarServiceProvider.CarServiceOnConnectedListener mCarServiceLifecycleListener =
44             car -> {
45                 mCarPowerManager = car.getCarManager(CarPowerManager.class);
46             };
47 
48     @AssistedInject
QCScreenOffButtonController(@ssisted QCFooterView view, CarSystemBarElementStatusBarDisableController disableController, CarSystemBarElementStateController stateController, Context context, UserTracker userTracker, CarServiceProvider carServiceProvider)49     protected QCScreenOffButtonController(@Assisted QCFooterView view,
50             CarSystemBarElementStatusBarDisableController disableController,
51             CarSystemBarElementStateController stateController, Context context,
52             UserTracker userTracker, CarServiceProvider carServiceProvider) {
53         super(view, disableController, stateController, context, userTracker);
54         mContext = context;
55         mUserTracker = userTracker;
56         mCarServiceProvider = carServiceProvider;
57     }
58 
59     @AssistedFactory
60     public interface Factory extends
61             CarSystemBarElementController.Factory<QCFooterView, QCScreenOffButtonController> {}
62 
63     @Override
onInit()64     protected void onInit() {
65         super.onInit();
66         mView.setOnClickListener(v -> turnScreenOff());
67     }
68 
69     @Override
onViewAttached()70     protected void onViewAttached() {
71         super.onViewAttached();
72         mCarServiceProvider.addListener(mCarServiceLifecycleListener);
73     }
74 
75     @Override
onViewDetached()76     protected void onViewDetached() {
77         super.onViewDetached();
78         mCarServiceProvider.removeListener(mCarServiceLifecycleListener);
79     }
80 
turnScreenOff()81     private void turnScreenOff() {
82         mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS),
83                 mUserTracker.getUserHandle());
84         if (mCarPowerManager != null) {
85             mCarPowerManager.setDisplayPowerState(getContext().getDisplayId(), /* enable= */ false);
86         }
87     }
88 }
89