VendorUtils.java revision 571293ad96eae0a10d61fa2bd9e78f8cbb78803e
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.vendor;
18
19import android.annotation.SystemApi;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ResolveInfo;
24import android.net.Uri;
25import android.telephony.mbms.DownloadRequest;
26import android.telephony.mbms.MbmsDownloadReceiver;
27
28import java.io.File;
29import java.util.List;
30
31/**
32 * Contains constants and utility methods for MBMS Download middleware apps to communicate with
33 * frontend apps.
34 * @hide
35 */
36//@SystemApi
37public class VendorUtils {
38
39    /**
40     * The MBMS middleware should send this when a download of single file has completed or
41     * failed. Mandatory extras are
42     * {@link android.telephony.MbmsDownloadManager#EXTRA_RESULT}
43     * {@link android.telephony.MbmsDownloadManager#EXTRA_FILE_INFO}
44     * {@link #EXTRA_REQUEST}
45     * {@link #EXTRA_TEMP_LIST}
46     * {@link #EXTRA_FINAL_URI}
47     */
48    public static final String ACTION_DOWNLOAD_RESULT_INTERNAL =
49            "android.telephony.mbms.action.DOWNLOAD_RESULT_INTERNAL";
50
51    /**
52     * The MBMS middleware should send this when it wishes to request {@code content://} URIs to
53     * serve as temp files for downloads or when it wishes to resume paused downloads. Mandatory
54     * extras are
55     * {@link #EXTRA_SERVICE_ID}
56     *
57     * Optional extras are
58     * {@link #EXTRA_FD_COUNT} (0 if not present)
59     * {@link #EXTRA_PAUSED_LIST} (empty if not present)
60     */
61    public static final String ACTION_FILE_DESCRIPTOR_REQUEST =
62            "android.telephony.mbms.action.FILE_DESCRIPTOR_REQUEST";
63
64    /**
65     * The MBMS middleware should send this when it wishes to clean up temp  files in the app's
66     * filesystem. Mandatory extras are:
67     * {@link #EXTRA_TEMP_FILES_IN_USE}
68     */
69    public static final String ACTION_CLEANUP =
70            "android.telephony.mbms.action.CLEANUP";
71
72    /**
73     * Extra containing a {@link List} of {@link Uri}s that were used as temp files for this
74     * completed file. These {@link Uri}s should have scheme {@code file://}, and the temp
75     * files will be deleted upon receipt of the intent.
76     * May be null.
77     */
78    public static final String EXTRA_TEMP_LIST = "android.telephony.mbms.extra.TEMP_LIST";
79
80    /**
81     * Extra containing an integer indicating the number of temp files requested.
82     */
83    public static final String EXTRA_FD_COUNT = "android.telephony.mbms.extra.FD_COUNT";
84
85    /**
86     * Extra containing a list of {@link Uri}s that the middleware is requesting access to via
87     * {@link #ACTION_FILE_DESCRIPTOR_REQUEST} in order to resume downloading. These {@link Uri}s
88     * should have scheme {@code file://}.
89     */
90    public static final String EXTRA_PAUSED_LIST = "android.telephony.mbms.extra.PAUSED_LIST";
91
92    /**
93     * Extra containing a list of {@link android.telephony.mbms.UriPathPair}s, used in the
94     * response to {@link #ACTION_FILE_DESCRIPTOR_REQUEST}. These are temp files that are meant
95     * to be used for new file downloads.
96     */
97    public static final String EXTRA_FREE_URI_LIST = "android.telephony.mbms.extra.FREE_URI_LIST";
98
99    /**
100     * Extra containing a list of {@link android.telephony.mbms.UriPathPair}s, used in the
101     * response to {@link #ACTION_FILE_DESCRIPTOR_REQUEST}. These
102     * {@link android.telephony.mbms.UriPathPair}s contain {@code content://} URIs that provide
103     * access to previously paused downloads.
104     */
105    public static final String EXTRA_PAUSED_URI_LIST =
106            "android.telephony.mbms.extra.PAUSED_URI_LIST";
107
108    /**
109     * Extra containing a string that points to the middleware's knowledge of where the temp file
110     * root for the app is. The path should be a canonical path as returned by
111     * {@link File#getCanonicalPath()}
112     */
113    public static final String EXTRA_TEMP_FILE_ROOT =
114            "android.telephony.mbms.extra.TEMP_FILE_ROOT";
115
116    /**
117     * Extra containing a list of {@link Uri}s indicating temp files which the middleware is
118     * still using.
119     */
120    public static final String EXTRA_TEMP_FILES_IN_USE =
121            "android.telephony.mbms.extra.TEMP_FILES_IN_USE";
122
123    /**
124     * Extra containing the {@link DownloadRequest} for which the download result or file
125     * descriptor request is for. Must not be null.
126     */
127    public static final String EXTRA_REQUEST = "android.telephony.mbms.extra.REQUEST";
128
129    /**
130     * Extra containing a single {@link Uri} indicating the path to the temp file in which the
131     * decoded downloaded file resides. Must not be null.
132     */
133    public static final String EXTRA_FINAL_URI = "android.telephony.mbms.extra.FINAL_URI";
134
135    /**
136     * Extra containing a String representing a service ID, used by
137     * file-descriptor requests and cleanup requests to specify which service they want to
138     * request temp files or clean up temp files for, respectively.
139     */
140    public static final String EXTRA_SERVICE_ID =
141            "android.telephony.mbms.extra.SERVICE_ID";
142
143    /**
144     * Retrieves the {@link ComponentName} for the {@link android.content.BroadcastReceiver} that
145     * the various intents from the middleware should be targeted towards.
146     * @param packageName The package name of the app.
147     * @return The component name of the receiver that the middleware should send its intents to,
148     * or null if the app didn't declare it in the manifest.
149     */
150    public static ComponentName getAppReceiverFromPackageName(Context context, String packageName) {
151        ComponentName candidate = new ComponentName(packageName,
152                MbmsDownloadReceiver.class.getCanonicalName());
153        Intent queryIntent = new Intent();
154        queryIntent.setComponent(candidate);
155        List<ResolveInfo> receivers =
156                context.getPackageManager().queryBroadcastReceivers(queryIntent, 0);
157        if (receivers != null && receivers.size() > 0) {
158            return candidate;
159        }
160        return null;
161    }
162}
163