1 /* 2 * Copyright (C) 2020 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.notification; 18 19 import android.annotation.NonNull; 20 import android.car.Car; 21 import android.car.hardware.power.CarPowerManager; 22 import android.car.hardware.power.CarPowerManager.CarPowerStateListener; 23 import android.util.Log; 24 25 import com.android.systemui.car.CarServiceProvider; 26 import com.android.systemui.dagger.SysUISingleton; 27 28 import javax.inject.Inject; 29 30 /** 31 * Helper class for connecting to the {@link CarPowerManager} and listening for power state changes. 32 */ 33 @SysUISingleton 34 public class PowerManagerHelper { 35 public static final String TAG = "PowerManagerHelper"; 36 37 private final CarServiceProvider mCarServiceProvider; 38 39 private CarPowerManager mCarPowerManager; 40 private CarPowerStateListener mCarPowerStateListener; 41 42 private final CarServiceProvider.CarServiceOnConnectedListener mCarServiceLifecycleListener; 43 44 @Inject PowerManagerHelper(CarServiceProvider carServiceProvider)45 public PowerManagerHelper(CarServiceProvider carServiceProvider) { 46 mCarServiceProvider = carServiceProvider; 47 mCarServiceLifecycleListener = car -> { 48 Log.d(TAG, "Car Service connected"); 49 mCarPowerManager = (CarPowerManager) car.getCarManager(Car.POWER_SERVICE); 50 if (mCarPowerManager != null) { 51 mCarPowerManager.setListener(mCarPowerStateListener); 52 } else { 53 Log.e(TAG, "CarPowerManager service not available"); 54 } 55 }; 56 } 57 58 /** 59 * Sets a {@link CarPowerStateListener}. Should be set before {@link #connectToCarService()}. 60 */ setCarPowerStateListener(@onNull CarPowerStateListener listener)61 public void setCarPowerStateListener(@NonNull CarPowerStateListener listener) { 62 mCarPowerStateListener = listener; 63 } 64 65 /** 66 * Connect to Car service. 67 */ connectToCarService()68 public void connectToCarService() { 69 mCarServiceProvider.addListener(mCarServiceLifecycleListener); 70 } 71 } 72