Merge "Create display context when switching IME to new display"
am: 849ba2a546
Change-Id: I5e9d6fd9fd877f51fea52ab759729278398caf54
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
android_app {
|
android_app {
|
||||||
name: "SoftKeyboard",
|
name: "SoftKeyboard",
|
||||||
srcs: ["**/*.java"],
|
srcs: ["**/*.java"],
|
||||||
|
min_sdk_version: "19",
|
||||||
|
target_sdk_version: "29",
|
||||||
sdk_version: "current",
|
sdk_version: "current",
|
||||||
dex_preopt: {
|
static_libs: [
|
||||||
enabled: false,
|
"androidx.annotation_annotation",
|
||||||
},
|
],
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,22 +17,28 @@
|
|||||||
package com.example.android.softkeyboard;
|
package com.example.android.softkeyboard;
|
||||||
|
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
|
import android.content.Context;
|
||||||
import android.inputmethodservice.InputMethodService;
|
import android.inputmethodservice.InputMethodService;
|
||||||
import android.inputmethodservice.Keyboard;
|
import android.inputmethodservice.Keyboard;
|
||||||
import android.inputmethodservice.KeyboardView;
|
import android.inputmethodservice.KeyboardView;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.text.InputType;
|
import android.text.InputType;
|
||||||
import android.text.method.MetaKeyKeyListener;
|
import android.text.method.MetaKeyKeyListener;
|
||||||
|
import android.view.Display;
|
||||||
import android.view.KeyCharacterMap;
|
import android.view.KeyCharacterMap;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
|
import android.view.WindowManager;
|
||||||
import android.view.inputmethod.CompletionInfo;
|
import android.view.inputmethod.CompletionInfo;
|
||||||
import android.view.inputmethod.EditorInfo;
|
import android.view.inputmethod.EditorInfo;
|
||||||
import android.view.inputmethod.InputConnection;
|
import android.view.inputmethod.InputConnection;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
import android.view.inputmethod.InputMethodManager;
|
||||||
import android.view.inputmethod.InputMethodSubtype;
|
import android.view.inputmethod.InputMethodSubtype;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -43,7 +49,7 @@ import java.util.List;
|
|||||||
* a basic example for how you would get started writing an input method, to
|
* a basic example for how you would get started writing an input method, to
|
||||||
* be fleshed out as appropriate.
|
* be fleshed out as appropriate.
|
||||||
*/
|
*/
|
||||||
public class SoftKeyboard extends InputMethodService
|
public class SoftKeyboard extends InputMethodService
|
||||||
implements KeyboardView.OnKeyboardActionListener {
|
implements KeyboardView.OnKeyboardActionListener {
|
||||||
static final boolean DEBUG = false;
|
static final boolean DEBUG = false;
|
||||||
|
|
||||||
@@ -88,12 +94,35 @@ public class SoftKeyboard extends InputMethodService
|
|||||||
mInputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
|
mInputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
|
||||||
mWordSeparators = getResources().getString(R.string.word_separators);
|
mWordSeparators = getResources().getString(R.string.word_separators);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create new context object whose resources are adjusted to match the metrics of the display
|
||||||
|
* which is managed by WindowManager.
|
||||||
|
*
|
||||||
|
* @see {@link Context#createDisplayContext(Display)}
|
||||||
|
*/
|
||||||
|
@NonNull Context getDisplayContext() {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
|
||||||
|
// createDisplayContext is not available.
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
// TODO (b/133825283): Non-activity components Resources / DisplayMetrics update when
|
||||||
|
// moving to external display.
|
||||||
|
// An issue in Q that non-activity components Resources / DisplayMetrics in
|
||||||
|
// Context doesn't well updated when the IME window moving to external display.
|
||||||
|
// Currently we do a workaround is to create new display context directly and re-init
|
||||||
|
// keyboard layout with this context.
|
||||||
|
final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
|
||||||
|
return createDisplayContext(wm.getDefaultDisplay());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the point where you can do all of your UI initialization. It
|
* This is the point where you can do all of your UI initialization. It
|
||||||
* is called after creation and any configuration change.
|
* is called after creation and any configuration change.
|
||||||
*/
|
*/
|
||||||
@Override public void onInitializeInterface() {
|
@Override public void onInitializeInterface() {
|
||||||
|
final Context displayContext = getDisplayContext();
|
||||||
|
|
||||||
if (mQwertyKeyboard != null) {
|
if (mQwertyKeyboard != null) {
|
||||||
// Configuration changes can happen after the keyboard gets recreated,
|
// Configuration changes can happen after the keyboard gets recreated,
|
||||||
// so we need to be able to re-build the keyboards if the available
|
// so we need to be able to re-build the keyboards if the available
|
||||||
@@ -102,9 +131,9 @@ public class SoftKeyboard extends InputMethodService
|
|||||||
if (displayWidth == mLastDisplayWidth) return;
|
if (displayWidth == mLastDisplayWidth) return;
|
||||||
mLastDisplayWidth = displayWidth;
|
mLastDisplayWidth = displayWidth;
|
||||||
}
|
}
|
||||||
mQwertyKeyboard = new LatinKeyboard(this, R.xml.qwerty);
|
mQwertyKeyboard = new LatinKeyboard(displayContext, R.xml.qwerty);
|
||||||
mSymbolsKeyboard = new LatinKeyboard(this, R.xml.symbols);
|
mSymbolsKeyboard = new LatinKeyboard(displayContext, R.xml.symbols);
|
||||||
mSymbolsShiftedKeyboard = new LatinKeyboard(this, R.xml.symbols_shift);
|
mSymbolsShiftedKeyboard = new LatinKeyboard(displayContext, R.xml.symbols_shift);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -133,7 +162,7 @@ public class SoftKeyboard extends InputMethodService
|
|||||||
* be generated, like {@link #onCreateInputView}.
|
* be generated, like {@link #onCreateInputView}.
|
||||||
*/
|
*/
|
||||||
@Override public View onCreateCandidatesView() {
|
@Override public View onCreateCandidatesView() {
|
||||||
mCandidateView = new CandidateView(this);
|
mCandidateView = new CandidateView(getDisplayContext());
|
||||||
mCandidateView.setService(this);
|
mCandidateView.setService(this);
|
||||||
return mCandidateView;
|
return mCandidateView;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user