Home | History | Annotate | Download | only in graphics
      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 
     17 package com.android.launcher3.graphics;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.graphics.Bitmap;
     22 import android.graphics.Canvas;
     23 import android.graphics.Color;
     24 import android.graphics.Path;
     25 import android.graphics.Rect;
     26 import android.graphics.drawable.BitmapDrawable;
     27 import android.graphics.drawable.Drawable;
     28 import android.os.Process;
     29 import android.os.UserHandle;
     30 import android.support.annotation.UiThread;
     31 import android.util.ArrayMap;
     32 import android.util.Log;
     33 import com.android.launcher3.FastBitmapDrawable;
     34 import com.android.launcher3.ItemInfo;
     35 import com.android.launcher3.R;
     36 import com.android.launcher3.Utilities;
     37 import com.android.launcher3.allapps.AllAppsBackgroundDrawable;
     38 
     39 /**
     40  * Factory for creating new drawables.
     41  */
     42 public class DrawableFactory {
     43 
     44     private static final String TAG = "DrawableFactory";
     45 
     46     private static DrawableFactory sInstance;
     47     private static final Object LOCK = new Object();
     48 
     49     private Path mPreloadProgressPath;
     50 
     51     public static DrawableFactory get(Context context) {
     52         synchronized (LOCK) {
     53             if (sInstance == null) {
     54                 sInstance = Utilities.getOverrideObject(DrawableFactory.class,
     55                         context.getApplicationContext(), R.string.drawable_factory_class);
     56             }
     57             return sInstance;
     58         }
     59     }
     60 
     61     protected final UserHandle mMyUser = Process.myUserHandle();
     62     protected final ArrayMap<UserHandle, Bitmap> mUserBadges = new ArrayMap<>();
     63 
     64     /**
     65      * Returns a FastBitmapDrawable with the icon.
     66      */
     67     public FastBitmapDrawable newIcon(Bitmap icon, ItemInfo info) {
     68         return new FastBitmapDrawable(icon);
     69     }
     70 
     71     /**
     72      * Returns a FastBitmapDrawable with the icon.
     73      */
     74     public PreloadIconDrawable newPendingIcon(Bitmap icon, Context context) {
     75         if (mPreloadProgressPath == null) {
     76             mPreloadProgressPath = getPreloadProgressPath(context);
     77         }
     78         return new PreloadIconDrawable(icon, mPreloadProgressPath, context);
     79     }
     80 
     81 
     82     protected Path getPreloadProgressPath(Context context) {
     83         if (Utilities.ATLEAST_OREO) {
     84             try {
     85                 // Try to load the path from Mask Icon
     86                 Drawable icon = context.getDrawable(R.drawable.adaptive_icon_drawable_wrapper);
     87                 icon.setBounds(0, 0,
     88                         PreloadIconDrawable.PATH_SIZE, PreloadIconDrawable.PATH_SIZE);
     89                 return (Path) icon.getClass().getMethod("getIconMask").invoke(icon);
     90             } catch (Exception e) {
     91                 Log.e(TAG, "Error loading mask icon", e);
     92             }
     93         }
     94 
     95         // Create a circle static from top center and going clockwise.
     96         Path p = new Path();
     97         p.moveTo(PreloadIconDrawable.PATH_SIZE / 2, 0);
     98         p.addArc(0, 0, PreloadIconDrawable.PATH_SIZE, PreloadIconDrawable.PATH_SIZE, -90, 360);
     99         return p;
    100     }
    101 
    102     public AllAppsBackgroundDrawable getAllAppsBackground(Context context) {
    103         return new AllAppsBackgroundDrawable(context);
    104     }
    105 
    106     /**
    107      * Returns a drawable that can be used as a badge for the user or null.
    108      */
    109     @UiThread
    110     public Drawable getBadgeForUser(UserHandle user, Context context) {
    111         if (mMyUser.equals(user)) {
    112             return null;
    113         }
    114 
    115         Bitmap badgeBitmap = getUserBadge(user, context);
    116         FastBitmapDrawable d = new FastBitmapDrawable(badgeBitmap);
    117         d.setFilterBitmap(true);
    118         d.setBounds(0, 0, badgeBitmap.getWidth(), badgeBitmap.getHeight());
    119         return d;
    120     }
    121 
    122     protected synchronized Bitmap getUserBadge(UserHandle user, Context context) {
    123         Bitmap badgeBitmap = mUserBadges.get(user);
    124         if (badgeBitmap != null) {
    125             return badgeBitmap;
    126         }
    127 
    128         final Resources res = context.getApplicationContext().getResources();
    129         int badgeSize = res.getDimensionPixelSize(R.dimen.profile_badge_size);
    130         badgeBitmap = Bitmap.createBitmap(badgeSize, badgeSize, Bitmap.Config.ARGB_8888);
    131 
    132         Drawable drawable = context.getPackageManager().getUserBadgedDrawableForDensity(
    133                 new BitmapDrawable(res, badgeBitmap), user, new Rect(0, 0, badgeSize, badgeSize),
    134                 0);
    135         if (drawable instanceof BitmapDrawable) {
    136             badgeBitmap = ((BitmapDrawable) drawable).getBitmap();
    137         } else {
    138             badgeBitmap.eraseColor(Color.TRANSPARENT);
    139             Canvas c = new Canvas(badgeBitmap);
    140             drawable.setBounds(0, 0, badgeSize, badgeSize);
    141             drawable.draw(c);
    142             c.setBitmap(null);
    143         }
    144 
    145         mUserBadges.put(user, badgeBitmap);
    146         return badgeBitmap;
    147     }
    148 }
    149