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:com.udacity.stockhawk.widget.StockAppWidgetProvider.java

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

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.stock_app_widget);

        // Set up the collection
        views.setRemoteAdapter(R.id.lv_stock_list, new Intent(context, StockAppRemoteViewsService.class));

        Intent clickIntentTemplate = new Intent(context, ChartActivity.class);
        PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(clickIntentTemplate)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.lv_stock_list, clickPendingIntentTemplate);

        views.setEmptyView(R.id.lv_stock_list, R.id.tv_widget_empty);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }//  ww  w  .ja v  a 2s .c  o m
}

From source file:org.gnucash.android.ui.homescreen.WidgetConfigurationActivity.java

/**
 * Updates the widget with id <code>appWidgetId</code> with information from the 
 * account with record ID <code>accountId</code>
  * If the account has been deleted, then a notice is posted in the widget
 * @param appWidgetId ID of the widget to be updated
 *///from  ww  w .jav a 2 s .  co  m
public static void updateWidget(final Context context, int appWidgetId) {
    Log.i("WidgetConfiguration", "Updating widget: " + appWidgetId);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    loadOldPreferences(context, appWidgetId);

    SharedPreferences preferences = context.getSharedPreferences("widget:" + appWidgetId, MODE_PRIVATE);
    String bookUID = preferences.getString(UxArgument.BOOK_UID, null);
    String accountUID = preferences.getString(UxArgument.SELECTED_ACCOUNT_UID, null);
    boolean hideAccountBalance = preferences.getBoolean(UxArgument.HIDE_ACCOUNT_BALANCE_IN_WIDGET, false);

    if (bookUID == null || accountUID == null) {
        return;
    }

    AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(BookDbHelper.getDatabase(bookUID));

    final Account account;
    try {
        account = accountsDbAdapter.getRecord(accountUID);
    } catch (IllegalArgumentException e) {
        Log.i("WidgetConfiguration", "Account not found, resetting widget " + appWidgetId);
        //if account has been deleted, let the user know
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_4x1);
        views.setTextViewText(R.id.account_name, context.getString(R.string.toast_account_deleted));
        views.setTextViewText(R.id.transactions_summary, "");
        //set it to simply open the app
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, AccountsActivity.class), 0);
        views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
        views.setOnClickPendingIntent(R.id.btn_new_transaction, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, views);
        Editor editor = PreferenceActivity.getActiveBookSharedPreferences().edit(); //PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.remove(UxArgument.SELECTED_ACCOUNT_UID + appWidgetId);
        editor.apply();
        return;
    }

    final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_4x1);
    views.setTextViewText(R.id.account_name, account.getName());

    Money accountBalance = accountsDbAdapter.getAccountBalance(accountUID, -1, System.currentTimeMillis());

    if (hideAccountBalance) {
        views.setViewVisibility(R.id.transactions_summary, View.GONE);
    } else {
        views.setTextViewText(R.id.transactions_summary, accountBalance.formattedString(Locale.getDefault()));
        int color = accountBalance.isNegative() ? R.color.debit_red : R.color.credit_green;
        views.setTextColor(R.id.transactions_summary, context.getResources().getColor(color));
    }

    Intent accountViewIntent = new Intent(context, TransactionsActivity.class);
    accountViewIntent.setAction(Intent.ACTION_VIEW);
    accountViewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    accountViewIntent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, accountUID);
    accountViewIntent.putExtra(UxArgument.BOOK_UID, bookUID);
    PendingIntent accountPendingIntent = PendingIntent.getActivity(context, appWidgetId, accountViewIntent, 0);
    views.setOnClickPendingIntent(R.id.widget_layout, accountPendingIntent);

    if (accountsDbAdapter.isPlaceholderAccount(accountUID)) {
        views.setOnClickPendingIntent(R.id.btn_view_account, accountPendingIntent);
        views.setViewVisibility(R.id.btn_new_transaction, View.GONE);
    } else {
        Intent newTransactionIntent = new Intent(context, FormActivity.class);
        newTransactionIntent.setAction(Intent.ACTION_INSERT_OR_EDIT);
        newTransactionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        newTransactionIntent.putExtra(UxArgument.FORM_TYPE, FormActivity.FormType.TRANSACTION.name());
        newTransactionIntent.putExtra(UxArgument.BOOK_UID, bookUID);
        newTransactionIntent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, accountUID);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, newTransactionIntent, 0);
        views.setOnClickPendingIntent(R.id.btn_new_transaction, pendingIntent);
        views.setViewVisibility(R.id.btn_view_account, View.GONE);
    }

    appWidgetManager.updateAppWidget(appWidgetId, views);
}

From source file:org.gaeproxy.GAEProxyService.java

