Search in sources :

Example 6 with LauncherAppWidgetProviderInfo

use of com.android.launcher3.widget.LauncherAppWidgetProviderInfo in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetHostViewLoader method preloadWidget.

/**
 * Start preloading the widget.
 */
private boolean preloadWidget() {
    final LauncherAppWidgetProviderInfo pInfo = mInfo.info;
    if (pInfo.isCustomWidget()) {
        return false;
    }
    final Bundle options = mInfo.getDefaultSizeOptions(mLauncher);
    // If there is a configuration activity, do not follow thru bound and inflate.
    if (mInfo.getHandler().needsConfigure()) {
        mInfo.bindOptions = options;
        return false;
    }
    mBindWidgetRunnable = new Runnable() {

        @Override
        public void run() {
            mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
            if (LOGD) {
                Log.d(TAG, "Binding widget, id: " + mWidgetLoadingId);
            }
            if (new WidgetManagerHelper(mLauncher).bindAppWidgetIdIfAllowed(mWidgetLoadingId, pInfo, options)) {
                // Widget id bound. Inflate the widget.
                mHandler.post(mInflateWidgetRunnable);
            }
        }
    };
    mInflateWidgetRunnable = new Runnable() {

        @Override
        public void run() {
            if (LOGD) {
                Log.d(TAG, "Inflating widget, id: " + mWidgetLoadingId);
            }
            if (mWidgetLoadingId == -1) {
                return;
            }
            AppWidgetHostView hostView = mLauncher.getAppWidgetHost().createView((Context) mLauncher, mWidgetLoadingId, pInfo);
            mInfo.boundWidget = hostView;
            // We used up the widget Id in binding the above view.
            mWidgetLoadingId = -1;
            hostView.setVisibility(View.INVISIBLE);
            int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(mInfo);
            // We want the first widget layout to be the correct size. This will be important
            // for width size reporting to the AppWidgetManager.
            DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0], unScaledSize[1]);
            lp.x = lp.y = 0;
            lp.customPosition = true;
            hostView.setLayoutParams(lp);
            if (LOGD) {
                Log.d(TAG, "Adding host view to drag layer");
            }
            mLauncher.getDragLayer().addView(hostView);
            mView.setTag(mInfo);
        }
    };
    if (LOGD) {
        Log.d(TAG, "About to bind/inflate widget");
    }
    mHandler.post(mBindWidgetRunnable);
    return true;
}
Also used : Context(android.content.Context) AppWidgetHostView(android.appwidget.AppWidgetHostView) DragLayer(com.android.launcher3.dragndrop.DragLayer) Bundle(android.os.Bundle)

Example 7 with LauncherAppWidgetProviderInfo

use of com.android.launcher3.widget.LauncherAppWidgetProviderInfo in project android_packages_apps_Launcher3 by crdroidandroid.

the class LauncherAppWidgetProviderInfoTest method isMinSizeFulfilled_minWidthAndHeightWithinGridSize_shouldReturnTrue.

@Test
public void isMinSizeFulfilled_minWidthAndHeightWithinGridSize_shouldReturnTrue() {
    LauncherAppWidgetProviderInfo info = new LauncherAppWidgetProviderInfo();
    info.minWidth = 80;
    info.minHeight = 80;
    info.minResizeWidth = 50;
    info.minResizeHeight = 50;
    InvariantDeviceProfile idp = createIDP();
    info.initSpans(mContext, idp);
    assertThat(info.isMinSizeFulfilled()).isTrue();
}
Also used : InvariantDeviceProfile(com.android.launcher3.InvariantDeviceProfile) Test(org.junit.Test)

Example 8 with LauncherAppWidgetProviderInfo

use of com.android.launcher3.widget.LauncherAppWidgetProviderInfo in project android_packages_apps_Launcher3 by crdroidandroid.

the class Launcher method inflateAppWidget.

