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
17package com.android.launcher3;
18
19import android.content.Context;
20import 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;
26import android.os.Bundle;
27import android.text.Layout;
28import android.text.StaticLayout;
29import android.text.TextPaint;
30import android.util.TypedValue;
31import android.view.View;
32import android.view.View.OnClickListener;
33
34public 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    private Launcher mLauncher;
46
47    private Bitmap mIcon;
48
49    private Drawable mCenterDrawable;
50    private Drawable mTopCornerDrawable;
51
52    private boolean mDrawableSizeChanged;
53
54    private final TextPaint mPaint;
55    private Layout mSetupTextLayout;
56
57    public PendingAppWidgetHostView(Context context, LauncherAppWidgetInfo info,
58            boolean disabledForSafeMode) {
59        super(context);
60
61        mLauncher = (Launcher) context;
62        mInfo = info;
63        mStartState = info.restoreStatus;
64        mIconLookupIntent = new Intent().setComponent(info.providerName);
65        mDisabledForSafeMode = disabledForSafeMode;
66
67        mPaint = new TextPaint();
68        mPaint.setColor(0xFFFFFFFF);
69        mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
70                mLauncher.getDeviceProfile().iconTextSizePx, getResources().getDisplayMetrics()));
71        setBackgroundResource(R.drawable.quantum_panel_dark);
72        setWillNotDraw(false);
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) {
84            mDefaultView = mInflater.inflate(R.layout.appwidget_not_ready, this, false);
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
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;
114        if (mCenterDrawable != null) {
115            mCenterDrawable.setCallback(null);
116            mCenterDrawable = null;
117        }
118        if (mIcon != null) {
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) {
124                FastBitmapDrawable disabledIcon = mLauncher.createIconDrawable(mIcon);
125                disabledIcon.setGhostModeEnabled(true);
126                mCenterDrawable = disabledIcon;
127                mTopCornerDrawable = null;
128            } else if (isReadyForClickSetup()) {
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
137                FastBitmapDrawable drawable = mLauncher.createIconDrawable(mIcon);
138                mCenterDrawable = new PreloadIconDrawable(drawable, sPreloaderTheme);
139                mCenterDrawable.setCallback(this);
140                mTopCornerDrawable = null;
141                applyState();
142            }
143            mDrawableSizeChanged = true;
144        }
145    }
146
147    @Override
148    protected boolean verifyDrawable(Drawable who) {
149        return (who == mCenterDrawable) || super.verifyDrawable(who);
150    }
151
152    public void applyState() {
153        if (mCenterDrawable != null) {
154            mCenterDrawable.setLevel(Math.max(mInfo.installProgress, 0));
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() {
168        return (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0
169                && (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0;
170    }
171
172    @Override
173    protected void onDraw(Canvas canvas) {
174        if (mCenterDrawable == null) {
175            // Nothing to draw
176            return;
177        }
178
179        DeviceProfile grid = mLauncher.getDeviceProfile();
180        if (mTopCornerDrawable == null) {
181            if (mDrawableSizeChanged) {
182                int outset = (mCenterDrawable instanceof PreloadIconDrawable) ?
183                        ((PreloadIconDrawable) mCenterDrawable).getOutset() : 0;
184                int maxSize = grid.iconSizePx + 2 * outset;
185                int size = Math.min(maxSize, Math.min(
186                        getWidth() - getPaddingLeft() - getPaddingRight(),
187                        getHeight() - getPaddingTop() - getPaddingBottom()));
188
189                mRect.set(0, 0, size, size);
190                mRect.inset(outset, outset);
191                mRect.offsetTo((getWidth() - mRect.width()) / 2, (getHeight() - mRect.height()) / 2);
192                mCenterDrawable.setBounds(mRect);
193                mDrawableSizeChanged = false;
194            }
195            mCenterDrawable.draw(canvas);
196        } else  {
197            // Draw the top corner icon and "Setup" text is possible
198            if (mDrawableSizeChanged) {
199                int iconSize = grid.iconSizePx;
200                int paddingTop = getPaddingTop();
201                int paddingBottom = getPaddingBottom();
202                int paddingLeft = getPaddingLeft();
203                int paddingRight = getPaddingRight();
204
205                int availableWidth = getWidth() - paddingLeft - paddingRight;
206                int availableHeight = getHeight() - paddingTop - paddingBottom;
207
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                }
241                mDrawableSizeChanged = false;
242            }
243
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            }
254        }
255    }
256}
257