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