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