Example usage for android.widget RemoteViews setViewVisibility

List of usage examples for android.widget RemoteViews setViewVisibility

Introduction

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

Prototype

public void setViewVisibility(int viewId, int visibility) 

Source Link

Document

Equivalent to calling View#setVisibility(int)

Usage

From source file:au.com.wallaceit.reddinator.Rservice.java

private void hideWidgetLoader(boolean goToTopOfList, boolean showError) {
    AppWidgetManager mgr = AppWidgetManager.getInstance(mContext);
    // get theme layout id
    int layout = 1;
    switch (Integer.valueOf(mSharedPreferences.getString("widgetthemepref", "1"))) {
    case 1://  w  w w . jav a 2s .c  o m
        layout = R.layout.widgetmain;
        break;
    case 2:
        layout = R.layout.widgetdark;
        break;
    case 3:
        layout = R.layout.widgetholo;
        break;
    case 4:
        layout = R.layout.widgetdarkholo;
        break;
    case 5:
        layout = R.layout.widgettrans;
        break;
    }
    RemoteViews views = new RemoteViews(mContext.getPackageName(), layout);
    views.setViewVisibility(R.id.srloader, View.INVISIBLE);
    // go to the top of the list view
    if (goToTopOfList) {
        views.setScrollPosition(R.id.listview, 0);
    }
    if (showError) {
        views.setViewVisibility(R.id.erroricon, View.VISIBLE);
    }
    mgr.partiallyUpdateAppWidget(appWidgetId, views);
}

From source file:com.ultrafunk.network_info.receiver.WifiStatusReceiver.java

@Override
protected void updateView(Context context, RemoteViews remoteViews, Bundle widgetOptions) {
    if ((wifiState == WifiManager.WIFI_STATE_DISABLED) || (wifiState == WifiManager.WIFI_STATE_UNKNOWN)) {
        setStateColor(context, remoteViews, STATE_OFF);
        remoteViews.setTextViewText(R.id.wifiNameTextView, context.getString(R.string.wifi));
        remoteViews.setImageViewResource(R.id.wifiStateImageView, R.drawable.ic_signal_wifi_off);
        remoteViews.setViewVisibility(R.id.wifiInfoTopTextView, View.VISIBLE);
        remoteViews.setTextViewText(R.id.wifiInfoTopTextView, context.getString(R.string.tap_to_change));
        remoteViews.setViewVisibility(R.id.wifiInfoBottomTextView, View.GONE);
    } else if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
        remoteViews.setImageViewResource(R.id.wifiStateImageView, R.drawable.ic_signal_wifi_on);
        remoteViews.setViewVisibility(R.id.wifiInfoTopTextView, View.VISIBLE);

        if (wifiInfo.getIpAddress() != 0) {
            setStateColor(context, remoteViews, STATE_ON);
            remoteViews.setTextViewText(R.id.wifiNameTextView, wifiInfo.getSSID().replace("\"", ""));

            if (!detailsString.isEmpty()) {
                remoteViews.setTextViewText(R.id.wifiInfoTopTextView, detailsString);
                remoteViews.setViewVisibility(R.id.wifiInfoBottomTextView, View.VISIBLE);
                remoteViews.setTextViewText(R.id.wifiInfoBottomTextView,
                        WifiUtils.getIpAddressString(wifiInfo.getIpAddress()));
            }/*from  ww w.  j  av a  2  s. c o  m*/
        } else {
            remoteViews.setViewVisibility(R.id.wifiInfoBottomTextView, View.GONE);

            if (detailedState == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
                remoteViews.setTextViewText(R.id.wifiInfoTopTextView, context.getString(R.string.connecting));
            } else {
                setStateColor(context, remoteViews, STATE_ON);
                remoteViews.setTextViewText(R.id.wifiNameTextView, context.getString(R.string.wifi));
                remoteViews.setImageViewResource(R.id.wifiStateImageView, R.drawable.ic_signal_wifi_enabled);
                remoteViews.setTextViewText(R.id.wifiInfoTopTextView, context.getString(R.string.no_network));
            }
        }
    }
}

