Esempio n. 1
0
 public void handleKeyboardType(int type, boolean autocorrect) {
   // Switched the keyboard handler to use the inputType rather than the rawInputType
   // This is kinda brute-force but more effective for most use-cases
   switch (type) {
     case KEYBOARD_ASCII:
       tv.setKeyListener(TextKeyListener.getInstance(autocorrect, Capitalize.NONE));
       tv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
       // tv.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
       break;
     case KEYBOARD_NUMBERS_PUNCTUATION:
       tv.setInputType(InputType.TYPE_CLASS_NUMBER);
       // tv.setKeyListener(DigitsKeyListener.getInstance());
       break;
     case KEYBOARD_URL:
       Log.i(LCAT, "Setting keyboard type URL-3");
       // tv.setKeyListener(TextKeyListener.getInstance(autocorrect, Capitalize.NONE));
       tv.setImeOptions(EditorInfo.IME_ACTION_GO);
       // tv.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
       tv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
       break;
     case KEYBOARD_NUMBER_PAD:
       tv.setKeyListener(DigitsKeyListener.getInstance(true, true));
       // tv.setRawInputType(InputType.TYPE_CLASS_NUMBER);
       tv.setInputType(InputType.TYPE_CLASS_NUMBER);
       break;
     case KEYBOARD_PHONE_PAD:
       tv.setKeyListener(DialerKeyListener.getInstance());
       // tv.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_CLASS_PHONE);
       tv.setInputType(InputType.TYPE_CLASS_PHONE);
       break;
     case KEYBOARD_EMAIL_ADDRESS:
       // tv.setKeyListener(TextKeyListener.getInstance(autocorrect, Capitalize.NONE));
       tv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
       break;
     case KEYBOARD_DEFAULT:
       tv.setKeyListener(TextKeyListener.getInstance(autocorrect, Capitalize.NONE));
       // tv.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
       tv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
       break;
     case KEYBOARD_PASSWORD:
       tv.setKeyListener(TextKeyListener.getInstance(false, Capitalize.NONE));
       // tv.setRawInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
       tv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
       break;
   }
 }
  public void handleKeyboard(KrollDict d) {
    int type = KEYBOARD_ASCII;
    boolean passwordMask = false;
    boolean editable = true;
    int autocorrect = InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;
    int autoCapValue = 0;

    if (d.containsKey(TiC.PROPERTY_AUTOCORRECT)
        && !TiConvert.toBoolean(d, TiC.PROPERTY_AUTOCORRECT, true)) {
      autocorrect = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
    }

    if (d.containsKey(TiC.PROPERTY_EDITABLE)) {
      editable = TiConvert.toBoolean(d, TiC.PROPERTY_EDITABLE, true);
    }

    if (d.containsKey(TiC.PROPERTY_AUTOCAPITALIZATION)) {

      switch (TiConvert.toInt(
          d.get(TiC.PROPERTY_AUTOCAPITALIZATION), TEXT_AUTOCAPITALIZATION_NONE)) {
        case TEXT_AUTOCAPITALIZATION_NONE:
          autoCapValue = 0;
          break;
        case TEXT_AUTOCAPITALIZATION_ALL:
          autoCapValue =
              InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
                  | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
                  | InputType.TYPE_TEXT_FLAG_CAP_WORDS;
          break;
        case TEXT_AUTOCAPITALIZATION_SENTENCES:
          autoCapValue = InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
          break;

        case TEXT_AUTOCAPITALIZATION_WORDS:
          autoCapValue = InputType.TYPE_TEXT_FLAG_CAP_WORDS;
          break;
        default:
          Log.w(
              TAG,
              "Unknown AutoCapitalization Value ["
                  + d.getString(TiC.PROPERTY_AUTOCAPITALIZATION)
                  + "]");
          break;
      }
    }

    if (d.containsKey(TiC.PROPERTY_PASSWORD_MASK)) {
      passwordMask = TiConvert.toBoolean(d, TiC.PROPERTY_PASSWORD_MASK, false);
    }

    if (d.containsKey(TiC.PROPERTY_KEYBOARD_TYPE)) {
      type = TiConvert.toInt(d.get(TiC.PROPERTY_KEYBOARD_TYPE), KEYBOARD_DEFAULT);
    }

    int typeModifiers = autocorrect | autoCapValue;
    int textTypeAndClass = typeModifiers;
    // For some reason you can't set both TYPE_CLASS_TEXT and TYPE_TEXT_FLAG_NO_SUGGESTIONS
    // together.
    // Also, we need TYPE_CLASS_TEXT for passwords.
    if ((autocorrect != InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS || passwordMask)
        && type != KEYBOARD_DECIMAL_PAD) {
      textTypeAndClass = textTypeAndClass | InputType.TYPE_CLASS_TEXT;
    }

    tv.setCursorVisible(true);
    switch (type) {
      case KEYBOARD_DEFAULT:
      case KEYBOARD_ASCII:
        // Don't need a key listener, inputType handles that.
        break;
      case KEYBOARD_NUMBERS_PUNCTUATION:
        textTypeAndClass |= (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_CLASS_TEXT);
        tv.setKeyListener(
            new NumberKeyListener() {
              @Override
              public int getInputType() {
                return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_CLASS_TEXT;
              }

              @Override
              protected char[] getAcceptedChars() {
                return new char[] {
                  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-', '+', '_', '*', '-',
                  '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '=', '{', '}', '[', ']', '|',
                  '\\', '<', '>', ',', '?', '/', ':', ';', '\'', '"', '~'
                };
              }
            });
        break;
      case KEYBOARD_URL:
        Log.d(TAG, "Setting keyboard type URL-3", Log.DEBUG_MODE);
        tv.setImeOptions(EditorInfo.IME_ACTION_GO);
        textTypeAndClass |= InputType.TYPE_TEXT_VARIATION_URI;
        break;
      case KEYBOARD_DECIMAL_PAD:
        textTypeAndClass |=
            (InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
      case KEYBOARD_NUMBER_PAD:
        tv.setKeyListener(DigitsKeyListener.getInstance(true, true));
        textTypeAndClass |= InputType.TYPE_CLASS_NUMBER;
        break;
      case KEYBOARD_PHONE_PAD:
        tv.setKeyListener(DialerKeyListener.getInstance());
        textTypeAndClass |= InputType.TYPE_CLASS_PHONE;
        break;
      case KEYBOARD_EMAIL_ADDRESS:
        textTypeAndClass |= InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
        break;
    }

    if (passwordMask) {
      textTypeAndClass |= InputType.TYPE_TEXT_VARIATION_PASSWORD;
      // Sometimes password transformation does not work properly when the input type is set after
      // the transformation method.
      // This issue has been filed at http://code.google.com/p/android/issues/detail?id=7092
      tv.setInputType(textTypeAndClass);
      tv.setTransformationMethod(PasswordTransformationMethod.getInstance());

      // turn off text UI in landscape mode b/c Android numeric passwords are not masked correctly
      // in landscape mode.
      if (type == KEYBOARD_NUMBERS_PUNCTUATION
          || type == KEYBOARD_DECIMAL_PAD
          || type == KEYBOARD_NUMBER_PAD) {
        tv.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
      }

    } else {
      tv.setInputType(textTypeAndClass);
      if (tv.getTransformationMethod() instanceof PasswordTransformationMethod) {
        tv.setTransformationMethod(null);
      }
    }
    if (!editable) {
      tv.setKeyListener(null);
      tv.setCursorVisible(false);
    }

    // setSingleLine() append the flag TYPE_TEXT_FLAG_MULTI_LINE to the current inputType, so we
    // want to call this
    // after we set inputType.
    if (!field) {
      tv.setSingleLine(false);
    }
  }