Example usage for android.widget RemoteViews setOnClickPendingIntent

List of usage examples for android.widget RemoteViews setOnClickPendingIntent

Introduction

In this page you can find the example usage for android.widget RemoteViews setOnClickPendingIntent.

Prototype

public void setOnClickPendingIntent(int viewId, PendingIntent pendingIntent) 

Source Link

Document

Equivalent to calling android.view.View#setOnClickListener(android.view.View.OnClickListener) to launch the provided PendingIntent .

Usage

From source file:Main.java

public static void setClickPendingIntent(RemoteViews remoteButton, Intent intent, Context context,
        int buttonId) {

    PendingIntent clickPI = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    remoteButton.setOnClickPendingIntent(buttonId, clickPI);
}

From source file:Main.java

public static final void BindClickAction(final Context context, final RemoteViews views, final int resId,
        final int widgetId, final Intent intent) {
    final int requestCode = generateRequestCode(resId, widgetId);
    PendingIntent pIntent = PendingIntent.getService(context, requestCode, intent, PENDING_INTENT_FLAGS);
    views.setOnClickPendingIntent(resId, pIntent);
}

From source file:Main.java

public static final void BindClickActionActivity(final Context context, final RemoteViews views,
        final int resId, final int widgetId, final Intent intent) {
    final int requestCode = generateRequestCode(resId, widgetId);
    PendingIntent pIntent = PendingIntent.getActivity(context, requestCode, intent, PENDING_INTENT_FLAGS);
    views.setOnClickPendingIntent(resId, pIntent);
}

From source file:android.support.v7.app.NotificationCompatImplBase.java

private static RemoteViews generateMediaActionButton(Context context, NotificationCompatBase.Action action) {
    final boolean tombstone = (action.getActionIntent() == null);
    RemoteViews button = new RemoteViews(context.getPackageName(), R.layout.notification_media_action);
    button.setImageViewResource(R.id.action0, action.getIcon());
    if (!tombstone) {
        button.setOnClickPendingIntent(R.id.action0, action.getActionIntent());
    }/*  w w  w . j  a  v a 2  s. com*/
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        button.setContentDescription(R.id.action0, action.getTitle());
    }
    return button;
}

From source file:org.gnucash.android.ui.widget.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
  * @param accountId Database ID of the account tied to the widget
 *//*from ww  w.ja  v  a2  s  .c o  m*/
public static void updateWidget(Context context, int appWidgetId, long accountId) {
    Log.i("WidgetConfiguration", "Updating widget: " + appWidgetId);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(context);
    Account account = accountsDbAdapter.getAccount(accountId);

    if (account == null) {
        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 = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.remove(UxArgument.SELECTED_ACCOUNT_ID + appWidgetId);
        editor.commit();
        return;
    }

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_4x1);
    views.setTextViewText(R.id.account_name, account.getName());
    Money accountBalance = accountsDbAdapter.getAccountBalance(accountId);

    views.setTextViewText(R.id.transactions_summary, accountBalance.formattedString(Locale.getDefault()));
    int color = account.getBalance().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_ID, accountId);
    PendingIntent accountPendingIntent = PendingIntent.getActivity(context, appWidgetId, accountViewIntent, 0);
    views.setOnClickPendingIntent(R.id.widget_layout, accountPendingIntent);

    Intent newTransactionIntent = new Intent(context, TransactionsActivity.class);
    newTransactionIntent.setAction(Intent.ACTION_INSERT_OR_EDIT);
    newTransactionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    newTransactionIntent.putExtra(UxArgument.SELECTED_ACCOUNT_ID, accountId);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, newTransactionIntent, 0);
    views.setOnClickPendingIntent(R.id.btn_new_transaction, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, views);
}

From source file:ru.kaefik.isaifutdinov.an_weather_widget.AnWeatherWidget.java

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
        throws JSONException {

    Log.i(TAG_SERVICE, "start    updateAppWidget" + "  appWidgetId = "
            + String.valueOf(appWidgetId));

    String nameCity = ConfigActivity.loadStringParametersFromFile(context, String.valueOf(appWidgetId));
    CityModel mCityModel = new CityModel(nameCity);

    //   ? // w w w. jav a2s. c o m
    mCityModel = GetWeatherCityService.restoreCityInfoFromFile(context, mCityModel);
    Log.i(TAG_SERVICE, "updateAppWidget  ->  ??   " + mCityModel.getName()
            + " -> " + mCityModel.getTemp());

    if (!nameCity.trim().equals("")) {
        Log.i(TAG_SERVICE, "   updateAppWidget - >: " + nameCity
                + "  appWidgetId = " + String.valueOf(appWidgetId));
        //    ? CLICK_WIDGET_BUTTON      onReceive
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.an_weather_widget);
        // Intent ? Broadcast
        Intent active = new Intent(context, AnWeatherWidget.class);
        active.setAction(CLICK_WIDGET_BUTTON);
        //?  ?
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        //?  ?
        remoteViews.setOnClickPendingIntent(R.id.refreshButton, actionPendingIntent);
        //? 
        remoteViews.setTextViewText(R.id.cityNameText, mCityModel.getName());
        remoteViews.setTextViewText(R.id.tempCityText, mCityModel.getTemp() + "C");
        remoteViews.setTextViewText(R.id.windText, Utils.windGradus2Rumb(mCityModel.getWinddirection()) + " ("
                + Float.toString(mCityModel.getWindspeed()) + " /?)");
        remoteViews.setTextViewText(R.id.timeRefreshText, mCityModel.getTimeRefresh());
        remoteViews.setTextViewText(R.id.descriptionWeatherText, mCityModel.getWeather("description"));
        remoteViews.setImageViewUri(R.id.weatherImageView,
                Uri.parse("android.resource://ru.kaefik.isaifutdinov.an_weather_widget/mipmap/" + "weather"
                        + mCityModel.getWeather("icon")));

        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        // END -    ? CLICK_WIDGET_BUTTON      onReceive

        startGetWeatherCityService(context, appWidgetId, mCityModel);

    } else {
        Log.i(TAG_SERVICE, "   updateAppWidget - > ? "
                + "  appWidgetId = " + String.valueOf(appWidgetId));
    }
}

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 w w w  .  j ava2s. c  o  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:st.brothas.mtgoxwidget.MtGoxWidgetProvider.java