From source file:com.audiokernel.euphonyrmt.service.NotificationHandler.java

/**
 * This method constructs the notification base, otherwise known as the collapsed notification.
 * The expanded notification method builds upon this method.
 *
 * @param resultView The RemoteView to begin with, be it new or from the current notification.
 *///from w w w .j  av  a 2 s.com
private void buildBaseNotification(final RemoteViews resultView) {
    final PendingIntent closeAction = buildPendingIntent(MPDroidService.ACTION_STOP);
    final PendingIntent nextAction = buildPendingIntent(MPDControl.ACTION_NEXT);

    resultView.setViewVisibility(R.id.notificationClose, View.VISIBLE);
    resultView.setOnClickPendingIntent(R.id.notificationClose, closeAction);

    updateStatePaused(resultView);

    resultView.setOnClickPendingIntent(R.id.notificationNext, nextAction);

    resultView.setImageViewResource(R.id.notificationIcon, AlbumCoverDownloadListener.getNoCoverResource());
}

From source file:com.ultrafunk.network_info.receiver.MobileDataStatusReceiver.java

@Override
protected void updateView(Context context, RemoteViews remoteViews, Bundle widgetOptions) {
    if (isMobileDataEnabled && isMobileOutOfService) {
        setStateColor(context, remoteViews, STATE_ON);
        remoteViews.setTextViewText(R.id.mobileNameTextView, context.getString(R.string.mobile_data));
        remoteViews.setImageViewResource(R.id.mobileStateImageView, R.drawable.ic_signal_cellular_enabled);
        remoteViews.setViewVisibility(R.id.mobileInfoTopTextView, View.VISIBLE);
        remoteViews.setTextViewText(R.id.mobileInfoTopTextView, context.getString(R.string.no_service));
        remoteViews.setViewVisibility(R.id.mobileInfoBottomTextView, View.GONE);
        return;//from   w  w  w.  ja  va2  s .co  m
    }

    if (dataState == TelephonyManager.DATA_DISCONNECTED) {
        remoteViews.setViewVisibility(R.id.mobileInfoTopTextView, View.VISIBLE);
        remoteViews.setViewVisibility(R.id.mobileInfoBottomTextView, View.VISIBLE);

        if (isMobileDataEnabled && !isAirplaneModeOn) {
            setStateColor(context, remoteViews, STATE_ON);
            remoteViews.setTextViewText(R.id.mobileNameTextView, networkOperatorAndServiceProvider);
            remoteViews.setImageViewResource(R.id.mobileStateImageView, R.drawable.ic_signal_cellular_enabled);
            remoteViews.setTextViewText(R.id.mobileInfoTopTextView, context.getString(R.string.not_connected));
            remoteViews.setTextColor(R.id.mobileInfoBottomTextView,
                    ContextCompat.getColor(context, R.color.medium_gray));
            remoteViews.setTextViewText(R.id.mobileInfoBottomTextView,
                    MobileDataUtils.getDataUsageString(context, NetworkStateService.getDataUsageBytes()));
        } else {
            setStateColor(context, remoteViews, STATE_OFF);
            remoteViews.setTextViewText(R.id.mobileNameTextView,
                    isAirplaneModeOn ? context.getString(R.string.mobile_data)
                            : networkOperatorAndServiceProvider);
            remoteViews.setImageViewResource(R.id.mobileStateImageView, R.drawable.ic_signal_cellular_off);

            if (isAirplaneModeOn) {
                remoteViews.setTextViewText(R.id.mobileInfoTopTextView,
                        context.getString(R.string.flight_mode));
                remoteViews.setViewVisibility(R.id.mobileInfoBottomTextView, View.GONE);
            } else {
                remoteViews.setTextViewText(R.id.mobileInfoTopTextView,
                        MobileDataUtils.getNetworkTypeString(telephonyManager.getNetworkType())
                                + (isDataRoaming ? " - " + context.getString(R.string.roaming) : ""));
                remoteViews.setTextViewText(R.id.mobileInfoBottomTextView,
                        MobileDataUtils.getDataUsageString(context, NetworkStateService.getDataUsageBytes()));
            }
        }
    } else {
        setStateColor(context, remoteViews, STATE_ON);
        remoteViews.setTextViewText(R.id.mobileNameTextView, networkOperatorAndServiceProvider);
        remoteViews.setImageViewResource(R.id.mobileStateImageView, R.drawable.ic_signal_cellular_on);
        remoteViews.setViewVisibility(R.id.mobileInfoTopTextView, View.VISIBLE);
        remoteViews.setTextViewText(R.id.mobileInfoTopTextView,
                MobileDataUtils.getNetworkTypeString(telephonyManager.getNetworkType())
                        + (isDataRoaming ? " - " + context.getString(R.string.roaming) : ""));
        remoteViews.setViewVisibility(R.id.mobileInfoBottomTextView, View.VISIBLE);

        boolean isConnecting = ((dataState == TelephonyManager.DATA_CONNECTING)
                || NetworkStateService.isWaitingForDataUsage());
        remoteViews.setTextViewText(R.id.mobileInfoBottomTextView,
                isConnecting ? context.getString(R.string.connecting)
                        : MobileDataUtils.getDataUsageString(context, dataUsageBytes));
    }
}

