blob: 74b9463c38890fd0108ef80d6c8b5d5f9dbfebed [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Dianne Hackborn0cd63762009-07-29 17:36:27 -070036 /**
37 * @deprecated Use {@link #ImageSpan(Context, Bitmap)} instead.
38 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -070039 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 public ImageSpan(Bitmap b) {
Dianne Hackborn0cd63762009-07-29 17:36:27 -070041 this(null, b, ALIGN_BOTTOM);
42 }
43
44 /**
45 * @deprecated Use {@link #ImageSpan(Context, Bitmap, int) instead.
46 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -070047 @Deprecated
Dianne Hackborn0cd63762009-07-29 17:36:27 -070048 public ImageSpan(Bitmap b, int verticalAlignment) {
49 this(null, b, verticalAlignment);
50 }
51
52 public ImageSpan(Context context, Bitmap b) {
53 this(context, b, ALIGN_BOTTOM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 }
55
56 /**
57 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
58 * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
59 */
Dianne Hackborn0cd63762009-07-29 17:36:27 -070060 public ImageSpan(Context context, Bitmap b, int verticalAlignment) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 super(verticalAlignment);
Dianne Hackborn0cd63762009-07-29 17:36:27 -070062 mContext = context;
63 mDrawable = context != null
64 ? new BitmapDrawable(context.getResources(), b)
65 : new BitmapDrawable(b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 int width = mDrawable.getIntrinsicWidth();
67 int height = mDrawable.getIntrinsicHeight();
68 mDrawable.setBounds(0, 0, width > 0 ? width : 0, height > 0 ? height : 0);
69 }
70
71 public ImageSpan(Drawable d) {
72 this(d, ALIGN_BOTTOM);
73 }
74
75 /**
76 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
77 * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
78 */
79 public ImageSpan(Drawable d, int verticalAlignment) {
80 super(verticalAlignment);
81 mDrawable = d;
82 }
83
84 public ImageSpan(Drawable d, String source) {
85 this(d, source, ALIGN_BOTTOM);
86 }
87
88 /**
89 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
90 * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
91 */
92 public ImageSpan(Drawable d, String source, int verticalAlignment) {
93 super(verticalAlignment);
94 mDrawable = d;
95 mSource = source;
96 }
97
98 public ImageSpan(Context context, Uri uri) {
99 this(context, uri, ALIGN_BOTTOM);
100 }
101
102 /**
103 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
104 * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
105 */
106 public ImageSpan(Context context, Uri uri, int verticalAlignment) {
107 super(verticalAlignment);
108 mContext = context;
109 mContentUri = uri;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700110 mSource = uri.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 }
112
113 public ImageSpan(Context context, int resourceId) {
114 this(context, resourceId, ALIGN_BOTTOM);
115 }
116
117 /**
118 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
119 * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
120 */
121 public ImageSpan(Context context, int resourceId, int verticalAlignment) {
122 super(verticalAlignment);
123 mContext = context;
124 mResourceId = resourceId;
125 }
126
127 @Override
128 public Drawable getDrawable() {
129 Drawable drawable = null;
130
131 if (mDrawable != null) {
132 drawable = mDrawable;
133 } else if (mContentUri != null) {
134 Bitmap bitmap = null;
135 try {
136 InputStream is = mContext.getContentResolver().openInputStream(
137 mContentUri);
138 bitmap = BitmapFactory.decodeStream(is);
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700139 drawable = new BitmapDrawable(mContext.getResources(), bitmap);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700140 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
141 drawable.getIntrinsicHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 is.close();
143 } catch (Exception e) {
144 Log.e("sms", "Failed to loaded content " + mContentUri, e);
145 }
146 } else {
147 try {
148 drawable = mContext.getResources().getDrawable(mResourceId);
149 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
150 drawable.getIntrinsicHeight());
151 } catch (Exception e) {
152 Log.e("sms", "Unable to find resource: " + mResourceId);
153 }
154 }
155
156 return drawable;
157 }
158
159 /**
160 * Returns the source string that was saved during construction.
161 */
162 public String getSource() {
163 return mSource;
164 }
165
166}