Search in sources :

Example 6 with AppWidgetHostView

use of android.appwidget.AppWidgetHostView in project Fairphone by Kwamecorp.

the class AppsCustomizePagedView method cleanupWidgetPreloading.

private void cleanupWidgetPreloading(boolean widgetWasAdded) {
    if (!widgetWasAdded) {
        // If the widget was not added, we may need to do further cleanup.
        PendingAddWidgetInfo info = mCreateWidgetInfo;
        mCreateWidgetInfo = null;
        if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
            // We never did any preloading, so just remove pending callbacks to do so
            removeCallbacks(mBindWidgetRunnable);
            removeCallbacks(mInflateWidgetRunnable);
        } else if (mWidgetCleanupState == WIDGET_BOUND) {
            // Delete the widget id which was allocated
            if (mWidgetLoadingId != -1) {
                mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
            }
            // We never got around to inflating the widget, so remove the callback to do so.
            removeCallbacks(mInflateWidgetRunnable);
        } else if (mWidgetCleanupState == WIDGET_INFLATED) {
            // Delete the widget id which was allocated
            if (mWidgetLoadingId != -1) {
                mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
            }
            // The widget was inflated and added to the DragLayer -- remove it.
            AppWidgetHostView widget = info.boundWidget;
            mLauncher.getDragLayer().removeView(widget);
        }
    }
    mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
    mWidgetLoadingId = -1;
    mCreateWidgetInfo = null;
    PagedViewWidget.resetShortPressTarget();
}
Also used : AppWidgetHostView(android.appwidget.AppWidgetHostView)

Example 7 with AppWidgetHostView

use of android.appwidget.AppWidgetHostView in project Fairphone by Kwamecorp.

the class AppsCustomizePagedView method preloadWidget.

private void preloadWidget(final PendingAddWidgetInfo info) {
    final AppWidgetProviderInfo pInfo = info.info;
    final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
    if (pInfo.configure != null) {
        info.bindOptions = options;
        return;
    }
    mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
    mBindWidgetRunnable = new Runnable() {

        @Override
        public void run() {
            mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
            // SDK level check.
            if (options == null) {
                if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) {
                    mWidgetCleanupState = WIDGET_BOUND;
                }
            } else {
                if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName, options)) {
                    mWidgetCleanupState = WIDGET_BOUND;
                }
            }
        }
    };
    post(mBindWidgetRunnable);
    mInflateWidgetRunnable = new Runnable() {

        @Override
        public void run() {
            if (mWidgetCleanupState != WIDGET_BOUND) {
                return;
            }
            AppWidgetHostView hostView = mLauncher.getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
            info.boundWidget = hostView;
            mWidgetCleanupState = WIDGET_INFLATED;
            hostView.setVisibility(INVISIBLE);
            int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX, info.spanY, info, false);
            // 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);
            mLauncher.getDragLayer().addView(hostView);
        }
    };
    post(mInflateWidgetRunnable);
}
Also used : AppWidgetHostView(android.appwidget.AppWidgetHostView) Bundle(android.os.Bundle) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo)

Example 8 with AppWidgetHostView

use of android.appwidget.AppWidgetHostView in project Fairphone by Kwamecorp.

the class Workspace method onDropExternal.

/**
     * Drop an item that didn't originate on one of the workspace screens.
     * It may have come from Launcher (e.g. from all apps or customize), or it may have
     * come from another app altogether.
     *
     * NOTE: This can also be called when we are outside of a drag event, when we want
     * to add an item to one of the workspace screens.
     */
