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