ResourceRequestKey.java revision ad6ca3f895022ded1a11f3eedc50d70ea90cd4da
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.bitmap;
18
19import android.content.res.Resources;
20
21import java.io.IOException;
22import java.io.InputStream;
23
24/**
25 * Simple RequestKey for decoding from a resource id.
26 */
27public 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     */
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
43    private ResourceRequestKey(Resources res, int resId) {
44        mResources = res;
45        mResId = resId;
46    }
47
48    @Override
49    public Cancelable createFileDescriptorFactoryAsync(RequestKey requestKey, Callback callback) {
50        return null;
51    }
52
53    @Override
54    public InputStream createInputStream() throws IOException {
55        return mResources.openRawResource(mResId);
56    }
57
58    @Override
59    public boolean hasOrientationExif() throws IOException {
60        return false;
61    }
62
63    // START AUTO-GENERATED CODE
64
65    @Override
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
84    public int hashCode() {
85        return mResId;
86    }
87
88    // END AUTO-GENERATED CODE
89
90    @Override
91    public String toString() {
92        return String.format("ResourceRequestKey: %d", mResId);
93    }
94}
95