1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.am;
18 
19 import static android.provider.DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_NATIVE_BOOT;
20 
21 import android.annotation.IntDef;
22 import android.annotation.NonNull;
23 import android.app.ActivityManager;
24 import android.compat.annotation.ChangeId;
25 import android.compat.annotation.EnabledAfter;
26 import android.compat.annotation.Overridable;
27 import android.content.ContentResolver;
28 import android.database.ContentObserver;
29 import android.os.Build;
30 import android.os.Handler;
31 import android.os.HandlerExecutor;
32 import android.os.SystemProperties;
33 import android.provider.DeviceConfig;
34 import android.provider.Settings;
35 import android.util.IndentingPrintWriter;
36 import android.util.KeyValueListParser;
37 import android.util.Slog;
38 import android.util.TimeUtils;
39 
40 import dalvik.annotation.optimization.NeverCompile;
41 
42 import java.lang.annotation.Retention;
43 import java.lang.annotation.RetentionPolicy;
44 
45 /**
46  * Tunable parameters for broadcast dispatch policy
47  */
48 public class BroadcastConstants {
49     private static final String TAG = "BroadcastConstants";
50 
51     // TODO: migrate remaining constants to be loaded from DeviceConfig
52     // TODO: migrate fg/bg values into single constants instance
53 
54     // Value element names within the Settings record
55     static final String KEY_TIMEOUT = "bcast_timeout";
56     static final String KEY_SLOW_TIME = "bcast_slow_time";
57     static final String KEY_DEFERRAL = "bcast_deferral";
58     static final String KEY_DEFERRAL_DECAY_FACTOR = "bcast_deferral_decay_factor";
59     static final String KEY_DEFERRAL_FLOOR = "bcast_deferral_floor";
60     static final String KEY_ALLOW_BG_ACTIVITY_START_TIMEOUT =
61             "bcast_allow_bg_activity_start_timeout";
62 
63     // All time intervals are in milliseconds
64     private static final long DEFAULT_TIMEOUT = 10_000 * Build.HW_TIMEOUT_MULTIPLIER;
65     private static final long DEFAULT_SLOW_TIME = 5_000 * Build.HW_TIMEOUT_MULTIPLIER;
66     private static final long DEFAULT_DEFERRAL = 5_000 * Build.HW_TIMEOUT_MULTIPLIER;
67     private static final float DEFAULT_DEFERRAL_DECAY_FACTOR = 0.75f;
68     private static final long DEFAULT_DEFERRAL_FLOOR = 0;
69     private static final long DEFAULT_ALLOW_BG_ACTIVITY_START_TIMEOUT =
70             10_000 * Build.HW_TIMEOUT_MULTIPLIER;
71 
72     /**
73      * Defer LOCKED_BOOT_COMPLETED and BOOT_COMPLETED broadcasts until the first time any process in
74      * the UID is started.
75      */
76     @ChangeId
77     @EnabledAfter(targetSdkVersion = android.os.Build.VERSION_CODES.S_V2)
78     @Overridable
79     static final long DEFER_BOOT_COMPLETED_BROADCAST_CHANGE_ID = 203704822L;
80 
81     /**
82      * Do not defer LOCKED_BOOT_COMPLETED and BOOT_COMPLETED broadcasts.
83      */
84     public static final int DEFER_BOOT_COMPLETED_BROADCAST_NONE = 0;
85     /**
86      * Defer all LOCKED_BOOT_COMPLETED and BOOT_COMPLETED broadcasts.
87      */
88     public static final int DEFER_BOOT_COMPLETED_BROADCAST_ALL = 1 << 0;
89     /**
90      * Defer LOCKED_BOOT_COMPLETED and BOOT_COMPLETED broadcasts if app is background restricted.
91      */
92     public static final int DEFER_BOOT_COMPLETED_BROADCAST_BACKGROUND_RESTRICTED_ONLY = 1 << 1;
93     /**
94      * Defer LOCKED_BOOT_COMPLETED and BOOT_COMPLETED broadcasts if app's targetSdkVersion is T
95      * and above.
96      */
97     public static final int DEFER_BOOT_COMPLETED_BROADCAST_TARGET_T_ONLY = 1 << 2;
98 
99     /**
100      * The list of DEFER_BOOT_COMPLETED_BROADCAST types.
101      * If multiple flags are selected, all conditions must be met to defer the broadcast.
102      * @hide
103      */
104     @IntDef(flag = true, prefix = { "DEFER_BOOT_COMPLETED_BROADCAST_" }, value = {
105             DEFER_BOOT_COMPLETED_BROADCAST_NONE,
106             DEFER_BOOT_COMPLETED_BROADCAST_ALL,
107             DEFER_BOOT_COMPLETED_BROADCAST_BACKGROUND_RESTRICTED_ONLY,
108             DEFER_BOOT_COMPLETED_BROADCAST_TARGET_T_ONLY,
109     })
110     @Retention(RetentionPolicy.SOURCE)
111     public @interface DeferBootCompletedBroadcastType {}
112 
113     // All time constants are in milliseconds
114 
115     // Timeout period for this broadcast queue
116     public long TIMEOUT = DEFAULT_TIMEOUT;
117     // Handling time above which we declare that a broadcast recipient was "slow".  Any
118     // value <= zero is interpreted as disabling broadcast deferral policy entirely.
119     public long SLOW_TIME = DEFAULT_SLOW_TIME;
120     // How long to initially defer broadcasts, if an app is slow to handle one
121     public long DEFERRAL = DEFAULT_DEFERRAL;
122     // Decay factor for successive broadcasts' deferral time
123     public float DEFERRAL_DECAY_FACTOR = DEFAULT_DEFERRAL_DECAY_FACTOR;
124     // Minimum that the deferral time can decay to until the backlog fully clears
125     public long DEFERRAL_FLOOR = DEFAULT_DEFERRAL_FLOOR;
126     // For a receiver that has been allowed to start background activities, how long after it
127     // started its process can start a background activity.
128     public long ALLOW_BG_ACTIVITY_START_TIMEOUT = DEFAULT_ALLOW_BG_ACTIVITY_START_TIMEOUT;
129 
130     /**
131      * Flag indicating if we should use {@link BroadcastQueueModernImpl} instead
132      * of the default {@link BroadcastQueueImpl}.
133      */
134     public boolean MODERN_QUEUE_ENABLED = DEFAULT_MODERN_QUEUE_ENABLED;
135     private static final String KEY_MODERN_QUEUE_ENABLED = "modern_queue_enabled";
136     private static final boolean DEFAULT_MODERN_QUEUE_ENABLED = true;
137 
138     /**
139      * For {@link BroadcastQueueModernImpl}: Maximum dispatch parallelism
140      * that we'll tolerate for ordinary broadcast dispatch.
141      */
142     public int MAX_RUNNING_PROCESS_QUEUES = DEFAULT_MAX_RUNNING_PROCESS_QUEUES;
143     private static final String KEY_MAX_RUNNING_PROCESS_QUEUES = "bcast_max_running_process_queues";
144     private static final int DEFAULT_MAX_RUNNING_PROCESS_QUEUES =
145             ActivityManager.isLowRamDeviceStatic() ? 2 : 4;
146 
147     /**
148      * For {@link BroadcastQueueModernImpl}: Additional running process queue parallelism beyond
149      * {@link #MAX_RUNNING_PROCESS_QUEUES} for dispatch of "urgent" broadcasts.
150      */
151     public int EXTRA_RUNNING_URGENT_PROCESS_QUEUES = DEFAULT_EXTRA_RUNNING_URGENT_PROCESS_QUEUES;
152     private static final String KEY_EXTRA_RUNNING_URGENT_PROCESS_QUEUES =
153             "bcast_extra_running_urgent_process_queues";
154     private static final int DEFAULT_EXTRA_RUNNING_URGENT_PROCESS_QUEUES = 1;
155 
156     /**
157      * For {@link BroadcastQueueModernImpl}: Maximum number of consecutive urgent
158      * broadcast dispatches allowed before letting broadcasts in lower priority queue
159      * to be scheduled in order to avoid starvation.
160      */
161     public int MAX_CONSECUTIVE_URGENT_DISPATCHES = DEFAULT_MAX_CONSECUTIVE_URGENT_DISPATCHES;
162     private static final String KEY_MAX_CONSECUTIVE_URGENT_DISPATCHES =
163             "bcast_max_consecutive_urgent_dispatches";
164     private static final int DEFAULT_MAX_CONSECUTIVE_URGENT_DISPATCHES = 3;
165 
166     /**
167      * For {@link BroadcastQueueModernImpl}: Maximum number of consecutive normal
168      * broadcast dispatches allowed before letting broadcasts in lower priority queue
169      * to be scheduled in order to avoid starvation.
170      */
171     public int MAX_CONSECUTIVE_NORMAL_DISPATCHES = DEFAULT_MAX_CONSECUTIVE_NORMAL_DISPATCHES;
172     private static final String KEY_MAX_CONSECUTIVE_NORMAL_DISPATCHES =
173             "bcast_max_consecutive_normal_dispatches";
174     private static final int DEFAULT_MAX_CONSECUTIVE_NORMAL_DISPATCHES = 10;
175 
176     /**
177      * For {@link BroadcastQueueModernImpl}: Maximum number of active broadcasts
178      * to dispatch to a "running" process queue before we retire them back to
179      * being "runnable" to give other processes a chance to run.
180      */
181     public int MAX_RUNNING_ACTIVE_BROADCASTS = DEFAULT_MAX_RUNNING_ACTIVE_BROADCASTS;
182     private static final String KEY_MAX_RUNNING_ACTIVE_BROADCASTS =
183             "bcast_max_running_active_broadcasts";
184     private static final int DEFAULT_MAX_RUNNING_ACTIVE_BROADCASTS =
185             ActivityManager.isLowRamDeviceStatic() ? 8 : 16;
186 
187     /**
188      * For {@link BroadcastQueueModernImpl}: Maximum number of active "blocking" broadcasts
189      * to dispatch to a "running" System process queue before we retire them back to
190      * being "runnable" to give other processes a chance to run. Here "blocking" refers to
191      * whether or not we are going to block on the finishReceiver() to be called before moving
192      * to the next broadcast.
193      */
194     public int MAX_CORE_RUNNING_BLOCKING_BROADCASTS = DEFAULT_MAX_CORE_RUNNING_BLOCKING_BROADCASTS;
195     private static final String KEY_CORE_MAX_RUNNING_BLOCKING_BROADCASTS =
196             "bcast_max_core_running_blocking_broadcasts";
197     private static final int DEFAULT_MAX_CORE_RUNNING_BLOCKING_BROADCASTS =
198             ActivityManager.isLowRamDeviceStatic() ? 8 : 16;
199 
200     /**
201      * For {@link BroadcastQueueModernImpl}: Maximum number of active non-"blocking" broadcasts
202      * to dispatch to a "running" System process queue before we retire them back to
203      * being "runnable" to give other processes a chance to run. Here "blocking" refers to
204      * whether or not we are going to block on the finishReceiver() to be called before moving
205      * to the next broadcast.
206      */
207     public int MAX_CORE_RUNNING_NON_BLOCKING_BROADCASTS =
208             DEFAULT_MAX_CORE_RUNNING_NON_BLOCKING_BROADCASTS;
209     private static final String KEY_CORE_MAX_RUNNING_NON_BLOCKING_BROADCASTS =
210             "bcast_max_core_running_non_blocking_broadcasts";
211     private static final int DEFAULT_MAX_CORE_RUNNING_NON_BLOCKING_BROADCASTS =
212             ActivityManager.isLowRamDeviceStatic() ? 32 : 64;
213 
214     /**
215      * For {@link BroadcastQueueModernImpl}: Maximum number of pending
216      * broadcasts to hold for a process before we ignore any delays that policy
217      * might have applied to that process.
218      */
219     public int MAX_PENDING_BROADCASTS = DEFAULT_MAX_PENDING_BROADCASTS;
220     private static final String KEY_MAX_PENDING_BROADCASTS = "bcast_max_pending_broadcasts";
221     private static final int DEFAULT_MAX_PENDING_BROADCASTS =
222             ActivityManager.isLowRamDeviceStatic() ? 128 : 256;
223 
224     /**
225      * For {@link BroadcastQueueModernImpl}: Delay to apply to normal
226      * broadcasts, giving a chance for debouncing of rapidly changing events.
227      */
228     public long DELAY_NORMAL_MILLIS = DEFAULT_DELAY_NORMAL_MILLIS;
229     private static final String KEY_DELAY_NORMAL_MILLIS = "bcast_delay_normal_millis";
230     private static final long DEFAULT_DELAY_NORMAL_MILLIS = +500;
231 
232     /**
233      * For {@link BroadcastQueueModernImpl}: Delay to apply to broadcasts
234      * targeting cached applications.
235      */
236     public long DELAY_CACHED_MILLIS = DEFAULT_DELAY_CACHED_MILLIS;
237     private static final String KEY_DELAY_CACHED_MILLIS = "bcast_delay_cached_millis";
238     private static final long DEFAULT_DELAY_CACHED_MILLIS = +120_000;
239 
240     /**
241      * For {@link BroadcastQueueModernImpl}: Delay to apply to urgent
242      * broadcasts, typically a negative value to indicate they should be
243      * executed before most other pending broadcasts.
244      */
245     public long DELAY_URGENT_MILLIS = DEFAULT_DELAY_URGENT_MILLIS;
246     private static final String KEY_DELAY_URGENT_MILLIS = "bcast_delay_urgent_millis";
247     private static final long DEFAULT_DELAY_URGENT_MILLIS = -120_000;
248 
249     /**
250      * For {@link BroadcastQueueModernImpl}: Delay to apply to broadcasts to
251      * foreground processes, typically a negative value to indicate they should be
252      * executed before most other pending broadcasts.
253      */
254     public long DELAY_FOREGROUND_PROC_MILLIS = DEFAULT_DELAY_FOREGROUND_PROC_MILLIS;
255     private static final String KEY_DELAY_FOREGROUND_PROC_MILLIS =
256             "bcast_delay_foreground_proc_millis";
257     private static final long DEFAULT_DELAY_FOREGROUND_PROC_MILLIS = -120_000;
258 
259     /**
260      * For {@link BroadcastQueueModernImpl}: Delay to apply to broadcasts to
261      * persistent processes, typically a negative value to indicate they should be
262      * executed before most other pending broadcasts.
263      */
264     public long DELAY_PERSISTENT_PROC_MILLIS = DEFAULT_DELAY_FOREGROUND_PROC_MILLIS;
265     private static final String KEY_DELAY_PERSISTENT_PROC_MILLIS =
266             "bcast_delay_persistent_proc_millis";
267     private static final long DEFAULT_DELAY_PERSISTENT_PROC_MILLIS = -120_000;
268 
269     /**
270      * For {@link BroadcastQueueModernImpl}: Maximum number of complete
271      * historical broadcasts to retain for debugging purposes.
272      */
273     public int MAX_HISTORY_COMPLETE_SIZE = DEFAULT_MAX_HISTORY_COMPLETE_SIZE;
274     private static final String KEY_MAX_HISTORY_COMPLETE_SIZE = "bcast_max_history_complete_size";
275     private static final int DEFAULT_MAX_HISTORY_COMPLETE_SIZE =
276             ActivityManager.isLowRamDeviceStatic() ? 64 : 256;
277 
278     /**
279      * For {@link BroadcastQueueModernImpl}: Maximum number of summarized
280      * historical broadcasts to retain for debugging purposes.
281      */
282     public int MAX_HISTORY_SUMMARY_SIZE = DEFAULT_MAX_HISTORY_SUMMARY_SIZE;
283     private static final String KEY_MAX_HISTORY_SUMMARY_SIZE = "bcast_max_history_summary_size";
284     private static final int DEFAULT_MAX_HISTORY_SUMMARY_SIZE =
285             ActivityManager.isLowRamDeviceStatic() ? 256 : 1024;
286 
287     /**
288      * For {@link BroadcastRecord}: Default to treating all broadcasts sent by
289      * the system as be {@link BroadcastOptions#DEFERRAL_POLICY_UNTIL_ACTIVE}.
290      */
291     public boolean CORE_DEFER_UNTIL_ACTIVE = DEFAULT_CORE_DEFER_UNTIL_ACTIVE;
292     private static final String KEY_CORE_DEFER_UNTIL_ACTIVE = "bcast_core_defer_until_active";
293     private static final boolean DEFAULT_CORE_DEFER_UNTIL_ACTIVE = true;
294 
295     /**
296      * For {@link BroadcastQueueModernImpl}: How frequently we should check for the pending
297      * cold start validity.
298      */
299     public long PENDING_COLD_START_CHECK_INTERVAL_MILLIS = 30 * 1000;
300     private static final String KEY_PENDING_COLD_START_CHECK_INTERVAL_MILLIS =
301             "pending_cold_start_check_interval_millis";
302     private static final long DEFAULT_PENDING_COLD_START_CHECK_INTERVAL_MILLIS = 30_000;
303 
304     // Settings override tracking for this instance
305     private String mSettingsKey;
306     private SettingsObserver mSettingsObserver;
307     private ContentResolver mResolver;
308     private final KeyValueListParser mParser = new KeyValueListParser(',');
309 
310     class SettingsObserver extends ContentObserver {
SettingsObserver(Handler handler)311         SettingsObserver(Handler handler) {
312             super(handler);
313         }
314 
315         @Override
onChange(boolean selfChange)316         public void onChange(boolean selfChange) {
317             updateSettingsConstants();
318         }
319     }
320 
321     // A given constants instance is configured to observe specific keys from which
322     // that instance's values are drawn.
BroadcastConstants(String settingsKey)323     public BroadcastConstants(String settingsKey) {
324         mSettingsKey = settingsKey;
325 
326         // Load initial values at least once before we start observing below
327         updateDeviceConfigConstants();
328     }
329 
330     /**
331      * Spin up the observer lazily, since it can only happen once the settings provider
332      * has been brought into service
333      */
startObserving(Handler handler, ContentResolver resolver)334     public void startObserving(Handler handler, ContentResolver resolver) {
335         mResolver = resolver;
336 
337         mSettingsObserver = new SettingsObserver(handler);
338         mResolver.registerContentObserver(Settings.Global.getUriFor(mSettingsKey),
339                 false, mSettingsObserver);
340         updateSettingsConstants();
341 
342         DeviceConfig.addOnPropertiesChangedListener(NAMESPACE_ACTIVITY_MANAGER_NATIVE_BOOT,
343                 new HandlerExecutor(handler), this::updateDeviceConfigConstants);
344         updateDeviceConfigConstants();
345     }
346 
getMaxRunningQueues()347     public int getMaxRunningQueues() {
348         return MAX_RUNNING_PROCESS_QUEUES + EXTRA_RUNNING_URGENT_PROCESS_QUEUES;
349     }
350 
updateSettingsConstants()351     private void updateSettingsConstants() {
352         synchronized (this) {
353             try {
354                 mParser.setString(Settings.Global.getString(mResolver, mSettingsKey));
355             } catch (IllegalArgumentException e) {
356                 Slog.e(TAG, "Bad broadcast settings in key '" + mSettingsKey + "'", e);
357                 return;
358             }
359 
360             // Unspecified fields retain their current value rather than revert to default
361             TIMEOUT = mParser.getLong(KEY_TIMEOUT, TIMEOUT);
362             SLOW_TIME = mParser.getLong(KEY_SLOW_TIME, SLOW_TIME);
363             DEFERRAL = mParser.getLong(KEY_DEFERRAL, DEFERRAL);
364             DEFERRAL_DECAY_FACTOR = mParser.getFloat(KEY_DEFERRAL_DECAY_FACTOR,
365                     DEFERRAL_DECAY_FACTOR);
366             DEFERRAL_FLOOR = mParser.getLong(KEY_DEFERRAL_FLOOR, DEFERRAL_FLOOR);
367             ALLOW_BG_ACTIVITY_START_TIMEOUT = mParser.getLong(KEY_ALLOW_BG_ACTIVITY_START_TIMEOUT,
368                     ALLOW_BG_ACTIVITY_START_TIMEOUT);
369         }
370     }
371 
372     /**
373      * Return the {@link SystemProperty} name for the given key in our
374      * {@link DeviceConfig} namespace.
375      */
propertyFor(@onNull String key)376     private @NonNull String propertyFor(@NonNull String key) {
377         return "persist.device_config." + NAMESPACE_ACTIVITY_MANAGER_NATIVE_BOOT + "." + key;
378     }
379 
380     /**
381      * Return the {@link SystemProperty} name for the given key in our
382      * {@link DeviceConfig} namespace, but with a different prefix that can be
383      * used to locally override the {@link DeviceConfig} value.
384      */
propertyOverrideFor(@onNull String key)385     private @NonNull String propertyOverrideFor(@NonNull String key) {
386         return "persist.sys." + NAMESPACE_ACTIVITY_MANAGER_NATIVE_BOOT + "." + key;
387     }
388 
getDeviceConfigBoolean(@onNull String key, boolean def)389     private boolean getDeviceConfigBoolean(@NonNull String key, boolean def) {
390         return SystemProperties.getBoolean(propertyOverrideFor(key),
391                 SystemProperties.getBoolean(propertyFor(key), def));
392     }
393 
getDeviceConfigInt(@onNull String key, int def)394     private int getDeviceConfigInt(@NonNull String key, int def) {
395         return SystemProperties.getInt(propertyOverrideFor(key),
396                 SystemProperties.getInt(propertyFor(key), def));
397     }
398 
getDeviceConfigLong(@onNull String key, long def)399     private long getDeviceConfigLong(@NonNull String key, long def) {
400         return SystemProperties.getLong(propertyOverrideFor(key),
401                 SystemProperties.getLong(propertyFor(key), def));
402     }
403 
updateDeviceConfigConstants(@onNull DeviceConfig.Properties properties)404     private void updateDeviceConfigConstants(@NonNull DeviceConfig.Properties properties) {
405         updateDeviceConfigConstants();
406     }
407 
408     /**
409      * Since our values are stored in a "native boot" namespace, we load them
410      * directly from the system properties.
411      */
updateDeviceConfigConstants()412     private void updateDeviceConfigConstants() {
413         synchronized (this) {
414             MODERN_QUEUE_ENABLED = getDeviceConfigBoolean(KEY_MODERN_QUEUE_ENABLED,
415                     DEFAULT_MODERN_QUEUE_ENABLED);
416             MAX_RUNNING_PROCESS_QUEUES = getDeviceConfigInt(KEY_MAX_RUNNING_PROCESS_QUEUES,
417                     DEFAULT_MAX_RUNNING_PROCESS_QUEUES);
418             EXTRA_RUNNING_URGENT_PROCESS_QUEUES = getDeviceConfigInt(
419                     KEY_EXTRA_RUNNING_URGENT_PROCESS_QUEUES,
420                     DEFAULT_EXTRA_RUNNING_URGENT_PROCESS_QUEUES);
421             MAX_CONSECUTIVE_URGENT_DISPATCHES = getDeviceConfigInt(
422                     KEY_MAX_CONSECUTIVE_URGENT_DISPATCHES,
423                     DEFAULT_MAX_CONSECUTIVE_URGENT_DISPATCHES);
424             MAX_CONSECUTIVE_NORMAL_DISPATCHES = getDeviceConfigInt(
425                     KEY_MAX_CONSECUTIVE_NORMAL_DISPATCHES,
426                     DEFAULT_MAX_CONSECUTIVE_NORMAL_DISPATCHES);
427             MAX_RUNNING_ACTIVE_BROADCASTS = getDeviceConfigInt(KEY_MAX_RUNNING_ACTIVE_BROADCASTS,
428                     DEFAULT_MAX_RUNNING_ACTIVE_BROADCASTS);
429             MAX_CORE_RUNNING_BLOCKING_BROADCASTS = getDeviceConfigInt(
430                     KEY_CORE_MAX_RUNNING_BLOCKING_BROADCASTS,
431                     DEFAULT_MAX_CORE_RUNNING_BLOCKING_BROADCASTS);
432             MAX_CORE_RUNNING_NON_BLOCKING_BROADCASTS = getDeviceConfigInt(
433                     KEY_CORE_MAX_RUNNING_NON_BLOCKING_BROADCASTS,
434                     DEFAULT_MAX_CORE_RUNNING_NON_BLOCKING_BROADCASTS);
435             MAX_PENDING_BROADCASTS = getDeviceConfigInt(KEY_MAX_PENDING_BROADCASTS,
436                     DEFAULT_MAX_PENDING_BROADCASTS);
437             DELAY_NORMAL_MILLIS = getDeviceConfigLong(KEY_DELAY_NORMAL_MILLIS,
438                     DEFAULT_DELAY_NORMAL_MILLIS);
439             DELAY_CACHED_MILLIS = getDeviceConfigLong(KEY_DELAY_CACHED_MILLIS,
440                     DEFAULT_DELAY_CACHED_MILLIS);
441             DELAY_URGENT_MILLIS = getDeviceConfigLong(KEY_DELAY_URGENT_MILLIS,
442                     DEFAULT_DELAY_URGENT_MILLIS);
443             DELAY_FOREGROUND_PROC_MILLIS = getDeviceConfigLong(KEY_DELAY_FOREGROUND_PROC_MILLIS,
444                     DEFAULT_DELAY_FOREGROUND_PROC_MILLIS);
445             DELAY_PERSISTENT_PROC_MILLIS = getDeviceConfigLong(KEY_DELAY_PERSISTENT_PROC_MILLIS,
446                     DEFAULT_DELAY_PERSISTENT_PROC_MILLIS);
447             MAX_HISTORY_COMPLETE_SIZE = getDeviceConfigInt(KEY_MAX_HISTORY_COMPLETE_SIZE,
448                     DEFAULT_MAX_HISTORY_COMPLETE_SIZE);
449             MAX_HISTORY_SUMMARY_SIZE = getDeviceConfigInt(KEY_MAX_HISTORY_SUMMARY_SIZE,
450                     DEFAULT_MAX_HISTORY_SUMMARY_SIZE);
451             CORE_DEFER_UNTIL_ACTIVE = getDeviceConfigBoolean(KEY_CORE_DEFER_UNTIL_ACTIVE,
452                     DEFAULT_CORE_DEFER_UNTIL_ACTIVE);
453             PENDING_COLD_START_CHECK_INTERVAL_MILLIS = getDeviceConfigLong(
454                     KEY_PENDING_COLD_START_CHECK_INTERVAL_MILLIS,
455                     DEFAULT_PENDING_COLD_START_CHECK_INTERVAL_MILLIS);
456         }
457 
458         // TODO: migrate BroadcastRecord to accept a BroadcastConstants
459         BroadcastRecord.CORE_DEFER_UNTIL_ACTIVE = CORE_DEFER_UNTIL_ACTIVE;
460     }
461 
462     /**
463      * Standard dumpsys support; invoked from BroadcastQueue dump
464      */
465     @NeverCompile
dump(@onNull IndentingPrintWriter pw)466     public void dump(@NonNull IndentingPrintWriter pw) {
467         synchronized (this) {
468             pw.print("Broadcast parameters (key=");
469             pw.print(mSettingsKey);
470             pw.print(", observing=");
471             pw.print(mSettingsObserver != null);
472             pw.println("):");
473             pw.increaseIndent();
474             pw.print(KEY_TIMEOUT, TimeUtils.formatDuration(TIMEOUT)).println();
475             pw.print(KEY_SLOW_TIME, TimeUtils.formatDuration(SLOW_TIME)).println();
476             pw.print(KEY_DEFERRAL, TimeUtils.formatDuration(DEFERRAL)).println();
477             pw.print(KEY_DEFERRAL_DECAY_FACTOR, DEFERRAL_DECAY_FACTOR).println();
478             pw.print(KEY_DEFERRAL_FLOOR, DEFERRAL_FLOOR).println();
479             pw.print(KEY_ALLOW_BG_ACTIVITY_START_TIMEOUT,
480                     TimeUtils.formatDuration(ALLOW_BG_ACTIVITY_START_TIMEOUT)).println();
481             pw.decreaseIndent();
482             pw.println();
483 
484             pw.print("Broadcast parameters (namespace=");
485             pw.print(NAMESPACE_ACTIVITY_MANAGER_NATIVE_BOOT);
486             pw.println("):");
487             pw.increaseIndent();
488             pw.print(KEY_MODERN_QUEUE_ENABLED, MODERN_QUEUE_ENABLED).println();
489             pw.print(KEY_MAX_RUNNING_PROCESS_QUEUES, MAX_RUNNING_PROCESS_QUEUES).println();
490             pw.print(KEY_MAX_RUNNING_ACTIVE_BROADCASTS, MAX_RUNNING_ACTIVE_BROADCASTS).println();
491             pw.print(KEY_CORE_MAX_RUNNING_BLOCKING_BROADCASTS,
492                     MAX_CORE_RUNNING_BLOCKING_BROADCASTS).println();
493             pw.print(KEY_CORE_MAX_RUNNING_NON_BLOCKING_BROADCASTS,
494                     MAX_CORE_RUNNING_NON_BLOCKING_BROADCASTS).println();
495             pw.print(KEY_MAX_PENDING_BROADCASTS, MAX_PENDING_BROADCASTS).println();
496             pw.print(KEY_DELAY_NORMAL_MILLIS,
497                     TimeUtils.formatDuration(DELAY_NORMAL_MILLIS)).println();
498             pw.print(KEY_DELAY_CACHED_MILLIS,
499                     TimeUtils.formatDuration(DELAY_CACHED_MILLIS)).println();
500             pw.print(KEY_DELAY_URGENT_MILLIS,
501                     TimeUtils.formatDuration(DELAY_URGENT_MILLIS)).println();
502             pw.print(KEY_DELAY_FOREGROUND_PROC_MILLIS,
503                     TimeUtils.formatDuration(DELAY_FOREGROUND_PROC_MILLIS)).println();
504             pw.print(KEY_DELAY_PERSISTENT_PROC_MILLIS,
505                     TimeUtils.formatDuration(DELAY_PERSISTENT_PROC_MILLIS)).println();
506             pw.print(KEY_MAX_HISTORY_COMPLETE_SIZE, MAX_HISTORY_COMPLETE_SIZE).println();
507             pw.print(KEY_MAX_HISTORY_SUMMARY_SIZE, MAX_HISTORY_SUMMARY_SIZE).println();
508             pw.print(KEY_MAX_CONSECUTIVE_URGENT_DISPATCHES,
509                     MAX_CONSECUTIVE_URGENT_DISPATCHES).println();
510             pw.print(KEY_MAX_CONSECUTIVE_NORMAL_DISPATCHES,
511                     MAX_CONSECUTIVE_NORMAL_DISPATCHES).println();
512             pw.print(KEY_CORE_DEFER_UNTIL_ACTIVE,
513                     CORE_DEFER_UNTIL_ACTIVE).println();
514             pw.print(KEY_PENDING_COLD_START_CHECK_INTERVAL_MILLIS,
515                     PENDING_COLD_START_CHECK_INTERVAL_MILLIS).println();
516             pw.decreaseIndent();
517             pw.println();
518         }
519     }
520 }
521