From source file:org.dalol.orthodoxmezmurmedia.modules.player.PlayerNotificationDelegate.java

public void showNotification(MezmurPlayerService service, String text) {

    Intent playerIntent = new Intent(service, MezmursPlayerActivity.class);
    Intent dashboardIntent = new Intent(service, MezmurDashboardActivity.class);

    PendingIntent intent = TaskStackBuilder.create(service).addNextIntentWithParentStack(dashboardIntent)
            .addNextIntent(playerIntent).getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    RemoteViews views = new RemoteViews(service.getPackageName(), R.layout.player_small_notification);
    views.setOnClickPendingIntent(R.id.player_pause, getPendingAction(service, "pause"));
    views.setOnClickPendingIntent(R.id.player_previous, getPendingAction(service, "previous"));
    views.setOnClickPendingIntent(R.id.player_next, getPendingAction(service, "next"));
    views.setOnClickPendingIntent(R.id.player_close, getPendingAction(service, "stop"));
    views.setTextViewText(R.id.player_song_name, text);
    views.setViewVisibility(R.id.player_progress_bar, View.GONE);

    mNotificationBuilder = new NotificationCompat.Builder(service).setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(NotificationCompat.PRIORITY_MAX).setWhen(System.currentTimeMillis())
            .setContentIntent(intent).setOngoing(true).setContent(views);

    service.startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
}

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
 *//*  w ww.  jav a2  s .  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:com.sip.pwc.sipphone.service.Downloader.java

