1/*
2 * Copyright (C) 2017 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 android.telephony.mbms;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.content.pm.*;
24import android.content.pm.ServiceInfo;
25import android.util.Log;
26
27import java.io.File;
28import java.io.IOException;
29import java.util.List;
30
31/**
32 * @hide
33 */
34public class MbmsUtils {
35    private static final String LOG_TAG = "MbmsUtils";
36
37    public static boolean isContainedIn(File parent, File child) {
38        try {
39            String parentPath = parent.getCanonicalPath();
40            String childPath = child.getCanonicalPath();
41            return childPath.startsWith(parentPath);
42        } catch (IOException e) {
43            throw new RuntimeException("Failed to resolve canonical paths: " + e);
44        }
45    }
46
47    public static ComponentName toComponentName(ComponentInfo ci) {
48        return new ComponentName(ci.packageName, ci.name);
49    }
50
51    public static ServiceInfo getMiddlewareServiceInfo(Context context, String serviceAction) {
52        // Query for the proper service
53        PackageManager packageManager = context.getPackageManager();
54        Intent queryIntent = new Intent();
55        queryIntent.setAction(serviceAction);
56        List<ResolveInfo> downloadServices = packageManager.queryIntentServices(queryIntent,
57                PackageManager.MATCH_SYSTEM_ONLY);
58
59        if (downloadServices == null || downloadServices.size() == 0) {
60            Log.w(LOG_TAG, "No download services found, cannot get service info");
61            return null;
62        }
63
64        if (downloadServices.size() > 1) {
65            Log.w(LOG_TAG, "More than one download service found, cannot get unique service");
66            return null;
67        }
68        return downloadServices.get(0).serviceInfo;
69    }
70
71    public static int startBinding(Context context, String serviceAction,
72            ServiceConnection serviceConnection) {
73        Intent bindIntent = new Intent();
74        ServiceInfo mbmsServiceInfo =
75                MbmsUtils.getMiddlewareServiceInfo(context, serviceAction);
76
77        if (mbmsServiceInfo == null) {
78            return MbmsErrors.ERROR_NO_UNIQUE_MIDDLEWARE;
79        }
80
81        bindIntent.setComponent(MbmsUtils.toComponentName(mbmsServiceInfo));
82
83        context.bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
84        return MbmsErrors.SUCCESS;
85    }
86
87    /**
88     * Returns a File linked to the directory used to store temp files for this file service
89     */
90    public static File getEmbmsTempFileDirForService(Context context, String serviceId) {
91        File embmsTempFileDir = MbmsTempFileProvider.getEmbmsTempFileDir(context);
92
93        return new File(embmsTempFileDir, serviceId);
94    }
95}
96