Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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.server; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
| 23 | import android.os.BatteryManager; |
| 24 | import android.os.BatteryManagerInternal; |
| 25 | import android.os.OsProtoEnums; |
| 26 | import android.os.PowerManager; |
| 27 | import android.util.Slog; |
| 28 | |
| 29 | import com.android.internal.os.CachedDeviceState; |
| 30 | |
| 31 | /** |
| 32 | * Tracks changes to the device state (e.g. charging/on battery, screen on/off) to share it with |
| 33 | * the System Server telemetry services. |
| 34 | * |
| 35 | * @hide Only for use within the system server. |
| 36 | */ |
| 37 | public class CachedDeviceStateService extends SystemService { |
| 38 | private static final String TAG = "CachedDeviceStateService"; |
| 39 | private final CachedDeviceState mDeviceState = new CachedDeviceState(); |
| 40 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 41 | @Override |
| 42 | public void onReceive(Context context, Intent intent) { |
| 43 | switch (intent.getAction()) { |
| 44 | case Intent.ACTION_BATTERY_CHANGED: |
| 45 | mDeviceState.setCharging( |
| 46 | intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, |
| 47 | OsProtoEnums.BATTERY_PLUGGED_NONE) |
| 48 | != OsProtoEnums.BATTERY_PLUGGED_NONE); |
| 49 | break; |
| 50 | case Intent.ACTION_SCREEN_ON: |
| 51 | mDeviceState.setScreenInteractive(true); |
| 52 | break; |
| 53 | case Intent.ACTION_SCREEN_OFF: |
| 54 | mDeviceState.setScreenInteractive(false); |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | public CachedDeviceStateService(Context context) { |
| 61 | super(context); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onStart() { |
| 66 | publishLocalService(CachedDeviceState.Readonly.class, mDeviceState.getReadonlyClient()); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onBootPhase(int phase) { |
| 71 | if (SystemService.PHASE_SYSTEM_SERVICES_READY == phase) { |
| 72 | final IntentFilter filter = new IntentFilter(); |
| 73 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 74 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 75 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 76 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 77 | getContext().registerReceiver(mBroadcastReceiver, filter); |
| 78 | mDeviceState.setCharging(queryIsCharging()); |
| 79 | mDeviceState.setScreenInteractive(queryScreenInteractive(getContext())); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private boolean queryIsCharging() { |
| 84 | final BatteryManagerInternal batteryManager = |
| 85 | LocalServices.getService(BatteryManagerInternal.class); |
| 86 | if (batteryManager == null) { |
| 87 | Slog.wtf(TAG, "BatteryManager null while starting CachedDeviceStateService"); |
| 88 | // Default to true to not collect any data. |
| 89 | return true; |
| 90 | } else { |
| 91 | return batteryManager.getPlugType() != OsProtoEnums.BATTERY_PLUGGED_NONE; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private boolean queryScreenInteractive(Context context) { |
| 96 | final PowerManager powerManager = context.getSystemService(PowerManager.class); |
| 97 | if (powerManager == null) { |
| 98 | Slog.wtf(TAG, "PowerManager null while starting CachedDeviceStateService"); |
| 99 | return false; |
| 100 | } else { |
| 101 | return powerManager.isInteractive(); |
| 102 | } |
| 103 | } |
| 104 | } |