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