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