Example usage for android.appwidget AppWidgetManager updateAppWidget

List of usage examples for android.appwidget AppWidgetManager updateAppWidget

Introduction

In this page you can find the example usage for android.appwidget AppWidgetManager updateAppWidget.

Prototype

public void updateAppWidget(ComponentName provider, RemoteViews views) 

Source Link

Document

Set the RemoteViews to use for all AppWidget instances for the supplied AppWidget provider.

Usage

From source file:me.sandrin.xkcdwidget.XKCDAppWidgetProvider.java

private void update(Context context) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget_layout);
    ComponentName watchWidget = new ComponentName(context, XKCDAppWidgetProvider.class);

    title = context.getResources().getString(R.string.error_title);
    image = null;/*from w  w w.ja  va 2 s. c o m*/
    altText = context.getResources().getString(R.string.error_text);
    updateComicInfo();
    updateRemoteViews(context, remoteViews);

    appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}

From source file:dk.cafeanalog.AnalogWidget.java

private void handleError(Context mContext) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
    RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.analog_widget);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(mContext, AnalogWidget.class));
    views.setTextViewText(R.id.appwidget_text, "Error");
    views.setOnClickPendingIntent(R.id.appwidget_text, getPendingSelfIntent(mContext));
    // Instruct the widget manager to update the widget
    for (int appWidgetId : appWidgetIds) {
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }//from w w w  .  j a va2  s  .c  o  m
}

From source file:dk.cafeanalog.AnalogWidget.java

private void handleIsOpen(final Context mContext, final OpeningStatus openingStatus) {
    final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
    final int[] appWidgetIds = appWidgetManager
            .getAppWidgetIds(new ComponentName(mContext, AnalogWidget.class));
    final RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.analog_widget);
    views.setTextViewText(R.id.appwidget_text, mContext.getText(R.string.refreshing_analog));
    views.setTextColor(R.id.appwidget_text,
            ContextCompat.getColor(mContext, android.R.color.primary_text_dark));
    // Instruct the widget manager to update the widget
    for (int appWidgetId : appWidgetIds) {
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }/*ww w  .  j  a v a  2s.  co  m*/

    CharSequence widgetText;
    if (openingStatus.open) {
        widgetText = mContext.getString(R.string.widget_open_analog);
        views.setTextColor(R.id.appwidget_text, ContextCompat.getColor(mContext, R.color.openColor));
    } else {
        widgetText = mContext.getString(R.string.widget_closed_analog);
        views.setTextColor(R.id.appwidget_text, ContextCompat.getColor(mContext, R.color.closedColor));
    }

    views.setTextViewText(R.id.appwidget_text, widgetText);
    views.setOnClickPendingIntent(R.id.appwidget_text, getPendingSelfIntent(mContext));
    // Instruct the widget manager to update the widget
    for (int appWidgetId : appWidgetIds) {
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

From source file:leoisasmendi.android.com.suricatepodcast.services.MediaPlayerService.java

private void updateWidgets(PlaybackStatus playbackStatus) {

    final RemoteViews view = new RemoteViews(getPackageName(), R.layout.podcast_widget_player);

    if (playbackStatus == PlaybackStatus.PLAYING) {
        view.setImageViewResource(R.id.widget_play, R.drawable.media_player_pause_24x24);
    } else if (playbackStatus == PlaybackStatus.PAUSED) {
        view.setImageViewResource(R.id.widget_play, R.drawable.media_player_play_24x24);
    }// w  ww. j a  v a 2  s  .co  m

    view.setTextViewText(R.id.widget_title, activeAudio.getTitle());
    view.setTextViewText(R.id.widget_length, activeAudio.getDuration());

    Picasso.with(getBaseContext()).setLoggingEnabled(true);

    Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            view.setImageViewBitmap(R.id.widget_thumbail, bitmap);
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            view.setImageViewResource(R.id.widget_thumbail, R.drawable.picture);
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            view.setImageViewResource(R.id.widget_thumbail, R.drawable.picture);
        }
    };

    Picasso.with(getBaseContext()).load(activeAudio.getPoster()).into(target);

    // Push update for this widget to the home screen
    ComponentName thisWidget = new ComponentName(this, PodcastWidgetProvider.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(thisWidget, view);
}

From source file:com.udacity.stockhawk.ui.widget.StockWidget.java

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int widgetId : appWidgetIds) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_stock);

        Intent clickIntentTemplate = new Intent(context, StockChart.class);

        PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(clickIntentTemplate)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setPendingIntentTemplate(R.id.lv_widget_stock, clickPendingIntentTemplate);

        // Set the collection and update the widget
        setRemoteAdapter(context, remoteViews);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);

    }/*from   w ww. j ava 2  s  . co  m*/

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

From source file:cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.ui.widget.ListWidgetConfig.java

void setupActionBar() {
    final WidgetPrefs widgetPrefs = new WidgetPrefs(this, appWidgetId);

    LayoutInflater inflater = (LayoutInflater) getActionBar().getThemedContext()
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    final View customActionBarView = inflater.inflate(R.layout.actionbar_custom_view_done, null);
    customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(new View.OnClickListener() {
        @Override//www  . j ava2 s  .  c om
        public void onClick(View v) {
            // "Done"
            // // Set success
            widgetPrefs.setPresent();
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            setResult(RESULT_OK, resultValue);
            // Build/Update widget
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
            // Log.d(TAG, "finishing WidgetId " + appWidgetId);
            appWidgetManager.updateAppWidget(appWidgetId, ListWidgetProvider
                    .buildRemoteViews(getApplicationContext(), appWidgetManager, appWidgetId, widgetPrefs));
            // Update list items
            appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.notesList);
            // Destroy activity
            finish();
        }
    });
    // Show the custom action bar view and hide the normal Home icon and
    // title.
    final ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
            ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
    actionBar.setCustomView(customActionBarView);
}

