1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.monkeyrunner.recorder.actions; 17 18 import com.android.chimpchat.core.IChimpDevice; 19 20 /** 21 * Action to drag the "finger" across the device. 22 */ 23 public class DragAction implements Action { 24 private final long timeMs; 25 private final int steps; 26 private final int startx; 27 private final int starty; 28 private final int endx; 29 private final int endy; 30 private final Direction dir; 31 32 public enum Direction { 33 NORTH, SOUTH, EAST, WEST; 34 35 private static String[] names; 36 static { 37 Direction[] values = Direction.values(); 38 names = new String[values.length]; 39 for (int x = 0; x < values.length; x++) { 40 names[x] = values[x].name(); 41 } 42 } 43 getNames()44 public static String[] getNames() { 45 return names; 46 } 47 } 48 DragAction(Direction dir, int startx, int starty, int endx, int endy, int numSteps, long millis)49 public DragAction(Direction dir, 50 int startx, int starty, int endx, int endy, 51 int numSteps, long millis) { 52 this.dir = dir; 53 this.startx = startx; 54 this.starty = starty; 55 this.endx = endx; 56 this.endy = endy; 57 steps = numSteps; 58 timeMs = millis; 59 } 60 61 @Override getDisplayName()62 public String getDisplayName() { 63 return String.format("Fling %s", dir.name().toLowerCase()); 64 } 65 66 @Override serialize()67 public String serialize() { 68 float duration = timeMs / 1000.0f; 69 70 String pydict = PyDictUtilBuilder.newBuilder(). 71 addTuple("start", startx, starty). 72 addTuple("end", endx, endy). 73 add("duration", duration). 74 add("steps", steps). 75 build(); 76 return "DRAG|" + pydict; 77 } 78 79 @Override execute(IChimpDevice device)80 public void execute(IChimpDevice device) { 81 device.drag(startx, starty, endx, endy, steps, timeMs); 82 } 83 } 84