private View inflateAppWidget(LauncherAppWidgetInfo item) {
    if (item.hasOptionFlag(LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET)) {
        item.providerName = QsbContainerView.getSearchComponentName(this);
        if (item.providerName == null) {
            getModelWriter().deleteItemFromDatabase(item);
            return null;
        }
    }
    final AppWidgetHostView view;
    if (mIsSafeModeEnabled) {
        view = new PendingAppWidgetHostView(this, item, mIconCache, true);
        prepareAppWidget(view, item);
        return view;
    }
    Object traceToken = TraceHelper.INSTANCE.beginSection("BIND_WIDGET_id=" + item.appWidgetId);
    try {
        final LauncherAppWidgetProviderInfo appWidgetInfo;
        String removalReason = "";
        if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
            // If the provider is not ready, bind as a pending widget.
            appWidgetInfo = null;
            removalReason = "the provider isn't ready.";
        } 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);
            if (appWidgetInfo == null) {
                if (WidgetsModel.GO_DISABLE_WIDGETS) {
                    removalReason = "widgets are disabled on go device.";
                } else {
                    removalReason = "WidgetManagerHelper cannot find a provider from provider info.";
                }
            }
        } else {
            appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
            if (appWidgetInfo == null) {
                if (item.appWidgetId <= LauncherAppWidgetInfo.CUSTOM_WIDGET_ID) {
                    removalReason = "CustomWidgetManager cannot find provider from that widget id.";
                } else {
                    removalReason = "AppWidgetManager cannot find provider for that widget id." + " It could be because AppWidgetService is not available, or the" + " appWidgetId has not been bound to a the provider yet, or you" + " don't have access to that 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) {
                FileLog.d(TAG, "Removing restored widget: id=" + item.appWidgetId + " belongs to component " + item.providerName + " user " + item.user + ", as the provider is null and " + removalReason);
                getModelWriter().deleteItemFromDatabase(item);
                return null;
            }
            // If we do not have a valid id, try to bind an id.
            if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
                if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
                    // Id has not been allocated yet. Allocate a new id.
                    item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
                    item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
                    // Also try to bind the widget. If the bind fails, the user will be shown
                    // a click to setup UI, which will ask for the bind permission.
                    PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, item.sourceContainer);
                    pendingInfo.spanX = item.spanX;
                    pendingInfo.spanY = item.spanY;
                    pendingInfo.minSpanX = item.minSpanX;
                    pendingInfo.minSpanY = item.minSpanY;
                    Bundle options = pendingInfo.getDefaultSizeOptions(this);
                    boolean isDirectConfig = item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
                    if (isDirectConfig && item.bindOptions != null) {
                        Bundle newOptions = item.bindOptions.getExtras();
                        if (options != null) {
                            newOptions.putAll(options);
                        }
                        options = newOptions;
                    }
                    boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(item.appWidgetId, appWidgetInfo, options);
                    // We tried to bind once. If we were not able to bind, we would need to
                    // go through the permission dialog, which means we cannot skip the config
                    // activity.
                    item.bindOptions = null;
                    item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
                    // Bind succeeded
                    if (success) {
                        // If the widget has a configure activity, it is still needs to set it
                        // up, otherwise the widget is ready to go.
                        item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig ? LauncherAppWidgetInfo.RESTORE_COMPLETED : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
                    }
                    getModelWriter().updateItemInDatabase(item);
                }
            } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY) && (appWidgetInfo.configure == null)) {
                // The widget was marked as UI not ready, but there is no configure activity to
                // update the UI.
                item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
                getModelWriter().updateItemInDatabase(item);
            } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY) && appWidgetInfo.configure != null) {
                if (mAppWidgetManager.isAppWidgetRestored(item.appWidgetId)) {
                    item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
                    getModelWriter().updateItemInDatabase(item);
                }
            }
        }
        if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
            // Verify that we own the widget
            if (appWidgetInfo == null) {
                FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
                getModelWriter().deleteWidgetInfo(item, getAppWidgetHost());
                return null;
            }
            item.minSpanX = appWidgetInfo.minSpanX;
            item.minSpanY = appWidgetInfo.minSpanY;
            view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
        } else if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) && appWidgetInfo != null) {
            mAppWidgetHost.addPendingView(item.appWidgetId, new PendingAppWidgetHostView(this, item, mIconCache, false));
            view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
        } else {
            view = new PendingAppWidgetHostView(this, item, mIconCache, false);
        }
        prepareAppWidget(view, item);
    } finally {
        TraceHelper.INSTANCE.endSection(traceToken);
    }
    return view;
}
Also used : LauncherAppWidgetProviderInfo(com.android.launcher3.widget.LauncherAppWidgetProviderInfo) PendingAppWidgetHostView(com.android.launcher3.widget.PendingAppWidgetHostView) LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) AppWidgetHostView(android.appwidget.AppWidgetHostView) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) Bundle(android.os.Bundle) DragObject(com.android.launcher3.DropTarget.DragObject) PendingAppWidgetHostView(com.android.launcher3.widget.PendingAppWidgetHostView)

