summaryrefslogtreecommitdiff
path: root/core/java/android/inputmethodservice/ImsConfigurationTracker.java
diff options
context:
space:
mode:
authorTaran Singh <tarandeep@google.com>2021-03-19 15:34:02 +0000
committerTaran Singh <tarandeep@google.com>2021-03-30 20:03:55 +0000
commit71ab752fc59e3db45e12d6ed9c922eb1ffca84a7 (patch)
treeee6a9b0704e6483fb6f97c2cf7b26189475f14fd /core/java/android/inputmethodservice/ImsConfigurationTracker.java
parent3bff4d5edc64f96ce73bfde1a4359381ab8de7ec (diff)
Avoid IME restart for configChanges
Handle onConfigurationChanged() in order to prevent restarting InputMethodService everytime. We introduce a new API attribute "configChanges" in InputMethod(attrs.xml) which when declared by IME, will be responsible for handling mentioned configuration changes. This CL re-introduces [1] with fix: Use new Configuration instance for IMS#mLastKnownConfig and also handle followup comments. [1] Ib94fddadb0dae648cf73a4c1642e51edebd19f50 Note: this change has no impact for devices not using DisplayAreas. Bug: 167948419 Test: atest InputMethodServiceTest Manually: 1. Patch Ie91e7a8e06b80864ef9409031e8543858552d70d to use dual display area. 2. Open applications with editors on both display areas. 3. Attach a debug point for IMS#onConfigurationChanged(). 4. Make sure IMS#resetStateForNewConfiguration() is not called when IME moves between these two identical DisplayAreas Also verify that bug 182604598 don't happen. Change-Id: I43b6b80cdb35410554412ee1d3b0917ee3198272
Diffstat (limited to 'core/java/android/inputmethodservice/ImsConfigurationTracker.java')
-rw-r--r--core/java/android/inputmethodservice/ImsConfigurationTracker.java102
1 files changed, 102 insertions, 0 deletions
diff --git a/core/java/android/inputmethodservice/ImsConfigurationTracker.java b/core/java/android/inputmethodservice/ImsConfigurationTracker.java
new file mode 100644
index 000000000000..3c788884371b
--- /dev/null
+++ b/core/java/android/inputmethodservice/ImsConfigurationTracker.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * 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 android.inputmethodservice;
+
+import android.annotation.MainThread;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.res.Configuration;
+import android.content.res.Resources;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.Preconditions;
+
+/**
+ * Helper class that takes care of Configuration change behavior of {@link InputMethodService}.
+ * Note: this class is public for testing only. Never call any of it's methods for development
+ * of IMEs.
+ * @hide
+ */
+@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
+public final class ImsConfigurationTracker {
+
+ /**
+ * A constant value that represents {@link Configuration} has changed from the last time
+ * {@link InputMethodService#onConfigurationChanged(Configuration)} was called.
+ */
+ private static final int CONFIG_CHANGED = -1;
+
+ @Nullable
+ private Configuration mLastKnownConfig = null;
+ private int mHandledConfigChanges = 0;
+ private boolean mInitialized = false;
+
+ /**
+ * Called from {@link InputMethodService.InputMethodImpl
+ * #initializeInternal(IBinder, int, IInputMethodPrivilegedOperations, int)} ()}
+ * @param handledConfigChanges Configuration changes declared handled by IME
+ * {@link android.R.styleable#InputMethod_configChanges}.
+ */
+ @MainThread
+ public void onInitialize(int handledConfigChanges) {
+ Preconditions.checkState(!mInitialized, "onInitialize can be called only once.");
+ mInitialized = true;
+ mHandledConfigChanges = handledConfigChanges;
+ }
+
+ /**
+ * Called from {@link InputMethodService.InputMethodImpl#onBindInput()}
+ */
+ @MainThread
+ public void onBindInput(@Nullable Resources resources) {
+ Preconditions.checkState(mInitialized,
+ "onBindInput can be called only after onInitialize().");
+ if (mLastKnownConfig == null && resources != null) {
+ mLastKnownConfig = new Configuration(resources.getConfiguration());
+ }
+ }
+
+ /**
+ * Dynamically set handled configChanges.
+ * Note: this method is public for testing only.
+ */
+ public void setHandledConfigChanges(int configChanges) {
+ mHandledConfigChanges = configChanges;
+ }
+
+ /**
+ * Called from {@link InputMethodService.InputMethodImpl#onConfigurationChanged(Configuration)}}
+ */
+ @MainThread
+ public void onConfigurationChanged(@NonNull Configuration newConfig,
+ @NonNull Runnable resetStateForNewConfigurationRunner) {
+ if (!mInitialized) {
+ return;
+ }
+ final int diff = mLastKnownConfig != null
+ ? mLastKnownConfig.diffPublicOnly(newConfig) : CONFIG_CHANGED;
+ // If the new config is the same as the config this Service is already running with,
+ // then don't bother calling resetStateForNewConfiguration.
+ final int unhandledDiff = (diff & ~mHandledConfigChanges);
+ if (unhandledDiff != 0) {
+ resetStateForNewConfigurationRunner.run();
+ }
+ if (diff != 0) {
+ mLastKnownConfig = new Configuration(newConfig);
+ }
+ }
+}