@Override
 public void handleClear() {
   typedText.setLength(0);
   final InputConnection ic = getCurrentInputConnection();
   ic.setSelection(0, 0);
   ic.deleteSurroundingText(MAX_INT, MAX_INT);
 }
 void resetModifiers() {
   InputConnection conn = getCurrentInputConnection();
   if (conn == null) {
     return;
   }
   conn.clearMetaKeyStates(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON | KeyEvent.META_SYM_ON);
 }
 public void onKey(int primaryCode, int[] keyCodes) {
   InputConnection ic = getCurrentInputConnection();
   playClick(primaryCode);
   switch (primaryCode) {
     case Keyboard.KEYCODE_DELETE:
       ic.deleteSurroundingText(1, 0);
       break;
     case Keyboard.KEYCODE_SHIFT:
       if (caps) {
         keyboard = new Keyboard(this, R.xml.qwerty);
         kv.setKeyboard(keyboard);
         kv.invalidateAllKeys();
         kv.setOnKeyboardActionListener(this);
       } else {
         keyboard = new Keyboard(this, R.xml.qwertytwo);
         kv.setKeyboard(keyboard);
         kv.invalidateAllKeys();
         kv.setOnKeyboardActionListener(this);
       }
       caps = !caps;
       keyboard.setShifted(caps);
       kv.invalidateAllKeys();
       break;
     case Keyboard.KEYCODE_DONE:
       ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
       break;
     default:
       char code = (char) primaryCode;
       if (Character.isLetter(code) && caps) {
         code = Character.toUpperCase(code);
       }
       ic.commitText(String.valueOf(code), 1);
   }
 }
 private void restoreFromHistory(@Nullable KeyboardInputHistoryState state) {
   if (state != null) {
     final InputConnection ic = getCurrentInputConnection();
     ic.deleteSurroundingText(MAX_INT, MAX_INT);
     ic.commitText(state.getCharSequence(), 1);
   }
 }
 @Override
 public void onClick(View view) {
   InputConnection ic = getCurrentInputConnection();
   if (ic != null) {
     ic.deleteSurroundingText(1, 0);
   }
   resetState();
 }
Exemple #6
0
 private void pickSuggestion(CharSequence suggestion) {
   InputConnection ic = mService.getCurrentInputConnection();
   if (ic != null) {
     ic.commitText(suggestion, 1);
   }
   mService.setCommittedLength(suggestion.length());
   mService.setSuggestions(null, false, false, false);
 }
Exemple #7
0
 @Override
 public void onText(CharSequence text) {
   InputConnection ic = mService.getCurrentInputConnection();
   if (ic == null) return;
   ic.beginBatchEdit();
   ic.commitText(text, 1);
   ic.endBatchEdit();
 }
 @Override
 public void onText(@Nullable CharSequence text) {
   final InputConnection ic = getCurrentInputConnection();
   ic.beginBatchEdit();
   commitTyped();
   commitText(ic, text, 0);
   ic.endBatchEdit();
 }
 private void selectAll(InputConnection conn) {
   ExtractedText text = conn.getExtractedText(req, 0);
   try {
     conn.setSelection(0, text.text.length());
   } catch (NullPointerException e) {
     // Potentially, text or text.text can be null
   }
 }
  public boolean processKey(
      InputConnection inputContext, KeyEvent event, boolean upperCase, boolean realAction) {
    if (null == inputContext || null == event) return false;

    int keyCode = event.getKeyCode();

    CharSequence prefix = null;
    prefix = inputContext.getTextBeforeCursor(2, 0);

    int keyChar;
    keyChar = 0;
    if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
      keyChar = keyCode - KeyEvent.KEYCODE_A + 'a';
      if (upperCase) {
        keyChar = keyChar + 'A' - 'a';
      }
    } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9)
      keyChar = keyCode - KeyEvent.KEYCODE_0 + '0';
    else if (keyCode == KeyEvent.KEYCODE_COMMA) keyChar = ',';
    else if (keyCode == KeyEvent.KEYCODE_PERIOD) keyChar = '.';
    else if (keyCode == KeyEvent.KEYCODE_APOSTROPHE) keyChar = '\'';
    else if (keyCode == KeyEvent.KEYCODE_AT) keyChar = '@';
    else if (keyCode == KeyEvent.KEYCODE_SLASH) keyChar = '/';

    if (0 == keyChar) {
      mLastKeyCode = keyCode;

      String insert = null;
      if (KeyEvent.KEYCODE_DEL == keyCode) {
        if (realAction) {
          inputContext.deleteSurroundingText(1, 0);
        }
      } else if (KeyEvent.KEYCODE_ENTER == keyCode) {
        insert = "\n";
      } else if (KeyEvent.KEYCODE_SPACE == keyCode) {
        insert = " ";
      } else {
        return false;
      }

      if (null != insert && realAction) inputContext.commitText(insert, insert.length());

      return true;
    }

    if (!realAction) return true;

    if (KeyEvent.KEYCODE_SHIFT_LEFT == mLastKeyCode
        || KeyEvent.KEYCODE_SHIFT_LEFT == mLastKeyCode) {
      if (keyChar >= 'a' && keyChar <= 'z') keyChar = keyChar - 'a' + 'A';
    } else if (KeyEvent.KEYCODE_ALT_LEFT == mLastKeyCode) {
    }

    String result = String.valueOf((char) keyChar);
    inputContext.commitText(result, result.length());
    mLastKeyCode = keyCode;
    return true;
  }
 @Override
 public void handleCursorLeft() {
   final InputConnection ic = getCurrentInputConnection();
   int selectionStart = getSelectionStart(ic);
   int selectionEnd = getSelectionEnd(ic, selectionStart);
   if (selectionStart < 0) {
     selectionStart = selectionStart - 1;
     ic.setSelection(selectionStart, selectionEnd);
   }
 }
