1 /*
2  * Copyright (C) 2007 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 android.content.pm;
18 
19 import android.Manifest;
20 import android.annotation.FlaggedApi;
21 import android.annotation.IntDef;
22 import android.annotation.RequiresPermission;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.util.Printer;
26 
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 
30 /**
31  * Information you can retrieve about a particular application
32  * service. This corresponds to information collected from the
33  * AndroidManifest.xml's <service> tags.
34  */
35 @android.ravenwood.annotation.RavenwoodKeepWholeClass
36 public class ServiceInfo extends ComponentInfo
37         implements Parcelable {
38     /**
39      * Optional name of a permission required to be able to access this
40      * Service.  From the "permission" attribute.
41      */
42     public String permission;
43 
44     /**
45      * Bit in {@link #flags}: If set, the service will automatically be
46      * stopped by the system if the user removes a task that is rooted
47      * in one of the application's activities.  Set from the
48      * {@link android.R.attr#stopWithTask} attribute.
49      */
50     public static final int FLAG_STOP_WITH_TASK = 0x0001;
51 
52     /**
53      * Bit in {@link #flags}: If set, the service will run in its own
54      * isolated process.  Set from the
55      * {@link android.R.attr#isolatedProcess} attribute.
56      */
57     public static final int FLAG_ISOLATED_PROCESS = 0x0002;
58 
59     /**
60      * Bit in {@link #flags}: If set, the service can be bound and run in the
61      * calling application's package, rather than the package in which it is
62      * declared.  Set from {@link android.R.attr#externalService} attribute.
63      */
64     public static final int FLAG_EXTERNAL_SERVICE = 0x0004;
65 
66     /**
67      * Bit in {@link #flags}: If set, the service (which must be isolated)
68      * will be spawned from an Application Zygote, instead of the regular Zygote.
69      * The Application Zygote will pre-initialize the application's class loader,
70      * and call a static callback into the application to allow it to perform
71      * application-specific preloads (such as loading a shared library). Therefore,
72      * spawning from the Application Zygote will typically reduce the service
73      * launch time and reduce its memory usage. The downside of using this flag
74      * is that you will have an additional process (the app zygote itself) that
75      * is taking up memory. Whether actual memory usage is improved therefore
76      * strongly depends on the number of isolated services that an application
77      * starts, and how much memory those services save by preloading. Therefore,
78      * it is recommended to measure memory usage under typical workloads to
79      * determine whether it makes sense to use this flag.
80      */
81     public static final int FLAG_USE_APP_ZYGOTE = 0x0008;
82 
83     /**
84      * Bit in {@link #flags}: If set, and this is an {@link android.R.attr#isolatedProcess}
85      * service, the service is allowed to be bound in a shared isolated process with other
86      * isolated services. Note that these other isolated services can also belong to other
87      * apps from different vendors.
88      *
89      * Shared isolated processes are created when using the
90      * {@link android.content.Context#BIND_SHARED_ISOLATED_PROCESS) during service binding.
91      *
92      * Note that when this flag is used, the {@link android.R.attr#process} attribute is
93      * ignored when the process is bound into a shared isolated process by a client.
94      */
95     public static final int FLAG_ALLOW_SHARED_ISOLATED_PROCESS = 0x0010;
96 
97     /**
98      * Bit in {@link #flags} indicating if the service is visible to ephemeral applications.
99      * @hide
100      */
101     public static final int FLAG_VISIBLE_TO_INSTANT_APP = 0x100000;
102 
103     /**
104      * @hide Bit in {@link #flags}: If set, this service will only be available
105      * for the system user.
106      * Set from the android.R.attr#systemUserOnly attribute.
107      * In Sync with {@link ActivityInfo#FLAG_SYSTEM_USER_ONLY}
108      */
109     public static final int FLAG_SYSTEM_USER_ONLY = ActivityInfo.FLAG_SYSTEM_USER_ONLY;
110 
111     /**
112      * Bit in {@link #flags}: If set, a single instance of the service will
113      * run for all users on the device.  Set from the
114      * {@link android.R.attr#singleUser} attribute.
115      */
116     public static final int FLAG_SINGLE_USER = 0x40000000;
117 
118     /**
119      * Options that have been set in the service declaration in the
120      * manifest.
121      * These include:
122      * {@link #FLAG_STOP_WITH_TASK}, {@link #FLAG_ISOLATED_PROCESS},
123      * {@link #FLAG_SINGLE_USER}.
124      */
125     public int flags;
126 
127     /**
128      * The default foreground service type if not been set in manifest file.
129      *
130      * <p>Apps targeting API level {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} and
131      * later should NOT use this type,
132      * calling {@link android.app.Service#startForeground(int, android.app.Notification, int)} with
133      * this type will get a {@link android.app.InvalidForegroundServiceTypeException}.</p>
134      *
135      * @deprecated Do not use.
136      */
137     @Deprecated
138     public static final int FOREGROUND_SERVICE_TYPE_NONE = 0;
139 
140     /**
141      * Constant corresponding to <code>dataSync</code> in
142      * the {@link android.R.attr#foregroundServiceType} attribute.
143      * Data(photo, file, account) upload/download, backup/restore, import/export, fetch,
144      * transfer over network between device and cloud.
145      *
146      * <p>This type has time limit of 6 hours starting from Android version
147      * {@link android.os.Build.VERSION_CODES#VANILLA_ICE_CREAM}.
148      * A foreground service of this type must be stopped within the timeout by
149      * {@link android.app.Service#stopSelf()},
150      * {@link android.content.Context#stopService(android.content.Intent)} or their overloads).
151      * {@link android.app.Service#stopForeground(int)} will also work, which will demote the
152      * service to a "background" service, which will soon be stopped by the system.
153      *
154      * <p>If the service isn't stopped within the timeout,
155      * {@link android.app.Service#onTimeout(int, int)} will be called.
156      *
157      * <p>Also note, even though
158      * {@link android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_DATA_SYNC} can be used on
159      * Android versions prior to {@link android.os.Build.VERSION_CODES#VANILLA_ICE_CREAM}, since
160      * {@link android.app.Service#onTimeout(int, int)} did not exist on such versions, it will
161      * never be called.
162      *
163      * Because of this, developers must make sure to stop the foreground service even if
164      * {@link android.app.Service#onTimeout(int, int)} is not called on such versions.
165      *
166      * @see android.app.Service#onTimeout(int, int)
167      */
168     @RequiresPermission(
169             value = Manifest.permission.FOREGROUND_SERVICE_DATA_SYNC,
170             conditional = true
171     )
172     public static final int FOREGROUND_SERVICE_TYPE_DATA_SYNC = 1 << 0;
173 
174     /**
175      * Constant corresponding to <code>mediaPlayback</code> in
176      * the {@link android.R.attr#foregroundServiceType} attribute.
177      * Music, video, news or other media playback.
178      *
179      * <p>Starting foreground service with this type from apps targeting API level
180      * {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} and later, will require permission
181      * {@link android.Manifest.permission#FOREGROUND_SERVICE_MEDIA_PLAYBACK}.
182      */
183     @RequiresPermission(
184             value = Manifest.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK,
185             conditional = true
186     )
187     public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK = 1 << 1;
188 
189     /**
190      * Constant corresponding to <code>phoneCall</code> in
191      * the {@link android.R.attr#foregroundServiceType} attribute.
192      * Ongoing operations related to phone calls, video conferencing,
193      * or similar interactive communication.
194      *
195      * <p>Starting foreground service with this type from apps targeting API level
196      * {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} and later, will require permission
197      * {@link android.Manifest.permission#FOREGROUND_SERVICE_PHONE_CALL} and
198      * {@link android.Manifest.permission#MANAGE_OWN_CALLS} or holding the default
199      * {@link android.app.role.RoleManager#ROLE_DIALER dialer role}.
200      */
201     @RequiresPermission(
202             allOf = {
203                 Manifest.permission.FOREGROUND_SERVICE_PHONE_CALL,
204             },
205             anyOf = {
206                 Manifest.permission.MANAGE_OWN_CALLS,
207             },
208             conditional = true
209     )
210     public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL = 1 << 2;
211 
212     /**
213      * Constant corresponding to <code>location</code> in
214      * the {@link android.R.attr#foregroundServiceType} attribute.
215      * GPS, map, navigation location update.
216      *
217      * <p>Starting foreground service with this type from apps targeting API level
218      * {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} and later, will require permission
219      * {@link android.Manifest.permission#FOREGROUND_SERVICE_LOCATION} and one of the
220      * following permissions:
221      * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION},
222      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
223      */
224     @RequiresPermission(
225             allOf = {
226                 Manifest.permission.FOREGROUND_SERVICE_LOCATION,
227             },
228             anyOf = {
229                 Manifest.permission.ACCESS_COARSE_LOCATION,
230                 Manifest.permission.ACCESS_FINE_LOCATION,
231             },
232             conditional = true
233     )
234     public static final int FOREGROUND_SERVICE_TYPE_LOCATION = 1 << 3;
235 
236     /**
237      * Constant corresponding to <code>connectedDevice</code> in
238      * the {@link android.R.attr#foregroundServiceType} attribute.
239      * Auto, bluetooth, TV or other devices connection, monitoring and interaction.
240      *
241      * <p>Starting foreground service with this type from apps targeting API level
242      * {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} and later, will require permission
243      * {@link android.Manifest.permission#FOREGROUND_SERVICE_CONNECTED_DEVICE} and one of the
244      * following permissions:
245      * {@link android.Manifest.permission#BLUETOOTH_ADVERTISE},
246      * {@link android.Manifest.permission#BLUETOOTH_CONNECT},
247      * {@link android.Manifest.permission#BLUETOOTH_SCAN},
248      * {@link android.Manifest.permission#CHANGE_NETWORK_STATE},
249      * {@link android.Manifest.permission#CHANGE_WIFI_STATE},
250      * {@link android.Manifest.permission#CHANGE_WIFI_MULTICAST_STATE},
251      * {@link android.Manifest.permission#NFC},
252      * {@link android.Manifest.permission#TRANSMIT_IR},
253      * {@link android.Manifest.permission#UWB_RANGING},
254      * or has been granted the access to one of the attached USB devices/accessories.
255      */
256     @RequiresPermission(
257             allOf = {
258                 Manifest.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE,
259             },
260             anyOf = {
261                 Manifest.permission.BLUETOOTH_ADVERTISE,
262                 Manifest.permission.BLUETOOTH_CONNECT,
263                 Manifest.permission.BLUETOOTH_SCAN,
264                 Manifest.permission.CHANGE_NETWORK_STATE,
265                 Manifest.permission.CHANGE_WIFI_STATE,
266                 Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
267                 Manifest.permission.NFC,
268                 Manifest.permission.TRANSMIT_IR,
269                 Manifest.permission.UWB_RANGING,
270             },
271             conditional = true
272     )
273     public static final int FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE = 1 << 4;
274 
275     /**
276      * Constant corresponding to {@code mediaProjection} in
277      * the {@link android.R.attr#foregroundServiceType foregroundServiceType} attribute.
278      *
279      * <p>
280      * To capture through {@link android.media.projection.MediaProjection}, an app must start a
281      * foreground service with the type corresponding to this constant. This type should only be
282      * used for {@link android.media.projection.MediaProjection}. Capturing screen contents via
283      * {@link android.media.projection.MediaProjection#createVirtualDisplay(String, int, int, int,
284      * int, android.view.Surface, android.hardware.display.VirtualDisplay.Callback,
285      * android.os.Handler) createVirtualDisplay} conveniently allows recording, presenting screen
286      * contents into a meeting, taking screenshots, or several other scenarios.
287      * </p>
288      *
289      * <p>Starting foreground service with this type from apps targeting API level
290      * {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} and later, will require permission
291      * {@link android.Manifest.permission#FOREGROUND_SERVICE_MEDIA_PROJECTION}, and the user must
292      * have allowed the screen capture request from this app.
293      */
294     @RequiresPermission(
295             value = Manifest.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION,
296             conditional = true
297     )
298     public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION = 1 << 5;
299 
300     /**
301      * Constant corresponding to {@code camera} in
302      * the {@link android.R.attr#foregroundServiceType} attribute.
303      * Use the camera device or record video.
304      * For apps with <code>targetSdkVersion</code> {@link android.os.Build.VERSION_CODES#R} and
305      * above, a foreground service will not be able to access the camera if this type is not
306      * specified in the manifest and in
307      * {@link android.app.Service#startForeground(int, android.app.Notification, int)}.
308      *
309      * <p>Starting foreground service with this type from apps targeting API level
310      * {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} and later, will require permission
311      * {@link android.Manifest.permission#FOREGROUND_SERVICE_CAMERA} and
312      * {@link android.Manifest.permission#CAMERA}.
313      */
314     @RequiresPermission(
315             allOf = {
316                 Manifest.permission.FOREGROUND_SERVICE_CAMERA,
317             },
318             anyOf = {
319                 Manifest.permission.CAMERA,
320             },
321             conditional = true
322     )
323     public static final int FOREGROUND_SERVICE_TYPE_CAMERA = 1 << 6;
324 
325     /**
326      * Constant corresponding to {@code microphone} in
327      * the {@link android.R.attr#foregroundServiceType} attribute.
328      * Use the microphone device or record audio.
329      * For apps with <code>targetSdkVersion</code> {@link android.os.Build.VERSION_CODES#R} and
330      * above, a foreground service will not be able to access the microphone if this type is not
331      * specified in the manifest and in
332      * {@link android.app.Service#startForeground(int, android.app.Notification, int)}.
333      *
334      * <p>Starting foreground service with this type from apps targeting API level
335      * {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} and later, will require permission
336      * {@link android.Manifest.permission#FOREGROUND_SERVICE_MICROPHONE} and one of the following
337      * permissions:
338      * {@link android.Manifest.permission#CAPTURE_AUDIO_OUTPUT},
339      * {@link android.Manifest.permission#RECORD_AUDIO}.
340      */
341     @RequiresPermission(
342             allOf = {
343                 Manifest.permission.FOREGROUND_SERVICE_MICROPHONE,
344             },
345             anyOf = {
346                 Manifest.permission.CAPTURE_AUDIO_OUTPUT,
347                 Manifest.permission.RECORD_AUDIO,
348             },
349             conditional = true
350     )
351     public static final int FOREGROUND_SERVICE_TYPE_MICROPHONE = 1 << 7;
352 
353     /**
354      * Constant corresponding to {@code health} in
355      * the {@link android.R.attr#foregroundServiceType} attribute.
356      * Health, wellness and fitness.
357      *
358      * <p>The caller app is required to have the permissions
359      * {@link android.Manifest.permission#FOREGROUND_SERVICE_HEALTH} and one of the following
360      * permissions:
361      * {@link android.Manifest.permission#ACTIVITY_RECOGNITION},
362      * {@link android.Manifest.permission#BODY_SENSORS},
363      * {@link android.Manifest.permission#HIGH_SAMPLING_RATE_SENSORS}.
364      */
365     @RequiresPermission(
366             allOf = {
367                 Manifest.permission.FOREGROUND_SERVICE_HEALTH,
368             },
369             anyOf = {
370                 Manifest.permission.ACTIVITY_RECOGNITION,
371                 Manifest.permission.BODY_SENSORS,
372                 Manifest.permission.HIGH_SAMPLING_RATE_SENSORS,
373             }
374     )
375     public static final int FOREGROUND_SERVICE_TYPE_HEALTH = 1 << 8;
376 
377     /**
378      * Constant corresponding to {@code remoteMessaging} in
379      * the {@link android.R.attr#foregroundServiceType} attribute.
380      * Messaging use cases which host local server to relay messages across devices.
381      */
382     @RequiresPermission(
383             value = Manifest.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING
384     )
385     public static final int FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING = 1 << 9;
386 
387     /**
388      * Constant corresponding to {@code systemExempted} in
389      * the {@link android.R.attr#foregroundServiceType} attribute.
390      * The system exempted foreground service use cases.
391      *
392      * <p class="note">Note, apps are allowed to use this type only in the following cases:
393      * <ul>
394      *   <li>App has a UID &lt; {@link android.os.Process#FIRST_APPLICATION_UID}</li>
395      *   <li>App is on Doze allowlist</li>
396      *   <li>Device is running in <a href="https://android.googlesource.com/platform/frameworks/base/+/master/packages/SystemUI/docs/demo_mode.md">Demo Mode</a></li>
397      *   <li><a href="https://source.android.com/devices/tech/admin/provision">Device owner app</a></li>
398      *   <li><a href="https://source.android.com/devices/tech/admin/managed-profiles">Profile owner apps</a></li>
399      *   <li>Persistent apps</li>
400      *   <li><a href="https://source.android.com/docs/core/connect/carrier">Carrier privileged apps</a></li>
401      *   <li>Apps that have the {@code android.app.role.RoleManager#ROLE_EMERGENCY} role</li>
402      *   <li>Headless system apps</li>
403      *   <li><a href="{@docRoot}guide/topics/admin/device-admin">Device admin apps</a></li>
404      *   <li>Active VPN apps</li>
405      *   <li>Apps holding {@link android.Manifest.permission#SCHEDULE_EXACT_ALARM} or
406      *       {@link android.Manifest.permission#USE_EXACT_ALARM} permission.</li>
407      * </ul>
408      * </p>
409      */
410     @RequiresPermission(
411             value = Manifest.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED
412     )
413     public static final int FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED = 1 << 10;
414 
415     /**
416      * A foreground service type for "short-lived" services, which corresponds to
417      * {@code shortService} in the {@link android.R.attr#foregroundServiceType} attribute in the
418      * manifest.
419      *
420      * <p>Unlike other foreground service types, this type is not associated with a specific use
421      * case, and it will not require any special permissions
422      * (besides {@link android.Manifest.permission#FOREGROUND_SERVICE}).
423      *
424      * However, this type has the following restrictions.
425      *
426      * <ul>
427      *     <li>
428      *         The type has a 3 minute timeout.
429      *         A foreground service of this type must be stopped within the timeout by
430      *         {@link android.app.Service#stopSelf()},
431      *         {@link android.content.Context#stopService(android.content.Intent)}
432      *         or their overloads).
433      *         {@link android.app.Service#stopForeground(int)} will also work,
434      *         which will demote the
435      *         service to a "background" service, which will soon be stopped by the system.
436      *
437      *         <p>If the service isn't stopped within the timeout,
438      *         {@link android.app.Service#onTimeout(int)} will be called. Note, even when the
439      *         system calls this callback, it will not stop the service automatically.
440      *         You still need to stop the service using one of the aforementioned
441      *         ways even when you get this callback.
442      *
443      *         <p>If the service is still not stopped after the callback,
444      *         the app will be declared an ANR, after a short grace period of several seconds.
445      *     <li>
446      *         A foreground service of this type cannot be made "sticky"
447      *         (see {@link android.app.Service#START_STICKY}). That is, if an app is killed
448      *         due to a crash or out-of memory while it's running a short foregorund-service,
449      *         the system will not restart the service.
450      *     <li>
451      *         Other foreground services cannot be started from short foreground services.
452      *         Unlike other foreground service types, when an app is running in the background
453      *         while only having a "short" foreground service, it's not allowed to start
454      *         other foreground services, due to the restriction describe here:
455      *         <a href="/guide/components/foreground-services#background-start-restrictions>
456      *             Restrictions on background starts
457      *         </a>
458      *     <li>
459      *         You can combine multiple foreground services types with {@code |}s, and you can
460      *         combine
461      *         {@link android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_SHORT_SERVICE}.
462      *         with other types as well.
463      *         However,
464      *         {@link android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_SHORT_SERVICE}
465      *         is for situations
466      *         where you have no other valid foreground services to use and the timeout is long
467      *         enough for the task, and if you can use other types, there's no point using
468      *         this type.
469      *         For this reason, if
470      *         {@link android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_SHORT_SERVICE}
471      *         is combined with other foreground service types, the system will simply ignore
472      *         it, and as a result,
473      *         none of the above restrictions will apply (e.g. there'll be no timeout).
474      * </ul>
475      *
476      * <p>Also note, even though
477      * {@link android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_SHORT_SERVICE}
478      * was added
479      * on Android version {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE},
480      * it can be also used on
481      * on prior android versions (just like other new foreground service types can be used).
482      * However, because {@link android.app.Service#onTimeout(int)} did not exist on prior versions,
483      * it will never called on such versions.
484      * Because of this, developers must make sure to stop the foreground service even if
485      * {@link android.app.Service#onTimeout(int)} is not called on such versions.
486      *
487      * @see android.app.Service#onTimeout(int)
488      */
489     public static final int FOREGROUND_SERVICE_TYPE_SHORT_SERVICE = 1 << 11;
490 
491     /**
492      * Constant corresponding to {@code fileManagement} in
493      * the {@link android.R.attr#foregroundServiceType} attribute.
494      * The file management use case which manages files/directories, often involving file I/O
495      * across the file system.
496      *
497      * @hide
498      */
499     @RequiresPermission(
500             value = Manifest.permission.FOREGROUND_SERVICE_FILE_MANAGEMENT
501     )
502     public static final int FOREGROUND_SERVICE_TYPE_FILE_MANAGEMENT = 1 << 12;
503 
504     /**
505      * Constant corresponding to {@code mediaProcessing} in
506      * the {@link android.R.attr#foregroundServiceType} attribute.
507      * Media processing use cases such as video or photo editing and processing.
508      *
509      * This type has time limit of 6 hours.
510      * A foreground service of this type must be stopped within the timeout by
511      * {@link android.app.Service#stopSelf()},
512      * {@link android.content.Context#stopService(android.content.Intent)} or their overloads).
513      * {@link android.app.Service#stopForeground(int)} will also work, which will demote the
514      * service to a "background" service, which will soon be stopped by the system.
515      *
516      * <p>If the service isn't stopped within the timeout,
517      * {@link android.app.Service#onTimeout(int, int)} will be called.
518      *
519      * <p>Also note, even though
520      * {@link android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING} was added in
521      * Android version {@link android.os.Build.VERSION_CODES#VANILLA_ICE_CREAM}, it can be also used
522      * on prior android versions (just like other new foreground service types can be used).
523      * However, because {@link android.app.Service#onTimeout(int, int)} did not exist on prior
524      * versions, it will never be called on such versions.
525      * Because of this, developers must make sure to stop the foreground service even if
526      * {@link android.app.Service#onTimeout(int, int)} is not called on such versions.
527      *
528      * @see android.app.Service#onTimeout(int, int)
529      */
530     @RequiresPermission(
531             value = Manifest.permission.FOREGROUND_SERVICE_MEDIA_PROCESSING
532     )
533     @FlaggedApi(Flags.FLAG_INTRODUCE_MEDIA_PROCESSING_TYPE)
534     public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING = 1 << 13;
535 
536     /**
537      * Constant corresponding to {@code specialUse} in
538      * the {@link android.R.attr#foregroundServiceType} attribute.
539      * Use cases that can't be categorized into any other foreground service types, but also
540      * can't use {@link android.app.job.JobInfo.Builder} APIs.
541      *
542      * <p>The use of this foreground service type may be restricted. Additionally, apps must declare
543      * a service-level {@link PackageManager#PROPERTY_SPECIAL_USE_FGS_SUBTYPE &lt;property&gt;} in
544      * {@code AndroidManifest.xml} as a hint of what the exact use case here is.
545      * Here is an example:
546      * <pre>
547      *  &lt;uses-permission
548      *      android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"
549      *  /&gt;
550      *  &lt;service
551      *      android:name=".MySpecialForegroundService"
552      *      android:foregroundServiceType="specialUse"&gt;
553      *      &lt;property
554      *          android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
555      *          android:value="foo"
556      *      /&gt;
557      * &lt;/service&gt;
558      * </pre>
559      *
560      * In a future release of Android, if the above foreground service type {@code foo} is supported
561      * by the platform, to offer the backward compatibility, the app could specify
562      * the {@code android:maxSdkVersion} attribute in the &lt;uses-permission&gt; section,
563      * and also add the foreground service type {@code foo} into
564      * the {@code android:foregroundServiceType}, therefore the same app could be installed
565      * in both platforms.
566      * <pre>
567      *  &lt;uses-permission
568      *      android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"
569      *      android:maxSdkVersion="last_sdk_version_without_type_foo"
570      *  /&gt;
571      *  &lt;service
572      *      android:name=".MySpecialForegroundService"
573      *      android:foregroundServiceType="specialUse|foo"&gt;
574      *      &lt;property
575      *          android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
576      *          android:value="foo"
577      *      /&gt;
578      * &lt;/service&gt;
579      * </pre>
580      */
581     @RequiresPermission(
582             value = Manifest.permission.FOREGROUND_SERVICE_SPECIAL_USE
583     )
584     public static final int FOREGROUND_SERVICE_TYPE_SPECIAL_USE = 1 << 30;
585 
586     /**
587      * The max index being used in the definition of foreground service types.
588      *
589      * @hide
590      */
591     public static final int FOREGROUND_SERVICE_TYPES_MAX_INDEX = 30;
592 
593     /**
594      * A special value indicates to use all types set in manifest file.
595      */
596     public static final int FOREGROUND_SERVICE_TYPE_MANIFEST = -1;
597 
598     /**
599      * The set of flags for foreground service type.
600      * The foreground service type is set in {@link android.R.attr#foregroundServiceType}
601      * attribute.
602      * @hide
603      */
604     @IntDef(flag = true, prefix = { "FOREGROUND_SERVICE_TYPE_" }, value = {
605             FOREGROUND_SERVICE_TYPE_MANIFEST,
606             FOREGROUND_SERVICE_TYPE_NONE,
607             FOREGROUND_SERVICE_TYPE_DATA_SYNC,
608             FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK,
609             FOREGROUND_SERVICE_TYPE_PHONE_CALL,
610             FOREGROUND_SERVICE_TYPE_LOCATION,
611             FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE,
612             FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION,
613             FOREGROUND_SERVICE_TYPE_CAMERA,
614             FOREGROUND_SERVICE_TYPE_MICROPHONE,
615             FOREGROUND_SERVICE_TYPE_HEALTH,
616             FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING,
617             FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED,
618             FOREGROUND_SERVICE_TYPE_SHORT_SERVICE,
619             FOREGROUND_SERVICE_TYPE_FILE_MANAGEMENT,
620             FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING,
621             FOREGROUND_SERVICE_TYPE_SPECIAL_USE,
622     })
623     @Retention(RetentionPolicy.SOURCE)
624     public @interface ForegroundServiceType {}
625 
626     /**
627      * The type of foreground service, set in
628      * {@link android.R.attr#foregroundServiceType} attribute by ORing flags in
629      * {@link ForegroundServiceType}
630      * @hide
631      */
632     public @ForegroundServiceType int mForegroundServiceType = FOREGROUND_SERVICE_TYPE_NONE;
633 
ServiceInfo()634     public ServiceInfo() {
635     }
636 
ServiceInfo(ServiceInfo orig)637     public ServiceInfo(ServiceInfo orig) {
638         super(orig);
639         permission = orig.permission;
640         flags = orig.flags;
641         mForegroundServiceType = orig.mForegroundServiceType;
642     }
643 
644     /**
645      * Return foreground service type specified in the manifest..
646      * @return foreground service type specified in the manifest.
647      */
getForegroundServiceType()648     public @ForegroundServiceType int getForegroundServiceType() {
649         return mForegroundServiceType;
650     }
651 
dump(Printer pw, String prefix)652     public void dump(Printer pw, String prefix) {
653         dump(pw, prefix, DUMP_FLAG_ALL);
654     }
655 
656     /** @hide */
dump(Printer pw, String prefix, int dumpFlags)657     void dump(Printer pw, String prefix, int dumpFlags) {
658         super.dumpFront(pw, prefix);
659         pw.println(prefix + "permission=" + permission);
660         pw.println(prefix + "flags=0x" + Integer.toHexString(flags));
661         super.dumpBack(pw, prefix, dumpFlags);
662     }
663 
toString()664     public String toString() {
665         return "ServiceInfo{"
666             + Integer.toHexString(System.identityHashCode(this))
667             + " " + name + "}";
668     }
669 
670     /**
671      * @return The label for the given foreground service type.
672      *
673      * @hide
674      */
foregroundServiceTypeToLabel(@oregroundServiceType int type)675     public static String foregroundServiceTypeToLabel(@ForegroundServiceType int type) {
676         switch (type) {
677             case FOREGROUND_SERVICE_TYPE_MANIFEST:
678                 return "manifest";
679             case FOREGROUND_SERVICE_TYPE_NONE:
680                 return "none";
681             case FOREGROUND_SERVICE_TYPE_DATA_SYNC:
682                 return "dataSync";
683             case FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK:
684                 return "mediaPlayback";
685             case FOREGROUND_SERVICE_TYPE_PHONE_CALL:
686                 return "phoneCall";
687             case FOREGROUND_SERVICE_TYPE_LOCATION:
688                 return "location";
689             case FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE:
690                 return "connectedDevice";
691             case FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION:
692                 return "mediaProjection";
693             case FOREGROUND_SERVICE_TYPE_CAMERA:
694                 return "camera";
695             case FOREGROUND_SERVICE_TYPE_MICROPHONE:
696                 return "microphone";
697             case FOREGROUND_SERVICE_TYPE_HEALTH:
698                 return "health";
699             case FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING:
700                 return "remoteMessaging";
701             case FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED:
702                 return "systemExempted";
703             case FOREGROUND_SERVICE_TYPE_SHORT_SERVICE:
704                 return "shortService";
705             case FOREGROUND_SERVICE_TYPE_FILE_MANAGEMENT:
706                 return "fileManagement";
707             case FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING:
708                 return "mediaProcessing";
709             case FOREGROUND_SERVICE_TYPE_SPECIAL_USE:
710                 return "specialUse";
711             default:
712                 return "unknown";
713         }
714     }
715 
describeContents()716     public int describeContents() {
717         return 0;
718     }
719 
writeToParcel(Parcel dest, int parcelableFlags)720     public void writeToParcel(Parcel dest, int parcelableFlags) {
721         super.writeToParcel(dest, parcelableFlags);
722         dest.writeString8(permission);
723         dest.writeInt(flags);
724         dest.writeInt(mForegroundServiceType);
725     }
726 
727     public static final @android.annotation.NonNull Creator<ServiceInfo> CREATOR =
728         new Creator<ServiceInfo>() {
729         public ServiceInfo createFromParcel(Parcel source) {
730             return new ServiceInfo(source);
731         }
732         public ServiceInfo[] newArray(int size) {
733             return new ServiceInfo[size];
734         }
735     };
736 
ServiceInfo(Parcel source)737     private ServiceInfo(Parcel source) {
738         super(source);
739         permission = source.readString8();
740         flags = source.readInt();
741         mForegroundServiceType = source.readInt();
742     }
743 }
744