public void handleCommand(Intent intent) {

    if (intent == null) {
        stopSelf();/*from   w  w  w  . j a va2  s .c o m*/
        return;
    }

    proxyType = settings.getString("proxyType", "GAE");
    sitekey = settings.getString("sitekey", "");
    try {
        port = Integer.valueOf(settings.getString("port", "1984"));
    } catch (NumberFormatException ex) {
        port = 1984;
    }

    isGlobalProxy = settings.getBoolean("isGlobalProxy", false);
    isGFWList = settings.getBoolean("isGFWList", false);
    isBypassApps = settings.getBoolean("isBypassApps", false);

    if (!parseProxyURL(settings.getString("proxy", "proxyofmax.appspot.com"))) {
        stopSelf();
        return;
    }

    if (!"GAE".equals(proxyType) && !"PaaS".equals(proxyType)) {
        proxyType = "GAE";
    }

    if ("fetch.py".equals(appPath))
        appPath = "2";

    Log.e(TAG, "Proxy: " + appId + " " + appPath);
    Log.e(TAG, "Local Port: " + port);

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

            handler.sendEmptyMessage(MSG_CONNECT_START);

            Log.d(TAG, "IPTABLES: " + Utils.getIptables());

            // Test for Redirect Support
            hasRedirectSupport = Utils.getHasRedirectSupport();

            if (handleConnection()) {
                // Connection and forward successful
                notifyAlert(getString(R.string.forward_success), getString(R.string.service_running));

                handler.sendEmptyMessageDelayed(MSG_CONNECT_SUCCESS, 500);

                // for widget, maybe exception here
                try {
                    RemoteViews views = new RemoteViews(getPackageName(), R.layout.gaeproxy_appwidget);
                    views.setImageViewResource(R.id.serviceToggle, R.drawable.on);
                    AppWidgetManager awm = AppWidgetManager.getInstance(GAEProxyService.this);
                    awm.updateAppWidget(
                            awm.getAppWidgetIds(
                                    new ComponentName(GAEProxyService.this, GAEProxyWidgetProvider.class)),
                            views);
                } catch (Exception ignore) {
                    // Nothing
                }
            } else {
                // Connection or forward unsuccessful
                notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed));

                stopSelf();
                handler.sendEmptyMessageDelayed(MSG_CONNECT_FAIL, 500);
            }

            handler.sendEmptyMessageDelayed(MSG_CONNECT_FINISH, 500);
        }
    }).start();
    markServiceStarted();
}

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

@TargetApi(16)
@Override/*from w w  w . ja  v  a2s  .c o  m*/
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId,
        Bundle newOptions) {
    if (newOptions != null) {
        int size;
        int height = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
        if (height == 0) {
            size = 2;
        } else if (height >= FOUR_CELLS) {
            size = 4;
        } else if (height >= THREE_CELLS) {
            size = 3;
        } else if (height >= TWO_CELLS) {
            size = 2;
        } else {
            size = 1;
        }
        HEIGHT_SIZE.put(appWidgetId, size);
        RemoteViews remoteViews = getRemoteViews(context, -1L, size);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
}

From source file:com.chainsaw.clearweather.OpenWeatherAPI.java

public OpenWeatherAPI(final Context context, final int widgetId) {
    final RemoteViews remote = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    final AppWidgetManager manager = AppWidgetManager.getInstance(context);

    asyncLoader = new BackgroundFetch(context) {
        @Override//from www  . ja  va 2  s  .  c o  m
        protected void onPostExecute(WeatherData result) {
            remote.setViewVisibility(R.id.loading, View.INVISIBLE);
            if (OpenWeatherAPI.this.listener != null)
                OpenWeatherAPI.this.listener.onDataReady(result);

        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            remote.setViewVisibility(R.id.temp, View.INVISIBLE);
            remote.setViewVisibility(R.id.tempUnit, View.INVISIBLE);
            remote.setViewVisibility(R.id.humidity, View.INVISIBLE);
            remote.setViewVisibility(R.id.humidityUnit, View.INVISIBLE);
            remote.setViewVisibility(R.id.location, View.INVISIBLE);
            remote.setViewVisibility(R.id.weather, View.INVISIBLE);
            remote.setViewVisibility(R.id.timestamp, View.INVISIBLE);
            remote.setViewVisibility(R.id.loading, View.VISIBLE);
            remote.setProgressBar(R.id.loading, 10, 5, true);
            manager.updateAppWidget(widgetId, remote);

            super.onProgressUpdate(values);

        }
    };
}

From source file:net.networksaremadeofstring.rhybudd.ZenossWidgetGraph.java

public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
    if (settings == null)
        settings = PreferenceManager.getDefaultSharedPreferences(context);

    wContext = context;//w ww  . j  av  a 2  s.c  o  m

    Refresh();

    handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Bundle Values = msg.getData();

                final int N = appWidgetIds.length;
                RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.zenoss_widget_graph);
                //Intent intent = new Intent(context, rhestr.class);
                //PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

                //Log.i("GraphWidget","Drawing Graph!");
                for (int i = 0; i < N; i++) {
                    int appWidgetId = appWidgetIds[i];
                    views.setImageViewBitmap(R.id.graphCanvas, RenderBarGraph(Values.getInt("CritCount"),
                            Values.getInt("ErrCount"), Values.getInt("WarnCount")));
                    appWidgetManager.updateAppWidget(appWidgetId, views);
                }
            }
        }
    };
}

