blob: 08f8e560184610c1d01d9d5264b59128faece958 [file] [log] [blame]
Sunny Goyal0fc1be12014-08-11 17:05:23 -07001/*
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 Goyalff572272014-07-23 13:58:07 -070017package com.android.launcher3;
18
19import android.content.Context;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070020import android.content.Intent;
21import android.content.res.Resources.Theme;
22import android.graphics.Bitmap;
23import android.graphics.Canvas;
24import android.graphics.Rect;
25import android.graphics.drawable.Drawable;
Sunny Goyalff572272014-07-23 13:58:07 -070026import android.os.Bundle;
Sunny Goyale7b8cd92014-08-27 14:04:33 -070027import android.text.Layout;
28import android.text.StaticLayout;
29import android.text.TextPaint;
30import android.util.TypedValue;
Sunny Goyalff572272014-07-23 13:58:07 -070031import android.view.View;
32import android.view.View.OnClickListener;
Sunny Goyalff572272014-07-23 13:58:07 -070033
34public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implements OnClickListener {
35
Sunny Goyal0fc1be12014-08-11 17:05:23 -070036 private static Theme sPreloaderTheme;
Sunny Goyalff572272014-07-23 13:58:07 -070037
Sunny Goyal0fc1be12014-08-11 17:05:23 -070038 private final Rect mRect = new Rect();
39 private View mDefaultView;
Sunny Goyalff572272014-07-23 13:58:07 -070040 private OnClickListener mClickListener;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070041 private final LauncherAppWidgetInfo mInfo;
42 private final int mStartState;
43 private final Intent mIconLookupIntent;
Sunny Goyal9b4b0812014-10-08 10:47:28 -070044 private final boolean mDisabledForSafeMode;
Adam Cohen2e6da152015-05-06 11:42:25 -070045 private Launcher mLauncher;
Sunny Goyalff572272014-07-23 13:58:07 -070046
Sunny Goyal0fc1be12014-08-11 17:05:23 -070047 private Bitmap mIcon;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070048
49 private Drawable mCenterDrawable;
50 private Drawable mTopCornerDrawable;
51
52 private boolean mDrawableSizeChanged;
53
Sunny Goyale7b8cd92014-08-27 14:04:33 -070054 private final TextPaint mPaint;
55 private Layout mSetupTextLayout;
56
Sunny Goyal9b4b0812014-10-08 10:47:28 -070057 public PendingAppWidgetHostView(Context context, LauncherAppWidgetInfo info,
58 boolean disabledForSafeMode) {
Sunny Goyalff572272014-07-23 13:58:07 -070059 super(context);
Adam Cohen2e6da152015-05-06 11:42:25 -070060
61 mLauncher = (Launcher) context;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070062 mInfo = info;
63 mStartState = info.restoreStatus;
64 mIconLookupIntent = new Intent().setComponent(info.providerName);
Sunny Goyal9b4b0812014-10-08 10:47:28 -070065 mDisabledForSafeMode = disabledForSafeMode;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070066
Sunny Goyale7b8cd92014-08-27 14:04:33 -070067 mPaint = new TextPaint();
68 mPaint.setColor(0xFFFFFFFF);
69 mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
Adam Cohen2e6da152015-05-06 11:42:25 -070070 mLauncher.getDeviceProfile().iconTextSizePx, getResources().getDisplayMetrics()));
Sunny Goyal0fc1be12014-08-11 17:05:23 -070071 setBackgroundResource(R.drawable.quantum_panel_dark);
72 setWillNotDraw(false);
Sunny Goyalff572272014-07-23 13:58:07 -070073 }
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 Goyal0fc1be12014-08-11 17:05:23 -070084 mDefaultView = mInflater.inflate(R.layout.appwidget_not_ready, this, false);
Sunny Goyalff572272014-07-23 13:58:07 -070085 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 Goyal0fc1be12014-08-11 17:05:23 -070096 @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 Goyal9b4b0812014-10-08 10:47:28 -0700114 if (mCenterDrawable != null) {
115 mCenterDrawable.setCallback(null);
116 mCenterDrawable = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700117 }
118 if (mIcon != null) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700119 // 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 Goyal53d7ee42015-05-22 12:25:45 -0700124 FastBitmapDrawable disabledIcon = mLauncher.createIconDrawable(mIcon);
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700125 disabledIcon.setGhostModeEnabled(true);
126 mCenterDrawable = disabledIcon;
127 mTopCornerDrawable = null;
128 } else if (isReadyForClickSetup()) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700129 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 Goyal53d7ee42015-05-22 12:25:45 -0700137 FastBitmapDrawable drawable = mLauncher.createIconDrawable(mIcon);
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700138 mCenterDrawable = new PreloadIconDrawable(drawable, sPreloaderTheme);
139 mCenterDrawable.setCallback(this);
140 mTopCornerDrawable = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700141 applyState();
142 }
143 mDrawableSizeChanged = true;
Sunny Goyalff572272014-07-23 13:58:07 -0700144 }
145 }
146
147 @Override
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700148 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700149 return (who == mCenterDrawable) || super.verifyDrawable(who);
Sunny Goyalff572272014-07-23 13:58:07 -0700150 }
151
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700152 public void applyState() {
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700153 if (mCenterDrawable != null) {
154 mCenterDrawable.setLevel(Math.max(mInfo.installProgress, 0));
Sunny Goyalff572272014-07-23 13:58:07 -0700155 }
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 Goyal0fc1be12014-08-11 17:05:23 -0700168 return (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0
169 && (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0;
Sunny Goyalff572272014-07-23 13:58:07 -0700170 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700171
172 @Override
173 protected void onDraw(Canvas canvas) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700174 if (mCenterDrawable == null) {
175 // Nothing to draw
176 return;
177 }
178
Adam Cohen2e6da152015-05-06 11:42:25 -0700179 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700180 if (mTopCornerDrawable == null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700181 if (mDrawableSizeChanged) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700182 int outset = (mCenterDrawable instanceof PreloadIconDrawable) ?
183 ((PreloadIconDrawable) mCenterDrawable).getOutset() : 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700184 int maxSize = grid.iconSizePx + 2 * outset;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700185 int size = Math.min(maxSize, Math.min(
186 getWidth() - getPaddingLeft() - getPaddingRight(),
187 getHeight() - getPaddingTop() - getPaddingBottom()));
188
189 mRect.set(0, 0, size, size);
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700190 mRect.inset(outset, outset);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700191 mRect.offsetTo((getWidth() - mRect.width()) / 2, (getHeight() - mRect.height()) / 2);
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700192 mCenterDrawable.setBounds(mRect);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700193 mDrawableSizeChanged = false;
194 }
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700195 mCenterDrawable.draw(canvas);
196 } else {
197 // Draw the top corner icon and "Setup" text is possible
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700198 if (mDrawableSizeChanged) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700199 int iconSize = grid.iconSizePx;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700200 int paddingTop = getPaddingTop();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700201 int paddingBottom = getPaddingBottom();
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700202 int paddingLeft = getPaddingLeft();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700203 int paddingRight = getPaddingRight();
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700204
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700205 int availableWidth = getWidth() - paddingLeft - paddingRight;
206 int availableHeight = getHeight() - paddingTop - paddingBottom;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700207
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700208 // 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 Goyal0fc1be12014-08-11 17:05:23 -0700241 mDrawableSizeChanged = false;
242 }
243
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700244 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 Goyal0fc1be12014-08-11 17:05:23 -0700254 }
255 }
Sunny Goyalff572272014-07-23 13:58:07 -0700256}