2014-08-11 17:05:23 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-07-23 13:58:07 -07:00
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2014-08-11 17:05:23 -07:00
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.res.Resources.Theme;
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
2014-07-23 13:58:07 -07:00
|
|
|
import android.os.Bundle;
|
2014-08-27 14:04:33 -07:00
|
|
|
import android.text.Layout;
|
|
|
|
|
import android.text.StaticLayout;
|
|
|
|
|
import android.text.TextPaint;
|
|
|
|
|
import android.util.TypedValue;
|
2014-07-23 13:58:07 -07:00
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
|
|
|
|
|
|
public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implements OnClickListener {
|
|
|
|
|
|
2014-08-11 17:05:23 -07:00
|
|
|
private static Theme sPreloaderTheme;
|
2014-07-23 13:58:07 -07:00
|
|
|
|
2014-08-11 17:05:23 -07:00
|
|
|
private final Rect mRect = new Rect();
|
|
|
|
|
private View mDefaultView;
|
2014-07-23 13:58:07 -07:00
|
|
|
private OnClickListener mClickListener;
|
2014-08-11 17:05:23 -07:00
|
|
|
private final LauncherAppWidgetInfo mInfo;
|
|
|
|
|
private final int mStartState;
|
|
|
|
|
private final Intent mIconLookupIntent;
|
2014-10-08 10:47:28 -07:00
|
|
|
private final boolean mDisabledForSafeMode;
|
2014-08-11 17:05:23 -07:00
|
|
|
|
|
|
|
|
private Bitmap mIcon;
|
|
|
|
|
|
|
|
|
|
private Drawable mCenterDrawable;
|
|
|
|
|
private Drawable mTopCornerDrawable;
|
|
|
|
|
|
|
|
|
|
private boolean mDrawableSizeChanged;
|
2014-07-23 13:58:07 -07:00
|
|
|
|
2014-08-27 14:04:33 -07:00
|
|
|
private final TextPaint mPaint;
|
|
|
|
|
private Layout mSetupTextLayout;
|
|
|
|
|
|
2014-10-08 10:47:28 -07:00
|
|
|
public PendingAppWidgetHostView(Context context, LauncherAppWidgetInfo info,
|
|
|
|
|
boolean disabledForSafeMode) {
|
2014-07-23 13:58:07 -07:00
|
|
|
super(context);
|
2014-08-11 17:05:23 -07:00
|
|
|
mInfo = info;
|
|
|
|
|
mStartState = info.restoreStatus;
|
|
|
|
|
mIconLookupIntent = new Intent().setComponent(info.providerName);
|
2014-10-08 10:47:28 -07:00
|
|
|
mDisabledForSafeMode = disabledForSafeMode;
|
2014-08-11 17:05:23 -07:00
|
|
|
|
2014-08-27 14:04:33 -07:00
|
|
|
mPaint = new TextPaint();
|
|
|
|
|
mPaint.setColor(0xFFFFFFFF);
|
|
|
|
|
mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
|
|
|
|
|
getDeviceProfile().iconTextSizePx, getResources().getDisplayMetrics()));
|
2014-08-11 17:05:23 -07:00
|
|
|
setBackgroundResource(R.drawable.quantum_panel_dark);
|
|
|
|
|
setWillNotDraw(false);
|
2014-07-23 13:58:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth,
|
|
|
|
|
int maxHeight) {
|
|
|
|
|
// No-op
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected View getDefaultView() {
|
|
|
|
|
if (mDefaultView == null) {
|
2014-08-11 17:05:23 -07:00
|
|
|
mDefaultView = mInflater.inflate(R.layout.appwidget_not_ready, this, false);
|
2014-07-23 13:58:07 -07:00
|
|
|
mDefaultView.setOnClickListener(this);
|
|
|
|
|
applyState();
|
|
|
|
|
}
|
|
|
|
|
return mDefaultView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setOnClickListener(OnClickListener l) {
|
|
|
|
|
mClickListener = l;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-11 17:05:23 -07:00
|
|
|
@Override
|
|
|
|
|
public boolean isReinflateRequired() {
|
|
|
|
|
// Re inflate is required any time the widget restore status changes
|
|
|
|
|
return mStartState != mInfo.restoreStatus;
|
2014-07-23 13:58:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-08-11 17:05:23 -07:00
|
|
|
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
|
|
|
|
super.onSizeChanged(w, h, oldw, oldh);
|
|
|
|
|
mDrawableSizeChanged = true;
|
2014-07-23 13:58:07 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-11 17:05:23 -07:00
|
|
|
public void updateIcon(IconCache cache) {
|
|
|
|
|
Bitmap icon = cache.getIcon(mIconLookupIntent, mInfo.user);
|
|
|
|
|
if (mIcon == icon) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mIcon = icon;
|
2014-10-08 10:47:28 -07:00
|
|
|
if (mCenterDrawable != null) {
|
|
|
|
|
mCenterDrawable.setCallback(null);
|
|
|
|
|
mCenterDrawable = null;
|
2014-08-11 17:05:23 -07:00
|
|
|
}
|
|
|
|
|
if (mIcon != null) {
|
2014-10-08 10:47:28 -07:00
|
|
|
// The view displays three modes,
|
|
|
|
|
// 1) App icon in the center
|
|
|
|
|
// 2) Preload icon in the center
|
|
|
|
|
// 3) Setup icon in the center and app icon in the top right corner.
|
|
|
|
|
if (mDisabledForSafeMode) {
|
|
|
|
|
FastBitmapDrawable disabledIcon = Utilities.createIconDrawable(mIcon);
|
|
|
|
|
disabledIcon.setGhostModeEnabled(true);
|
|
|
|
|
mCenterDrawable = disabledIcon;
|
|
|
|
|
mTopCornerDrawable = null;
|
|
|
|
|
} else if (isReadyForClickSetup()) {
|
2014-08-11 17:05:23 -07:00
|
|
|
mCenterDrawable = getResources().getDrawable(R.drawable.ic_setting);
|
|
|
|
|
mTopCornerDrawable = new FastBitmapDrawable(mIcon);
|
2014-07-23 13:58:07 -07:00
|
|
|
} else {
|
2014-08-11 17:05:23 -07:00
|
|
|
if (sPreloaderTheme == null) {
|
|
|
|
|
sPreloaderTheme = getResources().newTheme();
|
|
|
|
|
sPreloaderTheme.applyStyle(R.style.PreloadIcon, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FastBitmapDrawable drawable = Utilities.createIconDrawable(mIcon);
|
2014-10-08 10:47:28 -07:00
|
|
|
mCenterDrawable = new PreloadIconDrawable(drawable, sPreloaderTheme);
|
|
|
|
|
mCenterDrawable.setCallback(this);
|
|
|
|
|
mTopCornerDrawable = null;
|
2014-08-11 17:05:23 -07:00
|
|
|
applyState();
|
2014-07-23 13:58:07 -07:00
|
|
|
}
|
2014-08-11 17:05:23 -07:00
|
|
|
mDrawableSizeChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected boolean verifyDrawable(Drawable who) {
|
2014-10-08 10:47:28 -07:00
|
|
|
return (who == mCenterDrawable) || super.verifyDrawable(who);
|
2014-08-11 17:05:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void applyState() {
|
2014-10-08 10:47:28 -07:00
|
|
|
if (mCenterDrawable != null) {
|
|
|
|
|
mCenterDrawable.setLevel(Math.max(mInfo.installProgress, 0));
|
2014-07-23 13:58:07 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
// AppWidgetHostView blocks all click events on the root view. Instead handle click events
|
|
|
|
|
// on the content and pass it along.
|
|
|
|
|
if (mClickListener != null) {
|
|
|
|
|
mClickListener.onClick(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isReadyForClickSetup() {
|
2014-08-11 17:05:23 -07:00
|
|
|
return (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0
|
|
|
|
|
&& (mInfo.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0;
|
2014-07-23 13:58:07 -07:00
|
|
|
}
|
2014-08-11 17:05:23 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDraw(Canvas canvas) {
|
2014-10-08 10:47:28 -07:00
|
|
|
if (mCenterDrawable == null) {
|
|
|
|
|
// Nothing to draw
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mTopCornerDrawable == null) {
|
2014-08-11 17:05:23 -07:00
|
|
|
if (mDrawableSizeChanged) {
|
2014-10-08 10:47:28 -07:00
|
|
|
int outset = (mCenterDrawable instanceof PreloadIconDrawable) ?
|
|
|
|
|
((PreloadIconDrawable) mCenterDrawable).getOutset() : 0;
|
2014-08-11 17:05:23 -07:00
|
|
|
int maxSize = LauncherAppState.getInstance().getDynamicGrid()
|
2014-10-08 10:47:28 -07:00
|
|
|
.getDeviceProfile().iconSizePx + 2 * outset;
|
2014-08-11 17:05:23 -07:00
|
|
|
int size = Math.min(maxSize, Math.min(
|
|
|
|
|
getWidth() - getPaddingLeft() - getPaddingRight(),
|
|
|
|
|
getHeight() - getPaddingTop() - getPaddingBottom()));
|
|
|
|
|
|
|
|
|
|
mRect.set(0, 0, size, size);
|
2014-10-08 10:47:28 -07:00
|
|
|
mRect.inset(outset, outset);
|
2014-08-11 17:05:23 -07:00
|
|
|
mRect.offsetTo((getWidth() - mRect.width()) / 2, (getHeight() - mRect.height()) / 2);
|
2014-10-08 10:47:28 -07:00
|
|
|
mCenterDrawable.setBounds(mRect);
|
2014-08-11 17:05:23 -07:00
|
|
|
mDrawableSizeChanged = false;
|
|
|
|
|
}
|
2014-10-08 10:47:28 -07:00
|
|
|
mCenterDrawable.draw(canvas);
|
|
|
|
|
} else {
|
|
|
|
|
// Draw the top corner icon and "Setup" text is possible
|
2014-08-11 17:05:23 -07:00
|
|
|
if (mDrawableSizeChanged) {
|
2014-08-27 14:04:33 -07:00
|
|
|
DeviceProfile grid = getDeviceProfile();
|
|
|
|
|
int iconSize = grid.iconSizePx;
|
2014-08-11 17:05:23 -07:00
|
|
|
int paddingTop = getPaddingTop();
|
2014-08-27 14:04:33 -07:00
|
|
|
int paddingBottom = getPaddingBottom();
|
2014-08-11 17:05:23 -07:00
|
|
|
int paddingLeft = getPaddingLeft();
|
2014-08-27 14:04:33 -07:00
|
|
|
int paddingRight = getPaddingRight();
|
|
|
|
|
|
|
|
|
|
int availableWidth = getWidth() - paddingLeft - paddingRight;
|
|
|
|
|
int availableHeight = getHeight() - paddingTop - paddingBottom;
|
|
|
|
|
|
|
|
|
|
// Recreate the setup text.
|
|
|
|
|
mSetupTextLayout = new StaticLayout(
|
|
|
|
|
getResources().getText(R.string.gadget_setup_text), mPaint, availableWidth,
|
|
|
|
|
Layout.Alignment.ALIGN_CENTER, 1, 0, true);
|
|
|
|
|
if (mSetupTextLayout.getLineCount() == 1) {
|
|
|
|
|
// The text fits in a single line. No need to draw the setup icon.
|
|
|
|
|
int size = Math.min(iconSize, Math.min(availableWidth,
|
|
|
|
|
availableHeight - mSetupTextLayout.getHeight()));
|
|
|
|
|
mRect.set(0, 0, size, size);
|
|
|
|
|
mRect.offsetTo((getWidth() - mRect.width()) / 2,
|
|
|
|
|
(getHeight() - mRect.height() - mSetupTextLayout.getHeight()
|
|
|
|
|
- grid.iconDrawablePaddingPx) / 2);
|
|
|
|
|
|
|
|
|
|
mTopCornerDrawable.setBounds(mRect);
|
|
|
|
|
|
|
|
|
|
// Update left and top to indicate the position where the text will be drawn.
|
|
|
|
|
mRect.left = paddingLeft;
|
|
|
|
|
mRect.top = mRect.bottom + grid.iconDrawablePaddingPx;
|
|
|
|
|
} else {
|
|
|
|
|
// The text can't be drawn in a single line. Draw a setup icon instead.
|
|
|
|
|
mSetupTextLayout = null;
|
|
|
|
|
int size = Math.min(iconSize, Math.min(
|
|
|
|
|
getWidth() - paddingLeft - paddingRight,
|
|
|
|
|
getHeight() - paddingTop - paddingBottom));
|
|
|
|
|
mRect.set(0, 0, size, size);
|
|
|
|
|
mRect.offsetTo((getWidth() - mRect.width()) / 2, (getHeight() - mRect.height()) / 2);
|
|
|
|
|
mCenterDrawable.setBounds(mRect);
|
|
|
|
|
|
|
|
|
|
size = Math.min(size / 2,
|
|
|
|
|
Math.max(mRect.top - paddingTop, mRect.left - paddingLeft));
|
|
|
|
|
mTopCornerDrawable.setBounds(paddingLeft, paddingTop,
|
|
|
|
|
paddingLeft + size, paddingTop + size);
|
|
|
|
|
}
|
2014-08-11 17:05:23 -07:00
|
|
|
mDrawableSizeChanged = false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 14:04:33 -07:00
|
|
|
if (mSetupTextLayout == null) {
|
|
|
|
|
mCenterDrawable.draw(canvas);
|
|
|
|
|
mTopCornerDrawable.draw(canvas);
|
|
|
|
|
} else {
|
|
|
|
|
canvas.save();
|
|
|
|
|
canvas.translate(mRect.left, mRect.top);
|
|
|
|
|
mSetupTextLayout.draw(canvas);
|
|
|
|
|
canvas.restore();
|
|
|
|
|
mTopCornerDrawable.draw(canvas);
|
|
|
|
|
}
|
2014-08-11 17:05:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 14:04:33 -07:00
|
|
|
private DeviceProfile getDeviceProfile() {
|
|
|
|
|
return LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile();
|
|
|
|
|
}
|
2014-07-23 13:58:07 -07:00
|
|
|
}
|