Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.os.Build.VERSION_CODES.LOLLIPOP;
      4 
      5 import android.app.ActivityManager.AppTask;
      6 import android.app.ActivityManager.RecentTaskInfo;
      7 import android.app.IAppTask;
      8 import android.content.Context;
      9 import android.content.Intent;
     10 import android.os.Bundle;
     11 import org.robolectric.annotation.Implementation;
     12 import org.robolectric.annotation.Implements;
     13 import org.robolectric.util.ReflectionHelpers;
     14 
     15 @Implements(value = AppTask.class, minSdk = LOLLIPOP)
     16 public class ShadowAppTask {
     17   private boolean isFinished;
     18   private RecentTaskInfo recentTaskInfo;
     19   private boolean hasMovedToFront;
     20   private boolean isExcludedFromRecents;
     21 
     22   public static AppTask newInstance() {
     23     return ReflectionHelpers.callConstructor(
     24         AppTask.class, ReflectionHelpers.ClassParameter.from(IAppTask.class, null));
     25   }
     26 
     27   /**
     28    * For tests, marks the task as finished. Task is not finished when created initially.
     29    *
     30    * @see #isFinishedAndRemoved()
     31    */
     32   @Implementation
     33   protected void finishAndRemoveTask() {
     34     this.isFinished = true;
     35   }
     36 
     37   /**
     38    * For tests, returns the {@link RecentTaskInfo} set using {@link #setTaskInfo(RecentTaskInfo)}.
     39    * If nothing is set, it returns null.
     40    *
     41    * @see #setTaskInfo(RecentTaskInfo)
     42    */
     43   @Implementation
     44   protected RecentTaskInfo getTaskInfo() {
     45     return recentTaskInfo;
     46   }
     47 
     48   /**
     49    * For tests, marks the task as moved to the front. Task is created and marked as not moved to the
     50    * front.
     51    *
     52    * @see #hasMovedToFront()
     53    */
     54   @Implementation
     55   protected void moveToFront() {
     56     this.hasMovedToFront = true;
     57   }
     58 
     59   /**
     60    * Starts the activity using given context. Started activity can be checked using {@link
     61    * ShadowContextWrapper#getNextStartedActivity()}
     62    *
     63    * @param context Context with which the activity will be start.
     64    * @param intent Intent of the activity to be started.
     65    * @param options Extras passed to the activity.
     66    */
     67   @Implementation
     68   protected void startActivity(Context context, Intent intent, Bundle options) {
     69     context.startActivity(intent, options);
     70   }
     71 
     72   /**
     73    * For tests, marks the task as excluded from recents. Current, status can be checked using {@link
     74    * #isExcludedFromRecents()}.
     75    *
     76    * @param exclude Whether to exclude from recents.
     77    */
     78   @Implementation
     79   protected void setExcludeFromRecents(boolean exclude) {
     80     this.isExcludedFromRecents = exclude;
     81   }
     82 
     83   /** Returns true if {@link #finishAndRemoveTask()} has been called before. */
     84   public boolean isFinishedAndRemoved() {
     85     return isFinished;
     86   }
     87 
     88   /**
     89    * Sets the recentTaskInfo for the task. {@link #getTaskInfo()} returns the task info set using
     90    * this method.
     91    */
     92   public void setTaskInfo(RecentTaskInfo recentTaskInfo) {
     93     this.recentTaskInfo = recentTaskInfo;
     94   }
     95 
     96   /**
     97    * Returns true if task has been moved to the front.
     98    *
     99    * @see #moveToFront()
    100    */
    101   public boolean hasMovedToFront() {
    102     return hasMovedToFront;
    103   }
    104 
    105   /**
    106    * Returns true if task has been excluded from recents.
    107    *
    108    * @see #setExcludeFromRecents(boolean)
    109    */
    110   public boolean isExcludedFromRecents() {
    111     return isExcludedFromRecents;
    112   }
    113 }
    114