blob: cc72a7f38d690c6d5e5f15200f4402c9ee3a8869 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package com.xtremelabs.robolectric.shadows;
import android.os.PowerManager;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
/**
* Shadows the {@code android.os.PowerManager} class.
*/
@Implements(PowerManager.class)
public class ShadowPowerManager {
private boolean isScreenOn = true;
@Implementation
public PowerManager.WakeLock newWakeLock(int flags, String tag) {
return Robolectric.newInstanceOf(PowerManager.WakeLock.class);
}
@Implementation
public boolean isScreenOn() {
return isScreenOn;
}
public void setIsScreenOn(boolean screenOn) {
isScreenOn = screenOn;
}
@Implements(PowerManager.WakeLock.class)
public static class ShadowWakeLock {
private boolean refCounted = true;
private int refCount;
private boolean locked;
@Implementation
public void acquire() {
acquire(0);
}
@Implementation
public synchronized void acquire(long timeout) {
if (refCounted) {
refCount++;
} else {
locked = true;
}
}
@Implementation
public synchronized void release() {
if (refCounted) {
if (--refCount < 0) throw new RuntimeException("WakeLock under-locked");
} else {
locked = false;
}
}
@Implementation
public synchronized boolean isHeld() {
return refCounted ? refCount > 0 : locked;
}
@Implementation
public void setReferenceCounted(boolean value) {
refCounted = value;
}
}
}
|