Joe Onorato | abded11 | 2016-02-08 16:49:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.internal.os; |
| 18 | |
Narayan Kamath | 695cf72 | 2017-12-21 18:32:47 +0000 | [diff] [blame] | 19 | import android.os.Handler; |
| 20 | import android.os.Looper; |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 21 | import android.util.SparseIntArray; |
| 22 | |
Sudheer Shanka | 3838323 | 2017-07-25 09:55:03 -0700 | [diff] [blame] | 23 | import java.util.ArrayList; |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 24 | import java.util.concurrent.Future; |
Sudheer Shanka | 3838323 | 2017-07-25 09:55:03 -0700 | [diff] [blame] | 25 | |
Joe Onorato | abded11 | 2016-02-08 16:49:39 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Mocks a BatteryStatsImpl object. |
| 28 | */ |
| 29 | public class MockBatteryStatsImpl extends BatteryStatsImpl { |
| 30 | public BatteryStatsImpl.Clocks clocks; |
Amith Yamasani | ab9ad19 | 2016-12-06 12:46:59 -0800 | [diff] [blame] | 31 | public boolean mForceOnBattery; |
| 32 | |
| 33 | MockBatteryStatsImpl(Clocks clocks) { |
| 34 | super(clocks); |
| 35 | this.clocks = mClocks; |
Mike Ma | c2f518a | 2017-09-19 16:06:03 -0700 | [diff] [blame] | 36 | mScreenOnTimer = new BatteryStatsImpl.StopwatchTimer(clocks, null, -1, null, |
| 37 | mOnBatteryTimeBase); |
| 38 | mScreenDozeTimer = new BatteryStatsImpl.StopwatchTimer(clocks, null, -1, null, |
| 39 | mOnBatteryTimeBase); |
Bookatz | 867c0d7 | 2017-03-07 18:23:42 -0800 | [diff] [blame] | 40 | mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase); |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 41 | setExternalStatsSyncLocked(new DummyExternalStatsSync()); |
Narayan Kamath | 695cf72 | 2017-12-21 18:32:47 +0000 | [diff] [blame] | 42 | |
| 43 | // A no-op handler. |
| 44 | mHandler = new Handler(Looper.getMainLooper()) {}; |
Amith Yamasani | ab9ad19 | 2016-12-06 12:46:59 -0800 | [diff] [blame] | 45 | } |
Joe Onorato | abded11 | 2016-02-08 16:49:39 -0800 | [diff] [blame] | 46 | |
| 47 | MockBatteryStatsImpl() { |
Amith Yamasani | ab9ad19 | 2016-12-06 12:46:59 -0800 | [diff] [blame] | 48 | this(new MockClocks()); |
Joe Onorato | abded11 | 2016-02-08 16:49:39 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public TimeBase getOnBatteryTimeBase() { |
| 52 | return mOnBatteryTimeBase; |
| 53 | } |
| 54 | |
Mike Ma | c2f518a | 2017-09-19 16:06:03 -0700 | [diff] [blame] | 55 | public TimeBase getOnBatteryScreenOffTimeBase() { |
| 56 | return mOnBatteryScreenOffTimeBase; |
| 57 | } |
| 58 | |
| 59 | public int getScreenState() { |
| 60 | return mScreenState; |
| 61 | } |
| 62 | |
Amith Yamasani | ab9ad19 | 2016-12-06 12:46:59 -0800 | [diff] [blame] | 63 | public boolean isOnBattery() { |
| 64 | return mForceOnBattery ? true : super.isOnBattery(); |
| 65 | } |
Bookatz | 867c0d7 | 2017-03-07 18:23:42 -0800 | [diff] [blame] | 66 | |
Narayan Kamath | 695cf72 | 2017-12-21 18:32:47 +0000 | [diff] [blame] | 67 | public void forceRecordAllHistory() { |
| 68 | mHaveBatteryLevel = true; |
| 69 | mRecordingHistory = true; |
| 70 | mRecordAllHistory = true; |
| 71 | } |
| 72 | |
Bookatz | 867c0d7 | 2017-03-07 18:23:42 -0800 | [diff] [blame] | 73 | public TimeBase getOnBatteryBackgroundTimeBase(int uid) { |
| 74 | return getUidStatsLocked(uid).mOnBatteryBackgroundTimeBase; |
| 75 | } |
Bookatz | c8c4496 | 2017-05-11 12:12:54 -0700 | [diff] [blame] | 76 | |
| 77 | public TimeBase getOnBatteryScreenOffBackgroundTimeBase(int uid) { |
| 78 | return getUidStatsLocked(uid).mOnBatteryScreenOffBackgroundTimeBase; |
| 79 | } |
Sudheer Shanka | 3838323 | 2017-07-25 09:55:03 -0700 | [diff] [blame] | 80 | |
| 81 | public MockBatteryStatsImpl setPowerProfile(PowerProfile powerProfile) { |
| 82 | mPowerProfile = powerProfile; |
| 83 | return this; |
| 84 | } |
| 85 | |
| 86 | public MockBatteryStatsImpl setKernelUidCpuFreqTimeReader(KernelUidCpuFreqTimeReader reader) { |
| 87 | mKernelUidCpuFreqTimeReader = reader; |
| 88 | return this; |
| 89 | } |
| 90 | |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 91 | public MockBatteryStatsImpl setKernelUidCpuActiveTimeReader(KernelUidCpuActiveTimeReader reader) { |
| 92 | mKernelUidCpuActiveTimeReader = reader; |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | public MockBatteryStatsImpl setKernelUidCpuClusterTimeReader(KernelUidCpuClusterTimeReader reader) { |
| 97 | mKernelUidCpuClusterTimeReader = reader; |
| 98 | return this; |
| 99 | } |
| 100 | |
Sudheer Shanka | 3838323 | 2017-07-25 09:55:03 -0700 | [diff] [blame] | 101 | public MockBatteryStatsImpl setKernelUidCpuTimeReader(KernelUidCpuTimeReader reader) { |
| 102 | mKernelUidCpuTimeReader = reader; |
| 103 | return this; |
| 104 | } |
| 105 | |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 106 | public MockBatteryStatsImpl setKernelSingleUidTimeReader(KernelSingleUidTimeReader reader) { |
| 107 | mKernelSingleUidTimeReader = reader; |
| 108 | return this; |
| 109 | } |
| 110 | |
Sudheer Shanka | 3838323 | 2017-07-25 09:55:03 -0700 | [diff] [blame] | 111 | public MockBatteryStatsImpl setKernelCpuSpeedReaders(KernelCpuSpeedReader[] readers) { |
| 112 | mKernelCpuSpeedReaders = readers; |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | public MockBatteryStatsImpl setUserInfoProvider(UserInfoProvider provider) { |
| 117 | mUserInfoProvider = provider; |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | public MockBatteryStatsImpl setPartialTimers(ArrayList<StopwatchTimer> partialTimers) { |
| 122 | mPartialTimers = partialTimers; |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | public MockBatteryStatsImpl setLastPartialTimers(ArrayList<StopwatchTimer> lastPartialTimers) { |
| 127 | mLastPartialTimers = lastPartialTimers; |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | public MockBatteryStatsImpl setOnBatteryInternal(boolean onBatteryInternal) { |
| 132 | mOnBatteryInternal = onBatteryInternal; |
| 133 | return this; |
| 134 | } |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 135 | |
| 136 | public SparseIntArray getPendingUids() { |
| 137 | return mPendingUids; |
| 138 | } |
| 139 | |
| 140 | private class DummyExternalStatsSync implements ExternalStatsSync { |
| 141 | @Override |
| 142 | public Future<?> scheduleSync(String reason, int flags) { |
| 143 | return null; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Future<?> scheduleCpuSyncDueToRemovedUid(int uid) { |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | @Override |
Sudheer Shanka | 5c19b89 | 2018-01-05 17:25:46 -0800 | [diff] [blame] | 152 | public Future<?> scheduleCpuSyncDueToSettingChange() { |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | @Override |
Sudheer Shanka | e544d16 | 2017-12-28 17:06:20 -0800 | [diff] [blame] | 157 | public Future<?> scheduleReadProcStateCpuTimes( |
| 158 | boolean onBattery, boolean onBatteryScreenOff) { |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 159 | return null; |
| 160 | } |
| 161 | |
| 162 | @Override |
Sudheer Shanka | e544d16 | 2017-12-28 17:06:20 -0800 | [diff] [blame] | 163 | public Future<?> scheduleCopyFromAllUidsCpuTimes( |
| 164 | boolean onBattery, boolean onBatteryScreenOff) { |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 165 | return null; |
| 166 | } |
| 167 | |
| 168 | } |
Joe Onorato | abded11 | 2016-02-08 16:49:39 -0800 | [diff] [blame] | 169 | } |
| 170 | |