Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import android.widget.Scroller;
      4 import org.robolectric.annotation.Implementation;
      5 import org.robolectric.annotation.Implements;
      6 
      7 @Implements(Scroller.class)
      8 public class ShadowScroller {
      9   private int startX;
     10   private int startY;
     11   private int finalX;
     12   private int finalY;
     13   private long startTime;
     14   private long duration;
     15   private boolean started;
     16 
     17   @Implementation
     18   public int getStartX() {
     19     return startX;
     20   }
     21 
     22   @Implementation
     23   public int getStartY() {
     24     return startY;
     25   }
     26 
     27   @Implementation
     28   public int getCurrX() {
     29     long dt = deltaTime();
     30     return dt >= duration ? finalX : startX + (int) ((deltaX() * dt) / duration);
     31   }
     32 
     33   @Implementation
     34   public int getCurrY() {
     35     long dt = deltaTime();
     36     return dt >= duration ? finalY : startY + (int) ((deltaY() * dt) / duration);
     37   }
     38 
     39   @Implementation
     40   public int getFinalX() {
     41     return finalX;
     42   }
     43 
     44   @Implementation
     45   public int getFinalY() {
     46     return finalY;
     47   }
     48 
     49   @Implementation
     50   public int getDuration() {
     51     return (int) duration;
     52   }
     53 
     54   @Implementation
     55   public void startScroll(int startX, int startY, int dx, int dy, int duration) {
     56     this.startX = startX;
     57     this.startY = startY;
     58     finalX = startX + dx;
     59     finalY = startY + dy;
     60     startTime = ShadowApplication.getInstance().getForegroundThreadScheduler().getCurrentTime();
     61     this.duration = duration;
     62     started = true;
     63     // enque a dummy task so that the scheduler will actually run
     64     ShadowApplication.getInstance().getForegroundThreadScheduler().postDelayed(new Runnable() {
     65       @Override
     66       public void run() {
     67         // do nothing
     68       }
     69     }, duration);
     70   }
     71 
     72   @Implementation
     73   public boolean computeScrollOffset() {
     74     if (!started) {
     75       return false;
     76     }
     77     started &= deltaTime() < duration;
     78     return true;
     79   }
     80 
     81   @Implementation
     82   public boolean isFinished() {
     83     return deltaTime() > duration;
     84   }
     85 
     86   @Implementation
     87   public int timePassed() {
     88     return (int) deltaTime();
     89   }
     90 
     91   private long deltaTime() {
     92     return ShadowApplication.getInstance().getForegroundThreadScheduler().getCurrentTime() - startTime;
     93   }
     94 
     95   private int deltaX() {
     96     return (finalX - startX);
     97   }
     98 
     99   private int deltaY() {
    100     return (finalY - startY);
    101   }
    102 }
    103