blob: be5aa0f0794837019caca9d224686ad97e54df3f [file] [log] [blame]
// Copyright 2018 The Cobalt Authors. All Rights Reserved.
//
// 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 dev.cobalt.coat;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.Selection;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
/**
* The custom editor that receives text and displays it for the on-screen keyboard. It interacts
* with the Input Method Engine (IME) by receiving commands through the InputConnection interface
* and sending commands through InputMethodManager.
*/
public class KeyboardEditor extends View {
private final Context context;
private Editable editable;
private KeyboardInputConnection inputConnection;
public KeyboardEditor(Context context) {
this(context, null);
}
public KeyboardEditor(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setFocusable(true);
}
/**
* Create a new InputConnection for the on-screen keyboard InputMethod to interact with the view.
*/
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT;
outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE;
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN;
outAttrs.imeOptions |= EditorInfo.IME_ACTION_SEARCH;
outAttrs.initialSelStart = Selection.getSelectionStart(editable);
outAttrs.initialSelEnd = Selection.getSelectionEnd(editable);
this.inputConnection = new KeyboardInputConnection(context, this, outAttrs);
return inputConnection;
}
/** Show the on-screen keyboard. */
public void showKeyboard() {
final Activity activity = (Activity) context;
final KeyboardEditor view = this;
activity.runOnUiThread(
new Runnable() {
@Override
public void run() {
view.setFocusable(true);
view.requestFocus();
InputMethodManager imm =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, 0);
}
});
}
/** Hide the on-screen keyboard. */
public void hideKeyboard() {
final Activity activity = (Activity) context;
final KeyboardEditor view = this;
activity.runOnUiThread(
new Runnable() {
@Override
public void run() {
view.setFocusable(true);
view.requestFocus();
InputMethodManager imm =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
});
}
/** Send the current state of the editable to the Input Method Manager. */
public void updateSelection(View view, int selStart, int selEnd, int compStart, int compEnd) {
InputMethodManager imm =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.updateSelection(view, selStart, selEnd, compStart, compEnd);
}
}