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 
19 import android.os.Handler;
20 import android.os.Looper;
21 import android.util.SparseIntArray;
22 
23 import com.android.internal.location.gnssmetrics.GnssMetrics;
24 import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidActiveTimeReader;
25 import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidClusterTimeReader;
26 import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidFreqTimeReader;
27 import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidUserSysTimeReader;
28 
29 import java.util.ArrayList;
30 import java.util.Queue;
31 import java.util.concurrent.Future;
32 
33 /**
34  * Mocks a BatteryStatsImpl object.
35  */
36 public class MockBatteryStatsImpl extends BatteryStatsImpl {
37     public BatteryStatsImpl.Clocks clocks;
38     public boolean mForceOnBattery;
39 
MockBatteryStatsImpl(Clocks clocks)40     MockBatteryStatsImpl(Clocks clocks) {
41         super(clocks);
42         this.clocks = mClocks;
43         mScreenOnTimer = new BatteryStatsImpl.StopwatchTimer(clocks, null, -1, null,
44                 mOnBatteryTimeBase);
45         mScreenDozeTimer = new BatteryStatsImpl.StopwatchTimer(clocks, null, -1, null,
46                 mOnBatteryTimeBase);
47         mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
48         setExternalStatsSyncLocked(new DummyExternalStatsSync());
49 
50         for (int i = 0; i < GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS; i++) {
51             mGpsSignalQualityTimer[i] = new StopwatchTimer(clocks, null, -1000 - i, null,
52                     mOnBatteryTimeBase);
53         }
54 
55         // A no-op handler.
56         mHandler = new Handler(Looper.getMainLooper()) {
57         };
58     }
59 
MockBatteryStatsImpl()60     MockBatteryStatsImpl() {
61         this(new MockClocks());
62     }
63 
getOnBatteryTimeBase()64     public TimeBase getOnBatteryTimeBase() {
65         return mOnBatteryTimeBase;
66     }
67 
getOnBatteryScreenOffTimeBase()68     public TimeBase getOnBatteryScreenOffTimeBase() {
69         return mOnBatteryScreenOffTimeBase;
70     }
71 
getScreenState()72     public int getScreenState() {
73         return mScreenState;
74     }
75 
getPendingRemovedUids()76     public Queue<UidToRemove> getPendingRemovedUids() {
77         return mPendingRemovedUids;
78     }
79 
isOnBattery()80     public boolean isOnBattery() {
81         return mForceOnBattery ? true : super.isOnBattery();
82     }
83 
forceRecordAllHistory()84     public void forceRecordAllHistory() {
85         mHaveBatteryLevel = true;
86         mRecordingHistory = true;
87         mRecordAllHistory = true;
88     }
89 
getOnBatteryBackgroundTimeBase(int uid)90     public TimeBase getOnBatteryBackgroundTimeBase(int uid) {
91         return getUidStatsLocked(uid).mOnBatteryBackgroundTimeBase;
92     }
93 
getOnBatteryScreenOffBackgroundTimeBase(int uid)94     public TimeBase getOnBatteryScreenOffBackgroundTimeBase(int uid) {
95         return getUidStatsLocked(uid).mOnBatteryScreenOffBackgroundTimeBase;
96     }
97 
setPowerProfile(PowerProfile powerProfile)98     public MockBatteryStatsImpl setPowerProfile(PowerProfile powerProfile) {
99         mPowerProfile = powerProfile;
100         return this;
101     }
102 
setKernelCpuUidFreqTimeReader(KernelCpuUidFreqTimeReader reader)103     public MockBatteryStatsImpl setKernelCpuUidFreqTimeReader(KernelCpuUidFreqTimeReader reader) {
104         mCpuUidFreqTimeReader = reader;
105         return this;
106     }
107 
setKernelCpuUidActiveTimeReader( KernelCpuUidActiveTimeReader reader)108     public MockBatteryStatsImpl setKernelCpuUidActiveTimeReader(
109             KernelCpuUidActiveTimeReader reader) {
110         mCpuUidActiveTimeReader = reader;
111         return this;
112     }
113 
setKernelCpuUidClusterTimeReader( KernelCpuUidClusterTimeReader reader)114     public MockBatteryStatsImpl setKernelCpuUidClusterTimeReader(
115             KernelCpuUidClusterTimeReader reader) {
116         mCpuUidClusterTimeReader = reader;
117         return this;
118     }
119 
setKernelCpuUidUserSysTimeReader( KernelCpuUidUserSysTimeReader reader)120     public MockBatteryStatsImpl setKernelCpuUidUserSysTimeReader(
121             KernelCpuUidUserSysTimeReader reader) {
122         mCpuUidUserSysTimeReader = reader;
123         return this;
124     }
125 
setKernelSingleUidTimeReader(KernelSingleUidTimeReader reader)126     public MockBatteryStatsImpl setKernelSingleUidTimeReader(KernelSingleUidTimeReader reader) {
127         mKernelSingleUidTimeReader = reader;
128         return this;
129     }
130 
setKernelCpuSpeedReaders(KernelCpuSpeedReader[] readers)131     public MockBatteryStatsImpl setKernelCpuSpeedReaders(KernelCpuSpeedReader[] readers) {
132         mKernelCpuSpeedReaders = readers;
133         return this;
134     }
135 
setUserInfoProvider(UserInfoProvider provider)136     public MockBatteryStatsImpl setUserInfoProvider(UserInfoProvider provider) {
137         mUserInfoProvider = provider;
138         return this;
139     }
140 
setPartialTimers(ArrayList<StopwatchTimer> partialTimers)141     public MockBatteryStatsImpl setPartialTimers(ArrayList<StopwatchTimer> partialTimers) {
142         mPartialTimers = partialTimers;
143         return this;
144     }
145 
setLastPartialTimers(ArrayList<StopwatchTimer> lastPartialTimers)146     public MockBatteryStatsImpl setLastPartialTimers(ArrayList<StopwatchTimer> lastPartialTimers) {
147         mLastPartialTimers = lastPartialTimers;
148         return this;
149     }
150 
setOnBatteryInternal(boolean onBatteryInternal)151     public MockBatteryStatsImpl setOnBatteryInternal(boolean onBatteryInternal) {
152         mOnBatteryInternal = onBatteryInternal;
153         return this;
154     }
155 
getPendingUids()156     public SparseIntArray getPendingUids() {
157         return mPendingUids;
158     }
159 
160     private class DummyExternalStatsSync implements ExternalStatsSync {
161         @Override
scheduleSync(String reason, int flags)162         public Future<?> scheduleSync(String reason, int flags) {
163             return null;
164         }
165 
166         @Override
scheduleCpuSyncDueToRemovedUid(int uid)167         public Future<?> scheduleCpuSyncDueToRemovedUid(int uid) {
168             return null;
169         }
170 
171         @Override
scheduleCpuSyncDueToSettingChange()172         public Future<?> scheduleCpuSyncDueToSettingChange() {
173             return null;
174         }
175 
176         @Override
scheduleReadProcStateCpuTimes(boolean onBattery, boolean onBatteryScreenOff, long delayMillis)177         public Future<?> scheduleReadProcStateCpuTimes(boolean onBattery,
178                 boolean onBatteryScreenOff, long delayMillis) {
179             return null;
180         }
181 
182         @Override
scheduleCopyFromAllUidsCpuTimes( boolean onBattery, boolean onBatteryScreenOff)183         public Future<?> scheduleCopyFromAllUidsCpuTimes(
184                 boolean onBattery, boolean onBatteryScreenOff) {
185             return null;
186         }
187 
188         @Override
scheduleCpuSyncDueToScreenStateChange( boolean onBattery, boolean onBatteryScreenOff)189         public Future<?> scheduleCpuSyncDueToScreenStateChange(
190                 boolean onBattery, boolean onBatteryScreenOff) {
191             return null;
192         }
193 
194         @Override
scheduleCpuSyncDueToWakelockChange(long delayMillis)195         public Future<?> scheduleCpuSyncDueToWakelockChange(long delayMillis) {
196             return null;
197         }
198 
199         @Override
cancelCpuSyncDueToWakelockChange()200         public void cancelCpuSyncDueToWakelockChange() {
201         }
202 
203         @Override
scheduleSyncDueToBatteryLevelChange(long delayMillis)204         public Future<?> scheduleSyncDueToBatteryLevelChange(long delayMillis) {
205             return null;
206         }
207     }
208 }
209 
210