michael@0: /* michael@0: * Copyright (C) 2013 Square, Inc. michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: package com.squareup.picasso; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.Resources; michael@0: import android.graphics.Bitmap; michael@0: import android.graphics.BitmapFactory; michael@0: import java.io.IOException; michael@0: michael@0: import static com.squareup.picasso.Picasso.LoadedFrom.DISK; michael@0: michael@0: class ResourceBitmapHunter extends BitmapHunter { michael@0: private final Context context; michael@0: michael@0: ResourceBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache, michael@0: Stats stats, Action action) { michael@0: super(picasso, dispatcher, cache, stats, action); michael@0: this.context = context; michael@0: } michael@0: michael@0: @Override Bitmap decode(Request data) throws IOException { michael@0: Resources res = Utils.getResources(context, data); michael@0: int id = Utils.getResourceId(res, data); michael@0: return decodeResource(res, id, data); michael@0: } michael@0: michael@0: @Override Picasso.LoadedFrom getLoadedFrom() { michael@0: return DISK; michael@0: } michael@0: michael@0: private Bitmap decodeResource(Resources resources, int id, Request data) { michael@0: BitmapFactory.Options bitmapOptions = null; michael@0: if (data.hasSize()) { michael@0: bitmapOptions = new BitmapFactory.Options(); michael@0: bitmapOptions.inJustDecodeBounds = true; michael@0: BitmapFactory.decodeResource(resources, id, bitmapOptions); michael@0: calculateInSampleSize(data.targetWidth, data.targetHeight, bitmapOptions); michael@0: } michael@0: return BitmapFactory.decodeResource(resources, id, bitmapOptions); michael@0: } michael@0: }