1 /* 2 * Copyright (C) 2015 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 androidx.appcompat.mms; 18 19 import android.app.PendingIntent; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.os.Parcel; 26 import android.os.ParcelFileDescriptor; 27 import android.os.Parcelable; 28 import android.text.TextUtils; 29 import android.util.Log; 30 31 import java.io.IOException; 32 import java.util.concurrent.Callable; 33 import java.util.concurrent.Future; 34 import java.util.concurrent.TimeUnit; 35 36 /** 37 * Request to download an MMS 38 */ 39 class DownloadRequest extends MmsRequest { 40 DownloadRequest(final String locationUrl, final Uri pduUri, final PendingIntent sentIntent)41 DownloadRequest(final String locationUrl, final Uri pduUri, 42 final PendingIntent sentIntent) { 43 super(locationUrl, pduUri, sentIntent); 44 } 45 46 @Override loadRequest(final Context context, final Bundle mmsConfig)47 protected boolean loadRequest(final Context context, final Bundle mmsConfig) { 48 // No need to load PDU from app. Always true. 49 return true; 50 } 51 52 @Override transferResponse(Context context, Intent fillIn, byte[] response)53 protected boolean transferResponse(Context context, Intent fillIn, byte[] response) { 54 return writePduToContentUri(context, mPduUri, response); 55 } 56 57 @Override doHttp(Context context, MmsNetworkManager netMgr, ApnSettingsLoader.Apn apn, Bundle mmsConfig, String userAgent, String uaProfUrl)58 protected byte[] doHttp(Context context, MmsNetworkManager netMgr, ApnSettingsLoader.Apn apn, 59 Bundle mmsConfig, String userAgent, String uaProfUrl) throws MmsHttpException { 60 final MmsHttpClient httpClient = netMgr.getHttpClient(); 61 return httpClient.execute(getHttpRequestUrl(apn), null/*pdu*/, MmsHttpClient.METHOD_GET, 62 !TextUtils.isEmpty(apn.getMmsProxy()), apn.getMmsProxy(), apn.getMmsProxyPort(), 63 mmsConfig, userAgent, uaProfUrl); 64 65 } 66 67 @Override getHttpRequestUrl(final ApnSettingsLoader.Apn apn)68 protected String getHttpRequestUrl(final ApnSettingsLoader.Apn apn) { 69 return mLocationUrl; 70 } 71 72 /** 73 * Write pdu bytes to content provider uri 74 * 75 * @param contentUri content provider uri to which bytes should be written 76 * @param pdu Bytes to write 77 * @return true if all bytes successfully written else false 78 */ writePduToContentUri(final Context context, final Uri contentUri, final byte[] pdu)79 public boolean writePduToContentUri(final Context context, final Uri contentUri, 80 final byte[] pdu) { 81 if (contentUri == null || pdu == null) { 82 return false; 83 } 84 final Callable<Boolean> copyDownloadedPduToOutput = new Callable<Boolean>() { 85 public Boolean call() { 86 ParcelFileDescriptor.AutoCloseOutputStream outStream = null; 87 try { 88 final ContentResolver cr = context.getContentResolver(); 89 final ParcelFileDescriptor pduFd = cr.openFileDescriptor(contentUri, "w"); 90 outStream = new ParcelFileDescriptor.AutoCloseOutputStream(pduFd); 91 outStream.write(pdu); 92 return true; 93 } catch (IOException e) { 94 Log.e(MmsService.TAG, "Writing PDU to downloader: IO exception", e); 95 return false; 96 } finally { 97 if (outStream != null) { 98 try { 99 outStream.close(); 100 } catch (IOException ex) { 101 // Ignore 102 } 103 } 104 } 105 } 106 }; 107 final Future<Boolean> pendingResult = 108 mPduTransferExecutor.submit(copyDownloadedPduToOutput); 109 try { 110 return pendingResult.get(TASK_TIMEOUT_MS, TimeUnit.MILLISECONDS); 111 } catch (Exception e) { 112 // Typically a timeout occurred - cancel task 113 pendingResult.cancel(true); 114 } 115 return false; 116 } 117 118 public static final Parcelable.Creator<DownloadRequest> CREATOR 119 = new Parcelable.Creator<DownloadRequest>() { 120 public DownloadRequest createFromParcel(Parcel in) { 121 return new DownloadRequest(in); 122 } 123 124 public DownloadRequest[] newArray(int size) { 125 return new DownloadRequest[size]; 126 } 127 }; 128 DownloadRequest(Parcel in)129 private DownloadRequest(Parcel in) { 130 super(in); 131 } 132 } 133