2016-10-19 12:23:52 -07:00
|
|
|
// 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 io.flutter.plugin.editing;
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.text.InputType;
|
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
|
import android.view.inputmethod.InputConnection;
|
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2017-03-17 09:04:59 +01:00
|
|
|
|
2017-04-18 14:30:31 +02:00
|
|
|
import io.flutter.plugin.common.MethodChannel;
|
|
|
|
|
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
|
|
|
|
import io.flutter.plugin.common.MethodChannel.Result;
|
2017-03-17 09:04:59 +01:00
|
|
|
import io.flutter.plugin.common.JSONMethodCodec;
|
2017-04-06 10:15:06 -07:00
|
|
|
import io.flutter.plugin.common.JSONUtil;
|
2017-03-17 09:04:59 +01:00
|
|
|
import io.flutter.plugin.common.MethodCall;
|
2016-10-19 12:23:52 -07:00
|
|
|
import io.flutter.view.FlutterView;
|
2017-03-17 09:04:59 +01:00
|
|
|
|
|
|
|
|
import java.util.Map;
|
2016-10-19 12:23:52 -07:00
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Android implementation of the text input plugin.
|
|
|
|
|
*/
|
2017-03-17 09:04:59 +01:00
|
|
|
public class TextInputPlugin implements MethodCallHandler {
|
2016-10-19 12:23:52 -07:00
|
|
|
|
|
|
|
|
private final Activity mActivity;
|
2017-03-17 09:04:59 +01:00
|
|
|
private final FlutterView mView;
|
2017-04-18 14:30:31 +02:00
|
|
|
private final MethodChannel mFlutterChannel;
|
2016-10-19 12:23:52 -07:00
|
|
|
private int mClient = 0;
|
|
|
|
|
private JSONObject mConfiguration;
|
2016-11-30 15:07:37 -08:00
|
|
|
private JSONObject mLatestState;
|
2016-10-19 12:23:52 -07:00
|
|
|
|
2017-03-17 09:04:59 +01:00
|
|
|
public TextInputPlugin(Activity activity, FlutterView view) {
|
2016-10-19 12:23:52 -07:00
|
|
|
mActivity = activity;
|
2017-03-17 09:04:59 +01:00
|
|
|
mView = view;
|
2017-04-18 14:30:31 +02:00
|
|
|
mFlutterChannel = new MethodChannel(view, "flutter/textinput",
|
2017-03-17 09:04:59 +01:00
|
|
|
JSONMethodCodec.INSTANCE);
|
|
|
|
|
mFlutterChannel.setMethodCallHandler(this);
|
2016-10-19 12:23:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-04-18 14:30:31 +02:00
|
|
|
public void onMethodCall(MethodCall call, Result result) {
|
2017-03-17 09:04:59 +01:00
|
|
|
String method = call.method;
|
|
|
|
|
Object args = call.arguments;
|
|
|
|
|
try {
|
|
|
|
|
if (method.equals("TextInput.show")) {
|
|
|
|
|
showTextInput(mView);
|
2017-04-18 14:30:31 +02:00
|
|
|
result.success(null);
|
2017-03-17 09:04:59 +01:00
|
|
|
} else if (method.equals("TextInput.hide")) {
|
|
|
|
|
hideTextInput(mView);
|
2017-04-18 14:30:31 +02:00
|
|
|
result.success(null);
|
2017-03-17 09:04:59 +01:00
|
|
|
} else if (method.equals("TextInput.setClient")) {
|
|
|
|
|
final JSONArray argumentList = (JSONArray) args;
|
|
|
|
|
setTextInputClient(mView, argumentList.getInt(0), argumentList.getJSONObject(1));
|
2017-04-18 14:30:31 +02:00
|
|
|
result.success(null);
|
2017-03-17 09:04:59 +01:00
|
|
|
} else if (method.equals("TextInput.setEditingState")) {
|
|
|
|
|
setTextInputEditingState(mView, (JSONObject) args);
|
2017-04-18 14:30:31 +02:00
|
|
|
result.success(null);
|
2017-03-17 09:04:59 +01:00
|
|
|
} else if (method.equals("TextInput.clearClient")) {
|
|
|
|
|
clearTextInputClient();
|
2017-04-18 14:30:31 +02:00
|
|
|
result.success(null);
|
2017-03-17 09:04:59 +01:00
|
|
|
} else {
|
2017-04-18 14:30:31 +02:00
|
|
|
result.notImplemented();
|
2017-03-17 09:04:59 +01:00
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
2017-04-18 14:30:31 +02:00
|
|
|
result.error("error", "JSON error: " + e.getMessage(), null);
|
2016-10-19 12:23:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int inputTypeFromTextInputType(String inputType) {
|
|
|
|
|
if (inputType.equals("TextInputType.datetime"))
|
|
|
|
|
return InputType.TYPE_CLASS_DATETIME;
|
2016-10-28 12:26:56 -07:00
|
|
|
if (inputType.equals("TextInputType.number"))
|
2016-10-19 12:23:52 -07:00
|
|
|
return InputType.TYPE_CLASS_NUMBER;
|
2016-10-28 12:26:56 -07:00
|
|
|
if (inputType.equals("TextInputType.phone"))
|
2016-10-19 12:23:52 -07:00
|
|
|
return InputType.TYPE_CLASS_PHONE;
|
|
|
|
|
return InputType.TYPE_CLASS_TEXT;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 09:04:59 +01:00
|
|
|
public InputConnection createInputConnection(FlutterView view, EditorInfo outAttrs)
|
|
|
|
|
throws JSONException {
|
2016-10-19 12:23:52 -07:00
|
|
|
if (mClient == 0)
|
|
|
|
|
return null;
|
2017-03-17 09:04:59 +01:00
|
|
|
outAttrs.inputType = inputTypeFromTextInputType(mConfiguration.getString("inputType"));
|
|
|
|
|
outAttrs.actionLabel = mConfiguration.getString("actionLabel");
|
|
|
|
|
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_FULLSCREEN;
|
|
|
|
|
InputConnectionAdaptor connection = new InputConnectionAdaptor(view, mClient, this,
|
|
|
|
|
mFlutterChannel);
|
|
|
|
|
if (mLatestState != null) {
|
|
|
|
|
int selectionBase = (Integer) mLatestState.get("selectionBase");
|
|
|
|
|
int selectionExtent = (Integer) mLatestState.get("selectionExtent");
|
|
|
|
|
outAttrs.initialSelStart = selectionBase;
|
|
|
|
|
outAttrs.initialSelEnd = selectionExtent;
|
|
|
|
|
connection.getEditable().append((String) mLatestState.get("text"));
|
|
|
|
|
connection.setSelection(Math.max(selectionBase, 0),
|
|
|
|
|
Math.max(selectionExtent, 0));
|
|
|
|
|
connection.setComposingRegion((Integer) mLatestState.get("composingBase"),
|
|
|
|
|
(Integer) mLatestState.get("composingExtent"));
|
|
|
|
|
} else {
|
|
|
|
|
outAttrs.initialSelStart = 0;
|
|
|
|
|
outAttrs.initialSelEnd = 0;
|
2016-10-19 12:23:52 -07:00
|
|
|
}
|
2017-03-17 09:04:59 +01:00
|
|
|
return connection;
|
2016-10-19 12:23:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showTextInput(FlutterView view) {
|
|
|
|
|
InputMethodManager imm =
|
2017-03-17 09:04:59 +01:00
|
|
|
(InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
|
2016-10-19 12:23:52 -07:00
|
|
|
imm.showSoftInput(view, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void hideTextInput(FlutterView view) {
|
|
|
|
|
InputMethodManager imm =
|
2017-03-17 09:04:59 +01:00
|
|
|
(InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
|
2016-10-19 12:23:52 -07:00
|
|
|
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 09:04:59 +01:00
|
|
|
private void setTextInputClient(FlutterView view, int client, JSONObject configuration) {
|
2016-11-30 15:07:37 -08:00
|
|
|
mLatestState = null;
|
2016-10-19 12:23:52 -07:00
|
|
|
mClient = client;
|
|
|
|
|
mConfiguration = configuration;
|
|
|
|
|
InputMethodManager imm =
|
2017-03-17 09:04:59 +01:00
|
|
|
(InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
|
2016-10-19 12:23:52 -07:00
|
|
|
imm.restartInput(view);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 09:04:59 +01:00
|
|
|
private void setTextInputEditingState(FlutterView view, JSONObject state) {
|
2016-11-30 15:07:37 -08:00
|
|
|
mLatestState = state;
|
2016-10-19 12:23:52 -07:00
|
|
|
InputMethodManager imm =
|
2017-03-17 09:04:59 +01:00
|
|
|
(InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
|
2016-10-19 12:23:52 -07:00
|
|
|
imm.restartInput(view);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 09:04:59 +01:00
|
|
|
void setLatestEditingState(Map<String, Object> state) {
|
2017-04-06 10:15:06 -07:00
|
|
|
mLatestState = (JSONObject) JSONUtil.wrap(state);
|
2016-11-30 15:07:37 -08:00
|
|
|
}
|
2017-03-17 09:04:59 +01:00
|
|
|
|
2016-10-19 12:23:52 -07:00
|
|
|
private void clearTextInputClient() {
|
|
|
|
|
mClient = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|