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.launcher3; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.Color; 23 import android.graphics.PorterDuff; 24 import android.graphics.Rect; 25 import android.graphics.drawable.Drawable; 26 import android.os.Bundle; 27 import android.text.Layout; 28 import android.text.StaticLayout; 29 import android.text.TextPaint; 30 import android.util.TypedValue; 31 import android.view.ContextThemeWrapper; 32 import android.view.View; 33 import android.view.View.OnClickListener; 34 35 import com.android.launcher3.IconCache.ItemInfoUpdateReceiver; 36 import com.android.launcher3.graphics.DrawableFactory; 37 import com.android.launcher3.model.PackageItemInfo; 38 import com.android.launcher3.util.Themes; 39 40 public class PendingAppWidgetHostView extends LauncherAppWidgetHostView 41 implements OnClickListener, ItemInfoUpdateReceiver { 42 private static final float SETUP_ICON_SIZE_FACTOR = 2f / 5; 43 private static final float MIN_SATUNATION = 0.7f; 44 45 private final Rect mRect = new Rect(); 46 private View mDefaultView; 47 private OnClickListener mClickListener; 48 private final LauncherAppWidgetInfo mInfo; 49 private final int mStartState; 50 private final boolean mDisabledForSafeMode; 51 private Launcher mLauncher; 52 53 private Bitmap mIcon; 54 55 private Drawable mCenterDrawable; 56 private Drawable mSettingIconDrawable; 57 58 private boolean mDrawableSizeChanged; 59 60 private final TextPaint mPaint; 61 private Layout mSetupTextLayout; 62 PendingAppWidgetHostView(Context context, LauncherAppWidgetInfo info, IconCache cache, boolean disabledForSafeMode)63 public PendingAppWidgetHostView(Context context, LauncherAppWidgetInfo info, 64 IconCache cache, boolean disabledForSafeMode) { 65 super(new ContextThemeWrapper(context, R.style.WidgetContainerTheme)); 66 67 mLauncher = Launcher.getLauncher(context); 68 mInfo = info; 69 mStartState = info.restoreStatus; 70 mDisabledForSafeMode = disabledForSafeMode; 71 72 mPaint = new TextPaint(); 73 mPaint.setColor(Themes.getAttrColor(getContext(), android.R.attr.textColorPrimary)); 74 mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 75 mLauncher.getDeviceProfile().iconTextSizePx, getResources().getDisplayMetrics())); 76 setBackgroundResource(R.drawable.pending_widget_bg); 77 setWillNotDraw(false); 78 79 setElevation(getResources().getDimension(R.dimen.pending_widget_elevation)); 80 updateAppWidget(null); 81 setOnClickListener(mLauncher); 82 83 // Load icon 84 PackageItemInfo item = new PackageItemInfo(info.providerName.getPackageName()); 85 item.user = info.user; 86 cache.updateIconInBackground(this, item); 87 } 88 89 @Override updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth, int maxHeight)90 public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth, 91 int maxHeight) { 92 // No-op 93 } 94 95 @Override getDefaultView()96 protected View getDefaultView() { 97 if (mDefaultView == null) { 98 mDefaultView = mInflater.inflate(R.layout.appwidget_not_ready, this, false); 99 mDefaultView.setOnClickListener(this); 100 applyState(); 101 } 102 return mDefaultView; 103 } 104 105 @Override setOnClickListener(OnClickListener l)106 public void setOnClickListener(OnClickListener l) { 107 mClickListener = l; 108 } 109 110 @Override isReinflateRequired()111 public boolean isReinflateRequired() { 112 // Re inflate is required any time the widget restore status changes 113 return mStartState != mInfo.restoreStatus; 114 } 115 116 @Override onSizeChanged(int w, int h, int oldw, int oldh)117 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 118 super.onSizeChanged(w, h, oldw, oldh); 119 mDrawableSizeChanged = true; 120 } 121 122 @Override reapplyItemInfo(ItemInfoWithIcon info)123 public void reapplyItemInfo(ItemInfoWithIcon info) { 124 Bitmap icon = info.iconBitmap; 125 if (mIcon == icon) { 126 return; 127 } 128 mIcon = icon; 129 if (mCenterDrawable != null) { 130 mCenterDrawable.setCallback(null); 131 mCenterDrawable = null; 132 } 133 if (mIcon != null) { 134 // The view displays three modes, 135 // 1) App icon in the center 136 // 2) Preload icon in the center 137 // 3) Setup icon in the center and app icon in the top right corner. 138 DrawableFactory drawableFactory = DrawableFactory.get(getContext()); 139 if (mDisabledForSafeMode) { 140 FastBitmapDrawable disabledIcon = drawableFactory.newIcon(mIcon, mInfo); 141 disabledIcon.setIsDisabled(true); 142 mCenterDrawable = disabledIcon; 143 mSettingIconDrawable = null; 144 } else if (isReadyForClickSetup()) { 145 mCenterDrawable = drawableFactory.newIcon(mIcon, mInfo); 146 mSettingIconDrawable = getResources().getDrawable(R.drawable.ic_setting).mutate(); 147 148 updateSettingColor(); 149 } else { 150 mCenterDrawable = DrawableFactory.get(getContext()) 151 .newPendingIcon(mIcon, getContext()); 152 mCenterDrawable.setCallback(this); 153 mSettingIconDrawable = null; 154 applyState(); 155 } 156 mDrawableSizeChanged = true; 157 } 158 invalidate(); 159 } 160 updateSettingColor()161 private void updateSettingColor() { 162 int color = Utilities.findDominantColorByHue(mIcon, 20); 163 // Make the dominant color bright. 164 float[] hsv = new float[3]; 165 Color.colorToHSV(color, hsv); 166 hsv[1] = Math.min(hsv[1], MIN_SATUNATION); 167 hsv[2] = 1; 168 color = Color.HSVToColor(hsv); 169 170 mSettingIconDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); 171 } 172 173 @Override verifyDrawable(Drawable who)174 protected boolean verifyDrawable(Drawable who) { 175 return (who == mCenterDrawable) || super.verifyDrawable(who); 176 } 177 applyState()178 public void applyState() { 179 if (mCenterDrawable != null) { 180 mCenterDrawable.setLevel(Math.max(mInfo.installProgress, 0)); 181 } 182 } 183 184 @Override onClick(View v)185 public void onClick(View v) { 186 // AppWidgetHostView blocks all click events on the root view. Instead handle click events 187 // on the content and pass it along. 188 if (mClickListener != null) { 189 mClickListener.onClick(this); 190 } 191 } 192 193 /** 194 * A pending widget is ready for setup after the provider is installed and 195 * 1) Widget id is not valid: the widget id is not yet bound to the provider, probably 196 * because the launcher doesn't have appropriate permissions. 197 * Note that we would still have an allocated id as that does not 198 * require any permissions and can be done during view inflation. 199 * 2) UI is not ready: the id is valid and the bound. But the widget has a configure activity 200 * which needs to be called once. 201 */ isReadyForClickSetup()202 public boolean isReadyForClickSetup() { 203 return !mInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) 204 && (mInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY) 205 || mInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)); 206 } 207 updateDrawableBounds()208 private void updateDrawableBounds() { 209 DeviceProfile grid = mLauncher.getDeviceProfile(); 210 int paddingTop = getPaddingTop(); 211 int paddingBottom = getPaddingBottom(); 212 int paddingLeft = getPaddingLeft(); 213 int paddingRight = getPaddingRight(); 214 215 int minPadding = getResources() 216 .getDimensionPixelSize(R.dimen.pending_widget_min_padding); 217 218 int availableWidth = getWidth() - paddingLeft - paddingRight - 2 * minPadding; 219 int availableHeight = getHeight() - paddingTop - paddingBottom - 2 * minPadding; 220 221 if (mSettingIconDrawable == null) { 222 int maxSize = grid.iconSizePx; 223 int size = Math.min(maxSize, Math.min(availableWidth, availableHeight)); 224 225 mRect.set(0, 0, size, size); 226 mRect.offsetTo((getWidth() - mRect.width()) / 2, (getHeight() - mRect.height()) / 2); 227 mCenterDrawable.setBounds(mRect); 228 } else { 229 float iconSize = Math.max(0, Math.min(availableWidth, availableHeight)); 230 231 // Use twice the setting size factor, as the setting is drawn at a corner and the 232 // icon is drawn in the center. 233 float settingIconScaleFactor = 1 + SETUP_ICON_SIZE_FACTOR * 2; 234 int maxSize = Math.max(availableWidth, availableHeight); 235 if (iconSize * settingIconScaleFactor > maxSize) { 236 // There is an overlap 237 iconSize = maxSize / settingIconScaleFactor; 238 } 239 240 int actualIconSize = (int) Math.min(iconSize, grid.iconSizePx); 241 242 // Icon top when we do not draw the text 243 int iconTop = (getHeight() - actualIconSize) / 2; 244 mSetupTextLayout = null; 245 246 if (availableWidth > 0) { 247 // Recreate the setup text. 248 mSetupTextLayout = new StaticLayout( 249 getResources().getText(R.string.gadget_setup_text), mPaint, availableWidth, 250 Layout.Alignment.ALIGN_CENTER, 1, 0, true); 251 int textHeight = mSetupTextLayout.getHeight(); 252 253 // Extra icon size due to the setting icon 254 float minHeightWithText = textHeight + actualIconSize * settingIconScaleFactor 255 + grid.iconDrawablePaddingPx; 256 257 if (minHeightWithText < availableHeight) { 258 // We can draw the text as well 259 iconTop = (getHeight() - textHeight - 260 grid.iconDrawablePaddingPx - actualIconSize) / 2; 261 262 } else { 263 // We can't draw the text. Let the iconTop be same as before. 264 mSetupTextLayout = null; 265 } 266 } 267 268 mRect.set(0, 0, actualIconSize, actualIconSize); 269 mRect.offset((getWidth() - actualIconSize) / 2, iconTop); 270 mCenterDrawable.setBounds(mRect); 271 272 mRect.left = paddingLeft + minPadding; 273 mRect.right = mRect.left + (int) (SETUP_ICON_SIZE_FACTOR * actualIconSize); 274 mRect.top = paddingTop + minPadding; 275 mRect.bottom = mRect.top + (int) (SETUP_ICON_SIZE_FACTOR * actualIconSize); 276 mSettingIconDrawable.setBounds(mRect); 277 278 if (mSetupTextLayout != null) { 279 // Set up position for dragging the text 280 mRect.left = paddingLeft + minPadding; 281 mRect.top = mCenterDrawable.getBounds().bottom + grid.iconDrawablePaddingPx; 282 } 283 } 284 } 285 286 @Override onDraw(Canvas canvas)287 protected void onDraw(Canvas canvas) { 288 if (mCenterDrawable == null) { 289 // Nothing to draw 290 return; 291 } 292 293 if (mDrawableSizeChanged) { 294 updateDrawableBounds(); 295 mDrawableSizeChanged = false; 296 } 297 298 mCenterDrawable.draw(canvas); 299 if (mSettingIconDrawable != null) { 300 mSettingIconDrawable.draw(canvas); 301 } 302 if (mSetupTextLayout != null) { 303 canvas.save(); 304 canvas.translate(mRect.left, mRect.top); 305 mSetupTextLayout.draw(canvas); 306 canvas.restore(); 307 } 308 309 } 310 } 311