@Override
protected void onHandleIntent(Intent intent) {
    HttpGet getMethod = new HttpGet(intent.getData().toString());
    int result = Activity.RESULT_CANCELED;
    String outPath = intent.getStringExtra(EXTRA_OUTPATH);
    boolean checkMd5 = intent.getBooleanExtra(EXTRA_CHECK_MD5, false);
    int icon = intent.getIntExtra(EXTRA_ICON, 0);
    String title = intent.getStringExtra(EXTRA_TITLE);
    boolean showNotif = (icon > 0 && !TextUtils.isEmpty(title));

    // Build notification
    Builder nb = new Builder(this);
    nb.setWhen(System.currentTimeMillis());
    nb.setContentTitle(title);/*from  ww  w  .  ja v a  2s. com*/
    nb.setSmallIcon(android.R.drawable.stat_sys_download);
    nb.setOngoing(true);
    Intent i = new Intent(this, SipHome.class);
    nb.setContentIntent(PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));

    RemoteViews contentView = new RemoteViews(getApplicationContext().getPackageName(),
            R.layout.download_notif);
    contentView.setImageViewResource(R.id.status_icon, icon);
    contentView.setTextViewText(R.id.status_text, getResources().getString(R.string.downloading_text));
    contentView.setProgressBar(R.id.status_progress, 50, 0, false);
    contentView.setViewVisibility(R.id.status_progress_wrapper, View.VISIBLE);
    nb.setContent(contentView);

    final Notification notification = showNotif ? nb.build() : null;
    notification.contentView = contentView;
    if (!TextUtils.isEmpty(outPath)) {
        try {
            File output = new File(outPath);
            if (output.exists()) {
                output.delete();
            }

            if (notification != null) {
                notificationManager.notify(NOTIF_DOWNLOAD, notification);
            }
            ResponseHandler<Boolean> responseHandler = new FileStreamResponseHandler(output, new Progress() {
                private int oldState = 0;

                @Override
                public void run(long progress, long total) {
                    //Log.d(THIS_FILE, "Progress is "+progress+" on "+total);
                    int newState = (int) Math.round(progress * 50.0f / total);
                    if (oldState != newState) {

                        notification.contentView.setProgressBar(R.id.status_progress, 50, newState, false);
                        notificationManager.notify(NOTIF_DOWNLOAD, notification);
                        oldState = newState;
                    }

                }
            });
            boolean hasReply = client.execute(getMethod, responseHandler);

            if (hasReply) {

                if (checkMd5) {
                    URL url = new URL(intent.getData().toString().concat(".md5sum"));
                    InputStream content = (InputStream) url.getContent();
                    if (content != null) {
                        BufferedReader br = new BufferedReader(new InputStreamReader(content));
                        String downloadedMD5 = "";
                        try {
                            downloadedMD5 = br.readLine().split("  ")[0];
                        } catch (NullPointerException e) {
                            throw new IOException("md5_verification : no sum on server");
                        }
                        if (!MD5.checkMD5(downloadedMD5, output)) {
                            throw new IOException("md5_verification : incorrect");
                        }
                    }
                }
                PendingIntent pendingIntent = (PendingIntent) intent
                        .getParcelableExtra(EXTRA_PENDING_FINISH_INTENT);

                try {
                    Runtime.getRuntime().exec("chmod 644 " + outPath);
                } catch (IOException e) {
                    Log.e(THIS_FILE, "Unable to make the apk file readable", e);
                }

                Log.d(THIS_FILE, "Download finished of : " + outPath);
                if (pendingIntent != null) {

                    notification.contentIntent = pendingIntent;
                    notification.flags = Notification.FLAG_AUTO_CANCEL;
                    notification.icon = android.R.drawable.stat_sys_download_done;
                    notification.contentView.setViewVisibility(R.id.status_progress_wrapper, View.GONE);
                    notification.contentView.setTextViewText(R.id.status_text,
                            getResources().getString(R.string.done)
                                    // TODO should be a parameter of this class
                                    + " - Click to install");
                    notificationManager.notify(NOTIF_DOWNLOAD, notification);

                    /*
                    try {
                       pendingIntent.send();
                         notificationManager.cancel(NOTIF_DOWNLOAD);
                    } catch (CanceledException e) {
                       Log.e(THIS_FILE, "Impossible to start pending intent for download finish");
                    }
                    */
                } else {
                    Log.w(THIS_FILE, "Invalid pending intent for finish !!!");
                }

                result = Activity.RESULT_OK;
            }
        } catch (IOException e) {
            Log.e(THIS_FILE, "Exception in download", e);
        }
    }

    if (result == Activity.RESULT_CANCELED) {
        notificationManager.cancel(NOTIF_DOWNLOAD);
    }
}

