Chromium Code Reviews| Index: blimp/client/app/android/java/src/org/chromium/blimp/input/ImeHelperDialog.java |
| diff --git a/blimp/client/app/android/java/src/org/chromium/blimp/input/ImeHelperDialog.java b/blimp/client/app/android/java/src/org/chromium/blimp/input/ImeHelperDialog.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7777a464eaa3a89ed75139631985434e316104c8 |
| --- /dev/null |
| +++ b/blimp/client/app/android/java/src/org/chromium/blimp/input/ImeHelperDialog.java |
| @@ -0,0 +1,191 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.blimp.input; |
| + |
| +import android.app.Activity; |
| +import android.app.AlertDialog; |
| +import android.content.Context; |
| +import android.content.DialogInterface; |
| +import android.text.InputType; |
| +import android.view.KeyEvent; |
| +import android.view.View; |
| +import android.view.WindowManager; |
| +import android.view.inputmethod.EditorInfo; |
| +import android.widget.EditText; |
| +import android.widget.TextView; |
| + |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| +import org.chromium.blimp.R; |
| +import org.chromium.blimp.session.BlimpClientSession; |
| +import org.chromium.ui.base.ime.TextInputType; |
| + |
| +/** |
| + * Helper class showing the UI that allows users to enter text into a web page. |
| + * A pop up is created when user taps on a text area prompting user to start typing and closes as |
| + * soon as user hits enter or presses back button. |
| + */ |
| +@JNINamespace("blimp::client") |
| +public class ImeHelperDialog { |
| + private static final String TAG = "ImeHelperDialog"; |
| + private long mNativeImeHelperDialog; |
| + private Context mContext; |
|
David Trainor- moved to gerrit
2016/08/22 21:25:25
final?
shaktisahu
2016/08/23 17:35:14
Done.
|
| + private AlertDialog mAlertDialog; |
| + |
| + public ImeHelperDialog(Context context) { |
|
David Trainor- moved to gerrit
2016/08/22 21:25:24
Javadoc
shaktisahu
2016/08/23 17:35:14
Done.
|
| + mContext = context; |
| + } |
| + |
| + /** |
| + * To be called when the native library is loaded so that this class can initialize its native |
| + * components. |
| + * @param blimpClientSession The {@link BlimpClientSession} that contains the features |
| + * required by the native components of the ImeHelperDialog. |
| + */ |
| + public void initialize(BlimpClientSession blimpClientSession) { |
| + assert mNativeImeHelperDialog == 0; |
| + |
| + mNativeImeHelperDialog = nativeInit(blimpClientSession); |
| + } |
| + |
| + /** |
| + * To be called when this class should be torn down. This {@link View} should not be used after |
| + * this. |
| + */ |
| + public void destroy() { |
| + if (mNativeImeHelperDialog == 0) return; |
| + |
| + nativeDestroy(mNativeImeHelperDialog); |
| + mNativeImeHelperDialog = 0; |
| + } |
| + |
| + /** |
| + * Sends the text entered from IME to blimp engine. |
| + * @param text The text the user entered. |
| + */ |
| + public void onImeTextEntered(String text) { |
|
David Trainor- moved to gerrit
2016/08/22 21:25:25
Does this have to be public?
shaktisahu
2016/08/23 17:35:14
Done.
|
| + if (mNativeImeHelperDialog == 0) return; |
| + |
| + nativeOnImeTextEntered(mNativeImeHelperDialog, text); |
| + } |
| + |
| + @CalledByNative |
| + private void onShowImeRequested(int inputType, String text) { |
| + createTextInputPopup(inputType, text); |
| + } |
| + |
| + @CalledByNative |
| + private void onHideImeRequested() { |
| + if (mAlertDialog == null) return; |
| + |
| + mAlertDialog.dismiss(); |
| + } |
| + |
| + public void createTextInputPopup(int inputType, String existingText) { |
| + final View viewPopup = |
| + ((Activity) mContext).getLayoutInflater().inflate(R.layout.text_input_popup, null); |
| + final ImeEditText editText = (ImeEditText) viewPopup.findViewById(R.id.edit_text); |
| + mAlertDialog = new AlertDialog.Builder(mContext) |
| + .setTitle("Enter Text") |
|
shaktisahu
2016/08/19 22:36:55
This will be changed to reflect the actual field n
David Trainor- moved to gerrit
2016/08/22 21:25:25
Put the string inside the proper grd file.
shaktisahu
2016/08/23 17:35:14
Done.
|
| + .setPositiveButton(R.string.ok, |
| + new DialogInterface.OnClickListener() { |
| + @Override |
| + public void onClick(DialogInterface dialog, int which) { |
| + onImeTextEntered(editText.getText().toString()); |
| + dialog.dismiss(); |
| + } |
| + }) |
| + .setNegativeButton(R.string.cancel, |
| + new DialogInterface.OnClickListener() { |
| + @Override |
| + public void onClick(DialogInterface dialog, int id) { |
| + dialog.dismiss(); |
| + } |
| + }) |
| + .create(); |
| + editText.initialize(mAlertDialog); |
| + mAlertDialog.setView(viewPopup); |
| + mAlertDialog.getWindow().setSoftInputMode( |
| + WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); |
| + mAlertDialog.setCanceledOnTouchOutside(true); |
| + |
| + setEditorOptions(editText, inputType); |
| + editText.setText(existingText); |
| + editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { |
| + @Override |
| + public boolean onEditorAction(TextView tv, int actionId, KeyEvent event) { |
| + switch (actionId) { |
| + case EditorInfo.IME_ACTION_NEXT: |
| + case EditorInfo.IME_ACTION_DONE: |
| + case EditorInfo.IME_ACTION_SEARCH: |
| + case EditorInfo.IME_ACTION_GO: |
| + onImeTextEntered(tv.getText().toString()); |
| + mAlertDialog.dismiss(); |
| + return true; |
| + default: |
| + return false; |
| + } |
| + } |
| + }); |
| + |
| + editText.requestFocus(); |
| + |
| + mAlertDialog.show(); |
| + } |
| + |
| + /** |
| + * Set the IME options and input type for editor based on the type received from engine. |
| + * @param inputTypeEngine Input type received from engine, defined in {@link TextInputType}. |
|
David Trainor- moved to gerrit
2016/08/22 21:25:24
Add the editText param
shaktisahu
2016/08/23 17:35:14
Done.
|
| + */ |
| + private void setEditorOptions(EditText editText, int inputTypeEngine) { |
|
David Trainor- moved to gerrit
2016/08/22 21:25:24
static?
shaktisahu
2016/08/23 17:35:14
Done.
|
| + int inputType = InputType.TYPE_CLASS_TEXT; |
| + int imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN | EditorInfo.IME_FLAG_NO_EXTRACT_UI; |
| + |
| + switch (inputTypeEngine) { |
| + case TextInputType.TEXT: |
| + inputType = InputType.TYPE_CLASS_TEXT; |
| + imeOptions = EditorInfo.IME_ACTION_GO; |
| + break; |
| + case TextInputType.PASSWORD: |
| + inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD; |
| + imeOptions = EditorInfo.IME_ACTION_GO; |
| + break; |
| + case TextInputType.SEARCH: |
| + imeOptions = EditorInfo.IME_ACTION_SEARCH; |
| + break; |
| + case TextInputType.EMAIL: |
| + inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS; |
| + imeOptions = EditorInfo.IME_ACTION_GO; |
| + break; |
| + case TextInputType.NUMBER: |
| + inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL |
| + | InputType.TYPE_NUMBER_FLAG_DECIMAL; |
| + imeOptions = EditorInfo.IME_ACTION_NEXT; |
| + break; |
| + case TextInputType.TELEPHONE: |
| + inputType = InputType.TYPE_CLASS_PHONE; |
| + imeOptions = EditorInfo.IME_ACTION_NEXT; |
| + break; |
| + case TextInputType.URL: |
| + inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI; |
| + imeOptions = EditorInfo.IME_ACTION_GO; |
| + break; |
| + case TextInputType.TEXT_AREA: |
| + case TextInputType.CONTENT_EDITABLE: |
| + inputType = InputType.TYPE_TEXT_FLAG_MULTI_LINE; |
| + imeOptions = EditorInfo.IME_ACTION_NONE; |
| + break; |
| + default: |
| + inputType = InputType.TYPE_CLASS_TEXT; |
| + } |
| + |
| + editText.setInputType(inputType); |
| + editText.setImeOptions(imeOptions); |
| + } |
| + |
| + private native long nativeInit(BlimpClientSession blimpClientSession); |
| + private native void nativeDestroy(long nativeImeHelperDialog); |
| + private native void nativeOnImeTextEntered(long nativeImeHelperDialog, String text); |
| +} |