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