Example usage for android.appwidget AppWidgetHostView setLayoutParams

List of usage examples for android.appwidget AppWidgetHostView setLayoutParams

Introduction

In this page you can find the example usage for android.appwidget AppWidgetHostView setLayoutParams.

Prototype

public void setLayoutParams(ViewGroup.LayoutParams params) 

Source Link

Document

Set the layout parameters associated with this view.

Usage

From source file:com.farmerbb.taskbar.service.DashboardService.java

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.N)/*from w w  w.  ja va2s  .co m*/
private void showDashboard() {
    if (layout.getVisibility() == View.GONE) {
        layout.setOnClickListener(ocl);
        fadeIn();

        LocalBroadcastManager.getInstance(this)
                .sendBroadcast(new Intent("com.farmerbb.taskbar.DASHBOARD_APPEARING"));
        LocalBroadcastManager.getInstance(this)
                .sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));

        boolean inFreeformMode = FreeformHackHelper.getInstance().isInFreeformWorkspace();

        final SharedPreferences pref = U.getSharedPreferences(this);
        Intent intent = null;

        switch (pref.getString("theme", "light")) {
        case "light":
            intent = new Intent(this, DashboardActivity.class);
            break;
        case "dark":
            intent = new Intent(this, DashboardActivityDark.class);
            break;
        }

        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        }

        if (inFreeformMode) {
            if (intent != null && U.isOPreview())
                intent.putExtra("context_menu_fix", true);

            U.launchAppMaximized(this, intent);
        } else
            startActivity(intent);

        for (int i = 0; i < maxSize; i++) {
            final DashboardCell cellLayout = cells.get(i);
            final AppWidgetHostView hostView = widgets.get(i);

            if (hostView != null) {
                try {
                    getPackageManager()
                            .getApplicationInfo(hostView.getAppWidgetInfo().provider.getPackageName(), 0);
                    hostView.post(() -> {
                        ViewGroup.LayoutParams params = hostView.getLayoutParams();
                        params.width = cellLayout.getWidth();
                        params.height = cellLayout.getHeight();
                        hostView.setLayoutParams(params);
                        hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(),
                                cellLayout.getWidth(), cellLayout.getHeight());
                    });
                } catch (PackageManager.NameNotFoundException e) {
                    removeWidget(i, false);
                } catch (NullPointerException e) {
                    removeWidget(i, true);
                }
            }
        }

        if (!pref.getBoolean("dashboard_tutorial_shown", false)) {
            U.showToastLong(this, R.string.dashboard_tutorial);
            pref.edit().putBoolean("dashboard_tutorial_shown", true).apply();
        }
    }
}

From source file:com.farmerbb.taskbar.service.DashboardService.java

private void addWidget(int appWidgetId, int cellId, boolean shouldSave) {
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);

    final DashboardCell cellLayout = cells.get(cellId);
    final AppWidgetHostView hostView = mAppWidgetHost.createView(DashboardService.this, appWidgetId,
            appWidgetInfo);/*w w  w.j  ava  2s.  c  o  m*/
    hostView.setAppWidget(appWidgetId, appWidgetInfo);

    Bundle bundle = new Bundle();
    bundle.putInt("cellId", cellId);
    hostView.setTag(bundle);

    cellLayout.findViewById(R.id.empty).setVisibility(View.GONE);
    cellLayout.findViewById(R.id.placeholder).setVisibility(View.GONE);
    cellLayout.setOnLongClickListener(olcl);
    cellLayout.setOnGenericMotionListener(ogml);
    cellLayout.setOnInterceptedLongPressListener(listener);

    LinearLayout linearLayout = (LinearLayout) cellLayout.findViewById(R.id.dashboard);
    linearLayout.addView(hostView);

    Bundle bundle2 = (Bundle) cellLayout.getTag();
    bundle2.putInt("appWidgetId", appWidgetId);
    cellLayout.setTag(bundle2);

    widgets.put(cellId, hostView);

    if (shouldSave) {
        SharedPreferences pref = U.getSharedPreferences(this);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("dashboard_widget_" + Integer.toString(cellId), appWidgetId);
        editor.putString("dashboard_widget_" + Integer.toString(cellId) + "_provider",
                appWidgetInfo.provider.flattenToString());
        editor.remove("dashboard_widget_" + Integer.toString(cellId) + "_placeholder");
        editor.apply();
    }

    new Handler().post(() -> {
        ViewGroup.LayoutParams params = hostView.getLayoutParams();
        params.width = cellLayout.getWidth();
        params.height = cellLayout.getHeight();
        hostView.setLayoutParams(params);
        hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(),
                cellLayout.getHeight());
    });
}

From source file:com.android.launcher2.AsyncTaskCallback.java

private void preloadWidget(final PendingAddWidgetInfo info) {
    final AppWidgetProviderInfo pInfo = info.info;
    final Bundle options = getDefaultOptionsForWidget(mLauncher, info);

    if (pInfo.configure != null) {
        info.bindOptions = options;/*from  ww w .j  a  v  a 2 s.com*/
        return;
    }

    mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
    mBindWidgetRunnable = new Runnable() {
        @Override
        public void run() {
            mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
            // Options will be null for platforms with JB or lower, so this serves as an
            // SDK level check.
            if (options == null) {
                if (AppWidgetManagerCompat.bindAppWidgetIdIfAllowed(AppWidgetManager.getInstance(mLauncher),
                        mWidgetLoadingId, info.componentName)) {
                    mWidgetCleanupState = WIDGET_BOUND;
                }
            } else {
                if (AppWidgetManagerCompat.bindAppWidgetIdIfAllowed(AppWidgetManager.getInstance(mLauncher),
                        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);
}

From source file:com.phonemetra.turbo.launcher.AsyncTaskCallback.java

private void preloadWidget(final PendingAddWidgetInfo info) {
    final AppWidgetProviderInfo pInfo = info.info;
    final Bundle options = getDefaultOptionsForWidget(mLauncher, info);

    if (pInfo.configure != null) {
        info.bindOptions = options;//from   w  ww .java  2s  .  co  m
        return;
    }

    mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
    mBindWidgetRunnable = new Runnable() {
        @Override
        public void run() {
            mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();

            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);
}

From source file:com.zyk.launcher.AsyncTaskCallback.java

private void preloadWidget(final PendingAddWidgetInfo info) {
    final AppWidgetProviderInfo pInfo = info.info;
    final Bundle options = getDefaultOptionsForWidget(mLauncher, info);

    if (pInfo.configure != null) {
        info.bindOptions = options;/*from   ww  w  . j a  v  a  2  s.c  o  m*/
        return;
    }

    mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
    mBindWidgetRunnable = new Runnable() {
        @Override
        public void run() {
            mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
            if (AppWidgetManagerCompat.getInstance(mLauncher).bindAppWidgetIdIfAllowed(mWidgetLoadingId, pInfo,
                    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);
}