1 /*
2  * Copyright (C) 2019 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;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemService;
22 import android.app.ActivityThread;
23 import android.app.PendingIntent;
24 import android.content.Context;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.os.RemoteException;
28 import android.os.ServiceManager;
29 
30 import com.android.internal.telephony.IMms;
31 
32 /**
33  * Manages MMS operations such as sending multimedia messages.
34  * Get this object by calling Context#getSystemService(Context#MMS_SERVICE).
35  * @hide
36  */
37 @SystemService(Context.MMS_SERVICE)
38 public class MmsManager {
39     private static final String TAG = "MmsManager";
40     private final Context mContext;
41 
42     /**
43      * @hide
44      */
MmsManager(@onNull Context context)45     public MmsManager(@NonNull Context context) {
46         mContext = context;
47     }
48 
49     /**
50      * Send an MMS message
51      *
52      * @param subId the subscription id
53      * @param contentUri the content Uri from which the message pdu will be read
54      * @param locationUrl the optional location url where message should be sent to
55      * @param configOverrides the carrier-specific messaging configuration values to override for
56      *                        sending the message.
57      * @param sentIntent if not NULL this <code>PendingIntent</code> is broadcast when the message
58      *                   is successfully sent, or failed
59      * @param messageId an id that uniquely identifies the message requested to be sent.
60      *                  Used for logging and diagnostics purposes. The id may be 0. The messageId
61      *                  can be found in radio logs from logcat.
62      */
sendMultimediaMessage(int subId, @NonNull Uri contentUri, @Nullable String locationUrl, @Nullable Bundle configOverrides, @Nullable PendingIntent sentIntent, long messageId)63     public void sendMultimediaMessage(int subId, @NonNull Uri contentUri,
64             @Nullable String locationUrl, @Nullable Bundle configOverrides,
65             @Nullable PendingIntent sentIntent, long messageId) {
66         try {
67             final IMms iMms = IMms.Stub.asInterface(ServiceManager.getService("imms"));
68             if (iMms == null) {
69                 return;
70             }
71 
72             iMms.sendMessage(subId, ActivityThread.currentPackageName(), contentUri,
73                     locationUrl, configOverrides, sentIntent, messageId,
74                     mContext.getAttributionTag());
75         } catch (RemoteException e) {
76             // Ignore it
77         }
78     }
79 
80     /**
81      * Download an MMS message from carrier by a given location URL
82      *
83      * @param subId the subscription id
84      * @param locationUrl the location URL of the MMS message to be downloaded, usually obtained
85      *  from the MMS WAP push notification
86      * @param contentUri the content uri to which the downloaded pdu will be written
87      * @param configOverrides the carrier-specific messaging configuration values to override for
88      *  downloading the message.
89      * @param downloadedIntent if not NULL this <code>PendingIntent</code> is
90      *  broadcast when the message is downloaded, or the download is failed
91      * @param messageId an id that uniquely identifies the message requested to be downloaded.
92      *                  Used for logging and diagnostics purposes. The id may be 0. The messageId
93      *                  can be found in radio logs from logcat.
94      * @throws IllegalArgumentException if locationUrl or contentUri is empty
95      */
downloadMultimediaMessage(int subId, @NonNull String locationUrl, @NonNull Uri contentUri, @Nullable Bundle configOverrides, @Nullable PendingIntent downloadedIntent, long messageId)96     public void downloadMultimediaMessage(int subId, @NonNull String locationUrl,
97             @NonNull Uri contentUri, @Nullable Bundle configOverrides,
98             @Nullable PendingIntent downloadedIntent, long messageId) {
99         try {
100             final IMms iMms = IMms.Stub.asInterface(ServiceManager.getService("imms"));
101             if (iMms == null) {
102                 return;
103             }
104             iMms.downloadMessage(subId, ActivityThread.currentPackageName(),
105                     locationUrl, contentUri, configOverrides, downloadedIntent,
106                     messageId, mContext.getAttributionTag());
107         } catch (RemoteException e) {
108             // Ignore it
109         }
110     }
111 }
112