Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
      4 import static android.os.Build.VERSION_CODES.M;
      5 import static android.os.Build.VERSION_CODES.O;
      6 
      7 import android.app.Activity;
      8 import android.app.KeyguardManager;
      9 import android.app.KeyguardManager.KeyguardDismissCallback;
     10 import org.robolectric.annotation.Implementation;
     11 import org.robolectric.annotation.Implements;
     12 import org.robolectric.annotation.RealObject;
     13 import org.robolectric.shadow.api.Shadow;
     14 
     15 import java.util.Set;
     16 import java.util.HashSet;
     17 
     18 @Implements(KeyguardManager.class)
     19 public class ShadowKeyguardManager {
     20   @RealObject private KeyguardManager realKeyguardManager;
     21 
     22   private KeyguardManager.KeyguardLock keyguardLock =
     23       Shadow.newInstanceOf(KeyguardManager.KeyguardLock.class);
     24 
     25   private final Set<Integer> deviceLockedForUsers = new HashSet<Integer>();
     26   private final Set<Integer> deviceSecureForUsers = new HashSet<Integer>();
     27   private boolean inRestrictedInputMode;
     28   private boolean isKeyguardLocked;
     29   private boolean isDeviceLocked;
     30   private boolean isKeyguardSecure;
     31   private boolean isDeviceSecure;
     32   private KeyguardManager.KeyguardDismissCallback callback;
     33 
     34   /**
     35    * For tests, returns the value set via {@link #setinRestrictedInputMode(boolean)}, or `false` by
     36    * default.
     37    *
     38    * @see #setinRestrictedInputMode(boolean)
     39    */
     40   @Implementation
     41   public boolean inKeyguardRestrictedInputMode() {
     42     return inRestrictedInputMode;
     43   }
     44 
     45   @Implementation(minSdk = O)
     46   public void requestDismissKeyguard(
     47       Activity activity, KeyguardManager.KeyguardDismissCallback callback) {
     48     if (isKeyguardLocked) {
     49       if (this.callback != null) {
     50         callback.onDismissError();
     51       }
     52       this.callback = callback;
     53     } else {
     54       callback.onDismissError();
     55     }
     56   }
     57 
     58   /**
     59    * For tests, returns the value set via {@link #setKeyguardLocked(boolean)}, or `false` by
     60    * default.
     61    *
     62    * @see #setKeyguardLocked(boolean)
     63    */
     64   @Implementation
     65   public boolean isKeyguardLocked() {
     66     return isKeyguardLocked;
     67   }
     68 
     69   /**
     70    * Sets whether the device keyguard is locked or not. This affects the value to be returned by
     71    * {@link #isKeyguardLocked()} and also invokes callbacks set in
     72    *  {@link KeyguardManager#requestDismissKeyguard()}.
     73    *
     74    *  @param isKeyguardLocked true to lock the keyguard. If a KeyguardDismissCallback is set will
     75    *  fire {@link KeyguardDismissCallback#onDismissCancelled()} or false to unlock and dismiss the
     76    *  keyguard firing {@link KeyguardDismissCallback#onDismissSucceeded()} if a
     77    *  KeyguardDismissCallback is set.
     78    *  */
     79   public void setKeyguardLocked(boolean isKeyguardLocked) {
     80     this.isKeyguardLocked = isKeyguardLocked;
     81     if (callback != null) {
     82       if (isKeyguardLocked) {
     83         callback.onDismissCancelled();
     84       } else {
     85         callback.onDismissSucceeded();
     86       }
     87       callback = null;
     88     }
     89   }
     90 
     91   /**
     92    * For tests, returns a {@link ShadowKeyguardLock}.
     93    *
     94    * @see ShadowKeyguardLock
     95    */
     96   @Implementation
     97   public KeyguardManager.KeyguardLock newKeyguardLock(String tag) {
     98     return keyguardLock;
     99   }
    100 
    101   /**
    102    * Sets the value to be returned by {@link #isKeyguardRestrictedInputMode()}.
    103    *
    104    * @see #isKeyguardRestrictedInputMode()
    105    */
    106   public void setinRestrictedInputMode(boolean restricted) {
    107     inRestrictedInputMode = restricted;
    108   }
    109 
    110   /**
    111    * For tests, returns the value set by {@link #setIsKeyguardSecure(boolean)}, or `false` by
    112    * default.
    113    *
    114    * @see #setIsKeyguardSecure(boolean)
    115    */
    116   @Implementation
    117   public boolean isKeyguardSecure() {
    118     return isKeyguardSecure;
    119   }
    120 
    121   /**
    122    * Sets the value to be returned by {@link #isKeyguardSecure()}.
    123    *
    124    * @see #isKeyguardSecure()
    125    */
    126   public void setIsKeyguardSecure(boolean secure) {
    127     isKeyguardSecure = secure;
    128   }
    129 
    130   /**
    131    * For tests on Android >=M, returns the value set by {@link #setIsDeviceSecure(boolean)}, or
    132    * `false` by default.
    133    *
    134    * @see #setIsDeviceSecure(boolean)
    135    */
    136   @Implementation(minSdk = M)
    137   public boolean isDeviceSecure() {
    138     return isDeviceSecure;
    139   }
    140 
    141   /**
    142    * For tests on Android >=M, sets the value to be returned by {@link #isDeviceSecure()}.
    143    *
    144    * @see #isDeviceSecure()
    145    */
    146   public void setIsDeviceSecure(boolean isDeviceSecure) {
    147     this.isDeviceSecure = isDeviceSecure;
    148   }
    149 
    150   /**
    151    * For tests on Android >=M, returns the value set by {@link #setIsDeviceSecure(int, boolean)}, or
    152    * `false` by default.
    153    *
    154    * @see #setIsDeviceSecure(int, boolean)
    155    */
    156   @Implementation(minSdk = M)
    157   protected boolean isDeviceSecure(int userId) {
    158     return deviceSecureForUsers.contains(userId);
    159   }
    160 
    161   /**
    162    * For tests on Android >=M, sets the value to be returned by {@link #isDeviceSecure(int)}.
    163    *
    164    * @see #isDeviceSecure(int)
    165    */
    166   public void setIsDeviceSecure(int userId, boolean isDeviceSecure) {
    167     if (isDeviceSecure) {
    168       deviceSecureForUsers.add(userId);
    169     } else {
    170       deviceSecureForUsers.remove(userId);
    171     }
    172   }
    173 
    174   /**
    175    * For tests on Android >=L MR1, sets the value to be returned by {@link #isDeviceLocked()}.
    176    *
    177    * @see #isDeviceLocked()
    178    */
    179   public void setIsDeviceLocked(boolean isDeviceLocked) {
    180     this.isDeviceLocked = isDeviceLocked;
    181   }
    182 
    183   @Implementation(minSdk = LOLLIPOP_MR1)
    184   public boolean isDeviceLocked() {
    185     return isDeviceLocked;
    186   }
    187 
    188   /**
    189    * For tests on Android >= L MR1, sets the value to be returned by {@link #isDeviceLocked(int)}.
    190    *
    191    * @see #isDeviceLocked(int)
    192    */
    193   public void setIsDeviceLocked(int userId, boolean isLocked) {
    194     if (isLocked) {
    195       deviceLockedForUsers.add(userId);
    196     } else {
    197       deviceLockedForUsers.remove(userId);
    198     }
    199   }
    200 
    201   @Implementation(minSdk = LOLLIPOP_MR1)
    202   protected boolean isDeviceLocked(int userId) {
    203     return deviceLockedForUsers.contains(userId);
    204   }
    205 
    206   /** An implementation of {@link KeyguardManager#KeyguardLock}, for use in tests. */
    207   @Implements(KeyguardManager.KeyguardLock.class)
    208   public static class ShadowKeyguardLock {
    209     private boolean keyguardEnabled = true;
    210 
    211     /**
    212      * Sets the value to be returned by {@link #isEnabled()} to false.
    213      *
    214      * @see #isEnabled()
    215      */
    216     @Implementation
    217     public void disableKeyguard() {
    218       keyguardEnabled = false;
    219     }
    220 
    221     /**
    222      * Sets the value to be returned by {@link #isEnabled()} to true.
    223      *
    224      * @see #isEnabled()
    225      */
    226     @Implementation
    227     public void reenableKeyguard() {
    228       keyguardEnabled = true;
    229     }
    230 
    231     /**
    232      * For tests, returns the value set via {@link #disableKeyguard()} or {@link reenableKeyguard},
    233      * or `true` by default.
    234      *
    235      * @see #setKeyguardLocked(boolean)
    236      */
    237     public boolean isEnabled() {
    238       return keyguardEnabled;
    239     }
    240   }
    241 }
    242