From source file:org.cowboycoders.cyclisimo.widgets.TrackWidgetProvider.java

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    String action = intent.getAction();
    if (context.getString(R.string.track_paused_broadcast_action).equals(action)
            || context.getString(R.string.track_resumed_broadcast_action).equals(action)
            || context.getString(R.string.track_started_broadcast_action).equals(action)
            || context.getString(R.string.track_stopped_broadcast_action).equals(action)
            || context.getString(R.string.track_update_broadcast_action).equals(action)) {
        long trackId = intent.getLongExtra(context.getString(R.string.track_id_broadcast_extra), -1L);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        int[] appWidgetIds = appWidgetManager
                .getAppWidgetIds(new ComponentName(context, TrackWidgetProvider.class));
        for (int appWidgetId : appWidgetIds) {
            RemoteViews remoteViews = getRemoteViews(context, trackId,
                    HEIGHT_SIZE.get(appWidgetId, DEFAULT_SIZE));
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }// w w w  .ja v  a  2  s .co m
    }
}

From source file:org.namelessrom.devicecontrol.widgets.RebootWidget.java

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] ids) {
    final ComponentName widget = new ComponentName(context, RebootWidget.class);

    final int[] allWidgetInstancesIds = appWidgetManager.getAppWidgetIds(widget);
    for (int widgetId : allWidgetInstancesIds) {
        final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_reboot);

        final Intent intent = new Intent(context, RebootWidget.class);
        intent.setAction(SHOW_POPUP_DIALOG_REBOOT_ACTION);

        final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.widget_reboot_image, pendingIntent);

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }/*from  w ww .  j  a  v  a  2  s  . co m*/

    super.onUpdate(context, appWidgetManager, ids);
}

From source file:ch.fixme.status.Widget.java

protected static void updateWidget(final Context ctxt, int widgetId, AppWidgetManager manager, Bitmap bitmap,
        String text) {//from  ww w .  j a va  2s. com
    RemoteViews views = new RemoteViews(ctxt.getPackageName(), R.layout.widget);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctxt);
    Editor edit = prefs.edit();
    if (prefs.getBoolean(Prefs.KEY_WIDGET_TRANSPARENCY, Prefs.DEFAULT_WIDGET_TRANSPARENCY)) {
        views.setInt(R.id.widget_image, "setBackgroundResource", 0);
    } else {
        views.setInt(R.id.widget_image, "setBackgroundResource", android.R.drawable.btn_default_small);
    }
    if (bitmap != null) {
        views.setImageViewBitmap(R.id.widget_image, bitmap);
        edit.putBoolean(Main.PREF_FORCE_WIDGET + widgetId, false); // Don't
        // need
        // to
        // force
    } else {
        views.setImageViewResource(R.id.widget_image, android.R.drawable.ic_popup_sync);
        edit.putBoolean(Main.PREF_FORCE_WIDGET + widgetId, true); // Something
        // went
        // wrong
    }
    if (text != null) {
        views.setTextViewText(R.id.widget_status, text);
        views.setViewVisibility(R.id.widget_status, View.VISIBLE);
    } else {
        views.setViewVisibility(R.id.widget_status, View.GONE);
    }
    Intent clickIntent = new Intent(ctxt, Main.class);
    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctxt, widgetId, clickIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);
    manager.updateAppWidget(widgetId, views);
    // Is initialized
    edit.putBoolean(Main.PREF_INIT_WIDGET + widgetId, true);
    edit.commit();
}

From source file:org.gaeproxy.GAEProxyService.java

/** Called when the activity is closed. */
@Override//from   www  . j a v a 2 s  .co  m
public void onDestroy() {

    EasyTracker.getTracker().trackEvent("service", "stop", getVersionName(), 0L);

    if (mShutdownReceiver != null) {
        unregisterReceiver(mShutdownReceiver);
        mShutdownReceiver = null;
    }

    statusLock = true;

    stopForegroundCompat(1);

    notifyAlert(getString(R.string.forward_stop), getString(R.string.service_stopped),
            Notification.FLAG_AUTO_CANCEL);

    try {
        if (dnsServer != null)
            dnsServer.close();
    } catch (Exception e) {
        Log.e(TAG, "DNS Server close unexpected");
    }

    new Thread() {
        @Override
        public void run() {

            // Make sure the connection is closed, important here
            onDisconnect();
        }
    }.start();

    // for widget, maybe exception here
    try {
        RemoteViews views = new RemoteViews(getPackageName(), R.layout.gaeproxy_appwidget);
        views.setImageViewResource(R.id.serviceToggle, R.drawable.off);
        AppWidgetManager awm = AppWidgetManager.getInstance(this);
        awm.updateAppWidget(awm.getAppWidgetIds(new ComponentName(this, GAEProxyWidgetProvider.class)), views);
    } catch (Exception ignore) {
        // Nothing
    }

    Editor ed = settings.edit();
    ed.putBoolean("isRunning", false);
    ed.putBoolean("isConnecting", false);
    ed.commit();

    try {
        notificationManager.cancel(0);
    } catch (Exception ignore) {
        // Nothing
    }

    try {
        ProxySettings.resetProxy(this);
    } catch (Exception ignore) {
        // Nothing
    }

    super.onDestroy();

    statusLock = false;

    markServiceStopped();
}