@Test public void testInitialSnapshot() { ShadowSystemClock.setElapsedRealtime(5000); when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 20, 100)); DeviceBatteryMetrics metrics = new DeviceBatteryMetrics(); DeviceBatteryMetricsCollector collector = new DeviceBatteryMetricsCollector(mContext); ShadowSystemClock.setElapsedRealtime(10000); collector.getSnapshot(metrics); verifySnapshot(metrics, 20, 0, 5000); }
@Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(DeviceBatteryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.batteryLevelPct = getBatteryLevel(getBatteryIntent()); long now = SystemClock.elapsedRealtime(); synchronized (this) { if (mIsCurrentlyCharging) { snapshot.chargingRealtimeMs = mChargingRealtimeMs + (now - mLastUpdateMs); snapshot.batteryRealtimeMs = mBatteryRealtimeMs; } else { snapshot.chargingRealtimeMs = mChargingRealtimeMs; snapshot.batteryRealtimeMs = mBatteryRealtimeMs + (now - mLastUpdateMs); } return true; } }
DeviceBatteryMetricsCollector extends SystemMetricsCollector<DeviceBatteryMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(DeviceBatteryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.batteryLevelPct = getBatteryLevel(getBatteryIntent()); long now = SystemClock.elapsedRealtime(); synchronized (this) { if (mIsCurrentlyCharging) { snapshot.chargingRealtimeMs = mChargingRealtimeMs + (now - mLastUpdateMs); snapshot.batteryRealtimeMs = mBatteryRealtimeMs; } else { snapshot.chargingRealtimeMs = mChargingRealtimeMs; snapshot.batteryRealtimeMs = mBatteryRealtimeMs + (now - mLastUpdateMs); } return true; } } }
DeviceBatteryMetricsCollector extends SystemMetricsCollector<DeviceBatteryMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(DeviceBatteryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.batteryLevelPct = getBatteryLevel(getBatteryIntent()); long now = SystemClock.elapsedRealtime(); synchronized (this) { if (mIsCurrentlyCharging) { snapshot.chargingRealtimeMs = mChargingRealtimeMs + (now - mLastUpdateMs); snapshot.batteryRealtimeMs = mBatteryRealtimeMs; } else { snapshot.chargingRealtimeMs = mChargingRealtimeMs; snapshot.batteryRealtimeMs = mBatteryRealtimeMs + (now - mLastUpdateMs); } return true; } } DeviceBatteryMetricsCollector(Context context); }
DeviceBatteryMetricsCollector extends SystemMetricsCollector<DeviceBatteryMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(DeviceBatteryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.batteryLevelPct = getBatteryLevel(getBatteryIntent()); long now = SystemClock.elapsedRealtime(); synchronized (this) { if (mIsCurrentlyCharging) { snapshot.chargingRealtimeMs = mChargingRealtimeMs + (now - mLastUpdateMs); snapshot.batteryRealtimeMs = mBatteryRealtimeMs; } else { snapshot.chargingRealtimeMs = mChargingRealtimeMs; snapshot.batteryRealtimeMs = mBatteryRealtimeMs + (now - mLastUpdateMs); } return true; } } DeviceBatteryMetricsCollector(Context context); @Override @ThreadSafe(enableChecks = false) boolean getSnapshot(DeviceBatteryMetrics snapshot); @Override DeviceBatteryMetrics createMetrics(); }
DeviceBatteryMetricsCollector extends SystemMetricsCollector<DeviceBatteryMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(DeviceBatteryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.batteryLevelPct = getBatteryLevel(getBatteryIntent()); long now = SystemClock.elapsedRealtime(); synchronized (this) { if (mIsCurrentlyCharging) { snapshot.chargingRealtimeMs = mChargingRealtimeMs + (now - mLastUpdateMs); snapshot.batteryRealtimeMs = mBatteryRealtimeMs; } else { snapshot.chargingRealtimeMs = mChargingRealtimeMs; snapshot.batteryRealtimeMs = mBatteryRealtimeMs + (now - mLastUpdateMs); } return true; } } DeviceBatteryMetricsCollector(Context context); @Override @ThreadSafe(enableChecks = false) boolean getSnapshot(DeviceBatteryMetrics snapshot); @Override DeviceBatteryMetrics createMetrics();  }