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