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