From source file:com.csipsimple.service.Downloader.java

@Override
protected void onHandleIntent(Intent intent) {
    HttpGet getMethod = new HttpGet(intent.getData().toString());
    int result = Activity.RESULT_CANCELED;
    String outPath = intent.getStringExtra(EXTRA_OUTPATH);
    boolean checkMd5 = intent.getBooleanExtra(EXTRA_CHECK_MD5, false);
    int icon = intent.getIntExtra(EXTRA_ICON, 0);
    String title = intent.getStringExtra(EXTRA_TITLE);
    boolean showNotif = (icon > 0 && !TextUtils.isEmpty(title));

    // Build notification
    Builder nb = new NotificationCompat.Builder(this);
    nb.setWhen(System.currentTimeMillis());
    nb.setContentTitle(title);/*  www  .  j  a va2  s  .  c  om*/
    nb.setSmallIcon(android.R.drawable.stat_sys_download);
    nb.setOngoing(true);
    Intent i = new Intent(this, SipHome.class);
    nb.setContentIntent(PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));

    RemoteViews contentView = new RemoteViews(getApplicationContext().getPackageName(),
            R.layout.download_notif);
    contentView.setImageViewResource(R.id.status_icon, icon);
    contentView.setTextViewText(R.id.status_text, getResources().getString(R.string.downloading_text));
    contentView.setProgressBar(R.id.status_progress, 50, 0, false);
    contentView.setViewVisibility(R.id.status_progress_wrapper, View.VISIBLE);
    nb.setContent(contentView);

    final Notification notification = showNotif ? nb.build() : null;
    notification.contentView = contentView;
    if (!TextUtils.isEmpty(outPath)) {
        try {
            File output = new File(outPath);
            if (output.exists()) {
                output.delete();
            }

            if (notification != null) {
                notificationManager.notify(NOTIF_DOWNLOAD, notification);
            }
            ResponseHandler<Boolean> responseHandler = new FileStreamResponseHandler(output, new Progress() {
                private int oldState = 0;

                @Override
                public void run(long progress, long total) {
                    //Log.d(THIS_FILE, "Progress is "+progress+" on "+total);
                    int newState = (int) Math.round(progress * 50.0f / total);
                    if (oldState != newState) {

                        notification.contentView.setProgressBar(R.id.status_progress, 50, newState, false);
                        notificationManager.notify(NOTIF_DOWNLOAD, notification);
                        oldState = newState;
                    }

                }
            });
            boolean hasReply = client.execute(getMethod, responseHandler);

            if (hasReply) {

                if (checkMd5) {
                    URL url = new URL(intent.getData().toString().concat(".md5sum"));
                    InputStream content = (InputStream) url.getContent();
                    if (content != null) {
                        BufferedReader br = new BufferedReader(new InputStreamReader(content));
                        String downloadedMD5 = "";
                        try {
                            downloadedMD5 = br.readLine().split("  ")[0];
                        } catch (NullPointerException e) {
                            throw new IOException("md5_verification : no sum on server");
                        }
                        if (!MD5.checkMD5(downloadedMD5, output)) {
                            throw new IOException("md5_verification : incorrect");
                        }
                    }
                }
                PendingIntent pendingIntent = (PendingIntent) intent
                        .getParcelableExtra(EXTRA_PENDING_FINISH_INTENT);

                try {
                    Runtime.getRuntime().exec("chmod 644 " + outPath);
                } catch (IOException e) {
                    Log.e(THIS_FILE, "Unable to make the apk file readable", e);
                }

                Log.d(THIS_FILE, "Download finished of : " + outPath);
                if (pendingIntent != null) {

                    notification.contentIntent = pendingIntent;
                    notification.flags = Notification.FLAG_AUTO_CANCEL;
                    notification.icon = android.R.drawable.stat_sys_download_done;
                    notification.contentView.setViewVisibility(R.id.status_progress_wrapper, View.GONE);
                    notification.contentView.setTextViewText(R.id.status_text,
                            getResources().getString(R.string.done)
                                    // TODO should be a parameter of this class
                                    + " - Click to install");
                    notificationManager.notify(NOTIF_DOWNLOAD, notification);

                    /*
                    try {
                       pendingIntent.send();
                         notificationManager.cancel(NOTIF_DOWNLOAD);
                    } catch (CanceledException e) {
                       Log.e(THIS_FILE, "Impossible to start pending intent for download finish");
                    }
                    */
                } else {
                    Log.w(THIS_FILE, "Invalid pending intent for finish !!!");
                }

                result = Activity.RESULT_OK;
            }
        } catch (IOException e) {
            Log.e(THIS_FILE, "Exception in download", e);
        }
    }

    if (result == Activity.RESULT_CANCELED) {
        notificationManager.cancel(NOTIF_DOWNLOAD);
    }
}

