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.content.Intent; 21 import android.content.res.Resources.Theme; 22 import android.graphics.Bitmap; 23 import android.graphics.Canvas; 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.View; 32 import android.view.View.OnClickListener; 33 34 public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implements OnClickListener { 35 36 private static Theme sPreloaderTheme; 37 38 private final Rect mRect = new Rect(); 39 private View mDefaultView; 40 private OnClickListener mClickListener; 41 private final LauncherAppWidgetInfo mInfo; 42 private final int mStartState; 43 private final Intent mIconLookupIntent; 44 private final boolean mDisabledForSafeMode; 45 46 private Bitmap mIcon; 47 48 private Drawable mCenterDrawable; 49 private Drawable mTopCornerDrawable; 50 51 private boolean mDrawableSizeChanged; 52 53 private final TextPaint mPaint; 54 private Layout mSetupTextLayout; 55 PendingAppWidgetHostView(Context context, LauncherAppWidgetInfo info, boolean disabledForSafeMode)56 public PendingAppWidgetHostView(Context context, LauncherAppWidgetInfo info, 57 boolean disabledForSafeMode) { 58 super(context); 59 mInfo = info; 60 mStartState = info.restoreStatus; 61 mIconLookupIntent = new Intent().setComponent(info.providerName); 62 mDisabledForSafeMode = disabledForSafeMode; 63 64 mPaint = new TextPaint(); 65 mPaint.setColor(0xFFFFFFFF); 66 mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 67 getDeviceProfile().iconTextSizePx, getResources().getDisplayMetrics())); 68 setBackgroundResource(R.drawable.quantum_panel_dark); 69 setWillNotDraw(false); 70 } 71 72 @Override updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth, int maxHeight)73 public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth, 74 int maxHeight) { 75 // No-op 76 } 77 78 @Override getDefaultView()79 protected View getDefaultView() { 80 if (mDefaultView == null) { 81 mDefaultView = mInflater.inflate(R.layout.appwidget_not_ready, this, false); 82 mDefaultView.setOnClickListener(this); 83 applyState(); 84 } 85 return mDefaultView; 86 } 87 88 @Override setOnClickListener(OnClickListener l)89 public void setOnClickListener(OnClickListener l) { 90 mClickListener = l; 91 } 92 93 @Override isReinflateRequired()94 public boolean isReinflateRequired() { 95 // Re inflate is required any time the widget restore status changes 96 return mStartState != mInfo.restoreStatus; 97 } 98 99 @Override onSizeChanged(int w, int h, int oldw, int oldh)100 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 101 super.onSizeChanged(w, h, oldw, oldh); 102 mDrawableSizeChanged = true; 103 } 104 updateIcon(IconCache cache)105 public void updateIcon(IconCache cache) { 106 Bitmap icon = cache.getIcon(mIconLookupIntent, mInfo.user); 107 if (mIcon == icon) { 108 return; 109 } 110 mIcon = icon; 111 if (mCenterDrawable != null) { 112 mCenterDrawable.setCallback(null); 113 mCenterDrawable = null; 114 } 115 if (mIcon != null) { 116 // The view displays three modes, 117 // 1) App icon in the center 118 // 2) Preload icon in the center 119 // 3) Setup icon in the center and app icon in the top right corner. 120 if (mDisabledForSafeMode) { 121 FastBitmapDrawable disabledIcon = Utilities.createIconDrawable(mIcon); 122 disabledIcon.setGhostModeEnabled(true); 123 mCenterDrawable = disabledIcon; 124 mTopCornerDrawable = null; 125 } else if (isReadyForClickSetup()) { 126 mCenterDrawable = getResources().getDrawable(R.drawable.ic_setting); 127 mTopCornerDrawable = new FastBitmapDrawable(mIcon); 128 } else { 129 if (sPreloaderTheme == null) { 130 sPreloaderTheme = getResources().newTheme(); 131 sPreloaderTheme.applyStyle(R.style.PreloadIcon, true); 132 } 133 134 FastBitmapDrawable drawable = Utilities.createIconDrawable(mIcon); 135 mCenterDrawable = new PreloadIconDrawable(drawable, sPreloaderTheme); 136 mCenterDrawable.setCallback(this); 137 mTopCornerDrawable = null; 138 applyState(); 139 } 140 mDrawableSizeChanged = true; 141 } 142 } 143 144 @Override verifyDrawable(Drawable who)145 protected boolean verifyDrawable(Drawable who) { 146 return (who == mCenterDrawable) || super.verifyDrawable(who); 147 } 148 applyState()149 public void applyState() { 150 if (mCenterDrawable != null) { 151 mCenterDrawable.setLevel(Math.max(mInfo.installProgress, 0)); 152 } 153 } 154 155 @Override onClick(View v)156 public void onClick(View v) { 157 // AppWidgetHostView blocks all click events on the root view. Instead handle click events 158 // on the content and pass it along. 159 if (mClickListener != null) { 160 mClickListener.onClick(this); 161 } 162 } 163 isReadyForClickSetup()164 public boolean isReadyForClickSetup() { 165 return (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0 166 && (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0; 167 } 168 169 @Override onDraw(Canvas canvas)170 protected void onDraw(Canvas canvas) { 171 if (mCenterDrawable == null) { 172 // Nothing to draw 173 return; 174 } 175 176 if (mTopCornerDrawable == null) { 177 if (mDrawableSizeChanged) { 178 int outset = (mCenterDrawable instanceof PreloadIconDrawable) ? 179 ((PreloadIconDrawable) mCenterDrawable).getOutset() : 0; 180 int maxSize = LauncherAppState.getInstance().getDynamicGrid() 181 .getDeviceProfile().iconSizePx + 2 * outset; 182 int size = Math.min(maxSize, Math.min( 183 getWidth() - getPaddingLeft() - getPaddingRight(), 184 getHeight() - getPaddingTop() - getPaddingBottom())); 185 186 mRect.set(0, 0, size, size); 187 mRect.inset(outset, outset); 188 mRect.offsetTo((getWidth() - mRect.width()) / 2, (getHeight() - mRect.height()) / 2); 189 mCenterDrawable.setBounds(mRect); 190 mDrawableSizeChanged = false; 191 } 192 mCenterDrawable.draw(canvas); 193 } else { 194 // Draw the top corner icon and "Setup" text is possible 195 if (mDrawableSizeChanged) { 196 DeviceProfile grid = getDeviceProfile(); 197 int iconSize = grid.iconSizePx; 198 int paddingTop = getPaddingTop(); 199 int paddingBottom = getPaddingBottom(); 200 int paddingLeft = getPaddingLeft(); 201 int paddingRight = getPaddingRight(); 202 203 int availableWidth = getWidth() - paddingLeft - paddingRight; 204 int availableHeight = getHeight() - paddingTop - paddingBottom; 205 206 // Recreate the setup text. 207 mSetupTextLayout = new StaticLayout( 208 getResources().getText(R.string.gadget_setup_text), mPaint, availableWidth, 209 Layout.Alignment.ALIGN_CENTER, 1, 0, true); 210 if (mSetupTextLayout.getLineCount() == 1) { 211 // The text fits in a single line. No need to draw the setup icon. 212 int size = Math.min(iconSize, Math.min(availableWidth, 213 availableHeight - mSetupTextLayout.getHeight())); 214 mRect.set(0, 0, size, size); 215 mRect.offsetTo((getWidth() - mRect.width()) / 2, 216 (getHeight() - mRect.height() - mSetupTextLayout.getHeight() 217 - grid.iconDrawablePaddingPx) / 2); 218 219 mTopCornerDrawable.setBounds(mRect); 220 221 // Update left and top to indicate the position where the text will be drawn. 222 mRect.left = paddingLeft; 223 mRect.top = mRect.bottom + grid.iconDrawablePaddingPx; 224 } else { 225 // The text can't be drawn in a single line. Draw a setup icon instead. 226 mSetupTextLayout = null; 227 int size = Math.min(iconSize, Math.min( 228 getWidth() - paddingLeft - paddingRight, 229 getHeight() - paddingTop - paddingBottom)); 230 mRect.set(0, 0, size, size); 231 mRect.offsetTo((getWidth() - mRect.width()) / 2, (getHeight() - mRect.height()) / 2); 232 mCenterDrawable.setBounds(mRect); 233 234 size = Math.min(size / 2, 235 Math.max(mRect.top - paddingTop, mRect.left - paddingLeft)); 236 mTopCornerDrawable.setBounds(paddingLeft, paddingTop, 237 paddingLeft + size, paddingTop + size); 238 } 239 mDrawableSizeChanged = false; 240 } 241 242 if (mSetupTextLayout == null) { 243 mCenterDrawable.draw(canvas); 244 mTopCornerDrawable.draw(canvas); 245 } else { 246 canvas.save(); 247 canvas.translate(mRect.left, mRect.top); 248 mSetupTextLayout.draw(canvas); 249 canvas.restore(); 250 mTopCornerDrawable.draw(canvas); 251 } 252 } 253 } 254 getDeviceProfile()255 private DeviceProfile getDeviceProfile() { 256 return LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile(); 257 } 258 } 259