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 package com.android.messaging.datamodel.media; 17 18 import android.content.Context; 19 import android.graphics.Bitmap; 20 import android.graphics.BitmapFactory; 21 22 import com.android.messaging.Factory; 23 import com.android.messaging.util.Assert; 24 import com.android.messaging.util.ContentType; 25 import com.android.messaging.util.LogUtil; 26 27 import java.io.FileNotFoundException; 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.net.HttpURLConnection; 31 import java.net.MalformedURLException; 32 import java.net.URL; 33 34 /** 35 * Serves network content URI based image requests. 36 */ 37 public class NetworkUriImageRequest<D extends UriImageRequestDescriptor> extends 38 ImageRequest<D> { 39 NetworkUriImageRequest(Context context, D descriptor)40 public NetworkUriImageRequest(Context context, D descriptor) { 41 super(context, descriptor); 42 mOrientation = android.media.ExifInterface.ORIENTATION_UNDEFINED; 43 } 44 45 @Override getInputStreamForResource()46 protected InputStream getInputStreamForResource() throws FileNotFoundException { 47 Assert.isNotMainThread(); 48 // Since we need to have an open urlConnection to get the stream, but we don't want to keep 49 // that connection open. There is no good way to perform this method. 50 return null; 51 } 52 53 @Override isGif()54 protected boolean isGif() throws FileNotFoundException { 55 Assert.isNotMainThread(); 56 57 HttpURLConnection connection = null; 58 try { 59 final URL url = new URL(mDescriptor.uri.toString()); 60 connection = (HttpURLConnection) url.openConnection(); 61 connection.connect(); 62 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 63 return ContentType.IMAGE_GIF.equalsIgnoreCase(connection.getContentType()); 64 } 65 } catch (MalformedURLException e) { 66 LogUtil.e(LogUtil.BUGLE_TAG, 67 "MalformedUrl for image with url: " 68 + mDescriptor.uri.toString(), e); 69 } catch (IOException e) { 70 LogUtil.e(LogUtil.BUGLE_TAG, 71 "IOException trying to get inputStream for image with url: " 72 + mDescriptor.uri.toString(), e); 73 } finally { 74 if (connection != null) { 75 connection.disconnect(); 76 } 77 } 78 return false; 79 } 80 81 @SuppressWarnings("deprecation") 82 @Override loadBitmapInternal()83 public Bitmap loadBitmapInternal() throws IOException { 84 Assert.isNotMainThread(); 85 86 InputStream inputStream = null; 87 Bitmap bitmap = null; 88 HttpURLConnection connection = null; 89 try { 90 final URL url = new URL(mDescriptor.uri.toString()); 91 connection = (HttpURLConnection) url.openConnection(); 92 connection.setDoInput(true); 93 connection.connect(); 94 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 95 bitmap = BitmapFactory.decodeStream(connection.getInputStream()); 96 } 97 } catch (MalformedURLException e) { 98 LogUtil.e(LogUtil.BUGLE_TAG, 99 "MalformedUrl for image with url: " 100 + mDescriptor.uri.toString(), e); 101 } catch (final OutOfMemoryError e) { 102 LogUtil.e(LogUtil.BUGLE_TAG, 103 "OutOfMemoryError for image with url: " 104 + mDescriptor.uri.toString(), e); 105 Factory.get().reclaimMemory(); 106 } catch (IOException e) { 107 LogUtil.e(LogUtil.BUGLE_TAG, 108 "IOException trying to get inputStream for image with url: " 109 + mDescriptor.uri.toString(), e); 110 } finally { 111 if (inputStream != null) { 112 inputStream.close(); 113 } 114 if (connection != null) { 115 connection.disconnect(); 116 } 117 } 118 return bitmap; 119 } 120 } 121