| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.inputmethodservice; |
| 18 |
|
| 19 |
import android.annotation.MainThread; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.content.res.Configuration; |
| 23 |
import android.content.res.Resources; |
| 24 |
|
| 25 |
import com.android.internal.annotations.VisibleForTesting; |
| 26 |
import com.android.internal.util.Preconditions; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 35 |
public final class ImsConfigurationTracker { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private static final int CONFIG_CHANGED = -1; |
| 42 |
|
| 43 |
@Nullable |
| 44 |
private Configuration mLastKnownConfig = null; |
| 45 |
private int mHandledConfigChanges = 0; |
| 46 |
private boolean mInitialized = false; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
@MainThread |
| 55 |
public void onInitialize(int handledConfigChanges) { |
| 56 |
Preconditions.checkState(!mInitialized, "onInitialize can be called only once."); |
| 57 |
mInitialized = true; |
| 58 |
mHandledConfigChanges = handledConfigChanges; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@MainThread |
| 65 |
public void onBindInput(@Nullable Resources resources) { |
| 66 |
if (!mInitialized) { |
| 67 |
return; |
| 68 |
} |
| 69 |
if (mLastKnownConfig == null && resources != null) { |
| 70 |
mLastKnownConfig = new Configuration(resources.getConfiguration()); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public void setHandledConfigChanges(int configChanges) { |
| 79 |
mHandledConfigChanges = configChanges; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@MainThread |
| 86 |
public void onConfigurationChanged(@NonNull Configuration newConfig, |
| 87 |
@NonNull Runnable resetStateForNewConfigurationRunner) { |
| 88 |
if (!mInitialized) { |
| 89 |
return; |
| 90 |
} |
| 91 |
final int diff = mLastKnownConfig != null |
| 92 |
? mLastKnownConfig.diffPublicOnly(newConfig) : CONFIG_CHANGED; |
| 93 |
|
| 94 |
|
| 95 |
final int unhandledDiff = (diff & ~mHandledConfigChanges); |
| 96 |
if (unhandledDiff != 0) { |
| 97 |
resetStateForNewConfigurationRunner.run(); |
| 98 |
} |
| 99 |
if (diff != 0) { |
| 100 |
mLastKnownConfig = new Configuration(newConfig); |
| 101 |
} |
| 102 |
} |
| 103 |
} |