• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.IntDef;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import android.util.Printer;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * Information you can retrieve about a particular application
29  * service. This corresponds to information collected from the
30  * AndroidManifest.xml's <service> tags.
31  */
32 public class ServiceInfo extends ComponentInfo
33         implements Parcelable {
34     /**
35      * Optional name of a permission required to be able to access this
36      * Service.  From the "permission" attribute.
37      */
38     public String permission;
39 
40     /**
41      * Bit in {@link #flags}: If set, the service will automatically be
42      * stopped by the system if the user removes a task that is rooted
43      * in one of the application's activities.  Set from the
44      * {@link android.R.attr#stopWithTask} attribute.
45      */
46     public static final int FLAG_STOP_WITH_TASK = 0x0001;
47 
48     /**
49      * Bit in {@link #flags}: If set, the service will run in its own
50      * isolated process.  Set from the
51      * {@link android.R.attr#isolatedProcess} attribute.
52      */
53     public static final int FLAG_ISOLATED_PROCESS = 0x0002;
54 
55     /**
56      * Bit in {@link #flags}: If set, the service can be bound and run in the
57      * calling application's package, rather than the package in which it is
58      * declared.  Set from {@link android.R.attr#externalService} attribute.
59      */
60     public static final int FLAG_EXTERNAL_SERVICE = 0x0004;
61 
62     /**
63      * Bit in {@link #flags}: If set, the service (which must be isolated)
64      * will be spawned from an Application Zygote, instead of the regular Zygote.
65      * The Application Zygote will pre-initialize the application's class loader,
66      * and call a static callback into the application to allow it to perform
67      * application-specific preloads (such as loading a shared library). Therefore,
68      * spawning from the Application Zygote will typically reduce the service
69      * launch time and reduce its memory usage. The downside of using this flag
70      * is that you will have an additional process (the app zygote itself) that
71      * is taking up memory. Whether actual memory usage is improved therefore
72      * strongly depends on the number of isolated services that an application
73      * starts, and how much memory those services save by preloading. Therefore,
74      * it is recommended to measure memory usage under typical workloads to
75      * determine whether it makes sense to use this flag.
76      */
77     public static final int FLAG_USE_APP_ZYGOTE = 0x0008;
78 
79     /**
80      * Bit in {@link #flags} indicating if the service is visible to ephemeral applications.
81      * @hide
82      */
83     public static final int FLAG_VISIBLE_TO_INSTANT_APP = 0x100000;
84 
85     /**
86      * Bit in {@link #flags}: If set, a single instance of the service will
87      * run for all users on the device.  Set from the
88      * {@link android.R.attr#singleUser} attribute.
89      */
90     public static final int FLAG_SINGLE_USER = 0x40000000;
91 
92     /**
93      * Options that have been set in the service declaration in the
94      * manifest.
95      * These include:
96      * {@link #FLAG_STOP_WITH_TASK}, {@link #FLAG_ISOLATED_PROCESS},
97      * {@link #FLAG_SINGLE_USER}.
98      */
99     public int flags;
100 
101     /**
102      * The default foreground service type if not been set in manifest file.
103      */
104     public static final int FOREGROUND_SERVICE_TYPE_NONE = 0;
105 
106     /**
107      * Constant corresponding to <code>dataSync</code> in
108      * the {@link android.R.attr#foregroundServiceType} attribute.
109      * Data(photo, file, account) upload/download, backup/restore, import/export, fetch,
110      * transfer over network between device and cloud.
111      */
112     public static final int FOREGROUND_SERVICE_TYPE_DATA_SYNC = 1 << 0;
113 
114     /**
115      * Constant corresponding to <code>mediaPlayback</code> in
116      * the {@link android.R.attr#foregroundServiceType} attribute.
117      * Music, video, news or other media playback.
118      */
119     public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK = 1 << 1;
120 
121     /**
122      * Constant corresponding to <code>phoneCall</code> in
123      * the {@link android.R.attr#foregroundServiceType} attribute.
124      * Ongoing operations related to phone calls, video conferencing,
125      * or similar interactive communication.
126      */
127     public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL = 1 << 2;
128 
129     /**
130      * Constant corresponding to <code>location</code> in
131      * the {@link android.R.attr#foregroundServiceType} attribute.
132      * GPS, map, navigation location update.
133      */
134     public static final int FOREGROUND_SERVICE_TYPE_LOCATION = 1 << 3;
135 
136     /**
137      * Constant corresponding to <code>connectedDevice</code> in
138      * the {@link android.R.attr#foregroundServiceType} attribute.
139      * Auto, bluetooth, TV or other devices connection, monitoring and interaction.
140      */
141     public static final int FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE = 1 << 4;
142 
143     /**
144      * Constant corresponding to {@code mediaProjection} in
145      * the {@link android.R.attr#foregroundServiceType} attribute.
146      * Managing a media projection session, e.g for screen recording or taking screenshots.
147      */
148     public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION = 1 << 5;
149 
150     /**
151      * Constant corresponding to {@code camera} in
152      * the {@link android.R.attr#foregroundServiceType} attribute.
153      * Use the camera device or record video.
154      * For apps with <code>targetSdkVersion</code> {@link android.os.Build.VERSION_CODES#R} and
155      * above, a foreground service will not be able to access the camera if this type is not
156      * specified in the manifest and in
157      * {@link android.app.Service#startForeground(int, android.app.Notification, int)}.
158      */
159     public static final int FOREGROUND_SERVICE_TYPE_CAMERA = 1 << 6;
160 
161     /**
162      * Constant corresponding to {@code microphone} in
163      * the {@link android.R.attr#foregroundServiceType} attribute.
164      * Use the microphone device or record audio.
165      * For apps with <code>targetSdkVersion</code> {@link android.os.Build.VERSION_CODES#R} and
166      * above, a foreground service will not be able to access the microphone if this type is not
167      * specified in the manifest and in
168      * {@link android.app.Service#startForeground(int, android.app.Notification, int)}.
169      */
170     public static final int FOREGROUND_SERVICE_TYPE_MICROPHONE = 1 << 7;
171 
172     /**
173      * The number of foreground service types, this doesn't include
174      * the {@link #FOREGROUND_SERVICE_TYPE_MANIFEST} and {@link #FOREGROUND_SERVICE_TYPE_NONE}
175      * as they're not real service types.
176      *
177      * @hide
178      */
179     public static final int NUM_OF_FOREGROUND_SERVICE_TYPES = 8;
180 
181     /**
182      * A special value indicates to use all types set in manifest file.
183      */
184     public static final int FOREGROUND_SERVICE_TYPE_MANIFEST = -1;
185 
186     /**
187      * The set of flags for foreground service type.
188      * The foreground service type is set in {@link android.R.attr#foregroundServiceType}
189      * attribute.
190      * @hide
191      */
192     @IntDef(flag = true, prefix = { "FOREGROUND_SERVICE_TYPE_" }, value = {
193             FOREGROUND_SERVICE_TYPE_MANIFEST,
194             FOREGROUND_SERVICE_TYPE_NONE,
195             FOREGROUND_SERVICE_TYPE_DATA_SYNC,
196             FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK,
197             FOREGROUND_SERVICE_TYPE_PHONE_CALL,
198             FOREGROUND_SERVICE_TYPE_LOCATION,
199             FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE,
200             FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION,
201             FOREGROUND_SERVICE_TYPE_CAMERA,
202             FOREGROUND_SERVICE_TYPE_MICROPHONE
203     })
204     @Retention(RetentionPolicy.SOURCE)
205     public @interface ForegroundServiceType {}
206 
207     /**
208      * The type of foreground service, set in
209      * {@link android.R.attr#foregroundServiceType} attribute by ORing flags in
210      * {@link ForegroundServiceType}
211      * @hide
212      */
213     public @ForegroundServiceType int mForegroundServiceType = FOREGROUND_SERVICE_TYPE_NONE;
214 
ServiceInfo()215     public ServiceInfo() {
216     }
217 
ServiceInfo(ServiceInfo orig)218     public ServiceInfo(ServiceInfo orig) {
219         super(orig);
220         permission = orig.permission;
221         flags = orig.flags;
222         mForegroundServiceType = orig.mForegroundServiceType;
223     }
224 
225     /**
226      * Return foreground service type specified in the manifest..
227      * @return foreground service type specified in the manifest.
228      */
getForegroundServiceType()229     public @ForegroundServiceType int getForegroundServiceType() {
230         return mForegroundServiceType;
231     }
232 
dump(Printer pw, String prefix)233     public void dump(Printer pw, String prefix) {
234         dump(pw, prefix, DUMP_FLAG_ALL);
235     }
236 
237     /** @hide */
dump(Printer pw, String prefix, int dumpFlags)238     void dump(Printer pw, String prefix, int dumpFlags) {
239         super.dumpFront(pw, prefix);
240         pw.println(prefix + "permission=" + permission);
241         pw.println(prefix + "flags=0x" + Integer.toHexString(flags));
242         super.dumpBack(pw, prefix, dumpFlags);
243     }
244 
toString()245     public String toString() {
246         return "ServiceInfo{"
247             + Integer.toHexString(System.identityHashCode(this))
248             + " " + name + "}";
249     }
250 
251     /**
252      * @return The label for the given foreground service type.
253      *
254      * @hide
255      */
foregroundServiceTypeToLabel(@oregroundServiceType int type)256     public static String foregroundServiceTypeToLabel(@ForegroundServiceType int type) {
257         switch (type) {
258             case FOREGROUND_SERVICE_TYPE_MANIFEST:
259                 return "manifest";
260             case FOREGROUND_SERVICE_TYPE_NONE:
261                 return "none";
262             case FOREGROUND_SERVICE_TYPE_DATA_SYNC:
263                 return "dataSync";
264             case FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK:
265                 return "mediaPlayback";
266             case FOREGROUND_SERVICE_TYPE_PHONE_CALL:
267                 return "phoneCall";
268             case FOREGROUND_SERVICE_TYPE_LOCATION:
269                 return "location";
270             case FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE:
271                 return "connectedDevice";
272             case FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION:
273                 return "mediaProjection";
274             case FOREGROUND_SERVICE_TYPE_CAMERA:
275                 return "camera";
276             case FOREGROUND_SERVICE_TYPE_MICROPHONE:
277                 return "microphone";
278             default:
279                 return "unknown";
280         }
281     }
282 
describeContents()283     public int describeContents() {
284         return 0;
285     }
286 
writeToParcel(Parcel dest, int parcelableFlags)287     public void writeToParcel(Parcel dest, int parcelableFlags) {
288         super.writeToParcel(dest, parcelableFlags);
289         dest.writeString8(permission);
290         dest.writeInt(flags);
291         dest.writeInt(mForegroundServiceType);
292     }
293 
294     public static final @android.annotation.NonNull Creator<ServiceInfo> CREATOR =
295         new Creator<ServiceInfo>() {
296         public ServiceInfo createFromParcel(Parcel source) {
297             return new ServiceInfo(source);
298         }
299         public ServiceInfo[] newArray(int size) {
300             return new ServiceInfo[size];
301         }
302     };
303 
ServiceInfo(Parcel source)304     private ServiceInfo(Parcel source) {
305         super(source);
306         permission = source.readString8();
307         flags = source.readInt();
308         mForegroundServiceType = source.readInt();
309     }
310 }
311