Example usage for android.appwidget AppWidgetHostView setAppWidget

List of usage examples for android.appwidget AppWidgetHostView setAppWidget

Introduction

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

Prototype

public void setAppWidget(int appWidgetId, AppWidgetProviderInfo info) 

Source Link

Document

Set the AppWidget that will be displayed by this view.

Usage

From source file:com.launcher.silverfish.HomeScreenFragment.java

private void createWidgetFromId(int widget_id) {
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widget_id);

    // Create the host view
    AppWidgetHostView hostView = mAppWidgetHost.createView(getActivity().getBaseContext(), widget_id,
            appWidgetInfo);// w  ww .ja va 2s  .c  o  m
    hostView.setAppWidget(widget_id, appWidgetInfo);

    // And place the widget in widget area and save.
    placeWidget(hostView);
    sqlHelper.updateWidget(appWidgetInfo.provider.getPackageName(), appWidgetInfo.provider.getClassName());
}

From source file:com.launcher.silverfish.launcher.homescreen.HomeScreenFragment.java

private void createWidgetFromId(int widget_id) {
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widget_id);

    // Create the host view
    AppWidgetHostView hostView = mAppWidgetHost.createView(getActivity().getBaseContext(), widget_id,
            appWidgetInfo);/*from   ww  w. ja v  a  2 s .  c o m*/
    hostView.setAppWidget(widget_id, appWidgetInfo);

    // And place the widget in widget area and save.
    placeWidget(hostView);
    settings.setWidget(appWidgetInfo.provider.getPackageName(), appWidgetInfo.provider.getClassName());
}

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  w  ww.  j  ava 2  s  .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.launcher.silverfish.HomeScreenFragment.java

private void loadWidget() {
    ComponentName cn = sqlHelper.getWidgetContentName();

    Log.d("Widget creation", "Loaded from db: " + cn.getClassName() + " - " + cn.getPackageName());
    // Check that there actually is a widget in the database
    if (cn.getPackageName().isEmpty() && cn.getClassName().isEmpty()) {
        Log.d("Widget creation", "DB was empty");
        return;/*  www . j av  a  2 s.  co m*/
    }
    Log.d("Widget creation", "DB was not empty");

    final List<AppWidgetProviderInfo> infos = mAppWidgetManager.getInstalledProviders();

    // Get AppWidgetProviderInfo
    AppWidgetProviderInfo appWidgetInfo = null;
    // Just in case you want to see all package and class names of installed widget providers,
    // this code is useful
    for (final AppWidgetProviderInfo info : infos) {
        Log.d("AD3", info.provider.getPackageName() + " / " + info.provider.getClassName());
    }
    // Iterate through all infos, trying to find the desired one
    for (final AppWidgetProviderInfo info : infos) {
        if (info.provider.getClassName().equals(cn.getClassName())
                && info.provider.getPackageName().equals(cn.getPackageName())) {
            // We found it!
            appWidgetInfo = info;
            break;
        }
    }
    if (appWidgetInfo == null) {
        Log.d("Widget creation", "app info was null");
        return; // Stop here
    }

    // Allocate the hosted widget id
    int appWidgetId = mAppWidgetHost.allocateAppWidgetId();

    boolean allowed_to_bind = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);

    // Ask the user to allow this app to have access to their widgets
    if (!allowed_to_bind) {
        Log.d("Widget creation", "asking for permission");
        Intent i = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
        Bundle args = new Bundle();
        args.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        args.putParcelable(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, cn);
        if (Build.VERSION.SDK_INT >= 21) {
            args.putParcelable(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, null);
        }
        i.putExtras(args);
        startActivityForResult(i, REQUEST_BIND_APPWIDGET);
        return;
    } else {

        Log.d("Widget creation", "Allowed to bind");
        Log.d("Widget creation", "creating widget");
        //Intent i = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
        //createWidgetFromId(appWidgetId);
    }
    // Create the host view
    AppWidgetHostView hostView = mAppWidgetHost.createView(getActivity().getBaseContext(), appWidgetId,
            appWidgetInfo);

    // Set the desired widget
    hostView.setAppWidget(appWidgetId, appWidgetInfo);

    placeWidget(hostView);
}

From source file:com.launcher.silverfish.launcher.homescreen.HomeScreenFragment.java

private void loadWidget() {
    ComponentName cn = settings.getWidget();

    Log.d("Widget creation", "Loaded from db: " + cn.getClassName() + " - " + cn.getPackageName());
    // Check that there actually is a widget in the database
    if (cn.getPackageName().isEmpty() && cn.getClassName().isEmpty()) {
        Log.d("Widget creation", "DB was empty");
        return;//from ww w. ja v  a 2  s .  co  m
    }
    Log.d("Widget creation", "DB was not empty");

    final List<AppWidgetProviderInfo> infos = mAppWidgetManager.getInstalledProviders();

    // Get AppWidgetProviderInfo
    AppWidgetProviderInfo appWidgetInfo = null;
    // Just in case you want to see all package and class names of installed widget providers,
    // this code is useful
    for (final AppWidgetProviderInfo info : infos) {
        Log.d("AD3", info.provider.getPackageName() + " / " + info.provider.getClassName());
    }
    // Iterate through all infos, trying to find the desired one
    for (final AppWidgetProviderInfo info : infos) {
        if (info.provider.getClassName().equals(cn.getClassName())
                && info.provider.getPackageName().equals(cn.getPackageName())) {
            // We found it!
            appWidgetInfo = info;
            break;
        }
    }
    if (appWidgetInfo == null) {
        Log.d("Widget creation", "app info was null");
        return; // Stop here
    }

    // Allocate the hosted widget id
    int appWidgetId = mAppWidgetHost.allocateAppWidgetId();

    boolean allowed_to_bind = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);

    // Ask the user to allow this app to have access to their widgets
    if (!allowed_to_bind) {
        Log.d("Widget creation", "asking for permission");
        Intent i = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
        Bundle args = new Bundle();
        args.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        args.putParcelable(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, cn);
        if (Build.VERSION.SDK_INT >= 21) {
            args.putParcelable(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, null);
        }
        i.putExtras(args);
        startActivityForResult(i, REQUEST_BIND_APPWIDGET);
        return;
    } else {

        Log.d("Widget creation", "Allowed to bind");
        Log.d("Widget creation", "creating widget");
        //Intent i = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
        //createWidgetFromId(appWidgetId);
    }
    // Create the host view
    AppWidgetHostView hostView = mAppWidgetHost.createView(getActivity().getBaseContext(), appWidgetId,
            appWidgetInfo);

    // Set the desired widget
    hostView.setAppWidget(appWidgetId, appWidgetInfo);

    placeWidget(hostView);
}

From source file:de.spiritcroc.modular_remote.MainActivity.java

public View createWidget(int appWidgetId) {
    AppWidgetProviderInfo appWidgetProviderInfo = appWidgetManager.getAppWidgetInfo(appWidgetId);
    AppWidgetHostView hostView = appWidgetHost.createView(this, appWidgetId, appWidgetProviderInfo);
    hostView.setAppWidget(appWidgetId, appWidgetProviderInfo);
    return hostView;
}