Exemple #12
0
 public void onText(CharSequence text) {
   InputConnection ic = getCurrentInputConnection();
   if (ic == null) return;
   ic.beginBatchEdit();
   if (mComposing.length() > 0) {
     commitTyped(ic);
   }
   ic.commitText(text, 0);
   ic.endBatchEdit();
   updateShiftKeyState(getCurrentInputEditorInfo());
 }
Exemple #13
0
  public static boolean inputCursor(int position) {
    InputConnection connection = getInputConnection();

    if (connection != null) {
      if (connection.setSelection(position, position)) {
        return true;
      }
    }

    return false;
  }
Exemple #14
0
  public static boolean inputCharacter(char character) {
    InputConnection connection = getInputConnection();

    if (connection != null) {
      if (connection.commitText(Character.toString(character), 1)) {
        return true;
      }
    }

    return false;
  }
Exemple #15
0
  public static boolean inputKeyDelete() {
    InputConnection connection = getInputConnection();

    if (connection != null) {
      if (connection.deleteSurroundingText(0, 1)) {
        return true;
      }
    }

    return false;
  }
 String getText() {
   String text = "";
   try {
     InputConnection conn = getCurrentInputConnection();
     ExtractedTextRequest req = new ExtractedTextRequest();
     req.hintMaxChars = 1000000;
     req.hintMaxLines = 10000;
     req.flags = 0;
     req.token = 1;
     text = conn.getExtractedText(req, 0).text.toString();
   } catch (Throwable t) {
   }
   return text;
 }
 boolean setText(String text) {
   // FIXME: need feedback if the input was lost
   InputConnection conn = getCurrentInputConnection();
   if (conn == null) {
     //      Debug.d("connection closed");
     return false;
   }
   conn.beginBatchEdit();
   // FIXME: hack
   conn.deleteSurroundingText(100000, 100000);
   conn.commitText(text, text.length());
   conn.endBatchEdit();
   return true;
 }
  private void keyDel(InputConnection conn) {
    // if control key used -- delete word right
    if (pressedKeys.contains(KEY_CONTROL)) {
      deleteWordRight(conn);
      return;
    }

    if (pressedKeys.contains(KeyEvent.KEYCODE_SHIFT_LEFT)) {
      cut(conn);
      return;
    }
    conn.deleteSurroundingText(0, 1);
    conn.commitText("", 0);
  }
Exemple #19
0
 private void handleBackspace() {
   boolean deleteChar = false;
   InputConnection ic = mService.getCurrentInputConnection();
   if (ic == null) return;
   final int length = mComposing.length();
   if (length > 0) {
     mComposing.delete(length - 1, length);
     mWord.deleteLast();
     ic.setComposingText(mComposing, 1);
     postUpdateSuggestions();
   } else {
     ic.deleteSurroundingText(1, 0);
   }
 }
 @Override
 public void run() {
   final EditorInfo info = new EditorInfo();
   final InputConnection ic = getView().onCreateInputConnection(info);
   fAssertNotNull("Must have an InputConnection", ic);
   // Restore the IC to a clean state
   ic.clearMetaKeyStates(-1);
   ic.finishComposingText();
   mTest.test(ic, info);
   synchronized (this) {
     // Test finished; return from launch().
     mDone = true;
     notify();
   }
 }
