blob: 179c60a9827584065b350c957ab569bf7ec37959 [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;
Sunny Goyalff572272014-07-23 13:58:07 -070045
Sunny Goyal0fc1be12014-08-11 17:05:23 -070046 private Bitmap mIcon;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070047
48 private Drawable mCenterDrawable;
49 private Drawable mTopCornerDrawable;
50
51 private boolean mDrawableSizeChanged;
52
Sunny Goyale7b8cd92014-08-27 14:04:33 -070053 private final TextPaint mPaint;
54 private Layout mSetupTextLayout;
55
Sunny Goyal9b4b0812014-10-08 10:47:28 -070056 public PendingAppWidgetHostView(Context context, LauncherAppWidgetInfo info,
57 boolean disabledForSafeMode) {
Sunny Goyalff572272014-07-23 13:58:07 -070058 super(context);
Sunny Goyal0fc1be12014-08-11 17:05:23 -070059 mInfo = info;
60 mStartState = info.restoreStatus;
61 mIconLookupIntent = new Intent().setComponent(info.providerName);
Sunny Goyal9b4b0812014-10-08 10:47:28 -070062 mDisabledForSafeMode = disabledForSafeMode;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070063
Sunny Goyale7b8cd92014-08-27 14:04:33 -070064 mPaint = new TextPaint();
65 mPaint.setColor(0xFFFFFFFF);
66 mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
67 getDeviceProfile().iconTextSizePx, getResources().getDisplayMetrics()));
Sunny Goyal0fc1be12014-08-11 17:05:23 -070068 setBackgroundResource(R.drawable.quantum_panel_dark);
69 setWillNotDraw(false);
Sunny Goyalff572272014-07-23 13:58:07 -070070 }
71
72 @Override
73 public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth,
74 int maxHeight) {
75 // No-op
76 }
77
78 @Override
79 protected View getDefaultView() {
80 if (mDefaultView == null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070081 mDefaultView = mInflater.inflate(R.layout.appwidget_not_ready, this, false);
Sunny Goyalff572272014-07-23 13:58:07 -070082 mDefaultView.setOnClickListener(this);
83 applyState();
84 }
85 return mDefaultView;
86 }
87
88 @Override
89 public void setOnClickListener(OnClickListener l) {
90 mClickListener = l;
91 }
92
Sunny Goyal0fc1be12014-08-11 17:05:23 -070093 @Override
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
100 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
101 super.onSizeChanged(w, h, oldw, oldh);
102 mDrawableSizeChanged = true;
103 }
104
105 public void updateIcon(IconCache cache) {
106 Bitmap icon = cache.getIcon(mIconLookupIntent, mInfo.user);
107 if (mIcon == icon) {
108 return;
109 }
110 mIcon = icon;
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700111 if (mCenterDrawable != null) {
112 mCenterDrawable.setCallback(null);
113 mCenterDrawable = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700114 }
115 if (mIcon != null) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700116 // 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()) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700126 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);
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700135 mCenterDrawable = new PreloadIconDrawable(drawable, sPreloaderTheme);
136 mCenterDrawable.setCallback(this);
137 mTopCornerDrawable = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700138 applyState();
139 }
140 mDrawableSizeChanged = true;
Sunny Goyalff572272014-07-23 13:58:07 -0700141 }
142 }
143
144 @Override
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700145 protected boolean verifyDrawable(Drawable who) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700146 return (who == mCenterDrawable) || super.verifyDrawable(who);
Sunny Goyalff572272014-07-23 13:58:07 -0700147 }
148
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700149 public void applyState() {
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700150 if (mCenterDrawable != null) {
151 mCenterDrawable.setLevel(Math.max(mInfo.installProgress, 0));
Sunny Goyalff572272014-07-23 13:58:07 -0700152 }
153 }
154
155 @Override
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
164 public boolean isReadyForClickSetup() {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700165 return (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0
166 && (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0;
Sunny Goyalff572272014-07-23 13:58:07 -0700167 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700168
169 @Override
170 protected void onDraw(Canvas canvas) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700171 if (mCenterDrawable == null) {
172 // Nothing to draw
173 return;
174 }
175
176 if (mTopCornerDrawable == null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700177 if (mDrawableSizeChanged) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700178 int outset = (mCenterDrawable instanceof PreloadIconDrawable) ?
179 ((PreloadIconDrawable) mCenterDrawable).getOutset() : 0;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700180 int maxSize = LauncherAppState.getInstance().getDynamicGrid()
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700181 .getDeviceProfile().iconSizePx + 2 * outset;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700182 int size = Math.min(maxSize, Math.min(
183 getWidth() - getPaddingLeft() - getPaddingRight(),
184 getHeight() - getPaddingTop() - getPaddingBottom()));
185
186 mRect.set(0, 0, size, size);
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700187 mRect.inset(outset, outset);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700188 mRect.offsetTo((getWidth() - mRect.width()) / 2, (getHeight() - mRect.height()) / 2);
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700189 mCenterDrawable.setBounds(mRect);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700190 mDrawableSizeChanged = false;
191 }
Sunny Goyal9b4b0812014-10-08 10:47:28 -0700192 mCenterDrawable.draw(canvas);
193 } else {
194 // Draw the top corner icon and "Setup" text is possible
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700195 if (mDrawableSizeChanged) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700196 DeviceProfile grid = getDeviceProfile();
197 int iconSize = grid.iconSizePx;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700198 int paddingTop = getPaddingTop();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700199 int paddingBottom = getPaddingBottom();
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700200 int paddingLeft = getPaddingLeft();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700201 int paddingRight = getPaddingRight();
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700202
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700203 int availableWidth = getWidth() - paddingLeft - paddingRight;
204 int availableHeight = getHeight() - paddingTop - paddingBottom;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700205
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700206 // 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 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700239 mDrawableSizeChanged = false;
240 }
241
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700242 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 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700252 }
253 }
254
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700255 private DeviceProfile getDeviceProfile() {
256 return LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile();
257 }
Sunny Goyalff572272014-07-23 13:58:07 -0700258}