Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 NativeDriver committers Copyright 2011 Google Inc.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software distributed under the License
  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  * or implied. See the License for the specific language governing permissions and limitations under
  * the License.
  */
 
 package io.selendroid.android;
 
 import  io.selendroid.server.model.Keyboard;
 
Provides a method to send a string to an application under test. Keys are sent using an Instrumentation instance. The strings may contain any character in the AndroidKeys enum.

Note that this class does not focus on a particular View before sending keys, nor does it require that some View has focus. This is fine if you are sending the Menu key, or using the arrow keys to select an item in a list. If you are trying to type into a certain widget, be sure it has focus before using this class.

Author(s):
Matt DeVore
 
 public class KeySender {
   private class KeyboardImpl implements Keyboard {
     @Override
     public void pressKey(Keys keyToPress) {
       sendKeyEvent(.keyToPress.charAt(0));
     }
 
     @Override
     public void releaseKey(Keys keyToRelease) {
       sendKeyEvent(.keyToRelease.charAt(0));
     }
 
     @Override
     public void sendKeys(CharSequence... keysToSend) {
       StringBuilder sb = new StringBuilder();
       for (CharSequence keys : keysToSend) {
         sb.append(keys);
       }
       send(sb.toString());
     }
   }
 
   private final Instrumentation instrumentation;
   private final KeyboardImpl keyboardImpl;

  
Creates a new instance which sends keys to the given Instrumentation.
 
   public KeySender(Instrumentation instrumentation) {
     //this.instrumentation = Preconditions.checkNotNull(instrumentation);
     this.=instrumentation;
     this. = new KeyboardImpl();
   }

  
Returns a Keyboard object which sends key using this KeySender.
 
   public Keyboard getKeyboard() {
     return ;
   }
 
   private static int indexOfSpecialKey(CharSequence stringint startIndex) {
     for (int i = startIndexi < string.length(); i++) {
       if (AndroidKeys.hasAndroidKeyEvent(string.charAt(i))) {
         return i;
       }
     }
     return string.length();
   }

  
Sends a single key event to the Instrumentation for a given character.

Parameters:
action KeyEvent.ACTION_* code representing key action
keyCode character representing key to press, release, etc.
 
   public void sendKeyEvent(int actionchar keyCode) {
     try {
       .sendKeySync(new KeyEvent(action, AndroidKeys.keyCodeFor(keyCode)));
     } catch (SecurityException exception) {
       throw new SelendroidException(exception);
     }
   }

  
A convenience wrapper for sendKeyEvent(int, char) which takes an AndroidKeys object.

Parameters:
action KeyEvent.ACTION_* code representing key action
key AndroidKeys object representing key to press, release, etc.
  public void sendKeyEvent(int actionAndroidKeys key) {
    this.sendKeyEvent(actionkey.charAt(0));
  }

  
Sends key events to the Instrumentation. This method will send a portion of the given CharSequence as a single String if the portion does not contain any special keys.

Parameters:
string the keys to send to the Instrumentation.
  public void send(CharSequence string) {
    int currentIndex = 0;
    while (currentIndex < string.length()) {
      char currentCharacter = string.charAt(currentIndex);
      if (AndroidKeys.hasAndroidKeyEvent(currentCharacter)) {
        // The next character is special and must be sent individually
        .sendKeyDownUpSync(AndroidKeys.keyCodeFor(currentCharacter));
        currentIndex++;
      } else {
        // There is at least one "normal" character, that is a character
        // represented by a plain Unicode character that can be sent with
        // sendStringSync. So send as many such consecutive normal characters
        // as possible in a single String.
        int nextSpecialKey = indexOfSpecialKey(stringcurrentIndex);
        .sendStringSync(string.subSequence(currentIndexnextSpecialKey).toString());
        currentIndex = nextSpecialKey;
      }
    }
  }
New to GrepCode? Check out our FAQ X