Home | History | Annotate | Download | only in media
      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 
     20 import java.io.FileNotFoundException;
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.util.List;
     24 
     25 /**
     26  * Serves local content URI based image requests.
     27  */
     28 public class UriImageRequest<D extends UriImageRequestDescriptor> extends ImageRequest<D> {
     29     public UriImageRequest(final Context context, final D descriptor) {
     30         super(context, descriptor);
     31     }
     32 
     33     @Override
     34     protected InputStream getInputStreamForResource() throws FileNotFoundException {
     35         return mContext.getContentResolver().openInputStream(mDescriptor.uri);
     36     }
     37 
     38     @Override
     39     protected ImageResource loadMediaInternal(List<MediaRequest<ImageResource>> chainedTasks)
     40             throws IOException {
     41         final ImageResource resource = super.loadMediaInternal(chainedTasks);
     42         // Check if the caller asked for compression. If so, chain an encoding task if possible.
     43         if (mDescriptor.allowCompression && chainedTasks != null) {
     44             @SuppressWarnings("unchecked")
     45             final MediaRequest<ImageResource> chainedTask = (MediaRequest<ImageResource>)
     46                     resource.getMediaEncodingRequest(this);
     47             if (chainedTask != null) {
     48                 chainedTasks.add(chainedTask);
     49                 // Don't cache decoded image resource since we'll perform compression and cache
     50                 // the compressed resource.
     51                 if (resource instanceof DecodedImageResource) {
     52                     ((DecodedImageResource) resource).setCacheable(false);
     53                 }
     54             }
     55         }
     56         return resource;
     57     }
     58 }
     59