This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
Context.java
8104 lines (7572 loc) · 343 KB
/
Context.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006 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.content;
import static android.content.flags.Flags.FLAG_ENABLE_BIND_PACKAGE_ISOLATED_PROCESS;
import android.annotation.AttrRes;
import android.annotation.CallbackExecutor;
import android.annotation.CheckResult;
import android.annotation.ColorInt;
import android.annotation.ColorRes;
import android.annotation.DisplayContext;
import android.annotation.DrawableRes;
import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.LongDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.PermissionMethod;
import android.annotation.PermissionName;
import android.annotation.RequiresPermission;
import android.annotation.StringDef;
import android.annotation.StringRes;
import android.annotation.StyleRes;
import android.annotation.StyleableRes;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.annotation.UiContext;
import android.annotation.UserIdInt;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.BroadcastOptions;
import android.app.GameManager;
import android.app.GrammaticalInflectionManager;
import android.app.IApplicationThread;
import android.app.IServiceConnection;
import android.app.VrManager;
import android.app.ambientcontext.AmbientContextManager;
import android.app.people.PeopleManager;
import android.app.time.TimeManager;
import android.companion.virtual.VirtualDeviceManager;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.credentials.CredentialManager;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Flags;
import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.IBinder;
import android.os.Looper;
import android.os.StatFs;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.StorageManager;
import android.provider.E2eeContactKeysManager;
import android.provider.MediaStore;
import android.telephony.TelephonyRegistryManager;
import android.util.AttributeSet;
import android.view.Display;
import android.view.DisplayAdjustments;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams.WindowType;
import android.view.autofill.AutofillManager.AutofillClient;
import android.view.contentcapture.ContentCaptureManager.ContentCaptureClient;
import android.view.textclassifier.TextClassificationManager;
import android.window.WindowContext;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.compat.IPlatformCompat;
import com.android.internal.compat.IPlatformCompatNative;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
/**
* Interface to global information about an application environment. This is
* an abstract class whose implementation is provided by
* the Android system. It
* allows access to application-specific resources and classes, as well as
* up-calls for application-level operations such as launching activities,
* broadcasting and receiving intents, etc.
*/
public abstract class Context {
/**
* After {@link Build.VERSION_CODES#TIRAMISU},
* {@link #registerComponentCallbacks(ComponentCallbacks)} will add a {@link ComponentCallbacks}
* to {@link Activity} or {@link ContextWrapper#getBaseContext()} instead of always adding to
* {@link #getApplicationContext()}.
*
* @hide
*/
@ChangeId
@EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
@VisibleForTesting
public static final long OVERRIDABLE_COMPONENT_CALLBACKS = 193247900L;
/**
* The default device ID, which is the ID of the primary (non-virtual) device.
*/
public static final int DEVICE_ID_DEFAULT = 0;
/**
* Invalid device ID.
*/
public static final int DEVICE_ID_INVALID = -1;
/** @hide */
@IntDef(flag = true, prefix = { "MODE_" }, value = {
MODE_PRIVATE,
MODE_WORLD_READABLE,
MODE_WORLD_WRITEABLE,
MODE_APPEND,
})
@Retention(RetentionPolicy.SOURCE)
public @interface FileMode {}
/** @hide */
@IntDef(flag = true, prefix = { "MODE_" }, value = {
MODE_PRIVATE,
MODE_WORLD_READABLE,
MODE_WORLD_WRITEABLE,
MODE_MULTI_PROCESS,
})
@Retention(RetentionPolicy.SOURCE)
public @interface PreferencesMode {}
/** @hide */
@IntDef(flag = true, prefix = { "MODE_" }, value = {
MODE_PRIVATE,
MODE_WORLD_READABLE,
MODE_WORLD_WRITEABLE,
MODE_ENABLE_WRITE_AHEAD_LOGGING,
MODE_NO_LOCALIZED_COLLATORS,
})
@Retention(RetentionPolicy.SOURCE)
public @interface DatabaseMode {}
/**
* File creation mode: the default mode, where the created file can only
* be accessed by the calling application (or all applications sharing the
* same user ID).
*/
public static final int MODE_PRIVATE = 0x0000;
/**
* File creation mode: allow all other applications to have read access to
* the created file.
* <p>
* Starting from {@link android.os.Build.VERSION_CODES#N}, attempting to use this
* mode throws a {@link SecurityException}.
*
* @deprecated Creating world-readable files is very dangerous, and likely
* to cause security holes in applications. It is strongly
* discouraged; instead, applications should use more formal
* mechanism for interactions such as {@link ContentProvider},
* {@link BroadcastReceiver}, and {@link android.app.Service}.
* There are no guarantees that this access mode will remain on
* a file, such as when it goes through a backup and restore.
* @see androidx.core.content.FileProvider
* @see Intent#FLAG_GRANT_WRITE_URI_PERMISSION
*/
@Deprecated
public static final int MODE_WORLD_READABLE = 0x0001;
/**
* File creation mode: allow all other applications to have write access to
* the created file.
* <p>
* Starting from {@link android.os.Build.VERSION_CODES#N}, attempting to use this
* mode will throw a {@link SecurityException}.
*
* @deprecated Creating world-writable files is very dangerous, and likely
* to cause security holes in applications. It is strongly
* discouraged; instead, applications should use more formal
* mechanism for interactions such as {@link ContentProvider},
* {@link BroadcastReceiver}, and {@link android.app.Service}.
* There are no guarantees that this access mode will remain on
* a file, such as when it goes through a backup and restore.
* @see androidx.core.content.FileProvider
* @see Intent#FLAG_GRANT_WRITE_URI_PERMISSION
*/
@Deprecated
public static final int MODE_WORLD_WRITEABLE = 0x0002;
/**
* File creation mode: for use with {@link #openFileOutput}, if the file
* already exists then write data to the end of the existing file
* instead of erasing it.
* @see #openFileOutput
*/
public static final int MODE_APPEND = 0x8000;
/**
* SharedPreference loading flag: when set, the file on disk will
* be checked for modification even if the shared preferences
* instance is already loaded in this process. This behavior is
* sometimes desired in cases where the application has multiple
* processes, all writing to the same SharedPreferences file.
* Generally there are better forms of communication between
* processes, though.
*
* <p>This was the legacy (but undocumented) behavior in and
* before Gingerbread (Android 2.3) and this flag is implied when
* targeting such releases. For applications targeting SDK
* versions <em>greater than</em> Android 2.3, this flag must be
* explicitly set if desired.
*
* @see #getSharedPreferences
*
* @deprecated MODE_MULTI_PROCESS does not work reliably in
* some versions of Android, and furthermore does not provide any
* mechanism for reconciling concurrent modifications across
* processes. Applications should not attempt to use it. Instead,
* they should use an explicit cross-process data management
* approach such as {@link android.content.ContentProvider ContentProvider}.
*/
@Deprecated
public static final int MODE_MULTI_PROCESS = 0x0004;
/**
* Database open flag: when set, the database is opened with write-ahead
* logging enabled by default.
*
* @see #openOrCreateDatabase(String, int, CursorFactory)
* @see #openOrCreateDatabase(String, int, CursorFactory, DatabaseErrorHandler)
* @see SQLiteDatabase#enableWriteAheadLogging
*/
public static final int MODE_ENABLE_WRITE_AHEAD_LOGGING = 0x0008;
/**
* Database open flag: when set, the database is opened without support for
* localized collators.
*
* @see #openOrCreateDatabase(String, int, CursorFactory)
* @see #openOrCreateDatabase(String, int, CursorFactory, DatabaseErrorHandler)
* @see SQLiteDatabase#NO_LOCALIZED_COLLATORS
*/
public static final int MODE_NO_LOCALIZED_COLLATORS = 0x0010;
/**
* Flags used for bindService(int) APIs. Note, we now have long BIND_* flags.
* @hide
*/
@IntDef(flag = true, prefix = { "BIND_" }, value = {
BIND_AUTO_CREATE,
BIND_DEBUG_UNBIND,
BIND_NOT_FOREGROUND,
BIND_ABOVE_CLIENT,
BIND_ALLOW_OOM_MANAGEMENT,
BIND_WAIVE_PRIORITY,
BIND_IMPORTANT,
BIND_ADJUST_WITH_ACTIVITY,
BIND_NOT_PERCEPTIBLE,
BIND_ALLOW_ACTIVITY_STARTS,
BIND_INCLUDE_CAPABILITIES,
BIND_SHARED_ISOLATED_PROCESS,
BIND_PACKAGE_ISOLATED_PROCESS,
BIND_EXTERNAL_SERVICE
})
@Retention(RetentionPolicy.SOURCE)
public @interface BindServiceFlagsBits {}
/**
* Long version of BIND_* flags used for bindService(BindServiceFlags) APIs.
* @hide
*/
@LongDef(flag = true, prefix = { "BIND_" }, value = {
BIND_AUTO_CREATE,
BIND_DEBUG_UNBIND,
BIND_NOT_FOREGROUND,
BIND_ABOVE_CLIENT,
BIND_ALLOW_OOM_MANAGEMENT,
BIND_WAIVE_PRIORITY,
BIND_IMPORTANT,
BIND_ADJUST_WITH_ACTIVITY,
BIND_NOT_PERCEPTIBLE,
BIND_ALLOW_ACTIVITY_STARTS,
BIND_INCLUDE_CAPABILITIES,
BIND_SHARED_ISOLATED_PROCESS,
BIND_PACKAGE_ISOLATED_PROCESS,
// Intentionally not include BIND_EXTERNAL_SERVICE, because it'd cause sign-extension.
// This would allow Android Studio to show a warning, if someone tries to use
// BIND_EXTERNAL_SERVICE BindServiceFlags.
BIND_EXTERNAL_SERVICE_LONG,
// Make sure no flag uses the sign bit (most significant bit) of the long integer,
// to avoid future confusion.
BIND_BYPASS_USER_NETWORK_RESTRICTIONS,
BIND_MATCH_QUARANTINED_COMPONENTS,
})
@Retention(RetentionPolicy.SOURCE)
public @interface BindServiceFlagsLongBits {}
/**
* Specific flags used for bindService() call, which encapsulates a 64 bits long integer.
* Call {@link BindServiceFlags#of(long)} to obtain an
* object of {@code BindServiceFlags}.
*/
public final static class BindServiceFlags {
private final long mValue;
private BindServiceFlags(@BindServiceFlagsLongBits long value) {
mValue = value;
}
/**
* @return Return flags in 64 bits long integer.
* @hide
*/
public long getValue() {
return mValue;
}
/**
* Build {@link BindServiceFlags} from BIND_* FLAGS.
*
* Note, {@link #BIND_EXTERNAL_SERVICE} is not supported in this method, because
* it has the highest integer bit set and cause wrong flags to be set. Use
* {@link #BIND_EXTERNAL_SERVICE_LONG} instead.
*/
@NonNull
public static BindServiceFlags of(@BindServiceFlagsLongBits long value) {
if ((value & Integer.toUnsignedLong(BIND_EXTERNAL_SERVICE)) != 0){
throw new IllegalArgumentException(
"BIND_EXTERNAL_SERVICE is deprecated. Use BIND_EXTERNAL_SERVICE_LONG"
+ " instead");
}
return new BindServiceFlags(value);
}
}
/**
* Flag for {@link #bindService}: automatically create the service as long
* as the binding exists. Note that while this will create the service,
* its {@link android.app.Service#onStartCommand}
* method will still only be called due to an
* explicit call to {@link #startService}. Even without that, though,
* this still provides you with access to the service object while the
* service is created.
*
* <p>Note that prior to {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH},
* not supplying this flag would also impact how important the system
* consider's the target service's process to be. When set, the only way
* for it to be raised was by binding from a service in which case it will
* only be important when that activity is in the foreground. Now to
* achieve this behavior you must explicitly supply the new flag
* {@link #BIND_ADJUST_WITH_ACTIVITY}. For compatibility, old applications
* that don't specify {@link #BIND_AUTO_CREATE} will automatically have
* the flags {@link #BIND_WAIVE_PRIORITY} and
* {@link #BIND_ADJUST_WITH_ACTIVITY} set for them in order to achieve
* the same result.
*/
public static final int BIND_AUTO_CREATE = 0x0001;
/**
* Flag for {@link #bindService}: include debugging help for mismatched
* calls to unbind. When this flag is set, the callstack of the following
* {@link #unbindService} call is retained, to be printed if a later
* incorrect unbind call is made. Note that doing this requires retaining
* information about the binding that was made for the lifetime of the app,
* resulting in a leak -- this should only be used for debugging.
*/
public static final int BIND_DEBUG_UNBIND = 0x0002;
/**
* Flag for {@link #bindService}: don't allow this binding to raise
* the target service's process to the foreground scheduling priority.
* It will still be raised to at least the same memory priority
* as the client (so that its process will not be killable in any
* situation where the client is not killable), but for CPU scheduling
* purposes it may be left in the background. This only has an impact
* in the situation where the binding client is a foreground process
* and the target service is in a background process.
*/
public static final int BIND_NOT_FOREGROUND = 0x0004;
/**
* Flag for {@link #bindService}: indicates that the client application
* binding to this service considers the service to be more important than
* the app itself. When set, the platform will try to have the out of
* memory killer kill the app before it kills the service it is bound to, though
* this is not guaranteed to be the case.
*/
public static final int BIND_ABOVE_CLIENT = 0x0008;
/**
* Flag for {@link #bindService}: allow the process hosting the bound
* service to go through its normal memory management. It will be
* treated more like a running service, allowing the system to
* (temporarily) expunge the process if low on memory or for some other
* whim it may have, and being more aggressive about making it a candidate
* to be killed (and restarted) if running for a long time.
*/
public static final int BIND_ALLOW_OOM_MANAGEMENT = 0x0010;
/**
* Flag for {@link #bindService}: don't impact the scheduling or
* memory management priority of the target service's hosting process.
* Allows the service's process to be managed on the background LRU list
* just like a regular application process in the background.
*/
public static final int BIND_WAIVE_PRIORITY = 0x0020;
/**
* Flag for {@link #bindService}: this service is very important to
* the client, so should be brought to the foreground process level
* when the client is. Normally a process can only be raised to the
* visibility level by a client, even if that client is in the foreground.
*/
public static final int BIND_IMPORTANT = 0x0040;
/**
* Flag for {@link #bindService}: If binding from an activity, allow the
* target service's process importance to be raised based on whether the
* activity is visible to the user, regardless whether another flag is
* used to reduce the amount that the client process's overall importance
* is used to impact it.
*/
public static final int BIND_ADJUST_WITH_ACTIVITY = 0x0080;
/**
* Flag for {@link #bindService}: If binding from an app that is visible or user-perceptible,
* lower the target service's importance to below the perceptible level. This allows
* the system to (temporarily) expunge the bound process from memory to make room for more
* important user-perceptible processes.
*/
public static final int BIND_NOT_PERCEPTIBLE = 0x00000100;
/**
* Flag for {@link #bindService}: If binding from an app that is visible, the bound service is
* allowed to start an activity from background. This was the default behavior before SDK
* version {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE}. Since then, the default
* behavior changed to disallow the bound service to start a background activity even if the app
* bound to it is in foreground, unless this flag is specified when binding.
*/
public static final int BIND_ALLOW_ACTIVITY_STARTS = 0X000000200;
/**
* Flag for {@link #bindService}: If binding from an app that has specific capabilities
* due to its foreground state such as an activity or foreground service, then this flag will
* allow the bound app to get the same capabilities, as long as it has the required permissions
* as well.
*
* If binding from a top app and its target SDK version is at or above
* {@link android.os.Build.VERSION_CODES#R}, the app needs to
* explicitly use BIND_INCLUDE_CAPABILITIES flag to pass all capabilities to the service so the
* other app can have while-in-use access such as location, camera, microphone from background.
* If binding from a top app and its target SDK version is below
* {@link android.os.Build.VERSION_CODES#R}, BIND_INCLUDE_CAPABILITIES is implicit.
*/
public static final int BIND_INCLUDE_CAPABILITIES = 0x000001000;
/**
* Flag for {@link #bindIsolatedService}: Bind the service into a shared isolated process.
* Specifying this flag allows multiple isolated services to be running in a single shared
* isolated process.
*
* The shared isolated process instance is identified by the <var>instanceName</var>
* parameter in {@link #bindIsolatedService(Intent, int, String, Executor, ServiceConnection)}.
*
* Subsequent calls to {@link #bindIsolatedService} with the same <var>instanceName</var>
* will cause the isolated service to be co-located in the same shared isolated process.
*
* Note that the shared isolated process is scoped to the calling app; once created, only
* the calling app can bind additional isolated services into the shared process. However,
* the services themselves can come from different APKs and therefore different vendors.
*
* Only services that set the {@link android.R.attr#allowSharedIsolatedProcess} attribute
* to {@code true} are allowed to be bound into a shared isolated process.
*
*/
public static final int BIND_SHARED_ISOLATED_PROCESS = 0x00002000;
/**
* Flag for {@link #bindIsolatedService}: Bind the service into a shared isolated process,
* but only with other isolated services from the same package that declare the same process
* name.
*
* <p>Specifying this flag allows multiple isolated services defined in the same package to be
* running in a single shared isolated process. This shared isolated process must be specified
* since this flag will not work with the default application process.
*
* <p>This flag is different from {@link #BIND_SHARED_ISOLATED_PROCESS} since it only
* allows binding services from the same package in the same shared isolated process. This also
* means the shared package isolated process is global, and not scoped to each potential
* calling app.
*
* <p>The shared isolated process instance is identified by the "android:process" attribute
* defined by the service. This flag cannot be used without this attribute set.
*/
@FlaggedApi(FLAG_ENABLE_BIND_PACKAGE_ISOLATED_PROCESS)
public static final int BIND_PACKAGE_ISOLATED_PROCESS = 1 << 14;
/*********** Public flags above this line ***********/
/*********** Hidden flags below this line ***********/
/**
* Flag for {@link #bindService}: This flag is only intended to be used by the system to
* indicate that a service binding is not considered as real package component usage and should
* not generate a {@link android.app.usage.UsageEvents.Event#APP_COMPONENT_USED} event in usage
* stats.
* @hide
*/
public static final int BIND_NOT_APP_COMPONENT_USAGE = 0x00008000;
/**
* Flag for {@link #bindService}: allow the process hosting the target service to be treated
* as if it's as important as a perceptible app to the user and avoid the oom killer killing
* this process in low memory situations until there aren't any other processes left but the
* ones which are user-perceptible.
*
* @hide
*/
public static final int BIND_ALMOST_PERCEPTIBLE = 0x000010000;
/**
* Flag for {@link #bindService}: allow the process hosting the target service to gain
* {@link ActivityManager#PROCESS_CAPABILITY_POWER_RESTRICTED_NETWORK}, which allows it be able
* to access network regardless of any power saving restrictions.
*
* @hide
*/
public static final int BIND_BYPASS_POWER_NETWORK_RESTRICTIONS = 0x00020000;
/**
* Do not use. This flag is no longer needed nor used.
* @hide
*/
@SystemApi
@Deprecated
public static final int BIND_ALLOW_FOREGROUND_SERVICE_STARTS_FROM_BACKGROUND = 0x00040000;
/**
* Flag for {@link #bindService}: This flag is intended to be used only by the system to adjust
* the scheduling policy for IMEs (and any other out-of-process user-visible components that
* work closely with the top app) so that UI hosted in such services can have the same
* scheduling policy (e.g. SCHED_FIFO when it is enabled and TOP_APP_PRIORITY_BOOST otherwise)
* as the actual top-app.
* @hide
*/
public static final int BIND_SCHEDULE_LIKE_TOP_APP = 0x00080000;
/**
* Flag for {@link #bindService}: allow background activity starts from the bound service's
* process.
* This flag is only respected if the caller is holding
* {@link android.Manifest.permission#START_ACTIVITIES_FROM_BACKGROUND}.
* @hide
*/
@SystemApi
public static final int BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS = 0x00100000;
/**
* @hide Flag for {@link #bindService}: the service being bound to represents a
* protected system component, so must have association restrictions applied to it.
* That is, a system config must have one or more allow-association tags limiting
* which packages it can interact with. If it does not have any such association
* restrictions, a default empty set will be created.
*/
public static final int BIND_RESTRICT_ASSOCIATIONS = 0x00200000;
/**
* @hide Flag for {@link #bindService}: allows binding to a service provided
* by an instant app. Note that the caller may not have access to the instant
* app providing the service which is a violation of the instant app sandbox.
* This flag is intended ONLY for development/testing and should be used with
* great care. Only the system is allowed to use this flag.
*/
public static final int BIND_ALLOW_INSTANT = 0x00400000;
/**
* @hide Flag for {@link #bindService}: like {@link #BIND_NOT_FOREGROUND}, but puts it
* up in to the important background state (instead of transient).
*/
public static final int BIND_IMPORTANT_BACKGROUND = 0x00800000;
/**
* @hide Flag for {@link #bindService}: allows application hosting service to manage allowlists
* such as temporary allowing a {@code PendingIntent} to bypass Power Save mode.
*/
public static final int BIND_ALLOW_WHITELIST_MANAGEMENT = 0x01000000;
/**
* @hide Flag for {@link #bindService}: Like {@link #BIND_FOREGROUND_SERVICE},
* but only applies while the device is awake.
*/
public static final int BIND_FOREGROUND_SERVICE_WHILE_AWAKE = 0x02000000;
/**
* @hide Flag for {@link #bindService}: For only the case where the binding
* is coming from the system, set the process state to BOUND_FOREGROUND_SERVICE
* instead of the normal maximum of IMPORTANT_FOREGROUND. That is, this is
* saying that the process shouldn't participate in the normal power reduction
* modes (removing network access etc).
*/
public static final int BIND_FOREGROUND_SERVICE = 0x04000000;
/**
* @hide Flag for {@link #bindService}: Treat the binding as hosting
* an activity, an unbinding as the activity going in the background.
* That is, when unbinding, the process when empty will go on the activity
* LRU list instead of the regular one, keeping it around more aggressively
* than it otherwise would be. This is intended for use with IMEs to try
* to keep IME processes around for faster keyboard switching.
*/
public static final int BIND_TREAT_LIKE_ACTIVITY = 0x08000000;
/**
* @hide An idea that is not yet implemented.
* Flag for {@link #bindService}: If binding from an activity, consider
* this service to be visible like the binding activity is. That is,
* it will be treated as something more important to keep around than
* invisible background activities. This will impact the number of
* recent activities the user can switch between without having them
* restart. There is no guarantee this will be respected, as the system
* tries to balance such requests from one app vs. the importance of
* keeping other apps around.
*
* @deprecated Repurposed to {@link #BIND_TREAT_LIKE_VISIBLE_FOREGROUND_SERVICE}.
*/
@Deprecated
public static final int BIND_VISIBLE = 0x10000000;
/**
* @hide Flag for {@link #bindService}: Treat the binding as hosting a foreground service
* and also visible to the user. That is, the app hosting the service will get its process state
* bumped to the {@link android.app.ActivityManager#PROCESS_STATE_FOREGROUND_SERVICE},
* and it's considered as visible to the user, thus less likely to be expunged from memory
* on low memory situations. This is intented for use by processes with the process state
* better than the {@link android.app.ActivityManager#PROCESS_STATE_TOP}.
*/
public static final int BIND_TREAT_LIKE_VISIBLE_FOREGROUND_SERVICE = 0x10000000;
/**
* @hide
* Flag for {@link #bindService}: Consider this binding to be causing the target
* process to be showing UI, so it will be do a UI_HIDDEN memory trim when it goes
* away.
*/
public static final int BIND_SHOWING_UI = 0x20000000;
/**
* Flag for {@link #bindService}: Don't consider the bound service to be
* visible, even if the caller is visible.
* @hide
*/
public static final int BIND_NOT_VISIBLE = 0x40000000;
/**
* Flag for {@link #bindService}: The service being bound is an
* {@link android.R.attr#isolatedProcess isolated},
* {@link android.R.attr#externalService external} service. This binds the service into the
* calling application's package, rather than the package in which the service is declared.
* <p>
* When using this flag, the code for the service being bound will execute under the calling
* application's package name and user ID. Because the service must be an isolated process,
* it will not have direct access to the application's data, though.
*
* The purpose of this flag is to allow applications to provide services that are attributed
* to the app using the service, rather than the application providing the service.
* </p>
*
* <em>This flag is NOT compatible with {@link BindServiceFlags}. If you need to use
* {@link BindServiceFlags}, you must use {@link #BIND_EXTERNAL_SERVICE_LONG} instead.</em>
*/
public static final int BIND_EXTERNAL_SERVICE = 0x80000000;
/**
* Works in the same way as {@link #BIND_EXTERNAL_SERVICE}, but it's defined as a {@code long}
* value that is compatible to {@link BindServiceFlags}.
*/
public static final long BIND_EXTERNAL_SERVICE_LONG = 1L << 62;
/**
* Flag for {@link #bindService}: allow the process hosting the target service to gain
* {@link ActivityManager#PROCESS_CAPABILITY_USER_RESTRICTED_NETWORK}, which allows it be able
* to access network regardless of any user restrictions.
*
* @hide
*/
public static final long BIND_BYPASS_USER_NETWORK_RESTRICTIONS = 0x1_0000_0000L;
/**
* Flag for {@link #bindService}.
*
* @hide
*/
public static final long BIND_MATCH_QUARANTINED_COMPONENTS = 0x2_0000_0000L;
/**
* These bind flags reduce the strength of the binding such that we shouldn't
* consider it as pulling the process up to the level of the one that is bound to it.
* @hide
*/
public static final int BIND_REDUCTION_FLAGS =
Context.BIND_ALLOW_OOM_MANAGEMENT | Context.BIND_WAIVE_PRIORITY
| Context.BIND_NOT_PERCEPTIBLE | Context.BIND_NOT_VISIBLE;
/** @hide */
@IntDef(flag = true, prefix = { "RECEIVER_VISIBLE" }, value = {
RECEIVER_VISIBLE_TO_INSTANT_APPS, RECEIVER_EXPORTED, RECEIVER_NOT_EXPORTED,
RECEIVER_EXPORTED_UNAUDITED
})
@Retention(RetentionPolicy.SOURCE)
public @interface RegisterReceiverFlags {}
/**
* Flag for {@link #registerReceiver}: The receiver can receive broadcasts from Instant Apps.
*/
public static final int RECEIVER_VISIBLE_TO_INSTANT_APPS = 0x1;
/**
* Flag for {@link #registerReceiver}: The receiver can receive broadcasts from other Apps.
* Has the same behavior as marking a statically registered receiver with "exported=true"
*/
public static final int RECEIVER_EXPORTED = 0x2;
/**
* @deprecated Use {@link #RECEIVER_NOT_EXPORTED} or {@link #RECEIVER_EXPORTED} instead.
* @hide
*/
@Deprecated
@TestApi
public static final int RECEIVER_EXPORTED_UNAUDITED = RECEIVER_EXPORTED;
/**
* Flag for {@link #registerReceiver}: The receiver cannot receive broadcasts from other Apps.
* Has the same behavior as marking a statically registered receiver with "exported=false"
*/
public static final int RECEIVER_NOT_EXPORTED = 0x4;
/**
* Returns an AssetManager instance for the application's package.
* <p>
* <strong>Note:</strong> Implementations of this method should return
* an AssetManager instance that is consistent with the Resources instance
* returned by {@link #getResources()}. For example, they should share the
* same {@link Configuration} object.
*
* @return an AssetManager instance for the application's package
* @see #getResources()
*/
public abstract AssetManager getAssets();
/**
* Returns a Resources instance for the application's package.
* <p>
* <strong>Note:</strong> Implementations of this method should return
* a Resources instance that is consistent with the AssetManager instance
* returned by {@link #getAssets()}. For example, they should share the
* same {@link Configuration} object.
*
* @return a Resources instance for the application's package
* @see #getAssets()
*/
public abstract Resources getResources();
/** Return PackageManager instance to find global package information. */
public abstract PackageManager getPackageManager();
/** Return a ContentResolver instance for your application's package. */
public abstract ContentResolver getContentResolver();
/**
* Return the Looper for the main thread of the current process. This is
* the thread used to dispatch calls to application components (activities,
* services, etc).
* <p>
* By definition, this method returns the same result as would be obtained
* by calling {@link Looper#getMainLooper() Looper.getMainLooper()}.
* </p>
*
* @return The main looper.
*/
public abstract Looper getMainLooper();
/**
* Return an {@link Executor} that will run enqueued tasks on the main
* thread associated with this context. This is the thread used to dispatch
* calls to application components (activities, services, etc).
*/
public Executor getMainExecutor() {
// This is pretty inefficient, which is why ContextImpl overrides it
return new HandlerExecutor(new Handler(getMainLooper()));
}
/**
* Return the context of the single, global Application object of the
* current process. This generally should only be used if you need a
* Context whose lifecycle is separate from the current context, that is
* tied to the lifetime of the process rather than the current component.
*
* <p>Consider for example how this interacts with
* {@link #registerReceiver(BroadcastReceiver, IntentFilter)}:
* <ul>
* <li> <p>If used from an Activity context, the receiver is being registered
* within that activity. This means that you are expected to unregister
* before the activity is done being destroyed; in fact if you do not do
* so, the framework will clean up your leaked registration as it removes
* the activity and log an error. Thus, if you use the Activity context
* to register a receiver that is static (global to the process, not
* associated with an Activity instance) then that registration will be
* removed on you at whatever point the activity you used is destroyed.
* <li> <p>If used from the Context returned here, the receiver is being
* registered with the global state associated with your application. Thus
* it will never be unregistered for you. This is necessary if the receiver
* is associated with static data, not a particular component. However
* using the ApplicationContext elsewhere can easily lead to serious leaks
* if you forget to unregister, unbind, etc.
* </ul>
*/
public abstract Context getApplicationContext();
/** Non-activity related autofill ids are unique in the app */
private static int sLastAutofillId = View.NO_ID;
/**
* Gets the next autofill ID.
*
* <p>All IDs will be smaller or the same as {@link View#LAST_APP_AUTOFILL_ID}. All IDs
* returned will be unique.
*
* @return A ID that is unique in the process
*
* {@hide}
*/
public int getNextAutofillId() {
if (sLastAutofillId == View.LAST_APP_AUTOFILL_ID - 1) {
sLastAutofillId = View.NO_ID;
}
sLastAutofillId++;
return sLastAutofillId;
}
/**
* Add a new {@link ComponentCallbacks} to the base application of the
* Context, which will be called at the same times as the ComponentCallbacks
* methods of activities and other components are called. Note that you
* <em>must</em> be sure to use {@link #unregisterComponentCallbacks} when
* appropriate in the future; this will not be removed for you.
* <p>
* After {@link Build.VERSION_CODES#S}, registering the ComponentCallbacks to Context created
* via {@link #createWindowContext(int, Bundle)} or
* {@link #createWindowContext(Display, int, Bundle)} will receive
* {@link ComponentCallbacks#onConfigurationChanged(Configuration)} from Window Context rather
* than its base application. It is helpful if you want to handle UI components that
* associated with the Window Context when the Window Context has configuration changes.</p>
* <p>
* After {@link Build.VERSION_CODES#TIRAMISU}, registering the ComponentCallbacks to
* {@link Activity} context will receive
* {@link ComponentCallbacks#onConfigurationChanged(Configuration)} from
* {@link Activity#onConfigurationChanged(Configuration)} rather than its base application.</p>
* <p>
* After {@link Build.VERSION_CODES#UPSIDE_DOWN_CAKE}, registering the ComponentCallbacks to
* {@link android.inputmethodservice.InputMethodService} will receive
* {@link ComponentCallbacks#onConfigurationChanged(Configuration)} from InputmethodService
* rather than its base application. It is helpful if you want to handle UI components when the
* IME has configuration changes.</p>
*
* @param callback The interface to call. This can be either a
* {@link ComponentCallbacks} or {@link ComponentCallbacks2} interface.
*
* @see Context#createWindowContext(int, Bundle)
*/
public void registerComponentCallbacks(ComponentCallbacks callback) {
getApplicationContext().registerComponentCallbacks(callback);
}
/**
* Remove a {@link ComponentCallbacks} object that was previously registered
* with {@link #registerComponentCallbacks(ComponentCallbacks)}.
*/
public void unregisterComponentCallbacks(ComponentCallbacks callback) {
getApplicationContext().unregisterComponentCallbacks(callback);
}
/**
* Return a localized, styled CharSequence from the application's package's
* default string table.
*
* @param resId Resource id for the CharSequence text
*/
@NonNull
public final CharSequence getText(@StringRes int resId) {
return getResources().getText(resId);
}
/**
* Returns a localized string from the application's package's
* default string table.
*
* @param resId Resource id for the string
* @return The string data associated with the resource, stripped of styled
* text information.
*/
@NonNull
public final String getString(@StringRes int resId) {
return getResources().getString(resId);
}
/**
* Returns a localized formatted string from the application's package's
* default string table, substituting the format arguments as defined in
* {@link java.util.Formatter} and {@link java.lang.String#format}.
*
* @param resId Resource id for the format string
* @param formatArgs The format arguments that will be used for
* substitution.
* @return The string data associated with the resource, formatted and
* stripped of styled text information.
*/
@NonNull
public final String getString(@StringRes int resId, Object... formatArgs) {
return getResources().getString(resId, formatArgs);
}
/**
* Returns a color associated with a particular resource ID and styled for
* the current theme.
*
* @param id The desired resource identifier, as generated by the aapt
* tool. This integer encodes the package, type, and resource
* entry. The value 0 is an invalid identifier.
* @return A single color value in the form 0xAARRGGBB.
* @throws android.content.res.Resources.NotFoundException if the given ID
* does not exist.
*/
@ColorInt
public final int getColor(@ColorRes int id) {
return getResources().getColor(id, getTheme());
}
/**
* Returns a drawable object associated with a particular resource ID and
* styled for the current theme.
*
* @param id The desired resource identifier, as generated by the aapt
* tool. This integer encodes the package, type, and resource
* entry. The value 0 is an invalid identifier.
* @return An object that can be used to draw this resource.
* @throws android.content.res.Resources.NotFoundException if the given ID
* does not exist.
*/
@Nullable
public final Drawable getDrawable(@DrawableRes int id) {
return getResources().getDrawable(id, getTheme());
}
/**
* Returns a color state list associated with a particular resource ID and
* styled for the current theme.
*