Home | History | Annotate | Download | only in style
      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.content.Context;
     20 import android.graphics.Bitmap;
     21 import android.graphics.BitmapFactory;
     22 import android.graphics.drawable.BitmapDrawable;
     23 import android.graphics.drawable.Drawable;
     24 import android.net.Uri;
     25 import android.util.Log;
     26 
     27 import java.io.InputStream;
     28 
     29 public 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     /**
     37      * @deprecated Use {@link #ImageSpan(Context, Bitmap)} instead.
     38      */
     39     @Deprecated
     40     public ImageSpan(Bitmap b) {
     41         this(null, b, ALIGN_BOTTOM);
     42     }
     43 
     44     /**
     45      * @deprecated Use {@link #ImageSpan(Context, Bitmap, int) instead.
     46      */
     47     @Deprecated
     48     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);
     54     }
     55 
     56     /**
     57      * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
     58      * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
     59      */
     60     public ImageSpan(Context context, Bitmap b, int verticalAlignment) {
     61         super(verticalAlignment);
     62         mContext = context;
     63         mDrawable = context != null
     64                 ? new BitmapDrawable(context.getResources(), b)
     65                 : new BitmapDrawable(b);
     66         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;
    110         mSource = uri.toString();
    111     }
    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);
    139                 drawable = new BitmapDrawable(mContext.getResources(), bitmap);
    140                 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
    141                         drawable.getIntrinsicHeight());
    142                 is.close();
    143             } catch (Exception e) {
    144                 Log.e("sms", "Failed to loaded content " + mContentUri, e);
    145             }
    146         } else {
    147             try {
    148                 drawable = mContext.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 }
    167