1package io.flutter.embedding.engine.systemchannels;
3import static io.flutter.Build.API_LEVELS;
5import android.annotation.SuppressLint;
7import android.util.DisplayMetrics;
8import androidx.annotation.NonNull;
9import androidx.annotation.Nullable;
10import androidx.annotation.UiThread;
11import androidx.annotation.VisibleForTesting;
13import io.flutter.embedding.engine.dart.DartExecutor;
14import io.flutter.plugin.common.BasicMessageChannel;
15import io.flutter.plugin.common.JSONMessageCodec;
16import java.util.HashMap;
18import java.util.concurrent.ConcurrentLinkedQueue;
21 private static final String TAG =
"SettingsChannel";
24 private static final String TEXT_SCALE_FACTOR =
"textScaleFactor";
25 private static final String NATIVE_SPELL_CHECK_SERVICE_DEFINED =
"nativeSpellCheckServiceDefined";
26 private static final String BRIEFLY_SHOW_PASSWORD =
"brieflyShowPassword";
27 private static final String ALWAYS_USE_24_HOUR_FORMAT =
"alwaysUse24HourFormat";
28 private static final String PLATFORM_BRIGHTNESS =
"platformBrightness";
29 private static final String CONFIGURATION_ID =
"configurationId";
40 @SuppressLint(
"AnnotateVersionCheck")
48 final ConfigurationQueue.SentConfiguration configuration =
50 return configuration ==
null ? null : configuration.displayMetrics;
60 @NonNull
private Map<String, Object> message =
new HashMap<>();
61 @Nullable
private DisplayMetrics displayMetrics;
64 this.channel = channel;
69 this.displayMetrics = displayMetrics;
75 message.put(TEXT_SCALE_FACTOR, textScaleFactor);
81 boolean nativeSpellCheckServiceDefined) {
82 message.put(NATIVE_SPELL_CHECK_SERVICE_DEFINED, nativeSpellCheckServiceDefined);
88 message.put(BRIEFLY_SHOW_PASSWORD, brieflyShowPassword);
94 message.put(ALWAYS_USE_24_HOUR_FORMAT, use24HourFormat);
100 message.put(PLATFORM_BRIGHTNESS, brightness.name);
107 "Sending message: \n"
108 +
"textScaleFactor: "
109 +
message.get(TEXT_SCALE_FACTOR)
111 +
"alwaysUse24HourFormat: "
112 +
message.get(ALWAYS_USE_24_HOUR_FORMAT)
114 +
"platformBrightness: "
115 +
message.get(PLATFORM_BRIGHTNESS));
116 final DisplayMetrics metrics = this.displayMetrics;
121 final ConfigurationQueue.SentConfiguration sentConfiguration =
123 final BasicMessageChannel.Reply deleteCallback =
125 message.put(CONFIGURATION_ID, sentConfiguration.generationNumber);
126 channel.send(
message, deleteCallback);
176 private final ConcurrentLinkedQueue<SentConfiguration> sentQueue =
177 new ConcurrentLinkedQueue<>();
192 if (currentConfiguration ==
null) {
193 currentConfiguration = sentQueue.poll();
198 while (currentConfiguration !=
null
200 currentConfiguration = sentQueue.poll();
203 if (currentConfiguration ==
null) {
206 "Cannot find config with generation: "
207 + String.valueOf(configGeneration)
208 +
", after exhausting the queue.");
213 "Cannot find config with generation: "
214 + String.valueOf(configGeneration)
215 +
", the oldest config is now: "
219 return currentConfiguration;
222 private SentConfiguration previousEnqueuedConfiguration;
234 sentQueue.add(config);
236 previousEnqueuedConfiguration = config;
237 return configurationToRemove ==
null
242 public void reply(Object reply) {
246 sentQueue.remove(configurationToRemove);
247 if (!sentQueue.isEmpty()) {
250 "The queue becomes empty after removing config generation "
258 private static int nextConfigGeneration = Integer.MIN_VALUE;
261 @NonNull
private final DisplayMetrics displayMetrics;
264 this.generationNumber = nextConfigGeneration++;
265 this.displayMetrics = displayMetrics;
static void v(@NonNull String tag, @NonNull String message)
static void e(@NonNull String tag, @NonNull String message)
SentConfiguration(@NonNull DisplayMetrics displayMetrics)
final int generationNumber
BasicMessageChannel.Reply enqueueConfiguration(SentConfiguration config)
SentConfiguration getConfiguration(int configGeneration)
MessageBuilder setBrieflyShowPassword(@NonNull boolean brieflyShowPassword)
MessageBuilder setUse24HourFormat(boolean use24HourFormat)
MessageBuilder setDisplayMetrics(@NonNull DisplayMetrics displayMetrics)
MessageBuilder setNativeSpellCheckServiceDefined(boolean nativeSpellCheckServiceDefined)
MessageBuilder setPlatformBrightness(@NonNull PlatformBrightness brightness)
MessageBuilder setTextScaleFactor(float textScaleFactor)
MessageBuilder(@NonNull BasicMessageChannel< Object > channel)
static final String CHANNEL_NAME
static DisplayMetrics getPastDisplayMetrics(int configId)
SettingsChannel(@NonNull DartExecutor dartExecutor)
MessageBuilder startMessage()
static boolean hasNonlinearTextScalingSupport()
final BasicMessageChannel< Object > channel
static final JSONMessageCodec INSTANCE
DEF_SWITCHES_START aot vmservice shared library name