• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.M;
4 import static android.os.Build.VERSION_CODES.P;
5 
6 import android.os.BatteryManager;
7 import com.google.common.base.Preconditions;
8 import java.util.HashMap;
9 import java.util.Map;
10 import org.robolectric.annotation.Implementation;
11 import org.robolectric.annotation.Implements;
12 import org.robolectric.annotation.Resetter;
13 
14 @Implements(BatteryManager.class)
15 public class ShadowBatteryManager {
16   private static final Map<Integer, Long> longProperties = new HashMap<>();
17   private static final Map<Integer, Integer> intProperties = new HashMap<>();
18   private static boolean isCharging = false;
19   private static long chargeTimeRemaining = 0;
20 
21   @Resetter
reset()22   public static void reset() {
23     isCharging = false;
24     chargeTimeRemaining = 0;
25     longProperties.clear();
26     intProperties.clear();
27   }
28 
29   @Implementation(minSdk = M)
isCharging()30   protected boolean isCharging() {
31     return isCharging;
32   }
33 
setIsCharging(boolean charging)34   public void setIsCharging(boolean charging) {
35     isCharging = charging;
36   }
37 
38   @Implementation
getIntProperty(int id)39   protected int getIntProperty(int id) {
40     return intProperties.containsKey(id) ? intProperties.get(id) : Integer.MIN_VALUE;
41   }
42 
setIntProperty(int id, int value)43   public void setIntProperty(int id, int value) {
44     intProperties.put(id, value);
45   }
46 
47   @Implementation
getLongProperty(int id)48   protected long getLongProperty(int id) {
49     return longProperties.containsKey(id) ? longProperties.get(id) : Long.MIN_VALUE;
50   }
51 
setLongProperty(int id, long value)52   public void setLongProperty(int id, long value) {
53     longProperties.put(id, value);
54   }
55 
56   @Implementation(minSdk = P)
computeChargeTimeRemaining()57   protected long computeChargeTimeRemaining() {
58     return chargeTimeRemaining;
59   }
60 
61   /** Sets the value to be returned from {@link BatteryManager#computeChargeTimeRemaining} */
setChargeTimeRemaining(long chargeTimeRemaining)62   public void setChargeTimeRemaining(long chargeTimeRemaining) {
63     Preconditions.checkArgument(
64         chargeTimeRemaining == -1 || chargeTimeRemaining >= 0,
65         "chargeTimeRemaining must be -1 or non-negative.");
66     this.chargeTimeRemaining = chargeTimeRemaining;
67   }
68 }
69