From source file:com.sonetel.service.Downloader.java

@Override
protected void onHandleIntent(Intent intent) {
    HttpGet getMethod = new HttpGet(intent.getData().toString());
    int result = Activity.RESULT_CANCELED;
    String outPath = intent.getStringExtra(EXTRA_OUTPATH);
    boolean checkMd5 = intent.getBooleanExtra(EXTRA_CHECK_MD5, false);
    int icon = intent.getIntExtra(EXTRA_ICON, 0);
    String title = intent.getStringExtra(EXTRA_TITLE);
    boolean showNotif = (icon > 0 && !TextUtils.isEmpty(title));

    // Build notification
    Builder nb = new NotificationCompat.Builder(this);
    nb.setWhen(System.currentTimeMillis());
    nb.setContentTitle(title);//from  w  ww. java  2 s  .c  o  m
    nb.setSmallIcon(android.R.drawable.stat_sys_download);
    nb.setOngoing(true);
    Intent i = new Intent(this, SipHome.class);
    nb.setContentIntent(PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));

    RemoteViews contentView = new RemoteViews(getApplicationContext().getPackageName(),
            R.layout.download_notif);
    contentView.setImageViewResource(R.id.status_icon, icon);
    contentView.setTextViewText(R.id.status_text, getResources().getString(R.string.downloading_text));
    contentView.setProgressBar(R.id.status_progress, 50, 0, false);
    contentView.setViewVisibility(R.id.status_progress_wrapper, View.VISIBLE);
    nb.setContent(contentView);

    final Notification notification = showNotif ? nb.getNotification() : null;
    notification.contentView = contentView;
    if (!TextUtils.isEmpty(outPath)) {
        try {
            File output = new File(outPath);
            if (output.exists()) {
                output.delete();
            }

            if (notification != null) {
                notificationManager.notify(NOTIF_DOWNLOAD, notification);
            }
            ResponseHandler<Boolean> responseHandler = new FileStreamResponseHandler(output, new Progress() {
                private int oldState = 0;

                @Override
                public void run(long progress, long total) {
                    //Log.d(THIS_FILE, "Progress is "+progress+" on "+total);
                    int newState = (int) Math.round(progress * 50.0f / total);
                    if (oldState != newState) {

                        notification.contentView.setProgressBar(R.id.status_progress, 50, newState, false);
                        notificationManager.notify(NOTIF_DOWNLOAD, notification);
                        oldState = newState;
                    }

                }
            });
            boolean hasReply = client.execute(getMethod, responseHandler);

            if (hasReply) {

                if (checkMd5) {
                    URL url = new URL(intent.getData().toString().concat(".md5sum"));
                    InputStream content = (InputStream) url.getContent();
                    if (content != null) {
                        BufferedReader br = new BufferedReader(new InputStreamReader(content));
                        String downloadedMD5 = "";
                        try {
                            downloadedMD5 = br.readLine().split("  ")[0];
                        } catch (NullPointerException e) {
                            throw new IOException("md5_verification : no sum on server");
                        }
                        if (!MD5.checkMD5(downloadedMD5, output)) {
                            throw new IOException("md5_verification : incorrect");
                        }
                    }
                }
                PendingIntent pendingIntent = (PendingIntent) intent
                        .getParcelableExtra(EXTRA_PENDING_FINISH_INTENT);

                try {
                    Runtime.getRuntime().exec("chmod 644 " + outPath);
                } catch (IOException e) {
                    Log.e(THIS_FILE, "Unable to make the apk file readable", e);
                }

                Log.d(THIS_FILE, "Download finished of : " + outPath);
                if (pendingIntent != null) {

                    notification.contentIntent = pendingIntent;
                    notification.flags = Notification.FLAG_AUTO_CANCEL;
                    notification.icon = android.R.drawable.stat_sys_download_done;
                    notification.contentView.setViewVisibility(R.id.status_progress_wrapper, View.GONE);
                    notification.contentView.setTextViewText(R.id.status_text,
                            getResources().getString(R.string.done)
                                    // TODO should be a parameter of this class
                                    + " - Click to install");
                    notificationManager.notify(NOTIF_DOWNLOAD, notification);

                    /*
                    try {
                       pendingIntent.send();
                         notificationManager.cancel(NOTIF_DOWNLOAD);
                    } catch (CanceledException e) {
                       Log.e(THIS_FILE, "Impossible to start pending intent for download finish");
                    }
                    */
                } else {
                    Log.w(THIS_FILE, "Invalid pending intent for finish !!!");
                }

                result = Activity.RESULT_OK;
            }
        } catch (IOException e) {
            Log.e(THIS_FILE, "Exception in download", e);
        }
    }

    if (result == Activity.RESULT_CANCELED) {
        notificationManager.cancel(NOTIF_DOWNLOAD);
    }
}

