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