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