Example 9 with LauncherAppWidgetProviderInfo

use of com.android.launcher3.widget.LauncherAppWidgetProviderInfo in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsTableUtilsTest method initTestWidgets.

private void initTestWidgets() {
    List<Point> widgetSizes = List.of(new Point(1, 1), new Point(2, 2), new Point(2, 3), new Point(2, 4), new Point(4, 4));
    ArrayList<WidgetItem> widgetItems = new ArrayList<>();
    widgetSizes.stream().forEach(widgetSize -> {
        ShadowPackageManager packageManager = shadowOf(mContext.getPackageManager());
        AppWidgetProviderInfo info = new AppWidgetProviderInfo();
        info.provider = ComponentName.createRelative(TEST_PACKAGE, ".WidgetProvider_" + widgetSize.x + "x" + widgetSize.y);
        LauncherAppWidgetProviderInfo widgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
        widgetInfo.spanX = widgetSize.x;
        widgetInfo.spanY = widgetSize.y;
        ReflectionHelpers.setField(widgetInfo, "providerInfo", packageManager.addReceiverIfNotPresent(widgetInfo.provider));
        widgetItems.add(new WidgetItem(widgetInfo, mTestProfile, mIconCache));
    });
    mWidget1x1 = widgetItems.get(0);
    mWidget2x2 = widgetItems.get(1);
    mWidget2x3 = widgetItems.get(2);
    mWidget2x4 = widgetItems.get(3);
    mWidget4x4 = widgetItems.get(4);
}
Also used : LauncherAppWidgetProviderInfo(com.android.launcher3.widget.LauncherAppWidgetProviderInfo) ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) ArrayList(java.util.ArrayList) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) LauncherAppWidgetProviderInfo(com.android.launcher3.widget.LauncherAppWidgetProviderInfo) WidgetItem(com.android.launcher3.model.WidgetItem) Point(android.graphics.Point)

Example 10 with LauncherAppWidgetProviderInfo

use of com.android.launcher3.widget.LauncherAppWidgetProviderInfo in project android_packages_apps_Launcher3 by crdroidandroid.

the class BindWidgetTest method testPendingWidget_autoRestored.

@Test
public void testPendingWidget_autoRestored() {
    // A non-restored widget with no config screen gets restored automatically.
    LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(this, false);
    // Do not bind the widget
    LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), false);
    item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID;
    addItemToScreen(item);
    verifyWidgetPresent(info);
}
Also used : LauncherAppWidgetProviderInfo(com.android.launcher3.widget.LauncherAppWidgetProviderInfo) LauncherAppWidgetInfo(com.android.launcher3.model.data.LauncherAppWidgetInfo) AbstractLauncherUiTest(com.android.launcher3.ui.AbstractLauncherUiTest) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)89 LauncherAppWidgetProviderInfo (com.android.launcher3.widget.LauncherAppWidgetProviderInfo)65 InvariantDeviceProfile (com.android.launcher3.InvariantDeviceProfile)62 LauncherAppWidgetInfo (com.android.launcher3.model.data.LauncherAppWidgetInfo)54 SmallTest (androidx.test.filters.SmallTest)39 LargeTest (androidx.test.filters.LargeTest)36 AbstractLauncherUiTest (com.android.launcher3.ui.AbstractLauncherUiTest)36 LauncherAppWidgetProviderInfo (com.android.launcher3.LauncherAppWidgetProviderInfo)28 PendingAddWidgetInfo (com.android.launcher3.widget.PendingAddWidgetInfo)28 Bundle (android.os.Bundle)27 WidgetManagerHelper (com.android.launcher3.widget.WidgetManagerHelper)26 Point (android.graphics.Point)24 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)19 Context (android.content.Context)19 DeviceProfile (com.android.launcher3.DeviceProfile)19 ArrayList (java.util.ArrayList)18 AppWidgetHostView (android.appwidget.AppWidgetHostView)17 WidgetItem (com.android.launcher3.model.WidgetItem)14 LauncherAppWidgetHostView (com.android.launcher3.widget.LauncherAppWidgetHostView)12 WidgetAddFlowHandler (com.android.launcher3.widget.WidgetAddFlowHandler)12