summaryrefslogtreecommitdiff
path: root/core/java/android/inputmethodservice/ImsConfigurationTracker.java
blob: 30ef0a2a6f7f1da453e531ccf340724ca82efd7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * 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) {
        if (!mInitialized) {
            return;
        }
        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);
        }
    }
}