• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.systemui.car.users.CarSystemUIUserUtil.getCurrentUserHandle;
20 
21 import android.car.Car;
22 import android.car.hardware.power.CarPowerManager;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.util.AttributeSet;
26 
27 /**
28  * One of {@link QCFooterButtonView} for quick control panels, which turns off the screen.
29  */
30 
31 public class QCScreenOffButton extends QCFooterButtonView {
32     private static final String TAG = QCUserPickerButton.class.getSimpleName();
33 
34     private CarPowerManager mCarPowerManager;
35 
QCScreenOffButton(Context context)36     public QCScreenOffButton(Context context) {
37         this(context, null);
38     }
39 
QCScreenOffButton(Context context, AttributeSet attrs)40     public QCScreenOffButton(Context context, AttributeSet attrs) {
41         this(context, attrs, 0);
42     }
43 
QCScreenOffButton(Context context, AttributeSet attrs, int defStyleAttr)44     public QCScreenOffButton(Context context, AttributeSet attrs, int defStyleAttr) {
45         this(context, attrs, defStyleAttr, 0);
46     }
47 
QCScreenOffButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)48     public QCScreenOffButton(Context context, AttributeSet attrs, int defStyleAttr,
49             int defStyleRes) {
50         super(context, attrs, defStyleAttr, defStyleRes);
51 
52         mCarServiceLifecycleListener = (car, ready) -> {
53             if (!ready) {
54                 return;
55             }
56             mCarPowerManager = (CarPowerManager) car.getCarManager(Car.POWER_SERVICE);
57         };
58 
59         Car.createCar(getContext(), /* handler= */ null, Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER,
60                 mCarServiceLifecycleListener);
61 
62         mOnClickListener = v -> turnScreenOff();
63         setOnClickListener(mOnClickListener);
64     }
65 
turnScreenOff()66     private void turnScreenOff() {
67         getContext().sendBroadcastAsUser(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS),
68                 getCurrentUserHandle(getContext(), mUserTracker));
69         if (mCarPowerManager != null) {
70             mCarPowerManager.setDisplayPowerState(getContext().getDisplayId(), /* enable= */ false);
71         }
72     }
73 }
74