DownloadRequest.java revision 8c027a60c84d23672647a3775190ee3fa7655b34
1/* 2 * Copyright (C) 2014 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 com.android.mms.service; 18 19import com.google.android.mms.MmsException; 20import com.google.android.mms.pdu.GenericPdu; 21import com.google.android.mms.pdu.PduHeaders; 22import com.google.android.mms.pdu.PduParser; 23import com.google.android.mms.pdu.PduPersister; 24import com.google.android.mms.pdu.RetrieveConf; 25import com.google.android.mms.util.SqliteWrapper; 26 27import com.android.mms.service.exception.MmsHttpException; 28 29import android.app.Activity; 30import android.app.AppOpsManager; 31import android.app.PendingIntent; 32import android.content.ContentValues; 33import android.content.Context; 34import android.content.Intent; 35import android.database.sqlite.SQLiteException; 36import android.os.Binder; 37import android.os.UserHandle; 38import android.provider.Telephony; 39import android.text.TextUtils; 40import android.util.Log; 41 42/** 43 * Request to download an MMS 44 */ 45public class DownloadRequest extends MmsRequest { 46 private static final String LOCATION_SELECTION = 47 Telephony.Mms.MESSAGE_TYPE + "=? AND " + Telephony.Mms.CONTENT_LOCATION + " =?"; 48 49 private final String mLocationUrl; 50 private final PendingIntent mDownloadedIntent; 51 52 public DownloadRequest(RequestManager manager, long subId, String locationUrl, 53 PendingIntent downloadedIntent, String creator) { 54 super(manager, null/*messageUri*/, subId, creator); 55 mLocationUrl = locationUrl; 56 mDownloadedIntent = downloadedIntent; 57 } 58 59 @Override 60 protected byte[] doHttp(Context context, ApnSettings apn) throws MmsHttpException { 61 return HttpUtils.httpConnection( 62 context, 63 mLocationUrl, 64 null/*pdu*/, 65 HttpUtils.HTTP_GET_METHOD, 66 apn.isProxySet(), 67 apn.getProxyAddress(), 68 apn.getProxyPort()); 69 } 70 71 @Override 72 protected PendingIntent getPendingIntent() { 73 return mDownloadedIntent; 74 } 75 76 @Override 77 protected int getRunningQueue() { 78 return MmsService.QUEUE_INDEX_DOWNLOAD; 79 } 80 81 @Override 82 protected void updateStatus(Context context, int result, byte[] response) { 83 if (mRequestManager.getAutoPersistingPref()) { 84 storeInboxMessage(context, result, response); 85 } 86 } 87 88 private void storeInboxMessage(Context context, int result, byte[] response) { 89 if (response == null || response.length < 1) { 90 return; 91 } 92 final long identity = Binder.clearCallingIdentity(); 93 try { 94 final GenericPdu pdu = (new PduParser(response)).parse(); 95 if (pdu == null || !(pdu instanceof RetrieveConf)) { 96 Log.e(MmsService.TAG, "DownloadRequest.updateStatus: invalid parsed PDU"); 97 return; 98 } 99 // Store the downloaded message 100 final PduPersister persister = PduPersister.getPduPersister(context); 101 mMessageUri = persister.persist( 102 pdu, 103 Telephony.Mms.Inbox.CONTENT_URI, 104 true/*createThreadId*/, 105 true/*groupMmsEnabled*/, 106 null/*preOpenedFiles*/); 107 if (mMessageUri == null) { 108 Log.e(MmsService.TAG, "DownloadRequest.updateStatus: can not persist message"); 109 return; 110 } 111 // Update some of the properties of the message 112 ContentValues values = new ContentValues(4); 113 values.put(Telephony.Mms.DATE, System.currentTimeMillis() / 1000L); 114 values.put(Telephony.Mms.READ, 0); 115 values.put(Telephony.Mms.SEEN, 0); 116 if (!TextUtils.isEmpty(mCreator)) { 117 values.put(Telephony.Mms.CREATOR, mCreator); 118 } 119 if (SqliteWrapper.update( 120 context, 121 context.getContentResolver(), 122 mMessageUri, 123 values, 124 null/*where*/, 125 null/*selectionArg*/) != 1) { 126 Log.e(MmsService.TAG, "DownloadRequest.updateStatus: can not update message"); 127 } 128 // Delete the corresponding NotificationInd 129 SqliteWrapper.delete(context, 130 context.getContentResolver(), 131 Telephony.Mms.CONTENT_URI, 132 LOCATION_SELECTION, 133 new String[]{ 134 Integer.toString(PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND), 135 mLocationUrl 136 } 137 ); 138 } catch (MmsException e) { 139 Log.e(MmsService.TAG, "DownloadRequest.updateStatus: can not persist message", e); 140 } catch (SQLiteException e) { 141 Log.e(MmsService.TAG, "DownloadRequest.updateStatus: can not update message", e); 142 } catch (RuntimeException e) { 143 Log.e(MmsService.TAG, "DownloadRequest.updateStatus: can not parse response", e); 144 } finally { 145 Binder.restoreCallingIdentity(identity); 146 } 147 } 148 149 /** 150 * Try downloading via the carrier app by sending intent. 151 * 152 * @param context The context 153 */ 154 public void tryDownloadingByCarrierApp(Context context) { 155 Intent intent = new Intent(Telephony.Mms.Intents.MMS_DOWNLOAD_ACTION); 156 intent.setPackage(mRequestManager.getCarrierAppPackageName(intent)); 157 intent.putExtra("url", mLocationUrl); 158 intent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT); 159 context.sendOrderedBroadcastAsUser( 160 intent, 161 UserHandle.OWNER, 162 android.Manifest.permission.RECEIVE_MMS, 163 AppOpsManager.OP_RECEIVE_MMS, 164 mCarrierAppResultReceiver, 165 null/*scheduler*/, 166 Activity.RESULT_CANCELED, 167 null/*initialData*/, 168 null/*initialExtras*/); 169 } 170} 171