ImageSpan.java revision b798689749c64baba81f02e10cf2157c747d6b46
1/*
2 * Copyright (C) 2006 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.text.style;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
22import android.graphics.drawable.BitmapDrawable;
23import android.graphics.drawable.Drawable;
24import android.net.Uri;
25import android.util.Log;
26
27import java.io.InputStream;
28
29public class ImageSpan extends DynamicDrawableSpan {
30    private Drawable mDrawable;
31    private Uri mContentUri;
32    private int mResourceId;
33    private Context mContext;
34    private String mSource;
35
36    public ImageSpan(Bitmap b) {
37        this(b, ALIGN_BOTTOM);
38    }
39
40    /**
41     * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
42     * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
43     */
44    public ImageSpan(Bitmap b, int verticalAlignment) {
45        super(verticalAlignment);
46        mDrawable = new BitmapDrawable(b);
47        mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(),
48                mDrawable.getIntrinsicHeight());
49    }
50
51    public ImageSpan(Drawable d) {
52        this(d, ALIGN_BOTTOM);
53    }
54
55    /**
56     * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
57     * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
58     */
59    public ImageSpan(Drawable d, int verticalAlignment) {
60        super(verticalAlignment);
61        mDrawable = d;
62    }
63
64    public ImageSpan(Drawable d, String source) {
65        this(d, source, ALIGN_BOTTOM);
66    }
67
68    /**
69     * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
70     * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
71     */
72    public ImageSpan(Drawable d, String source, int verticalAlignment) {
73        super(verticalAlignment);
74        mDrawable = d;
75        mSource = source;
76    }
77
78    public ImageSpan(Context context, Uri uri) {
79        this(context, uri, ALIGN_BOTTOM);
80    }
81
82    /**
83     * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
84     * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
85     */
86    public ImageSpan(Context context, Uri uri, int verticalAlignment) {
87        super(verticalAlignment);
88        mContext = context;
89        mContentUri = uri;
90    }
91
92    public ImageSpan(Context context, int resourceId) {
93        this(context, resourceId, ALIGN_BOTTOM);
94    }
95
96    /**
97     * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
98     * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
99     */
100    public ImageSpan(Context context, int resourceId, int verticalAlignment) {
101        super(verticalAlignment);
102        mContext = context;
103        mResourceId = resourceId;
104    }
105
106    @Override
107    public Drawable getDrawable() {
108        Drawable drawable = null;
109
110        if (mDrawable != null) {
111            drawable = mDrawable;
112        } else  if (mContentUri != null) {
113            Bitmap bitmap = null;
114            try {
115                InputStream is = mContext.getContentResolver().openInputStream(
116                        mContentUri);
117                bitmap = BitmapFactory.decodeStream(is);
118                drawable = new BitmapDrawable(bitmap);
119                is.close();
120            } catch (Exception e) {
121                Log.e("sms", "Failed to loaded content " + mContentUri, e);
122            }
123        } else {
124            try {
125                drawable = mContext.getResources().getDrawable(mResourceId);
126                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
127                        drawable.getIntrinsicHeight());
128            } catch (Exception e) {
129                Log.e("sms", "Unable to find resource: " + mResourceId);
130            }
131        }
132
133        return drawable;
134    }
135
136    /**
137     * Returns the source string that was saved during construction.
138     */
139    public String getSource() {
140        return mSource;
141    }
142
143}
144