private void onDropExternal(final int[] touchXY, final Object dragInfo, final CellLayout cellLayout, boolean insertAtFirst, DragObject d) {
    final Runnable exitSpringLoadedRunnable = new Runnable() {

        @Override
        public void run() {
            mLauncher.exitSpringLoadedDragModeDelayed(true, false, null);
        }
    };
    ItemInfo info = (ItemInfo) dragInfo;
    int spanX = info.spanX;
    int spanY = info.spanY;
    if (mDragInfo != null) {
        spanX = mDragInfo.spanX;
        spanY = mDragInfo.spanY;
    }
    final long container = mLauncher.isHotseatLayout(cellLayout) ? LauncherSettings.Favorites.CONTAINER_HOTSEAT : LauncherSettings.Favorites.CONTAINER_DESKTOP;
    final int screen = indexOfChild(cellLayout);
    if (!mLauncher.isHotseatLayout(cellLayout) && screen != mCurrentPage && mState != State.SPRING_LOADED) {
        snapToPage(screen);
    }
    if (info instanceof PendingAddItemInfo) {
        final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo;
        boolean findNearestVacantCell = true;
        if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
            mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY, cellLayout, mTargetCell);
            float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell);
            if (willCreateUserFolder((ItemInfo) d.dragInfo, cellLayout, mTargetCell, distance, true) || willAddToExistingUserFolder((ItemInfo) d.dragInfo, cellLayout, mTargetCell, distance)) {
                findNearestVacantCell = false;
            }
        }
        final ItemInfo item = (ItemInfo) d.dragInfo;
        boolean updateWidgetSize = false;
        if (findNearestVacantCell) {
            int minSpanX = item.spanX;
            int minSpanY = item.spanY;
            if (item.minSpanX > 0 && item.minSpanY > 0) {
                minSpanX = item.minSpanX;
                minSpanY = item.minSpanY;
            }
            int[] resultSpan = new int[2];
            mTargetCell = cellLayout.createArea((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], minSpanX, minSpanY, info.spanX, info.spanY, null, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP_EXTERNAL);
            if (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY) {
                updateWidgetSize = true;
            }
            item.spanX = resultSpan[0];
            item.spanY = resultSpan[1];
        }
        Runnable onAnimationCompleteRunnable = new Runnable() {

            @Override
            public void run() {
                // widgets/shortcuts/folders in a slightly different way
                switch(pendingInfo.itemType) {
                    case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
                        int[] span = new int[2];
                        span[0] = item.spanX;
                        span[1] = item.spanY;
                        mLauncher.addAppWidgetFromDrop((PendingAddWidgetInfo) pendingInfo, container, screen, mTargetCell, span, null);
                        break;
                    case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
                        mLauncher.processShortcutFromDrop(pendingInfo.componentName, container, screen, mTargetCell, null);
                        break;
                    default:
                        throw new IllegalStateException("Unknown item type: " + pendingInfo.itemType);
                }
            }
        };
        View finalView = pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ? ((PendingAddWidgetInfo) pendingInfo).boundWidget : null;
        if (finalView instanceof AppWidgetHostView && updateWidgetSize) {
            AppWidgetHostView awhv = (AppWidgetHostView) finalView;
            AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, item.spanX, item.spanY);
        }
        int animationStyle = ANIMATE_INTO_POSITION_AND_DISAPPEAR;
        if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET && ((PendingAddWidgetInfo) pendingInfo).info.configure != null) {
            animationStyle = ANIMATE_INTO_POSITION_AND_REMAIN;
        }
        animateWidgetDrop(info, cellLayout, d.dragView, onAnimationCompleteRunnable, animationStyle, finalView, true);
    } else {
        // This is for other drag/drop cases, like dragging from All Apps
        View view = null;
        switch(info.itemType) {
            case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
            case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
                if (info.container == NO_ID && info instanceof ApplicationInfo) {
                    // Came from all apps -- make a copy
                    info = new ShortcutInfo((ApplicationInfo) info);
                }
                view = mLauncher.createShortcut(R.layout.application, cellLayout, (ShortcutInfo) info);
                break;
            case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
                view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout, (FolderInfo) info, mIconCache);
                break;
            default:
                throw new IllegalStateException("Unknown item type: " + info.itemType);
        }
        // dropped, without any consideration to whether there is an item there.
        if (touchXY != null) {
            mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY, cellLayout, mTargetCell);
            float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell);
            d.postAnimationRunnable = exitSpringLoadedRunnable;
            if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, distance, true, d.dragView, d.postAnimationRunnable)) {
                return;
            }
            if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, distance, d, true)) {
                return;
            }
        }
        if (touchXY != null) {
            // when dragging and dropping, just find the closest free spot
            mTargetCell = cellLayout.createArea((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], 1, 1, 1, 1, null, mTargetCell, null, CellLayout.MODE_ON_DROP_EXTERNAL);
        } else {
            cellLayout.findCellForSpan(mTargetCell, 1, 1);
        }
        addInScreen(view, container, screen, mTargetCell[0], mTargetCell[1], info.spanX, info.spanY, insertAtFirst);
        cellLayout.onDropChild(view);
        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
        cellLayout.getShortcutsAndWidgets().measureChild(view);
        LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen, lp.cellX, lp.cellY);
        if (d.dragView != null) {
            // We wrap the animation call in the temporary set and reset of the current
            // cellLayout to its final transform -- this means we animate the drag view to
            // the correct final location.
            setFinalTransitionTransform(cellLayout);
            mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view, exitSpringLoadedRunnable);
            resetTransitionTransform(cellLayout);
        }
    }
}
Also used : ImageView(android.widget.ImageView) AppWidgetHostView(android.appwidget.AppWidgetHostView) View(android.view.View) TextView(android.widget.TextView) Point(android.graphics.Point) AppWidgetHostView(android.appwidget.AppWidgetHostView)

Example 9 with AppWidgetHostView

use of android.appwidget.AppWidgetHostView in project robolectric by robolectric.

the class ShadowAppWidgetHostTest method createView_shouldSetViewsContext.

@Test
public void createView_shouldSetViewsContext() throws Exception {
    AppWidgetHostView hostView = appWidgetHost.createView(context, 0, null);
    assertThat(hostView.getContext()).isSameAs(context);
}
Also used : AppWidgetHostView(android.appwidget.AppWidgetHostView) Test(org.junit.Test)

Example 10 with AppWidgetHostView

use of android.appwidget.AppWidgetHostView in project robolectric by robolectric.

the class ShadowAppWidgetHostTest method createView_shouldReturnAppWidgetHostView.

@Test
public void createView_shouldReturnAppWidgetHostView() throws Exception {
    AppWidgetHostView hostView = appWidgetHost.createView(context, 0, null);
    assertNotNull(hostView);
}
Also used : AppWidgetHostView(android.appwidget.AppWidgetHostView) Test(org.junit.Test)

Aggregations

AppWidgetHostView (android.appwidget.AppWidgetHostView)45 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)18 Point (android.graphics.Point)11 View (android.view.View)10 Context (android.content.Context)6 Intent (android.content.Intent)6 Bundle (android.os.Bundle)6 ImageView (android.widget.ImageView)6 TextView (android.widget.TextView)6 AppWidgetHost (android.appwidget.AppWidgetHost)5 LinearLayout (android.widget.LinearLayout)5 Test (org.junit.Test)5 Rect (android.graphics.Rect)4 Resources (android.content.res.Resources)3 Bitmap (android.graphics.Bitmap)3 RecognizerIntent (android.speech.RecognizerIntent)3 LauncherIntent (mobi.intuitit.android.content.LauncherIntent)3 SuppressLint (android.annotation.SuppressLint)2 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Paint (android.graphics.Paint)1