static void updateAppWidget(final Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

    WidgetPreferences preferences = MtGoxPreferencesActivity.getWidgetPreferences(context, appWidgetId);

    if (preferences == null) {
        // Don't do anything unless the rate service has been chosen.
        // Show a "please remove this widget and add a new one"
        appWidgetManager.updateAppWidget(appWidgetId,
                new RemoteViews(context.getPackageName(), R.layout.appwidget_replace_me));
        return;//from  w ww  . ja va  2  s.c  om
    }

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider);
    Intent clickIntent = new Intent(context, GraphPopupActivity.class);
    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    clickIntent.setAction("dummyAction"); // Needed to get the extra variables included in the call
    // Note: the appWidgetId needs to be sent in the pendingIntent as request code, otherwise only ONE
    //       cached intent will be used for all widget instances!
    PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, clickIntent, 0);
    views.setOnClickPendingIntent(R.id.appwidget_box, pendingIntent);
    views.setTextViewText(R.id.appwidget_service_name, preferences.getRateService().getName());

    MtGoxDataOpenHelper dbHelper = new MtGoxDataOpenHelper(context);
    MtGoxTickerData prevData = dbHelper.getLastTickerData(preferences);

    MtGoxTickerData newData;
    JSONObject latestQuoteJSON = getLatestQuoteJSON(preferences);
    if (latestQuoteJSON != null) {
        newData = preferences.getRateService().parseJSON(latestQuoteJSON);
        newData.setCurrencyConversion(preferences.getCurrencyConversion());
        storeLastValueIfNotNull(dbHelper, newData);
        updateViews(views, prevData, newData, preferences);
    } else if (prevData != null) {
        newData = prevData;
        updateViews(views, prevData, newData, preferences);
    } else {
        updateViewsWithError(views, preferences);
    }
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

From source file:com.rks.musicx.services.NotificationHandler.java

private static void controls(RemoteViews remoteViews, RemoteViews smallViews, MusicXService musicXService) {
    PendingIntent toggleIntent = PendingIntent.getService(musicXService, 0,
            new Intent(musicXService, MusicXService.class).setAction(ACTION_TOGGLE), 0);
    PendingIntent nextIntent = PendingIntent.getService(musicXService, 0,
            new Intent(musicXService, MusicXService.class).setAction(ACTION_NEXT), 0);
    PendingIntent previousIntent = PendingIntent.getService(musicXService, 0,
            new Intent(musicXService, MusicXService.class).setAction(ACTION_PREVIOUS), 0);
    PendingIntent savefavIntent = PendingIntent.getService(musicXService, 0,
            new Intent(musicXService, MusicXService.class).setAction(ACTION_FAV), 0);

    remoteViews.setOnClickPendingIntent(R.id.toggle, toggleIntent);
    remoteViews.setOnClickPendingIntent(R.id.next, nextIntent);
    remoteViews.setOnClickPendingIntent(R.id.prev, previousIntent);
    remoteViews.setOnClickPendingIntent(R.id.action_favorite, savefavIntent);

    smallViews.setOnClickPendingIntent(R.id.small_toggle, toggleIntent);
    smallViews.setOnClickPendingIntent(R.id.small_next, nextIntent);
    smallViews.setOnClickPendingIntent(R.id.small_prev, previousIntent);
}

From source file:com.concentriclivers.mms.com.android.mms.widget.MmsWidgetProvider.java

/**
 * Update the widget appWidgetId//from   ww  w  .j a  va  2s .  c  o  m
 */
private static void updateWidget(Context context, int appWidgetId) {
    //        if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
    Log.v(TAG, "updateWidget appWidgetId: " + appWidgetId);
    //        }
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    PendingIntent clickIntent;

    // Launch an intent to avoid ANRs
    final Intent intent = new Intent(context, MmsWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(appWidgetId, R.id.conversation_list, intent);

    remoteViews.setTextViewText(R.id.widget_label, context.getString(R.string.app_label));

    // Open Mms's app conversation list when click on header
    final Intent convIntent = new Intent(context, ConversationList.class);
    clickIntent = PendingIntent.getActivity(context, 0, convIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);

    // On click intent for Compose
    final Intent composeIntent = new Intent(context, ComposeMessageActivity.class);
    composeIntent.setAction(Intent.ACTION_SENDTO);
    clickIntent = PendingIntent.getActivity(context, 0, composeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);

    // On click intent for Conversation
    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addParentStack(ComposeMessageActivity.class);
    Intent msgIntent = new Intent(Intent.ACTION_VIEW);
    msgIntent.setType("vnd.android-dir/mms-sms");
    taskStackBuilder.addNextIntent(msgIntent);
    remoteViews.setPendingIntentTemplate(R.id.conversation_list,
            taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));

    AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
}