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 17 package com.android.bitmap; 18 19 import android.content.res.Resources; 20 21 import java.io.IOException; 22 import java.io.InputStream; 23 24 /** 25 * Simple RequestKey for decoding from a resource id. 26 */ 27 public class ResourceRequestKey implements RequestKey { 28 29 private Resources mResources; 30 private int mResId; 31 32 /** 33 * Create a new request key with the given resource id. A resId of 0 will 34 * return a null request key. 35 */ from(Resources res, int resId)36 public static ResourceRequestKey from(Resources res, int resId) { 37 if (resId != 0) { 38 return new ResourceRequestKey(res, resId); 39 } 40 return null; 41 } 42 ResourceRequestKey(Resources res, int resId)43 private ResourceRequestKey(Resources res, int resId) { 44 mResources = res; 45 mResId = resId; 46 } 47 48 @Override createFileDescriptorFactoryAsync(RequestKey requestKey, Callback callback)49 public Cancelable createFileDescriptorFactoryAsync(RequestKey requestKey, Callback callback) { 50 return null; 51 } 52 53 @Override createInputStream()54 public InputStream createInputStream() throws IOException { 55 return mResources.openRawResource(mResId); 56 } 57 58 @Override hasOrientationExif()59 public boolean hasOrientationExif() throws IOException { 60 return false; 61 } 62 63 // START AUTO-GENERATED CODE 64 65 @Override equals(Object o)66 public boolean equals(Object o) { 67 if (this == o) { 68 return true; 69 } 70 if (o == null || getClass() != o.getClass()) { 71 return false; 72 } 73 74 ResourceRequestKey that = (ResourceRequestKey) o; 75 76 if (mResId != that.mResId) { 77 return false; 78 } 79 80 return true; 81 } 82 83 @Override hashCode()84 public int hashCode() { 85 return mResId; 86 } 87 88 // END AUTO-GENERATED CODE 89 90 @Override toString()91 public String toString() { 92 return String.format("ResourceRequestKey: %d", mResId); 93 } 94 } 95