feat: Add option to clear home screen in settings (#6125)

Signed-off-by: abhixv <abhi.sharma1@hotmail.com>
This commit is contained in:
Abhishek Sharma authored and Pun Butrach committed 2026-01-10 20:51:01 +07:00
1 parent 9898749619
commit 5f3a03f4fb
1577 files changed
+112559 -80244

No files matched your search

@@ -21,7 +21,7 @@ import static android.graphics.Paint.DITHER_FLAG;
import static android.graphics.Paint.FILTER_BITMAP_FLAG;
import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon;
import static com.android.launcher3.model.data.LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
import static com.android.launcher3.icons.FastBitmapDrawable.getDisabledColorFilter;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import android.appwidget.AppWidgetProviderInfo;
@@ -37,9 +37,6 @@ import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
@@ -63,10 +60,8 @@ import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
import com.android.launcher3.model.data.ItemInfoWithIcon;
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.model.data.PackageItemInfo;
import com.android.launcher3.util.RunnableList;
import com.android.launcher3.util.SafeCloseable;
import com.android.launcher3.util.Themes;
import com.android.launcher3.widget.ListenableAppWidgetHost.ProviderChangedListener;
import java.util.List;
@@ -88,8 +83,6 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
private final Matrix mMatrix = new Matrix();
private final RectF mPreviewBitmapRect = new RectF();
private final RectF mCanvasRect = new RectF();
private final Handler mHandler = new Handler(Looper.getMainLooper());
private final RunnableList mOnDetachCleanup = new RunnableList();
private final LauncherWidgetHolder mWidgetHolder;
private final LauncherAppWidgetProviderInfo mAppwidget;
@@ -99,6 +92,7 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
private final CharSequence mLabel;
private OnClickListener mClickListener;
private SafeCloseable mOnDetachCleanup;
private int mDragFlags;
@@ -113,7 +107,8 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
private final Paint mPreviewPaint;
private Layout mSetupTextLayout;
@Nullable private Bitmap mPreviewBitmap;
@Nullable
private Bitmap mPreviewBitmap;
public PendingAppWidgetHostView(Context context, LauncherWidgetHolder widgetHolder,
LauncherAppWidgetInfo info, @Nullable LauncherAppWidgetProviderInfo appWidget) {
@@ -152,7 +147,8 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
}
/**
* Set {@link Bitmap} of widget preview and update background drawable. When showing preview
* Set {@link Bitmap} of widget preview and update background drawable. When
* showing preview
* bitmap, we shouldn't draw background.
*/
public void setPreviewBitmapAndUpdateBackground(@Nullable Bitmap previewBitmap) {
@@ -218,15 +214,16 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mOnDetachCleanup.executeAllAndClear();
if ((mAppwidget != null)
&& !mInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)
&& mInfo.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED) {
// If the widget is not completely restored, but has a valid ID, then listen of
// updates from provider app for potential restore complete.
SafeCloseable updateCleanup = mWidgetHolder.addOnUpdateListener(
if (mOnDetachCleanup != null) {
mOnDetachCleanup.close();
}
mOnDetachCleanup = mWidgetHolder.addOnUpdateListener(
mInfo.appWidgetId, mAppwidget, this::checkIfRestored);
mOnDetachCleanup.add(updateCleanup::close);
checkIfRestored();
}
}
@@ -234,7 +231,10 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mOnDetachCleanup.executeAllAndClear();
if (mOnDetachCleanup != null) {
mOnDetachCleanup.close();
mOnDetachCleanup = null;
}
}
/**
@@ -246,13 +246,14 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
}
LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) getTag();
if (info == null) {
// This occurs when LauncherAppWidgetHostView is used to render a preview layout.
// This occurs when LauncherAppWidgetHostView is used to render a preview
// layout.
return;
}
if (mActivityContext instanceof Launcher launcher) {
// Remove and rebind the current widget (which was inflated in the wrong
// orientation), but don't delete it from the database
launcher.removeItem(this, info, false /* deleteFromDb */,
launcher.removeItem(this, info, false /* deleteFromDb */,
"widget removed because of configuration change");
launcher.bindAppWidget(info);
}
@@ -299,30 +300,43 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
mCenterDrawable.setCallback(null);
mCenterDrawable = null;
}
mDragFlags = FLAG_DRAW_ICON;
mDragFlags = 0;
if (info.bitmap.icon != null) {
mDragFlags = FLAG_DRAW_ICON;
// The view displays three modes,
// 1) App icon in the center
// 2) Preload icon in the center
// 3) App icon in the center with a setup icon on the top left corner.
if (mDisabledForSafeMode) {
FastBitmapDrawable disabledIcon = info.newIcon(getContext());
disabledIcon.setIsDisabled(true);
mCenterDrawable = disabledIcon;
mSettingIconDrawable = null;
} else if (isReadyForClickSetup()) {
mCenterDrawable = info.newIcon(getContext());
mSettingIconDrawable = getResources().getDrawable(R.drawable.ic_setting).mutate();
updateSettingColor(info.bitmap.color);
Drawable widgetCategoryIcon = getWidgetCategoryIcon();
// The view displays three modes,
// 1) App icon in the center
// 2) Preload icon in the center
// 3) App icon in the center with a setup icon on the top left corner.
if (mDisabledForSafeMode) {
if (widgetCategoryIcon == null) {
FastBitmapDrawable disabledIcon = info.newIcon(getContext());
disabledIcon.setIsDisabled(true);
mCenterDrawable = disabledIcon;
} else {
widgetCategoryIcon.setColorFilter(getDisabledColorFilter());
mCenterDrawable = widgetCategoryIcon;
}
mSettingIconDrawable = null;
} else if (isReadyForClickSetup()) {
mCenterDrawable = widgetCategoryIcon == null
? info.newIcon(getContext())
: widgetCategoryIcon;
mSettingIconDrawable = getResources().getDrawable(R.drawable.ic_setting).mutate();
updateSettingColor(info.bitmap.color);
mDragFlags |= FLAG_DRAW_SETTINGS | FLAG_DRAW_LABEL;
} else {
mCenterDrawable = newPendingIcon(getContext(), info);
mSettingIconDrawable = null;
applyState();
mDragFlags |= FLAG_DRAW_SETTINGS | FLAG_DRAW_LABEL;
} else {
mCenterDrawable = widgetCategoryIcon == null
? newPendingIcon(getContext(), info)
: widgetCategoryIcon;
mSettingIconDrawable = null;
applyState();
}
mCenterDrawable.setCallback(this);
mDrawableSizeChanged = true;
}
mCenterDrawable.setCallback(this);
mDrawableSizeChanged = true;
invalidate();
}
@@ -332,7 +346,7 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
Color.colorToHSV(dominantColor, hsv);
hsv[1] = Math.min(hsv[1], MIN_SATURATION);
hsv[2] = 1;
mSettingIconDrawable.setColorFilter(Color.HSVToColor(hsv), PorterDuff.Mode.SRC_IN);
mSettingIconDrawable.setColorFilter(Color.HSVToColor(hsv), PorterDuff.Mode.SRC_IN);
}
@Override
@@ -341,11 +355,6 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
}
public void applyState() {
if (mCenterDrawable instanceof FastBitmapDrawable fb
&& mInfo.pendingItemInfo != null
&& !fb.isSameInfo(mInfo.pendingItemInfo.bitmap)) {
reapplyItemInfo(mInfo.pendingItemInfo);
}
if (mCenterDrawable != null) {
mCenterDrawable.setLevel(Math.max(mInfo.installProgress, 0));
}
@@ -353,7 +362,8 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
@Override
public void onClick(View v) {
// AppWidgetHostView blocks all click events on the root view. Instead handle click events
// 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);
@@ -362,17 +372,19 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
/**
* A pending widget is ready for setup after the provider is installed and
* 1) Widget id is not valid: the widget id is not yet bound to the provider, probably
* because the launcher doesn't have appropriate permissions.
* Note that we would still have an allocated id as that does not
* require any permissions and can be done during view inflation.
* 2) UI is not ready: the id is valid and the bound. But the widget has a configure activity
* which needs to be called once.
* 1) Widget id is not valid: the widget id is not yet bound to the provider,
* probably
* because the launcher doesn't have appropriate permissions.
* Note that we would still have an allocated id as that does not
* require any permissions and can be done during view inflation.
* 2) UI is not ready: the id is valid and the bound. But the widget has a
* configure activity
* which needs to be called once.
*/
public boolean isReadyForClickSetup() {
return !mInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
&& (mInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
|| mInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID));
|| mInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID));
}
private void updateDrawableBounds() {
@@ -390,7 +402,8 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
float iconSize = ((mDragFlags & FLAG_DRAW_ICON) == 0) ? 0
: Math.max(0, Math.min(availableWidth, availableHeight));
// Use twice the setting size factor, as the setting is drawn at a corner and the
// Use twice the setting size factor, as the setting is drawn at a corner and
// the
// icon is drawn in the center.
float settingIconScaleFactor = ((mDragFlags & FLAG_DRAW_SETTINGS) == 0) ? 0
: 1 + SETUP_ICON_SIZE_FACTOR * 2;
@@ -482,72 +495,18 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
}
/**
* Creates a runnable runnable which tries to refresh the widget if it is restored
*/
public void postProviderAvailabilityCheck() {
if (!mInfo.hasRestoreFlag(FLAG_PROVIDER_NOT_READY) && getAppWidgetInfo() == null) {
// If the info state suggests that the provider is ready, but there is no
// provider info attached on this pending view, recreate when the provider is available
DeferredWidgetRefresh restoreRunnable = new DeferredWidgetRefresh();
mOnDetachCleanup.add(restoreRunnable::cleanup);
mHandler.post(restoreRunnable::notifyWidgetProvidersChanged);
}
}
/**
* Used as a workaround to ensure that the AppWidgetService receives the
* PACKAGE_ADDED broadcast before updating widgets.
* Returns the widget category icon for {@link #mInfo}.
*
* This class will periodically check for the availability of the WidgetProvider as a result
* of providerChanged callback from the host. When the provider is available or a timeout of
* 10-sec is reached, it reinflates the pending-widget which in-turn goes through the process
* of re-evaluating the pending state of the widget,
* <p>
* If {@link #mInfo}'s category is {@code PackageItemInfo#NO_CATEGORY} or
* unknown, returns
* {@code null}.
*/
private class DeferredWidgetRefresh implements Runnable, ProviderChangedListener {
private boolean mRefreshPending = true;
DeferredWidgetRefresh() {
mWidgetHolder.addProviderChangeListener(this);
// Force refresh after 10 seconds, if we don't get the provider changed event.
// This could happen when the provider is no longer available in the app.
Message msg = Message.obtain(getHandler(), this);
msg.obj = DeferredWidgetRefresh.class;
mHandler.sendMessageDelayed(msg, 10000);
}
/**
* Reinflate the widget if it is still attached.
*/
@Override
public void run() {
cleanup();
if (mRefreshPending) {
reInflate();
mRefreshPending = false;
}
}
@Override
public void notifyWidgetProvidersChanged() {
final AppWidgetProviderInfo widgetInfo;
WidgetManagerHelper widgetHelper = new WidgetManagerHelper(getContext());
if (mInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
widgetInfo = widgetHelper.findProvider(mInfo.providerName, mInfo.user);
} else {
widgetInfo = widgetHelper.getLauncherAppWidgetInfo(mInfo.appWidgetId,
mInfo.getTargetComponent());
}
if (widgetInfo != null) {
run();
}
}
/**
* Removes any scheduled callbacks and change listeners, no-op if nothing is scheduled
*/
public void cleanup() {
mWidgetHolder.removeProviderChangeListener(this);
mHandler.removeCallbacks(this);
@Nullable
private Drawable getWidgetCategoryIcon() {
if (mInfo.pendingItemInfo.widgetCategory == WidgetSections.NO_CATEGORY) {
return null;
}
return mInfo.pendingItemInfo.newIcon(getContext());
}
}