Exemple #21
0
 /** Commits the given text to the editing field. */
 public boolean commitText(InputConnection ic, CharSequence text) {
   if (ic != null) {
     if (text.length() > 1) {
       // Batch edit a sequence of characters.
       ic.beginBatchEdit();
       ic.commitText(text, 1);
       ic.endBatchEdit();
     } else {
       ic.commitText(text, 1);
     }
     // Composing-text in the editor has been cleared.
     composingText.setLength(0);
     return true;
   }
   return false;
 }
 private void commitText(@NotNull InputConnection ic, @Nullable CharSequence text, int position) {
   ic.commitText(text, position);
   if (!Strings.isEmpty(text)) {
     history.addState(
         new KeyboardInputHistoryState(AndroidKeyboardUtils.getTextFromInputConnection(ic), 0));
   }
 }
 private void keyEnd(InputConnection conn) {
   boolean control = pressedKeys.contains(KEY_CONTROL);
   boolean shift = pressedKeys.contains(KeyEvent.KEYCODE_SHIFT_LEFT);
   ExtractedText text = conn.getExtractedText(req, 0);
   if (text == null) return;
   int end;
   if (control) {
     end = text.text.length();
   } else {
     end = text.text.toString().indexOf('\n', text.selectionEnd);
     if (end == -1) end = text.text.length();
   }
   int start = shift ? text.selectionStart : end;
   Log.d("wifikeyboard", "start = " + start + " end = " + end);
   conn.setSelection(start, end);
 }
Exemple #24
0
 /** Helper function to commit any text being composed in to the editor. */
 private void commitTyped(InputConnection inputConnection) {
   if (mComposing.length() > 0) {
     inputConnection.commitText(mComposing, mComposing.length());
     mComposing.setLength(0);
     updateCandidates();
   }
 }
  private void deleteWordRight(InputConnection conn) {
    ExtractedText text = conn.getExtractedText(req, 0);
    if (text == null) return;

    int end = text.selectionEnd;
    String str = text.text.toString();
    int len = str.length();

    for (; end < len; end++) {
      if (!Character.isSpace(str.charAt(end))) break;
    }
    for (; end < len; end++) {
      if (Character.isSpace(str.charAt(end))) break;
    }

    conn.deleteSurroundingText(0, end - text.selectionEnd);
  }
Exemple #26
0
  public void forwardKeyEvent(int code, boolean press) {
    InputConnection connection = getCurrentInputConnection();

    if (connection != null) {
      int action = press ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP;
      KeyEvent event = new KeyEvent(action, code);
      event = KeyEvent.changeFlags(event, KeyEvent.FLAG_SOFT_KEYBOARD);

      if (connection.sendKeyEvent(event)) {
        logKeyEvent(code, press, "forwarded");
      } else {
        logKeyEvent(code, press, "not forwarded");
      }
    } else {
      logKeyEvent(code, press, "unforwardable");
    }
  }
  private void deleteWordLeft(InputConnection conn) {
    // TODO: what is the correct word deleting policy?
    // delete until next space? until next different character type?
    ExtractedText text = conn.getExtractedText(req, 0);
    if (text == null) return;

    int end = text.selectionEnd - 1;

    String str = text.text.toString();

    for (; end >= 0; end--) {
      if (!Character.isSpace(str.charAt(end))) break;
    }
    for (; end >= 0; end--) {
      if (Character.isSpace(str.charAt(end))) break;
    }
    end++;
    conn.deleteSurroundingText(text.selectionEnd - end, 0);
  }
  private void wordRight(InputConnection conn) {
    boolean shift = pressedKeys.contains(KeyEvent.KEYCODE_SHIFT_LEFT);
    ExtractedText text = conn.getExtractedText(req, 0);
    if (text == null) return;

    int end = text.selectionEnd;
    String str = text.text.toString();
    int len = str.length();

    for (; end < len; end++) {
      if (!Character.isSpace(str.charAt(end))) break;
    }
    for (; end < len; end++) {
      if (Character.isSpace(str.charAt(end))) break;
    }
    int start = shift ? text.selectionStart : end;
    Log.d("wifikeyboard", "start = " + start + " end = " + end);
    conn.setSelection(start, end);
  }
    /**
     * Processes pending events on the Gecko thread before returning. Must be called on the input
     * connection thread during a test.
     */
    protected void processGeckoEvents(final InputConnection ic) {
      fAssertSame(
          "Should be called on input connection thread",
          Looper.myLooper(),
          inputConnectionHandler.getLooper());

      fAssertTrue(
          "Should be able to process Gecko events",
          ic.performPrivateCommand("process-gecko-events", null));
    }
  @Override
  public boolean handleBackspace() {
    boolean changed = false;

    int length = typedText.length();

    final InputConnection ic = getCurrentInputConnection();
    if (length > 1) {
      typedText.delete(length - 1, length);
      ic.setComposingText(typedText, 1);
      changed = true;
    } else if (length > 0) {
      clearTypedText();
      commitText(ic, "", 0);
      changed = true;
    }

    return changed;
  }