Example usage for android.appwidget AppWidgetHostView updateAppWidgetSize

List of usage examples for android.appwidget AppWidgetHostView updateAppWidgetSize

Introduction

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

Prototype

public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth, int maxHeight) 

Source Link

Document

Provide guidance about the size of this widget to the AppWidgetManager.

Usage

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

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.N)/*from   www. j  ava2  s.c om*/
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);//from   ww  w  . j a  va  2s  . c om
    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());
    });
}