blob: fcbaecf7e059dcefe9ba41daae3104d252947abe [file] [log] [blame]
Makoto Onuki87d260a2018-09-26 16:58:32 -07001/*
2 * Copyright (C) 2018 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
17package com.android.server.appbinding;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.content.Intent;
22import android.content.pm.IPackageManager;
23import android.content.pm.ParceledListSlice;
24import android.content.pm.ResolveInfo;
25import android.content.pm.ServiceInfo;
26import android.os.RemoteException;
27import android.util.Log;
28
29import java.util.List;
30
31/**
32 * Utility class to find a persistent bound service within an app.
33 */
34public class AppBindingUtils {
35 private static final String TAG = "AppBindingUtils";
36 private AppBindingUtils() {
37 }
38
39 /**
40 * Find a service with the action {@code serviceAction} in the package {@code packageName}.
41 * Returns null in any of the following cases.
42 * - No service with the action is found.
43 * - More than 1 service with the action is found.
44 * - Found service is not protected with the permission {@code servicePermission}.
45 */
46 @Nullable
47 public static ServiceInfo findService(@NonNull String packageName, int userId,
48 String serviceAction, String servicePermission,
49 Class<?> serviceClassForLogging,
50 IPackageManager ipm,
51 StringBuilder errorMessage) {
52 final String simpleClassName = serviceClassForLogging.getSimpleName();
53 final Intent intent = new Intent(serviceAction);
54 intent.setPackage(packageName);
55
56 errorMessage.setLength(0); // Clear it.
57 try {
58 final ParceledListSlice<ResolveInfo> pls = ipm
59 .queryIntentServices(intent, null, /* flags=*/ 0, userId);
60 if (pls == null || pls.getList().size() == 0) {
61 errorMessage.append("Service with " + serviceAction + " not found.");
62 return null;
63 }
64 final List<ResolveInfo> list = pls.getList();
65 // Note if multiple services are found, that's an error, even if only one of them
66 // is exported.
67 if (list.size() > 1) {
68 errorMessage.append("More than one " + simpleClassName + "'s found in package "
69 + packageName + ". They'll all be ignored.");
70 Log.e(TAG, errorMessage.toString());
71 return null;
72 }
73 final ServiceInfo si = list.get(0).serviceInfo;
74
75 if (!servicePermission.equals(si.permission)) {
76 errorMessage.append(simpleClassName + " "
77 + si.getComponentName().flattenToShortString()
78 + " must be protected with " + servicePermission
79 + ".");
80 Log.e(TAG, errorMessage.toString());
81 return null;
82 }
83 return si;
84 } catch (RemoteException e) {
85 // Shouldn't happen
86 }
87 return null;
88 }
89}