Search in sources :

Example 26 with AppWidgetHostView

use of android.appwidget.AppWidgetHostView in project ADWLauncher2 by boombuler.

the class WidgetSpace method unbindWidgetScrollableId.

public synchronized boolean unbindWidgetScrollableId(int widgetId) {
    Log.d(TAG, "trying to completely unallocate widget ID=" + widgetId);
    CharSequence keyToDelete = null;
    for (ScrollViewInfos item : mScrollViewCursorInfos.values()) {
        Log.d(TAG, "Comparing widget ID=" + item.widgetId);
        if (item.widgetId == widgetId) {
            Log.d(TAG, "trying to completely unallocate widget stuff...");
            AppWidgetHostView widgetView = findWidget(widgetId);
            Log.d(TAG, "Widget view to KILL:" + widgetView);
            Context remoteContext = null;
            if (widgetView != null) {
                AppWidgetProviderInfo info = widgetView.getAppWidgetInfo();
                if (info != null && info.provider != null) {
                    final String packageName = info.provider.getPackageName();
                    try {
                        remoteContext = getContext().createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
                    } catch (NameNotFoundException e) {
                        Log.e(TAG, "couldn't find widget id:" + widgetId);
                    }
                } else
                    remoteContext = null;
            }
            if (remoteContext == null)
                remoteContext = getContext();
            if (item.lv != null) {
                Log.d(TAG, "Trying to KILL the ListView...");
                if (item.lvAdapter != null) {
                    item.lvAdapter.dropCache(remoteContext);
                }
                item.lv.setAdapter(null);
                item.lv = null;
            }
            remoteContext.getContentResolver().unregisterContentObserver(item.obs);
            item.obsHandler = null;
            item.obs = null;
            keyToDelete = item.key;
            item = null;
            ListViewImageManager.getInstance().clearCacheForWidget(getContext(), widgetId);
        }
    }
    if (keyToDelete != null)
        mScrollViewCursorInfos.remove(keyToDelete);
    ListViewImageManager.getInstance().unbindDrawables();
    if (FORCE_FREE_MEMORY) {
        System.gc();
    }
    Log.d(TAG, "AFTER REMOVING, Our Scrollable widgets array contains:" + mScrollViewCursorInfos.size());
    return false;
}
Also used : Context(android.content.Context) AppWidgetHostView(android.appwidget.AppWidgetHostView) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo)

Example 27 with AppWidgetHostView

use of android.appwidget.AppWidgetHostView in project Launcher3 by chislon.

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 long screenId = getIdForScreen(cellLayout);
    if (!mLauncher.isHotseatLayout(cellLayout) && screenId != getScreenIdForPageIndex(mCurrentPage) && mState != State.SPRING_LOADED) {
        snapToScreenId(screenId, null);
    }
    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, screenId, mTargetCell, span, null);
                        break;
                    case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
                        mLauncher.processShortcutFromDrop(pendingInfo.componentName, container, screenId, 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 AppInfo) {
                    // Came from all apps -- make a copy
                    info = new ShortcutInfo((AppInfo) 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, screenId, 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, screenId, 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 : View(android.view.View) TextView(android.widget.TextView) AppWidgetHostView(android.appwidget.AppWidgetHostView) Point(android.graphics.Point) AppWidgetHostView(android.appwidget.AppWidgetHostView)

Example 28 with AppWidgetHostView

use of android.appwidget.AppWidgetHostView in project Launcher3 by chislon.

the class LauncherTransitionable method addAppWidgetFromDrop.

/**
     * Process a widget drop.
     *
     * @param info The PendingAppWidgetInfo of the widget being added.
     * @param screenId The ID of the screen where it should be added
     * @param cell The cell it should be added to, optional
     * @param position The location on the screen where it was dropped, optional
     */
void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId, int[] cell, int[] span, int[] loc) {
    resetAddInfo();
    mPendingAddInfo.container = info.container = container;
    mPendingAddInfo.screenId = info.screenId = screenId;
    mPendingAddInfo.dropPos = loc;
    mPendingAddInfo.minSpanX = info.minSpanX;
    mPendingAddInfo.minSpanY = info.minSpanY;
    if (cell != null) {
        mPendingAddInfo.cellX = cell[0];
        mPendingAddInfo.cellY = cell[1];
    }
    if (span != null) {
        mPendingAddInfo.spanX = span[0];
        mPendingAddInfo.spanY = span[1];
    }
    AppWidgetHostView hostView = info.boundWidget;
    int appWidgetId;
    if (hostView != null) {
        appWidgetId = hostView.getAppWidgetId();
        addAppWidgetImpl(appWidgetId, info, hostView, info.info);
    } else {
        // In this case, we either need to start an activity to get permission to bind
        // the widget, or we need to start an activity to configure the widget, or both.
        appWidgetId = getAppWidgetHost().allocateAppWidgetId();
        Bundle options = info.bindOptions;
        boolean success = false;
        if (options != null) {
            success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName, options);
        } else {
            success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName);
        }
        if (success) {
            addAppWidgetImpl(appWidgetId, info, null, info.info);
        } else {
            mPendingAddWidgetInfo = info.info;
            Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
            // TODO: we need to make sure that this accounts for the options bundle.
            // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
            startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
        }
    }
}
Also used : AppWidgetHostView(android.appwidget.AppWidgetHostView) Bundle(android.os.Bundle) Intent(android.content.Intent) RecognizerIntent(android.speech.RecognizerIntent) Point(android.graphics.Point)

Example 29 with AppWidgetHostView

use of android.appwidget.AppWidgetHostView in project Launcher3 by chislon.

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 30 with AppWidgetHostView

use of android.appwidget.AppWidgetHostView in project Launcher3 by chislon.

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)

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