Jonathan Koo | 007ff07 | 2019-05-10 09:24:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.car; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.car.Car; |
Keun young Park | 9e395bb | 2019-10-11 20:00:22 -0700 | [diff] [blame] | 21 | import android.car.Car.CarServiceLifecycleListener; |
Jonathan Koo | 007ff07 | 2019-05-10 09:24:08 -0700 | [diff] [blame] | 22 | import android.car.hardware.power.CarPowerManager; |
| 23 | import android.car.hardware.power.CarPowerManager.CarPowerStateListener; |
Jonathan Koo | 007ff07 | 2019-05-10 09:24:08 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Jonathan Koo | 007ff07 | 2019-05-10 09:24:08 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| 27 | /** |
| 28 | * Helper class for connecting to the {@link CarPowerManager} and listening for power state changes. |
| 29 | */ |
| 30 | public 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 Park | 9e395bb | 2019-10-11 20:00:22 -0700 | [diff] [blame] | 39 | private final CarServiceLifecycleListener mCarServiceLifecycleListener; |
Jonathan Koo | 007ff07 | 2019-05-10 09:24:08 -0700 | [diff] [blame] | 40 | |
| 41 | PowerManagerHelper(Context context, @NonNull CarPowerStateListener listener) { |
| 42 | mContext = context; |
| 43 | mCarPowerStateListener = listener; |
Keun young Park | 9e395bb | 2019-10-11 20:00:22 -0700 | [diff] [blame] | 44 | 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 Koo | 007ff07 | 2019-05-10 09:24:08 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Connect to Car service. |
| 60 | */ |
| 61 | void connectToCarService() { |
Keun young Park | 9e395bb | 2019-10-11 20:00:22 -0700 | [diff] [blame] | 62 | mCar = Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT, |
| 63 | mCarServiceLifecycleListener); |
Jonathan Koo | 007ff07 | 2019-05-10 09:24:08 -0700 | [diff] [blame] | 64 | } |
| 65 | } |