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.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ResolveInfo;
24 import android.net.Uri;
25 import android.telephony.MbmsDownloadSession;
26 import android.telephony.mbms.MbmsDownloadReceiver;
27 
28 import java.io.File;
29 import 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
37 public class VendorUtils {
38 
39     /**
40      * The MBMS middleware should send this when a download of single file has completed or
41      * failed. The only mandatory extra is
42      * {@link MbmsDownloadSession#EXTRA_MBMS_DOWNLOAD_RESULT}
43      * and the following are required when the download has completed:
44      * {@link MbmsDownloadSession#EXTRA_MBMS_FILE_INFO}
45      * {@link MbmsDownloadSession#EXTRA_MBMS_DOWNLOAD_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 a single {@link Uri} indicating the path to the temp file in which the
126      * decoded downloaded file resides. Must not be null.
127      */
128     public static final String EXTRA_FINAL_URI = "android.telephony.mbms.extra.FINAL_URI";
129 
130     /**
131      * Extra containing a String representing a service ID, used by
132      * file-descriptor requests and cleanup requests to specify which service they want to
133      * request temp files or clean up temp files for, respectively.
134      */
135     public static final String EXTRA_SERVICE_ID =
136             "android.telephony.mbms.extra.SERVICE_ID";
137 
138     /**
139      * Retrieves the {@link ComponentName} for the {@link android.content.BroadcastReceiver} that
140      * the various intents from the middleware should be targeted towards.
141      * @param packageName The package name of the app.
142      * @return The component name of the receiver that the middleware should send its intents to,
143      * or null if the app didn't declare it in the manifest.
144      */
getAppReceiverFromPackageName(Context context, String packageName)145     public static ComponentName getAppReceiverFromPackageName(Context context, String packageName) {
146         ComponentName candidate = new ComponentName(packageName,
147                 MbmsDownloadReceiver.class.getCanonicalName());
148         Intent queryIntent = new Intent();
149         queryIntent.setComponent(candidate);
150         List<ResolveInfo> receivers =
151                 context.getPackageManager().queryBroadcastReceivers(queryIntent, 0);
152         if (receivers != null && receivers.size() > 0) {
153             return candidate;
154         }
155         return null;
156     }
157 }
158