Home | History | Annotate | Download | only in widget
      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 
     17 package com.android.tv.settings.widget;
     18 
     19 import android.content.Context;
     20 import android.content.Intent.ShortcutIconResource;
     21 import android.graphics.Bitmap;
     22 import android.net.Uri;
     23 import android.text.TextUtils;
     24 
     25 import com.android.tv.settings.util.UriUtils;
     26 
     27 /**
     28  * Options for loading bitmap resources from different sources and for scaling to an appropriate
     29  * resolution.
     30  *
     31  * @see BitmapWorkerTask
     32  */
     33 public class BitmapWorkerOptions {
     34 
     35     /** Max image size handled by android.graphics */
     36     static final int MAX_IMAGE_DIMENSION_PX = 2048;
     37 
     38     /** flag to force disable memory cache */
     39     public static final int CACHE_FLAG_MEM_DISABLED = 1;
     40     /** TODO support disk cache options */
     41     public static final int CACHE_FLAG_DISK_DISABLED = 2;
     42 
     43     private ShortcutIconResource mIconResource;
     44     private Uri mResourceUri;
     45 
     46     private int mWidth;
     47     private int mHeight;
     48     private Context mContext;
     49     private int mCacheFlag;
     50     private Bitmap.Config mBitmapConfig;
     51 
     52     private String mKey;
     53 
     54     /**
     55      * Builds options for a bitmap worker task.
     56      */
     57     public static class Builder {
     58 
     59         private String mPackageName;
     60         private String mResourceName;
     61         private Uri mResourceUri;
     62 
     63         private int mWidth;
     64         private int mHeight;
     65         private final Context mContext;
     66         private int mCacheFlag;
     67         private Bitmap.Config mBitmapConfig;
     68 
     69         public Builder(Context context) {
     70             mWidth = MAX_IMAGE_DIMENSION_PX;
     71             mHeight = MAX_IMAGE_DIMENSION_PX;
     72             mContext = context.getApplicationContext();
     73             mCacheFlag = 0;
     74             mBitmapConfig = null;
     75         }
     76 
     77         public BitmapWorkerOptions build() {
     78             BitmapWorkerOptions options = new BitmapWorkerOptions();
     79 
     80             if (!TextUtils.isEmpty(mPackageName)) {
     81                 options.mIconResource = new ShortcutIconResource();
     82                 options.mIconResource.packageName = mPackageName;
     83                 options.mIconResource.resourceName = mResourceName;
     84             }
     85 
     86             final int largestDim = Math.max(mWidth, mHeight);
     87             if (largestDim > MAX_IMAGE_DIMENSION_PX) {
     88                 double scale = (double) MAX_IMAGE_DIMENSION_PX / largestDim;
     89                 mWidth *= scale;
     90                 mHeight *= scale;
     91             }
     92 
     93             options.mResourceUri = mResourceUri;
     94             options.mWidth = mWidth;
     95             options.mHeight = mHeight;
     96             options.mContext = mContext;
     97             options.mCacheFlag = mCacheFlag;
     98             options.mBitmapConfig = mBitmapConfig;
     99             if (options.mIconResource == null && options.mResourceUri == null) {
    100                 throw new RuntimeException("Both Icon and ResourceUri are null");
    101             }
    102             return options;
    103         }
    104 
    105         public Builder resource(String packageName, String resourceName) {
    106             mPackageName = packageName;
    107             mResourceName = resourceName;
    108             return this;
    109         }
    110 
    111         public Builder resource(ShortcutIconResource iconResource) {
    112             mPackageName = iconResource.packageName;
    113             mResourceName = iconResource.resourceName;
    114             return this;
    115         }
    116 
    117         public Builder resource(Uri resourceUri) {
    118             mResourceUri = resourceUri;
    119             return this;
    120         }
    121 
    122         public Builder width(int width) {
    123             if (width > 0) {
    124                 mWidth = width;
    125             } else {
    126                 throw new IllegalArgumentException("Can't set width to " + width);
    127             }
    128             return this;
    129         }
    130 
    131         public Builder height(int height) {
    132             if (height > 0) {
    133                 mHeight = height;
    134             } else {
    135                 throw new IllegalArgumentException("Can't set height to " + height);
    136             }
    137             return this;
    138         }
    139 
    140         public Builder cacheFlag(int flag) {
    141             mCacheFlag = flag;
    142             return this;
    143         }
    144 
    145         public Builder bitmapConfig(Bitmap.Config config) {
    146             mBitmapConfig = config;
    147             return this;
    148         }
    149 
    150     }
    151 
    152     /**
    153      * Private constructor.
    154      * <p>
    155      * Use a {@link Builder} to create.
    156      */
    157     private BitmapWorkerOptions() {
    158     }
    159 
    160     public ShortcutIconResource getIconResource() {
    161         return mIconResource;
    162     }
    163 
    164     public Uri getResourceUri() {
    165         return mResourceUri;
    166     }
    167 
    168     public int getWidth() {
    169         return mWidth;
    170     }
    171 
    172     public int getHeight() {
    173         return mHeight;
    174     }
    175 
    176     public Context getContext() {
    177         return mContext;
    178     }
    179 
    180     public boolean isFromResource() {
    181         return getIconResource() != null ||
    182                 UriUtils.isAndroidResourceUri(getResourceUri())
    183                 || UriUtils.isShortcutIconResourceUri(getResourceUri());
    184     }
    185 
    186     /**
    187      * Combination of CACHE_FLAG_MEM_DISABLED and CACHE_FLAG_DISK_DISABLED,
    188      * 0 for fully cache enabled
    189      */
    190     public int getCacheFlag() {
    191         return mCacheFlag;
    192     }
    193 
    194     public boolean isMemCacheEnabled() {
    195         return (mCacheFlag & CACHE_FLAG_MEM_DISABLED) == 0;
    196     }
    197 
    198     public boolean isDiskCacheEnabled() {
    199         return (mCacheFlag & CACHE_FLAG_DISK_DISABLED) == 0;
    200     }
    201 
    202     /**
    203      * @return  preferred Bitmap config to decode bitmap, null for auto detect.
    204      * Use {@link Builder#bitmapConfig(android.graphics.Bitmap.Config)} to change it.
    205      */
    206     public Bitmap.Config getBitmapConfig() {
    207         return mBitmapConfig;
    208     }
    209 
    210     public String getCacheKey() {
    211         if (mKey == null) {
    212             mKey = mIconResource != null ? mIconResource.packageName + "/"
    213                     + mIconResource.resourceName : mResourceUri.toString();
    214         }
    215         return mKey;
    216     }
    217 
    218     @Override
    219     public String toString() {
    220         if (mIconResource == null) {
    221             return "URI: " + mResourceUri;
    222         } else {
    223             return "PackageName: " + mIconResource.packageName + " Resource: " + mIconResource
    224                     + " URI: " + mResourceUri;
    225         }
    226     }
    227 }
    228