blob: a27dd341d44954464e4a3926f910b79187825e19 [file] [log] [blame]
Jonathan Koo007ff072019-05-10 09:24:08 -07001/*
2 * Copyright (C) 2019 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
17package com.android.systemui.statusbar.car;
18
19import android.annotation.NonNull;
20import android.car.Car;
Keun young Park9e395bb2019-10-11 20:00:22 -070021import android.car.Car.CarServiceLifecycleListener;
Jonathan Koo007ff072019-05-10 09:24:08 -070022import android.car.hardware.power.CarPowerManager;
23import android.car.hardware.power.CarPowerManager.CarPowerStateListener;
Jonathan Koo007ff072019-05-10 09:24:08 -070024import android.content.Context;
Jonathan Koo007ff072019-05-10 09:24:08 -070025import android.util.Log;
26
27/**
28 * Helper class for connecting to the {@link CarPowerManager} and listening for power state changes.
29 */
30public class PowerManagerHelper {
31 public static final String TAG = "PowerManagerHelper";
32
33 private final Context mContext;
34 private final CarPowerStateListener mCarPowerStateListener;
35
36 private Car mCar;
37 private CarPowerManager mCarPowerManager;
38
Keun young Park9e395bb2019-10-11 20:00:22 -070039 private final CarServiceLifecycleListener mCarServiceLifecycleListener;
Jonathan Koo007ff072019-05-10 09:24:08 -070040
41 PowerManagerHelper(Context context, @NonNull CarPowerStateListener listener) {
42 mContext = context;
43 mCarPowerStateListener = listener;
Keun young Park9e395bb2019-10-11 20:00:22 -070044 mCarServiceLifecycleListener = (car, ready) -> {
45 if (!ready) {
46 return;
47 }
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 };
Jonathan Koo007ff072019-05-10 09:24:08 -070056 }
57
58 /**
59 * Connect to Car service.
60 */
61 void connectToCarService() {
Keun young Park9e395bb2019-10-11 20:00:22 -070062 mCar = Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT,
63 mCarServiceLifecycleListener);
Jonathan Koo007ff072019-05-10 09:24:08 -070064 }
65}