• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.google.common.collect.BiMap;
19 import com.google.common.collect.ImmutableBiMap;
20 
21 import com.android.monkeyrunner.MonkeyDevice;
22 import com.android.chimpchat.core.IChimpDevice;
23 import com.android.chimpchat.core.TouchPressType;
24 
25 /**
26  * Action to press a certain button.
27  */
28 public class PressAction implements Action {
29     public static String[] KEYS = {
30         "MENU", "HOME", "SEARCH",
31     };
32 
33     public static final BiMap<String, String> DOWNUP_FLAG_MAP =
34         ImmutableBiMap.of(MonkeyDevice.DOWN_AND_UP, "Press",
35                 MonkeyDevice.DOWN, "Down",
36                 MonkeyDevice.UP, "Up");
37 
38     private final String key;
39     private final String downUpFlag;
40 
PressAction(String key, String downUpFlag)41     public PressAction(String key, String downUpFlag) {
42         this.key = key;
43         this.downUpFlag = downUpFlag;
44     }
45 
PressAction(String key)46     public PressAction(String key) {
47         this(key, MonkeyDevice.DOWN_AND_UP);
48     }
49 
50     @Override
getDisplayName()51     public String getDisplayName() {
52         return String.format("%s button %s",
53                 DOWNUP_FLAG_MAP.get(downUpFlag), key);
54     }
55 
56     @Override
serialize()57     public String serialize() {
58         String pydict = PyDictUtilBuilder.newBuilder().
59         add("name", key).
60         add("type", downUpFlag).build();
61         return "PRESS|" + pydict;
62     }
63 
64     @Override
execute(IChimpDevice device)65     public void execute(IChimpDevice device) {
66         device.press(key, TouchPressType.fromIdentifier(downUpFlag));
67     }
68 }
69