• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 DroidDriver committers
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 
17 package io.appium.droiddriver.actions;
18 
19 import android.annotation.SuppressLint;
20 import android.annotation.TargetApi;
21 import android.os.Build;
22 import android.view.KeyEvent;
23 
24 import io.appium.droiddriver.UiElement;
25 import io.appium.droiddriver.util.Events;
26 import io.appium.droiddriver.util.Strings;
27 import io.appium.droiddriver.util.Strings.ToStringHelper;
28 
29 /**
30  * An action to press a single key. While it is convenient for navigating the UI, do not overuse it
31  * - the application may interpret key codes in a custom way and, more importantly, application
32  * users may not have access to it because the device (physical or virtual keyboard) may not support
33  * all key codes.
34  */
35 public class SingleKeyAction extends KeyAction {
36   // Common instances for convenience and memory preservation.
37   public static final SingleKeyAction MENU = new SingleKeyAction(KeyEvent.KEYCODE_MENU);
38   public static final SingleKeyAction SEARCH = new SingleKeyAction(KeyEvent.KEYCODE_SEARCH);
39   public static final SingleKeyAction BACK = new SingleKeyAction(KeyEvent.KEYCODE_BACK);
40   public static final SingleKeyAction DELETE = new SingleKeyAction(KeyEvent.KEYCODE_DEL);
41   /** Requires SDK API 11 or higher */
42   @SuppressLint("InlinedApi")
43   public static final SingleKeyAction CTRL_MOVE_HOME = new SingleKeyAction(
44       KeyEvent.KEYCODE_MOVE_HOME, KeyEvent.META_CTRL_LEFT_ON);
45   /** Requires SDK API 11 or higher */
46   @SuppressLint("InlinedApi")
47   public static final SingleKeyAction CTRL_MOVE_END = new SingleKeyAction(
48       KeyEvent.KEYCODE_MOVE_END, KeyEvent.META_CTRL_LEFT_ON);
49 
50   private final int keyCode;
51   private final int metaState;
52 
53   /** Defaults metaState to 0 */
SingleKeyAction(int keyCode)54   public SingleKeyAction(int keyCode) {
55     this(keyCode, 0);
56   }
57 
58   /** Defaults timeoutMillis to 100 and checkFocused to false */
SingleKeyAction(int keyCode, int metaState)59   public SingleKeyAction(int keyCode, int metaState) {
60     this(keyCode, metaState, 100L, false);
61   }
62 
SingleKeyAction(int keyCode, int metaState, long timeoutMillis, boolean checkFocused)63   public SingleKeyAction(int keyCode, int metaState, long timeoutMillis, boolean checkFocused) {
64     super(timeoutMillis, checkFocused);
65     this.keyCode = keyCode;
66     this.metaState = metaState;
67   }
68 
69   @Override
perform(InputInjector injector, UiElement element)70   public boolean perform(InputInjector injector, UiElement element) {
71     maybeCheckFocused(element);
72 
73     final long downTime = Events.keyDown(injector, keyCode, metaState);
74     Events.keyUp(injector, downTime, keyCode, metaState);
75 
76     return true;
77   }
78 
79   @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
80   @Override
toString()81   public String toString() {
82     String keyCodeString =
83         Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1 ? String.valueOf(keyCode)
84             : KeyEvent.keyCodeToString(keyCode);
85     ToStringHelper toStringHelper = Strings.toStringHelper(this);
86     if (metaState != 0) {
87       toStringHelper.add("metaState", metaState);
88     }
89     return toStringHelper.addValue(keyCodeString).toString();
90   }
91 }
92