Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2e1efb48 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Changing the widget loading strategy

Widget is loaded only when the user enters the overview mode and we keep
the list updated as long as the user is in the overview mode. Once the user
leaves the overview mode, we stop responding to widget updates

Bug: 26077457
Change-Id: I9e4904b8f1300bfe0d77e2bc5f59aa6963fad8d1
parent 726eb822
Loading
Loading
Loading
Loading
+54 −35
Original line number Diff line number Diff line
@@ -82,7 +82,6 @@ import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
@@ -1530,8 +1529,7 @@ public class Launcher extends Activity

        ItemInfo info = mPendingAddInfo;
        if (appWidgetInfo == null) {
            appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
                    mAppWidgetManager.getAppWidgetInfo(appWidgetId));
            appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
        }

        if (appWidgetInfo.isCustomWidget) {
@@ -2528,10 +2526,10 @@ public class Launcher extends Activity
        final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
        if (v.isReadyForClickSetup()) {
            int widgetId = info.appWidgetId;
            AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
            LauncherAppWidgetProviderInfo appWidgetInfo =
                    mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
            if (appWidgetInfo != null) {
                mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
                        this, appWidgetInfo);
                mPendingAddWidgetInfo = appWidgetInfo;
                mPendingAddInfo.copyFrom(info);
                mPendingAddWidgetId = widgetId;

@@ -3509,10 +3507,6 @@ public class Launcher extends Activity
        // TODO
    }

    protected void disableVoiceButtonProxy(boolean disable) {
        // NO-OP
    }

    public boolean launcherCallbacksProvidesSearch() {
        return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
    }
@@ -3926,6 +3920,16 @@ public class Launcher extends Activity
        sFolders = folders.clone();
    }

    private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
        PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
        view.updateIcon(mIconCache);
        item.hostView = view;
        item.hostView.updateAppWidget(null);
        item.hostView.setOnClickListener(this);
        addAppWidgetToWorkspace(item, null, false);
        mWorkspace.requestLayout();
    }

    /**
     * Add the views for a widget to the workspace.
     *
@@ -3941,19 +3945,31 @@ public class Launcher extends Activity
            return;
        }

        if (mIsSafeModeEnabled) {
            bindSafeModeWidget(item);
            return;
        }

        final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
        if (DEBUG_WIDGETS) {
            Log.d(TAG, "bindAppWidget: " + item);
        }
        final Workspace workspace = mWorkspace;

        LauncherAppWidgetProviderInfo appWidgetInfo =
                LauncherModel.getProviderInfo(this, item.providerName, item.user);
        final LauncherAppWidgetProviderInfo appWidgetInfo;

        if (!mIsSafeModeEnabled
                && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
                && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
        if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
            // If the provider is not ready, bind as a pending widget.
            appWidgetInfo = null;
        } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
            // The widget id is not valid. Try to find the widget based on the provider info.
            appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
        } else {
            appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
        }

        // If the provider is ready, but the width is not yet restored, try to restore it.
        if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
                (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
            if (appWidgetInfo == null) {
                if (DEBUG_WIDGETS) {
                    Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
@@ -3965,7 +3981,7 @@ public class Launcher extends Activity
            }

            // If we do not have a valid id, try to bind an id.
            if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
            if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
                // Note: This assumes that the id remap broadcast is received before this step.
                // If that is not the case, the id remap will be ignored and user may see the
                // click to setup view.
@@ -4001,46 +4017,42 @@ public class Launcher extends Activity
                        : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;

                LauncherModel.updateItemInDatabase(this, item);
            } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
            } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
                    && (appWidgetInfo.configure == null)) {
                // If the ID is already valid, verify if we need to configure or not.
                // The widget was marked as UI not ready, but there is no configure activity to
                // update the UI.
                item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
                LauncherModel.updateItemInDatabase(this, item);
            }
        }

        if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
            final int appWidgetId = item.appWidgetId;
        if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
            if (DEBUG_WIDGETS) {
                Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
                        + appWidgetInfo.provider);
            }

            // Verify that we own the widget
            AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
            if (info == null || appWidgetInfo == null ||
                    !info.provider.equals(appWidgetInfo.provider)) {
                Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId + " info=" + info
                        + " appWidgetInfo=" + appWidgetInfo);
            if (appWidgetInfo == null) {
                Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
                deleteWidgetInfo(item);
                return;
            }

            item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
            item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
            item.minSpanX = appWidgetInfo.minSpanX;
            item.minSpanY = appWidgetInfo.minSpanY;
            addAppWidgetToWorkspace(item, appWidgetInfo, false);
        } else {
            appWidgetInfo = null;
            PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
                    mIsSafeModeEnabled);
            view.updateIcon(mIconCache);
            item.hostView = view;
            item.hostView.updateAppWidget(null);
            item.hostView.setOnClickListener(this);
            addAppWidgetToWorkspace(item, null, false);
        }

        addAppWidgetToWorkspace(item, appWidgetInfo, false);
        workspace.requestLayout();
        mWorkspace.requestLayout();

        if (DEBUG_WIDGETS) {
            Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
@@ -4378,15 +4390,15 @@ public class Launcher extends Activity
        }
    }

    private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
    private Runnable mBindWidgetModelRunnable = new Runnable() {
            public void run() {
                bindAllPackages(mWidgetsModel);
                bindWidgetsModel(mWidgetsModel);
            }
        };

    @Override
    public void bindAllPackages(final WidgetsModel model) {
        if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
    public void bindWidgetsModel(WidgetsModel model) {
        if (waitUntilResume(mBindWidgetModelRunnable, true)) {
            mWidgetsModel = model;
            return;
        }
@@ -4397,6 +4409,13 @@ public class Launcher extends Activity
        }
    }

    @Override
    public void notifyWidgetProvidersChanged() {
        if (mWorkspace.getState().shouldUpdateWidget) {
            mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
        }
    }

    private int mapConfigurationOriActivityInfoOri(int configOri) {
        final Display d = getWindowManager().getDefaultDisplay();
        int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
+4 −0
Original line number Diff line number Diff line
@@ -102,6 +102,10 @@ public class LauncherAppWidgetHost extends AppWidgetHost {
                callback.run();
            }
        }

        if (Utilities.ATLEAST_MARSHMALLOW) {
            mLauncher.notifyWidgetProvidersChanged();
        }
    }

    public AppWidgetHostView createView(Context context, int appWidgetId,
+14 −11
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import com.android.launcher3.backup.nano.BackupProtos.Key;
import com.android.launcher3.backup.nano.BackupProtos.Resource;
import com.android.launcher3.backup.nano.BackupProtos.Screen;
import com.android.launcher3.backup.nano.BackupProtos.Widget;
import com.android.launcher3.compat.AppWidgetManagerCompat;
import com.android.launcher3.compat.UserHandleCompat;
import com.android.launcher3.compat.UserManagerCompat;
import com.android.launcher3.model.GridSizeMigrationTask;
@@ -660,12 +661,14 @@ public class LauncherBackupHelper implements BackupHelper {
                + getUserSelectionArg();
        Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION,
                where, null, null);
        AppWidgetManagerCompat widgetManager = AppWidgetManagerCompat.getInstance(mContext);
        try {
            cursor.moveToPosition(-1);
            while(cursor.moveToNext()) {
                final long id = cursor.getLong(ID_INDEX);
                final String providerName = cursor.getString(APPWIDGET_PROVIDER_INDEX);
                final ComponentName provider = ComponentName.unflattenFromString(providerName);

                Key key = null;
                String backupKey = null;
                if (provider != null) {
@@ -684,11 +687,14 @@ public class LauncherBackupHelper implements BackupHelper {
                } else if (backupKey != null) {
                    if (DEBUG) Log.d(TAG, "I can count this high: " + backupWidgetCount);
                    if (backupWidgetCount < MAX_WIDGETS_PER_PASS) {
                        LauncherAppWidgetProviderInfo widgetInfo = widgetManager
                                .getLauncherAppWidgetInfo(cursor.getInt(APPWIDGET_ID_INDEX));
                        if (widgetInfo != null) {
                            if (DEBUG) Log.d(TAG, "saving widget " + backupKey);
                        UserHandleCompat user = UserHandleCompat.myUserHandle();
                        writeRowToBackup(key, packWidget(dpi, provider, user), data);
                            writeRowToBackup(key, packWidget(dpi, widgetInfo), data);
                            mKeys.add(key);
                            backupWidgetCount ++;
                        }
                    } else {
                        if (VERBOSE) Log.v(TAG, "deferring widget backup " + backupKey);
                        // too many widgets for this pass, request another.
@@ -1004,16 +1010,14 @@ public class LauncherBackupHelper implements BackupHelper {
    }

    /** Serialize a widget for persistence, including a checksum wrapper. */
    private Widget packWidget(int dpi, ComponentName provider, UserHandleCompat user) {
        final LauncherAppWidgetProviderInfo info =
                LauncherModel.getProviderInfo(mContext, provider, user);
    private Widget packWidget(int dpi, LauncherAppWidgetProviderInfo info) {
        Widget widget = new Widget();
        widget.provider = provider.flattenToShortString();
        widget.provider = info.provider.flattenToShortString();
        widget.label = info.label;
        widget.configure = info.configure != null;
        if (info.icon != 0) {
            widget.icon = new Resource();
            Drawable fullResIcon = mIconCache.getFullResIcon(provider.getPackageName(), info.icon);
            Drawable fullResIcon = mIconCache.getFullResIcon(info.provider.getPackageName(), info.icon);
            Bitmap icon = Utilities.createIconBitmap(fullResIcon, mContext);
            widget.icon.data = Utilities.flattenBitmap(icon);
            widget.icon.dpi = dpi;
@@ -1022,7 +1026,6 @@ public class LauncherBackupHelper implements BackupHelper {
        Point spans = info.getMinSpans(mIdp, mContext);
        widget.minSpanX = spans.x;
        widget.minSpanY = spans.y;

        return widget;
    }

+41 −172

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -397,7 +397,7 @@ public class LauncherStateTransitionAnimation {

        // Animate the search bar
        final SearchDropTargetBar.State toSearchBarState =
                toWorkspaceState.getSearchDropTargetBarState();
                toWorkspaceState.searchDropTargetBarState;
        mLauncher.getSearchDropTargetBar().animateToState(toSearchBarState,
                animated ? revealDuration : 0, animation);

Loading