From source file:org.chromium.chrome.browser.notifications.CustomNotificationBuilder.java

@Override
public Notification build() {
    RemoteViews compactView = new RemoteViews(mContext.getPackageName(), R.layout.web_notification);
    RemoteViews bigView = new RemoteViews(mContext.getPackageName(), R.layout.web_notification_big);

    String time = DateFormat.getTimeFormat(mContext).format(new Date());
    for (RemoteViews view : new RemoteViews[] { compactView, bigView }) {
        view.setTextViewText(R.id.time, time);
        view.setTextViewText(R.id.title, mTitle);
        view.setTextViewText(R.id.body, mBody);
        view.setTextViewText(R.id.origin, mOrigin);
        view.setImageViewBitmap(R.id.icon, mLargeIcon);
    }/*  w  w  w.  j  av a 2 s.c  om*/

    if (!mActions.isEmpty()) {
        bigView.setViewVisibility(R.id.button_divider, View.VISIBLE);
        bigView.setViewVisibility(R.id.buttons, View.VISIBLE);
        for (Action action : mActions) {
            RemoteViews button = new RemoteViews(mContext.getPackageName(), R.layout.web_notification_button);
            button.setTextViewCompoundDrawablesRelative(R.id.button, action.getIcon(), 0, 0, 0);
            button.setTextViewText(R.id.button, action.getTitle());
            button.setOnClickPendingIntent(R.id.button, action.getActionIntent());
            bigView.addView(R.id.buttons, button);
        }
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
    builder.setTicker(mTickerText);
    builder.setSmallIcon(mSmallIconId);
    builder.setContentIntent(mContentIntent);
    builder.setDeleteIntent(mDeleteIntent);
    builder.setDefaults(mDefaults);
    builder.setVibrate(mVibratePattern);
    builder.setContent(compactView);

    Notification notification = builder.build();
    notification.bigContentView = bigView;
    return notification;
}