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; |
| 21 | import android.car.CarNotConnectedException; |
| 22 | import android.car.hardware.power.CarPowerManager; |
| 23 | import android.car.hardware.power.CarPowerManager.CarPowerStateListener; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | import android.content.ServiceConnection; |
| 27 | import android.os.IBinder; |
| 28 | import android.util.Log; |
| 29 | |
| 30 | /** |
| 31 | * Helper class for connecting to the {@link CarPowerManager} and listening for power state changes. |
| 32 | */ |
| 33 | public class PowerManagerHelper { |
| 34 | public static final String TAG = "PowerManagerHelper"; |
| 35 | |
| 36 | private final Context mContext; |
| 37 | private final CarPowerStateListener mCarPowerStateListener; |
| 38 | |
| 39 | private Car mCar; |
| 40 | private CarPowerManager mCarPowerManager; |
| 41 | |
| 42 | private final ServiceConnection mCarConnectionListener = |
| 43 | new ServiceConnection() { |
| 44 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 45 | Log.d(TAG, "Car Service connected"); |
| 46 | try { |
| 47 | mCarPowerManager = (CarPowerManager) mCar.getCarManager(Car.POWER_SERVICE); |
| 48 | if (mCarPowerManager != null) { |
| 49 | mCarPowerManager.setListener(mCarPowerStateListener); |
| 50 | } else { |
| 51 | Log.e(TAG, "CarPowerManager service not available"); |
| 52 | } |
| 53 | } catch (CarNotConnectedException e) { |
| 54 | Log.e(TAG, "Car not connected", e); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void onServiceDisconnected(ComponentName name) { |
| 60 | destroyCarPowerManager(); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | PowerManagerHelper(Context context, @NonNull CarPowerStateListener listener) { |
| 65 | mContext = context; |
| 66 | mCarPowerStateListener = listener; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Connect to Car service. |
| 71 | */ |
| 72 | void connectToCarService() { |
| 73 | mCar = Car.createCar(mContext, mCarConnectionListener); |
| 74 | if (mCar != null) { |
| 75 | mCar.connect(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Disconnects from Car service. |
| 81 | */ |
| 82 | void disconnectFromCarService() { |
| 83 | if (mCar != null) { |
| 84 | mCar.disconnect(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private void destroyCarPowerManager() { |
| 89 | if (mCarPowerManager != null) { |
| 90 | mCarPowerManager.clearListener(); |
| 91 | } |
| 92 | } |
| 93 | } |