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 android.support.v4.graphics.drawable;
18
19import android.content.res.Resources;
20import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
22import android.graphics.Rect;
23import android.os.Build;
24import android.support.v4.graphics.BitmapCompat;
25import android.support.v4.view.GravityCompat;
26import android.support.v4.view.ViewCompat;
27import android.util.Log;
28
29/**
30 * Constructs {@link RoundedBitmapDrawable RoundedBitmapDrawable} objects,
31 * either from Bitmaps directly, or from streams and files.
32 */
33public class RoundedBitmapDrawableFactory {
34    private static final String TAG = "RoundedBitmapDrawableFactory";
35
36    private static class DefaultRoundedBitmapDrawable extends RoundedBitmapDrawable {
37        DefaultRoundedBitmapDrawable(Resources res, Bitmap bitmap) {
38            super(res, bitmap);
39        }
40
41        @Override
42        public void setMipMap(boolean mipMap) {
43            if (mBitmap != null) {
44                BitmapCompat.setHasMipMap(mBitmap, mipMap);
45                invalidateSelf();
46            }
47        }
48
49        @Override
50        public boolean hasMipMap() {
51            return mBitmap != null && BitmapCompat.hasMipMap(mBitmap);
52        }
53
54        @Override
55        void gravityCompatApply(int gravity, int bitmapWidth, int bitmapHeight,
56                Rect bounds, Rect outRect) {
57            GravityCompat.apply(gravity, bitmapWidth, bitmapHeight,
58                    bounds, outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
59        }
60    }
61
62    /**
63     * Returns a new drawable by creating it from a bitmap, setting initial target density based on
64     * the display metrics of the resources.
65     */
66    public static RoundedBitmapDrawable create(Resources res, Bitmap bitmap) {
67        if (Build.VERSION.SDK_INT >= 21) {
68            return new RoundedBitmapDrawable21(res, bitmap);
69        }
70        return new DefaultRoundedBitmapDrawable(res, bitmap);
71    }
72
73    /**
74     * Returns a new drawable, creating it by opening a given file path and decoding the bitmap.
75     */
76    public static RoundedBitmapDrawable create(Resources res,
77            String filepath) {
78        final RoundedBitmapDrawable drawable = create(res, BitmapFactory.decodeFile(filepath));
79        if (drawable.getBitmap() == null) {
80            Log.w(TAG, "BitmapDrawable cannot decode " + filepath);
81        }
82        return drawable;
83    }
84
85
86    /**
87     * Returns a new drawable, creating it by decoding a bitmap from the given input stream.
88     */
89    public static RoundedBitmapDrawable create(Resources res,
90            java.io.InputStream is) {
91        final RoundedBitmapDrawable drawable = create(res, BitmapFactory.decodeStream(is));
92        if (drawable.getBitmap() == null) {
93            Log.w(TAG, "BitmapDrawable cannot decode " + is);
94        }
95        return drawable;
96    }
97
98}
99