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