From source file:net.naonedbus.appwidget.HoraireWidgetProvider.java

/**
 * Update the widget./*from   w ww . j a  v a  2s . c  o m*/
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
        final int appWidgetId) {
    final RemoteViews views = new RemoteViews(context.getPackageName(), this.mLayoutId);
    final int idFavori = WidgetConfigureActivity.getFavoriIdFromWidget(context, appWidgetId);
    final Favori favori = FavorisViewManager.getInstance().getSingle(context.getContentResolver(), idFavori);

    if (DBG)
        Log.i(LOG_TAG, Integer.toHexString(hashCode()) + " - " + appWidgetId + " - updateAppWidget " + favori);

    // Initialisation du nombre d'horaires
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        final Bundle bundle = appWidgetManager.getAppWidgetOptions(appWidgetId);
        mHoraireLimit = getHorairesCount(context, bundle);
    } else {
        if (mHoraireLimit == -1) {
            mHoraireLimit = context.getResources().getInteger(mHoraireLimitRes);
        }
    }

    if (favori != null) {
        prepareWidgetView(context, views, favori, appWidgetId);
        appWidgetManager.updateAppWidget(appWidgetId, views);
    } else {
        WidgetConfigureActivity.removeWidgetId(context, appWidgetId);
    }
}

From source file:barqsoft.footballscores.widget.WidgetListProvider.java

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int appWidgetId : appWidgetIds) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_list);

        // Create an Intent to launch MainActivity
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        views.setOnClickPendingIntent(R.id.widget_item, pendingIntent);

        // Set up the collection
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            setRemoteAdapter(context, views);
        } else {/*  ww  w  . ja  v a2 s .  co m*/
            setRemoteAdapterV11(context, views);
        }
        Intent clickIntentTemplate = new Intent(context, MainActivity.class);
        PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(clickIntentTemplate)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.widget_list, clickPendingIntentTemplate);
        views.setEmptyView(R.id.widget_list, R.id.widget_empty);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

From source file:com.abhijitvalluri.android.fitnotifications.widget.ServiceToggle.java

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    if (TOGGLE_CLICKED.equals(intent.getAction())) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.service_toggle_widget);
        if (NLService.isEnabled()) {
            NLService.setEnabled(false);
            views.setImageViewResource(R.id.widgetToggleButton, R.drawable.ic_speaker_notes_off_white_48dp);
            views.setInt(R.id.widgetToggleButton, "setBackgroundResource", R.drawable.round_rectangle_red);
            views.setTextViewText(R.id.widgetToggleText, context.getString(R.string.widget_off_text));
            views.setTextColor(R.id.widgetToggleText, ContextCompat.getColor(context, R.color.red));
        } else {/*w  w  w  .j  ava 2 s  .  c o m*/
            NLService.setEnabled(true);
            views.setImageViewResource(R.id.widgetToggleButton, R.drawable.ic_speaker_notes_white_48dp);
            views.setInt(R.id.widgetToggleButton, "setBackgroundResource", R.drawable.round_rectangle_green);
            views.setTextViewText(R.id.widgetToggleText, context.getString(R.string.widget_on_text));
            views.setTextColor(R.id.widgetToggleText, ContextCompat.getColor(context, R.color.green));
        }

        views.setOnClickPendingIntent(R.id.widgetToggleButton,
                getPendingSelfIntent(context, 0, TOGGLE_CLICKED));

        ComponentName componentName = new ComponentName(context, ServiceToggle.class);
        appWidgetManager.updateAppWidget(componentName, views);
    }
}

From source file:co.carlosjimenez.android.currencyalerts.app.widget.DetailWidgetProvider.java

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // Perform this loop procedure for each App Widget that belongs to this provider

    for (int appWidgetId : appWidgetIds) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_detail);

        // Create an Intent to launch MainActivity
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        views.setOnClickPendingIntent(R.id.widget, pendingIntent);

        // Set up the header
        setMainCurrencyDetails(context, views, appWidgetIds);

        // Set up the collection
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            setRemoteAdapter(context, views, appWidgetIds);
        } else {/* w  ww. java  2 s . c o  m*/
            setRemoteAdapterV11(context, views, appWidgetIds);
        }
        Intent clickIntentTemplate = new Intent(context, DetailActivity.class);

        PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(clickIntentTemplate)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.widget_list, clickPendingIntentTemplate);
        views.setEmptyView(R.id.widget_list, R.id.widget_empty);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}