10fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal/*
20fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal * Copyright (C) 2014 The Android Open Source Project
30fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal *
40fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
50fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal * you may not use this file except in compliance with the License.
60fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal * You may obtain a copy of the License at
70fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal *
80fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
90fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal *
100fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal * Unless required by applicable law or agreed to in writing, software
110fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
120fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal * See the License for the specific language governing permissions and
140fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal * limitations under the License.
150fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal */
160fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
17ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyalpackage com.android.launcher3;
18ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
19ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyalimport android.content.Context;
200fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyalimport android.content.Intent;
210fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyalimport android.content.res.Resources.Theme;
220fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyalimport android.graphics.Bitmap;
230fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyalimport android.graphics.Canvas;
240fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyalimport android.graphics.Rect;
250fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyalimport android.graphics.drawable.Drawable;
26ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyalimport android.os.Bundle;
27e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyalimport android.text.Layout;
28e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyalimport android.text.StaticLayout;
29e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyalimport android.text.TextPaint;
30e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyalimport android.util.TypedValue;
31ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyalimport android.view.View;
32ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyalimport android.view.View.OnClickListener;
33ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
34ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyalpublic class PendingAppWidgetHostView extends LauncherAppWidgetHostView implements OnClickListener {
35ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
360fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private static Theme sPreloaderTheme;
37ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
380fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private final Rect mRect = new Rect();
390fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private View mDefaultView;
40ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    private OnClickListener mClickListener;
410fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private final LauncherAppWidgetInfo mInfo;
420fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private final int mStartState;
430fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private final Intent mIconLookupIntent;
449b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal    private final boolean mDisabledForSafeMode;
452e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen    private Launcher mLauncher;
460fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
470fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private Bitmap mIcon;
480fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
490fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private Drawable mCenterDrawable;
500fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private Drawable mTopCornerDrawable;
510fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
520fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private boolean mDrawableSizeChanged;
53ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
54e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal    private final TextPaint mPaint;
55e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal    private Layout mSetupTextLayout;
56e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal
579b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal    public PendingAppWidgetHostView(Context context, LauncherAppWidgetInfo info,
589b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            boolean disabledForSafeMode) {
59ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        super(context);
602e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen
612e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        mLauncher = (Launcher) context;
620fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        mInfo = info;
630fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        mStartState = info.restoreStatus;
640fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        mIconLookupIntent = new Intent().setComponent(info.providerName);
659b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal        mDisabledForSafeMode = disabledForSafeMode;
660fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
67e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal        mPaint = new TextPaint();
68e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal        mPaint.setColor(0xFFFFFFFF);
69e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal        mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
702e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen                mLauncher.getDeviceProfile().iconTextSizePx, getResources().getDisplayMetrics()));
710fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        setBackgroundResource(R.drawable.quantum_panel_dark);
720fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        setWillNotDraw(false);
73ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
74ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
75ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    @Override
76ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth,
77ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal            int maxHeight) {
78ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        // No-op
79ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
80ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
81ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    @Override
82ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    protected View getDefaultView() {
83ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        if (mDefaultView == null) {
840fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mDefaultView = mInflater.inflate(R.layout.appwidget_not_ready, this, false);
85ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal            mDefaultView.setOnClickListener(this);
86ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal            applyState();
87ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        }
88ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        return mDefaultView;
89ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
90ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
91ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    @Override
92ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    public void setOnClickListener(OnClickListener l) {
93ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        mClickListener = l;
94ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
95ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
960fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    @Override
970fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    public boolean isReinflateRequired() {
980fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        // Re inflate is required any time the widget restore status changes
990fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        return mStartState != mInfo.restoreStatus;
100ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
101ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
102ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    @Override
1030fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1040fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        super.onSizeChanged(w, h, oldw, oldh);
1050fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        mDrawableSizeChanged = true;
106ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
107ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
1080fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    public void updateIcon(IconCache cache) {
1090fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        Bitmap icon = cache.getIcon(mIconLookupIntent, mInfo.user);
1100fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        if (mIcon == icon) {
1110fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            return;
1120fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        }
1130fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        mIcon = icon;
1149b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal        if (mCenterDrawable != null) {
1159b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            mCenterDrawable.setCallback(null);
1169b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            mCenterDrawable = null;
1170fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        }
1180fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        if (mIcon != null) {
1199b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            // The view displays three modes,
1209b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            //   1) App icon in the center
1219b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            //   2) Preload icon in the center
1229b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            //   3) Setup icon in the center and app icon in the top right corner.
1239b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            if (mDisabledForSafeMode) {
12453d7ee4285842732517edcccbdcaa92dccb1e107Sunny Goyal                FastBitmapDrawable disabledIcon = mLauncher.createIconDrawable(mIcon);
1259b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal                disabledIcon.setGhostModeEnabled(true);
1269b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal                mCenterDrawable = disabledIcon;
1279b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal                mTopCornerDrawable = null;
1289b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            } else if (isReadyForClickSetup()) {
1290fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                mCenterDrawable = getResources().getDrawable(R.drawable.ic_setting);
1300fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                mTopCornerDrawable = new FastBitmapDrawable(mIcon);
131ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal            } else {
1320fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                if (sPreloaderTheme == null) {
1330fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                    sPreloaderTheme = getResources().newTheme();
1340fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                    sPreloaderTheme.applyStyle(R.style.PreloadIcon, true);
1350fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                }
1360fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
13753d7ee4285842732517edcccbdcaa92dccb1e107Sunny Goyal                FastBitmapDrawable drawable = mLauncher.createIconDrawable(mIcon);
1389b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal                mCenterDrawable = new PreloadIconDrawable(drawable, sPreloaderTheme);
1399b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal                mCenterDrawable.setCallback(this);
1409b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal                mTopCornerDrawable = null;
1410fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                applyState();
142ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal            }
1430fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mDrawableSizeChanged = true;
1440fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        }
1450fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    }
1460fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
1470fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    @Override
1480fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    protected boolean verifyDrawable(Drawable who) {
1499b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal        return (who == mCenterDrawable) || super.verifyDrawable(who);
1500fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    }
1510fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
1520fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    public void applyState() {
1539b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal        if (mCenterDrawable != null) {
1549b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            mCenterDrawable.setLevel(Math.max(mInfo.installProgress, 0));
155ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        }
156ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
157ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
158ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    @Override
159ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    public void onClick(View v) {
160ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        // AppWidgetHostView blocks all click events on the root view. Instead handle click events
161ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        // on the content and pass it along.
162ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        if (mClickListener != null) {
163ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal            mClickListener.onClick(this);
164ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        }
165ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
166ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
167ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    public boolean isReadyForClickSetup() {
1680fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        return (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0
1690fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                && (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0;
170ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
1710fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
1720fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    @Override
1730fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    protected void onDraw(Canvas canvas) {
1749b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal        if (mCenterDrawable == null) {
1759b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            // Nothing to draw
1769b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            return;
1779b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal        }
1789b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal
1792e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        DeviceProfile grid = mLauncher.getDeviceProfile();
1809b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal        if (mTopCornerDrawable == null) {
1810fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            if (mDrawableSizeChanged) {
1829b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal                int outset = (mCenterDrawable instanceof PreloadIconDrawable) ?
1839b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal                        ((PreloadIconDrawable) mCenterDrawable).getOutset() : 0;
1842e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen                int maxSize = grid.iconSizePx + 2 * outset;
1850fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                int size = Math.min(maxSize, Math.min(
1860fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                        getWidth() - getPaddingLeft() - getPaddingRight(),
1870fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                        getHeight() - getPaddingTop() - getPaddingBottom()));
1880fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
1890fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                mRect.set(0, 0, size, size);
1909b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal                mRect.inset(outset, outset);
1910fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                mRect.offsetTo((getWidth() - mRect.width()) / 2, (getHeight() - mRect.height()) / 2);
1929b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal                mCenterDrawable.setBounds(mRect);
1930fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                mDrawableSizeChanged = false;
1940fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            }
1959b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            mCenterDrawable.draw(canvas);
1969b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal        } else  {
1979b4b0812fec41c40d81d507cfdb528ba87663f5fSunny Goyal            // Draw the top corner icon and "Setup" text is possible
1980fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            if (mDrawableSizeChanged) {
199e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                int iconSize = grid.iconSizePx;
2000fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                int paddingTop = getPaddingTop();
201e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                int paddingBottom = getPaddingBottom();
2020fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                int paddingLeft = getPaddingLeft();
203e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                int paddingRight = getPaddingRight();
204e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal
205e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                int availableWidth = getWidth() - paddingLeft - paddingRight;
206e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                int availableHeight = getHeight() - paddingTop - paddingBottom;
207e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal
208e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                // Recreate the setup text.
209e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                mSetupTextLayout = new StaticLayout(
210e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                        getResources().getText(R.string.gadget_setup_text), mPaint, availableWidth,
211e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                        Layout.Alignment.ALIGN_CENTER, 1, 0, true);
212e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                if (mSetupTextLayout.getLineCount() == 1) {
213e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    // The text fits in a single line. No need to draw the setup icon.
214e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    int size = Math.min(iconSize, Math.min(availableWidth,
215e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                            availableHeight - mSetupTextLayout.getHeight()));
216e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    mRect.set(0, 0, size, size);
217e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    mRect.offsetTo((getWidth() - mRect.width()) / 2,
218e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                            (getHeight() - mRect.height() - mSetupTextLayout.getHeight()
219e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                                    - grid.iconDrawablePaddingPx) / 2);
220e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal
221e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    mTopCornerDrawable.setBounds(mRect);
222e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal
223e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    // Update left and top to indicate the position where the text will be drawn.
224e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    mRect.left = paddingLeft;
225e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    mRect.top = mRect.bottom + grid.iconDrawablePaddingPx;
226e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                } else {
227e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    // The text can't be drawn in a single line. Draw a setup icon instead.
228e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    mSetupTextLayout = null;
229e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    int size = Math.min(iconSize, Math.min(
230e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                            getWidth() - paddingLeft - paddingRight,
231e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                            getHeight() - paddingTop - paddingBottom));
232e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    mRect.set(0, 0, size, size);
233e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    mRect.offsetTo((getWidth() - mRect.width()) / 2, (getHeight() - mRect.height()) / 2);
234e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    mCenterDrawable.setBounds(mRect);
235e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal
236e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    size = Math.min(size / 2,
237e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                            Math.max(mRect.top - paddingTop, mRect.left - paddingLeft));
238e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                    mTopCornerDrawable.setBounds(paddingLeft, paddingTop,
239e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                            paddingLeft + size, paddingTop + size);
240e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                }
2410fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                mDrawableSizeChanged = false;
2420fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            }
2430fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
244e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal            if (mSetupTextLayout == null) {
245e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                mCenterDrawable.draw(canvas);
246e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                mTopCornerDrawable.draw(canvas);
247e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal            } else {
248e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                canvas.save();
249e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                canvas.translate(mRect.left, mRect.top);
250e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                mSetupTextLayout.draw(canvas);
251e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                canvas.restore();
252e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal                mTopCornerDrawable.draw(canvas);
253e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal            }
2540fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        }
2550fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    }
256ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal}
257