List of usage examples for android.widget RemoteViews setChronometer
public void setChronometer(int viewId, long base, String format, boolean started)
From source file:br.com.bioscada.apps.biotracks.widgets.TrackWidgetProvider.java
/** * Updates total time./* w ww . ja va 2s . c o m*/ * * @param context the context * @param remoteViews the remote views * @param ids the item's ids * @param tripStatistics the trip statistics */ private static void updateTotalTime(Context context, RemoteViews remoteViews, int[] ids, TripStatistics tripStatistics, boolean isRecording, boolean isPaused) { if (isRecording && !isPaused && tripStatistics != null) { long time = tripStatistics.getTotalTime() + System.currentTimeMillis() - tripStatistics.getStopTime(); remoteViews.setChronometer(ids[3], SystemClock.elapsedRealtime() - time, null, true); remoteViews.setViewVisibility(ids[1], View.GONE); remoteViews.setViewVisibility(ids[2], View.GONE); remoteViews.setViewVisibility(ids[3], View.VISIBLE); } else { remoteViews.setChronometer(ids[3], SystemClock.elapsedRealtime(), null, false); remoteViews.setViewVisibility(ids[1], View.VISIBLE); remoteViews.setViewVisibility(ids[2], View.GONE); remoteViews.setViewVisibility(ids[3], View.GONE); String totalTime = tripStatistics == null ? context.getString(R.string.value_unknown) : StringUtils.formatElapsedTime(tripStatistics.getTotalTime()); remoteViews.setTextViewText(ids[0], context.getString(R.string.stats_total_time)); remoteViews.setTextViewText(ids[1], totalTime); } }
From source file:org.sipdroid.sipua.ui.Receiver.java
public static void onText(int type, String text, int mInCallResId, long base) { if (mSipdroidEngine != null && type == REGISTER_NOTIFICATION + mSipdroidEngine.pref) { cache_text = text;/*w w w .ja v a 2s .c o m*/ cache_res = mInCallResId; } if (type >= REGISTER_NOTIFICATION && mInCallResId == R.drawable.sym_presence_available && !PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getBoolean( org.sipdroid.sipua.ui.Settings.PREF_REGISTRATION, org.sipdroid.sipua.ui.Settings.DEFAULT_REGISTRATION)) text = null; NotificationManager mNotificationMgr = (NotificationManager) mContext .getSystemService(Context.NOTIFICATION_SERVICE); if (text != null) { Notification notification = new Notification(); NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); notification.icon = mInCallResId; if (type == MISSED_CALL_NOTIFICATION) { notification.flags |= Notification.FLAG_AUTO_CANCEL; //TODO /*notification.setLatestEventInfo(mContext, text, mContext.getString(R.string.app_name), PendingIntent.getActivity(mContext, 0, createCallLogIntent(), 0)); if (PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_NOTIFY, org.sipdroid.sipua.ui.Settings.DEFAULT_NOTIFY)) { notification.flags |= Notification.FLAG_SHOW_LIGHTS; notification.ledARGB = 0xff0000ff; blue notification.ledOnMS = 125; notification.ledOffMS = 2875; }*/ } else { switch (type) { case MWI_NOTIFICATION: notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.contentIntent = PendingIntent.getActivity(mContext, 0, createMWIIntent(), 0); notification.flags |= Notification.FLAG_SHOW_LIGHTS; notification.ledARGB = 0xff00ff00; /* green */ notification.ledOnMS = 125; notification.ledOffMS = 2875; break; case AUTO_ANSWER_NOTIFICATION: notification.contentIntent = PendingIntent.getActivity(mContext, 0, createIntent(AutoAnswer.class), 0); break; default: if (type >= REGISTER_NOTIFICATION && mSipdroidEngine != null && type != REGISTER_NOTIFICATION + mSipdroidEngine.pref && mInCallResId == R.drawable.sym_presence_available) { int key = type - REGISTER_NOTIFICATION; Intent in = createIntent(ChangeAccount.class); in.setAction(Long.toString(type)); in.putExtra(ChangeAccount.ChangeAccountKey, key); in.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); notification.contentIntent = PendingIntent.getActivity(mContext, key, in, PendingIntent.FLAG_UPDATE_CURRENT); } else notification.contentIntent = PendingIntent.getActivity(mContext, 0, createIntent(Sipdroid.class), 0); if (mInCallResId == R.drawable.sym_presence_away) { notification.flags |= Notification.FLAG_SHOW_LIGHTS; notification.ledARGB = 0xffff0000; /* red */ notification.ledOnMS = 125; notification.ledOffMS = 2875; } notification.flags |= Notification.FLAG_ONGOING_EVENT; break; } RemoteViews contentView = new RemoteViews(mContext.getPackageName(), R.layout.ongoing_call_notification); contentView.setImageViewResource(R.id.icon, notification.icon); if (base != 0) { contentView.setChronometer(R.id.text1, base, text + " (%s)", true); } else if (type >= REGISTER_NOTIFICATION) { if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean( org.sipdroid.sipua.ui.Settings.PREF_POS, org.sipdroid.sipua.ui.Settings.DEFAULT_POS)) contentView.setTextViewText(R.id.text2, text + "/" + mContext.getString(R.string.settings_pos3)); else contentView.setTextViewText(R.id.text2, text); if (mSipdroidEngine != null) contentView.setTextViewText(R.id.text1, mSipdroidEngine.user_profiles[type - REGISTER_NOTIFICATION].username + "@" + mSipdroidEngine.user_profiles[type - REGISTER_NOTIFICATION].realm_orig); } else contentView.setTextViewText(R.id.text1, text); notification.contentView = contentView; } mNotificationMgr.notify(type, notification); } else { mNotificationMgr.cancel(type); } if (type != AUTO_ANSWER_NOTIFICATION) updateAutoAnswer(); if (mSipdroidEngine != null && type >= REGISTER_NOTIFICATION && type != REGISTER_NOTIFICATION + mSipdroidEngine.pref) onText(REGISTER_NOTIFICATION + mSipdroidEngine.pref, cache_text, cache_res, 0); }
From source file:com.android.deskclock.data.StopwatchNotificationBuilderN.java
@Override public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) { @StringRes/*from www . j ava2 s. c om*/ final int eventLabel = R.string.label_notification; // Intent to load the app when the notification is tapped. final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); // Compute some values required below. final boolean running = stopwatch.isRunning(); final String pname = context.getPackageName(); final Resources res = context.getResources(); final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime(); final RemoteViews content = new RemoteViews(pname, R.layout.chronometer_notif_content); content.setChronometer(R.id.chronometer, base, null, running); final List<Notification.Action> actions = new ArrayList<>(2); if (running) { // Left button: Pause final Intent pause = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_pause_24dp); final CharSequence title1 = res.getText(R.string.sw_pause_button); final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); // Right button: Add Lap if (DataModel.getDataModel().canAddMoreLaps()) { final Intent lap = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Icon icon2 = Icon.createWithResource(context, R.drawable.ic_sw_lap_24dp); final CharSequence title2 = res.getText(R.string.sw_lap_button); final PendingIntent intent2 = Utils.pendingServiceIntent(context, lap); actions.add(new Notification.Action.Builder(icon2, title2, intent2).build()); } // Show the current lap number if any laps have been recorded. final int lapCount = DataModel.getDataModel().getLaps().size(); if (lapCount > 0) { final int lapNumber = lapCount + 1; final String lap = res.getString(R.string.sw_notification_lap_number, lapNumber); content.setTextViewText(R.id.state, lap); content.setViewVisibility(R.id.state, VISIBLE); } else { content.setViewVisibility(R.id.state, GONE); } } else { // Left button: Start final Intent start = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_start_24dp); final CharSequence title1 = res.getText(R.string.sw_start_button); final PendingIntent intent1 = Utils.pendingServiceIntent(context, start); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); // Right button: Reset (dismisses notification and resets stopwatch) final Intent reset = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Icon icon2 = Icon.createWithResource(context, R.drawable.ic_reset_24dp); final CharSequence title2 = res.getText(R.string.sw_reset_button); final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset); actions.add(new Notification.Action.Builder(icon2, title2, intent2).build()); // Indicate the stopwatch is paused. content.setTextViewText(R.id.state, res.getString(R.string.swn_paused)); content.setViewVisibility(R.id.state, VISIBLE); } // Swipe away will reset the stopwatch without bringing forward the app. final Intent reset = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); return new Notification.Builder(context).setLocalOnly(true).setOngoing(running) .setCustomContentView(content).setContentIntent(pendingShowApp).setAutoCancel(stopwatch.isPaused()) .setPriority(Notification.PRIORITY_MAX).setSmallIcon(R.drawable.stat_notify_stopwatch) .setGroup(nm.getStopwatchNotificationGroupKey()) .setStyle(new Notification.DecoratedCustomViewStyle()) .setDeleteIntent(Utils.pendingServiceIntent(context, reset)) .setActions(actions.toArray(new Notification.Action[actions.size()])) .setColor(ContextCompat.getColor(context, R.color.default_background)).build(); }
From source file:com.android.deskclock.data.StopwatchNotificationBuilderPreN.java
@Override public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) { @StringRes//from w w w .j a v a2s . co m final int eventLabel = R.string.label_notification; // Intent to load the app when the notification is tapped. final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); // Compute some values required below. final boolean running = stopwatch.isRunning(); final String pname = context.getPackageName(); final Resources res = context.getResources(); final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime(); final RemoteViews collapsed = new RemoteViews(pname, R.layout.stopwatch_notif_collapsed); collapsed.setChronometer(R.id.swn_collapsed_chronometer, base, null, running); collapsed.setOnClickPendingIntent(R.id.swn_collapsed_hitspace, pendingShowApp); collapsed.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch); final RemoteViews expanded = new RemoteViews(pname, R.layout.stopwatch_notif_expanded); expanded.setChronometer(R.id.swn_expanded_chronometer, base, null, running); expanded.setOnClickPendingIntent(R.id.swn_expanded_hitspace, pendingShowApp); expanded.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch); @IdRes final int leftButtonId = R.id.swn_left_button; @IdRes final int rightButtonId = R.id.swn_right_button; if (running) { // Left button: Pause expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_pause_button)); setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_pause_24dp); final Intent pause = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingPause = Utils.pendingServiceIntent(context, pause); expanded.setOnClickPendingIntent(leftButtonId, pendingPause); // Right button: Add Lap if (DataModel.getDataModel().canAddMoreLaps()) { expanded.setTextViewText(rightButtonId, res.getText(R.string.sw_lap_button)); setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_sw_lap_24dp); final Intent lap = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingLap = Utils.pendingServiceIntent(context, lap); expanded.setOnClickPendingIntent(rightButtonId, pendingLap); expanded.setViewVisibility(rightButtonId, VISIBLE); } else { expanded.setViewVisibility(rightButtonId, INVISIBLE); } // Show the current lap number if any laps have been recorded. final int lapCount = DataModel.getDataModel().getLaps().size(); if (lapCount > 0) { final int lapNumber = lapCount + 1; final String lap = res.getString(R.string.sw_notification_lap_number, lapNumber); collapsed.setTextViewText(R.id.swn_collapsed_laps, lap); collapsed.setViewVisibility(R.id.swn_collapsed_laps, VISIBLE); expanded.setTextViewText(R.id.swn_expanded_laps, lap); expanded.setViewVisibility(R.id.swn_expanded_laps, VISIBLE); } else { collapsed.setViewVisibility(R.id.swn_collapsed_laps, GONE); expanded.setViewVisibility(R.id.swn_expanded_laps, GONE); } } else { // Left button: Start expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_start_button)); setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_start_24dp); final Intent start = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingStart = Utils.pendingServiceIntent(context, start); expanded.setOnClickPendingIntent(leftButtonId, pendingStart); // Right button: Reset (dismisses notification and resets stopwatch) expanded.setViewVisibility(rightButtonId, VISIBLE); expanded.setTextViewText(rightButtonId, res.getText(R.string.sw_reset_button)); setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_reset_24dp); final Intent reset = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingReset = Utils.pendingServiceIntent(context, reset); expanded.setOnClickPendingIntent(rightButtonId, pendingReset); // Indicate the stopwatch is paused. collapsed.setTextViewText(R.id.swn_collapsed_laps, res.getString(R.string.swn_paused)); collapsed.setViewVisibility(R.id.swn_collapsed_laps, VISIBLE); expanded.setTextViewText(R.id.swn_expanded_laps, res.getString(R.string.swn_paused)); expanded.setViewVisibility(R.id.swn_expanded_laps, VISIBLE); } // Swipe away will reset the stopwatch without bringing forward the app. final Intent reset = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Notification notification = new NotificationCompat.Builder(context).setLocalOnly(true) .setOngoing(running).setContent(collapsed).setAutoCancel(stopwatch.isPaused()) .setPriority(NotificationCompat.PRIORITY_MAX).setSmallIcon(R.drawable.stat_notify_stopwatch) .setDeleteIntent(Utils.pendingServiceIntent(context, reset)) .setColor(ContextCompat.getColor(context, R.color.default_background)).build(); notification.bigContentView = expanded; return notification; }
From source file:com.wizardsofm.deskclock.data.StopwatchNotificationBuilderN.java
@Override public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) { @StringRes// www. j a v a 2s.com final int eventLabel = com.wizardsofm.deskclock.R.string.label_notification; // Intent to load the app when the notification is tapped. final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); // Compute some values required below. final boolean running = stopwatch.isRunning(); final String pname = context.getPackageName(); final Resources res = context.getResources(); final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime(); final RemoteViews content = new RemoteViews(pname, com.wizardsofm.deskclock.R.layout.chronometer_notif_content); content.setChronometer(com.wizardsofm.deskclock.R.id.chronometer, base, null, running); final List<Notification.Action> actions = new ArrayList<>(2); if (running) { // Left button: Pause final Intent pause = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Icon icon1 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_pause_24dp); final CharSequence title1 = res.getText(com.wizardsofm.deskclock.R.string.sw_pause_button); final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); // Right button: Add Lap if (DataModel.getDataModel().canAddMoreLaps()) { final Intent lap = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Icon icon2 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_sw_lap_24dp); final CharSequence title2 = res.getText(com.wizardsofm.deskclock.R.string.sw_lap_button); final PendingIntent intent2 = Utils.pendingServiceIntent(context, lap); actions.add(new Notification.Action.Builder(icon2, title2, intent2).build()); } // Show the current lap number if any laps have been recorded. final int lapCount = DataModel.getDataModel().getLaps().size(); if (lapCount > 0) { final int lapNumber = lapCount + 1; final String lap = res.getString(com.wizardsofm.deskclock.R.string.sw_notification_lap_number, lapNumber); content.setTextViewText(com.wizardsofm.deskclock.R.id.state, lap); content.setViewVisibility(com.wizardsofm.deskclock.R.id.state, VISIBLE); } else { content.setViewVisibility(com.wizardsofm.deskclock.R.id.state, GONE); } } else { // Left button: Start final Intent start = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Icon icon1 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_start_24dp); final CharSequence title1 = res.getText(com.wizardsofm.deskclock.R.string.sw_start_button); final PendingIntent intent1 = Utils.pendingServiceIntent(context, start); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); // Right button: Reset (dismisses notification and resets stopwatch) final Intent reset = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Icon icon2 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_reset_24dp); final CharSequence title2 = res.getText(com.wizardsofm.deskclock.R.string.sw_reset_button); final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset); actions.add(new Notification.Action.Builder(icon2, title2, intent2).build()); // Indicate the stopwatch is paused. content.setTextViewText(com.wizardsofm.deskclock.R.id.state, res.getString(com.wizardsofm.deskclock.R.string.swn_paused)); content.setViewVisibility(com.wizardsofm.deskclock.R.id.state, VISIBLE); } // Swipe away will reset the stopwatch without bringing forward the app. final Intent reset = new Intent(context, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); return new Notification.Builder(context).setLocalOnly(true).setOngoing(running) .setCustomContentView(content).setContentIntent(pendingShowApp).setAutoCancel(stopwatch.isPaused()) .setPriority(Notification.PRIORITY_MAX) .setSmallIcon(com.wizardsofm.deskclock.R.drawable.stat_notify_stopwatch) .setGroup(nm.getStopwatchNotificationGroupKey()) .setStyle(new Notification.DecoratedCustomViewStyle()) .setDeleteIntent(Utils.pendingServiceIntent(context, reset)) .setActions(actions.toArray(new Notification.Action[actions.size()])) .setColor(ContextCompat.getColor(context, com.wizardsofm.deskclock.R.color.default_background)) .build(); }
From source file:com.androidinspain.deskclock.data.StopwatchNotificationBuilder.java
public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) { @StringRes//www . ja v a 2 s . c om final int eventLabel = com.androidinspain.deskclock.R.string.label_notification; // Intent to load the app when the notification is tapped. final Intent showApp = new Intent(context, StopwatchService.class) .setAction(StopwatchService.ACTION_SHOW_STOPWATCH).putExtra(Events.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingShowApp = PendingIntent.getService(context, 0, showApp, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); // Compute some values required below. final boolean running = stopwatch.isRunning(); final String pname = context.getPackageName(); final Resources res = context.getResources(); final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime(); final RemoteViews content = new RemoteViews(pname, com.androidinspain.deskclock.R.layout.chronometer_notif_content); content.setChronometer(com.androidinspain.deskclock.R.id.chronometer, base, null, running); final List<Action> actions = new ArrayList<>(2); if (running) { // Left button: Pause final Intent pause = new Intent(context, StopwatchService.class) .setAction(StopwatchService.ACTION_PAUSE_STOPWATCH) .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel); @DrawableRes final int icon1 = com.androidinspain.deskclock.R.drawable.ic_pause_24dp; final CharSequence title1 = res.getText(com.androidinspain.deskclock.R.string.sw_pause_button); final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause); actions.add(new Action.Builder(icon1, title1, intent1).build()); // Right button: Add Lap if (DataModel.getDataModel().canAddMoreLaps()) { final Intent lap = new Intent(context, StopwatchService.class) .setAction(StopwatchService.ACTION_LAP_STOPWATCH) .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel); @DrawableRes final int icon2 = com.androidinspain.deskclock.R.drawable.ic_sw_lap_24dp; final CharSequence title2 = res.getText(com.androidinspain.deskclock.R.string.sw_lap_button); final PendingIntent intent2 = Utils.pendingServiceIntent(context, lap); actions.add(new Action.Builder(icon2, title2, intent2).build()); } // Show the current lap number if any laps have been recorded. final int lapCount = DataModel.getDataModel().getLaps().size(); if (lapCount > 0) { final int lapNumber = lapCount + 1; final String lap = res.getString(com.androidinspain.deskclock.R.string.sw_notification_lap_number, lapNumber); content.setTextViewText(com.androidinspain.deskclock.R.id.state, lap); content.setViewVisibility(com.androidinspain.deskclock.R.id.state, VISIBLE); } else { content.setViewVisibility(com.androidinspain.deskclock.R.id.state, GONE); } } else { // Left button: Start final Intent start = new Intent(context, StopwatchService.class) .setAction(StopwatchService.ACTION_START_STOPWATCH) .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel); @DrawableRes final int icon1 = com.androidinspain.deskclock.R.drawable.ic_start_24dp; final CharSequence title1 = res.getText(com.androidinspain.deskclock.R.string.sw_start_button); final PendingIntent intent1 = Utils.pendingServiceIntent(context, start); actions.add(new Action.Builder(icon1, title1, intent1).build()); // Right button: Reset (dismisses notification and resets stopwatch) final Intent reset = new Intent(context, StopwatchService.class) .setAction(StopwatchService.ACTION_RESET_STOPWATCH) .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel); @DrawableRes final int icon2 = com.androidinspain.deskclock.R.drawable.ic_reset_24dp; final CharSequence title2 = res.getText(com.androidinspain.deskclock.R.string.sw_reset_button); final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset); actions.add(new Action.Builder(icon2, title2, intent2).build()); // Indicate the stopwatch is paused. content.setTextViewText(com.androidinspain.deskclock.R.id.state, res.getString(com.androidinspain.deskclock.R.string.swn_paused)); content.setViewVisibility(com.androidinspain.deskclock.R.id.state, VISIBLE); } final Builder notification = new NotificationCompat.Builder(context).setLocalOnly(true).setOngoing(running) .setCustomContentView(content).setContentIntent(pendingShowApp).setAutoCancel(stopwatch.isPaused()) .setPriority(Notification.PRIORITY_MAX) .setSmallIcon(com.androidinspain.deskclock.R.drawable.stat_notify_stopwatch) .setStyle(new NotificationCompat.DecoratedCustomViewStyle()) .setColor(ContextCompat.getColor(context, com.androidinspain.deskclock.R.color.default_background)); if (Utils.isNOrLater()) { notification.setGroup(nm.getStopwatchNotificationGroupKey()); } for (Action action : actions) { notification.addAction(action); } return notification.build(); }
From source file:com.android.deskclock.data.TimerNotificationBuilderN.java
@Override public Notification buildHeadsUp(Context context, List<Timer> expired) { final Timer timer = expired.get(0); // First action intent is to reset all timers. final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_stop_24dp); final Intent reset = TimerService.createResetExpiredTimersIntent(context); final PendingIntent intent1 = Utils.pendingServiceIntent(context, reset); // Generate some descriptive text, a title, and an action name based on the timer count. final CharSequence stateText; final int count = expired.size(); final List<Notification.Action> actions = new ArrayList<>(2); if (count == 1) { final String label = timer.getLabel(); if (TextUtils.isEmpty(label)) { stateText = context.getString(R.string.timer_times_up); } else {/* ww w. j a va 2 s .co m*/ stateText = label; } // Left button: Reset single timer final CharSequence title1 = context.getString(R.string.timer_stop); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); // Right button: Add minute final Intent addTime = TimerService.createAddMinuteTimerIntent(context, timer.getId()); final PendingIntent intent2 = Utils.pendingServiceIntent(context, addTime); final Icon icon2 = Icon.createWithResource(context, R.drawable.ic_add_24dp); final CharSequence title2 = context.getString(R.string.timer_plus_1_min); actions.add(new Notification.Action.Builder(icon2, title2, intent2).build()); } else { stateText = context.getString(R.string.timer_multi_times_up, count); // Left button: Reset all timers final CharSequence title1 = context.getString(R.string.timer_stop_all); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); } final long base = getChronometerBase(timer); final String pname = context.getPackageName(); final RemoteViews contentView = new RemoteViews(pname, R.layout.chronometer_notif_content); contentView.setChronometerCountDown(R.id.chronometer, true); contentView.setChronometer(R.id.chronometer, base, null, true); contentView.setTextViewText(R.id.state, stateText); // Content intent shows the timer full screen when clicked. final Intent content = new Intent(context, ExpiredTimersActivity.class); final PendingIntent contentIntent = Utils.pendingActivityIntent(context, content); // Full screen intent has flags so it is different than the content intent. final Intent fullScreen = new Intent(context, ExpiredTimersActivity.class) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION); final PendingIntent pendingFullScreen = Utils.pendingActivityIntent(context, fullScreen); return new Notification.Builder(context).setOngoing(true).setLocalOnly(true).setShowWhen(false) .setAutoCancel(false).setContentIntent(contentIntent).setCustomContentView(contentView) .setPriority(Notification.PRIORITY_MAX).setDefaults(Notification.DEFAULT_LIGHTS) .setColor(ContextCompat.getColor(context, R.color.default_background)) .setSmallIcon(R.drawable.stat_notify_timer).setFullScreenIntent(pendingFullScreen, true) .setStyle(new Notification.DecoratedCustomViewStyle()) .setActions(actions.toArray(new Notification.Action[actions.size()])).build(); }
From source file:com.android.deskclock.data.TimerNotificationBuilderN.java
@Override public Notification build(Context context, NotificationModel nm, List<Timer> unexpired) { final Timer timer = unexpired.get(0); final int count = unexpired.size(); // Compute some values required below. final boolean running = timer.isRunning(); final Resources res = context.getResources(); final long base = getChronometerBase(timer); final String pname = context.getPackageName(); final RemoteViews content = new RemoteViews(pname, R.layout.chronometer_notif_content); content.setChronometerCountDown(R.id.chronometer, true); content.setChronometer(R.id.chronometer, base, null, running); final List<Notification.Action> actions = new ArrayList<>(2); final CharSequence stateText; if (count == 1) { if (running) { // Single timer is running. if (TextUtils.isEmpty(timer.getLabel())) { stateText = res.getString(R.string.timer_notification_label); } else { stateText = timer.getLabel(); }/*from w w w . jav a 2s. c om*/ // Left button: Pause final Intent pause = new Intent(context, TimerService.class) .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_TIMER) .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId()); final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_pause_24dp); final CharSequence title1 = res.getText(R.string.timer_pause); final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); // Right Button: +1 Minute final Intent addMinute = new Intent(context, TimerService.class) .setAction(HandleDeskClockApiCalls.ACTION_ADD_MINUTE_TIMER) .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId()); final Icon icon2 = Icon.createWithResource(context, R.drawable.ic_add_24dp); final CharSequence title2 = res.getText(R.string.timer_plus_1_min); final PendingIntent intent2 = Utils.pendingServiceIntent(context, addMinute); actions.add(new Notification.Action.Builder(icon2, title2, intent2).build()); } else { // Single timer is paused. stateText = res.getString(R.string.timer_paused); // Left button: Start final Intent start = new Intent(context, TimerService.class) .setAction(HandleDeskClockApiCalls.ACTION_START_TIMER) .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId()); final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_start_24dp); final CharSequence title1 = res.getText(R.string.sw_resume_button); final PendingIntent intent1 = Utils.pendingServiceIntent(context, start); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); // Right Button: Reset final Intent reset = new Intent(context, TimerService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_TIMER) .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId()); final Icon icon2 = Icon.createWithResource(context, R.drawable.ic_reset_24dp); final CharSequence title2 = res.getText(R.string.sw_reset_button); final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset); actions.add(new Notification.Action.Builder(icon2, title2, intent2).build()); } } else { if (running) { // At least one timer is running. stateText = res.getString(R.string.timers_in_use, count); } else { // All timers are paused. stateText = res.getString(R.string.timers_stopped, count); } final Intent reset = TimerService.createResetUnexpiredTimersIntent(context); final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_reset_24dp); final CharSequence title1 = res.getText(R.string.timer_reset_all); final PendingIntent intent1 = Utils.pendingServiceIntent(context, reset); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); } content.setTextViewText(R.id.state, stateText); // Intent to load the app and show the timer when the notification is tapped. final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_TIMERS) .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId()) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, R.string.label_notification); final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); return new Notification.Builder(context).setOngoing(true).setLocalOnly(true).setShowWhen(false) .setAutoCancel(false).setCustomContentView(content).setContentIntent(pendingShowApp) .setPriority(Notification.PRIORITY_HIGH).setCategory(Notification.CATEGORY_ALARM) .setSmallIcon(R.drawable.stat_notify_timer).setGroup(nm.getTimerNotificationGroupKey()) .setVisibility(Notification.VISIBILITY_PUBLIC).setStyle(new Notification.DecoratedCustomViewStyle()) .setActions(actions.toArray(new Notification.Action[actions.size()])) .setColor(ContextCompat.getColor(context, R.color.default_background)).build(); }
From source file:com.wizardsofm.deskclock.data.TimerNotificationBuilderN.java
@Override public Notification buildHeadsUp(Context context, List<Timer> expired) { final Timer timer = expired.get(0); // First action intent is to reset all timers. final Icon icon1 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_stop_24dp); final Intent reset = TimerService.createResetExpiredTimersIntent(context); final PendingIntent intent1 = Utils.pendingServiceIntent(context, reset); // Generate some descriptive text, a title, and an action name based on the timer count. final CharSequence stateText; final int count = expired.size(); final List<Notification.Action> actions = new ArrayList<>(2); if (count == 1) { final String label = timer.getLabel(); if (TextUtils.isEmpty(label)) { stateText = context.getString(com.wizardsofm.deskclock.R.string.timer_times_up); } else {/*from ww w . ja va 2s .com*/ stateText = label; } // Left button: Reset single timer final CharSequence title1 = context.getString(com.wizardsofm.deskclock.R.string.timer_stop); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); // Right button: Add minute final Intent addTime = TimerService.createAddMinuteTimerIntent(context, timer.getId()); final PendingIntent intent2 = Utils.pendingServiceIntent(context, addTime); final Icon icon2 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_add_24dp); final CharSequence title2 = context.getString(com.wizardsofm.deskclock.R.string.timer_plus_1_min); actions.add(new Notification.Action.Builder(icon2, title2, intent2).build()); } else { stateText = context.getString(com.wizardsofm.deskclock.R.string.timer_multi_times_up, count); // Left button: Reset all timers final CharSequence title1 = context.getString(com.wizardsofm.deskclock.R.string.timer_stop_all); actions.add(new Notification.Action.Builder(icon1, title1, intent1).build()); } final long base = getChronometerBase(timer); final String pname = context.getPackageName(); final RemoteViews contentView = new RemoteViews(pname, com.wizardsofm.deskclock.R.layout.chronometer_notif_content); contentView.setChronometerCountDown(com.wizardsofm.deskclock.R.id.chronometer, true); contentView.setChronometer(com.wizardsofm.deskclock.R.id.chronometer, base, null, true); contentView.setTextViewText(com.wizardsofm.deskclock.R.id.state, stateText); // Content intent shows the timer full screen when clicked. final Intent content = new Intent(context, ExpiredTimersActivity.class); final PendingIntent contentIntent = Utils.pendingActivityIntent(context, content); // Full screen intent has flags so it is different than the content intent. final Intent fullScreen = new Intent(context, ExpiredTimersActivity.class) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION); final PendingIntent pendingFullScreen = Utils.pendingActivityIntent(context, fullScreen); return new Notification.Builder(context).setOngoing(true).setLocalOnly(true).setShowWhen(false) .setAutoCancel(false).setContentIntent(contentIntent).setCustomContentView(contentView) .setPriority(Notification.PRIORITY_MAX).setDefaults(Notification.DEFAULT_LIGHTS) .setColor(ContextCompat.getColor(context, com.wizardsofm.deskclock.R.color.default_background)) .setSmallIcon(com.wizardsofm.deskclock.R.drawable.stat_notify_timer) .setFullScreenIntent(pendingFullScreen, true).setStyle(new Notification.DecoratedCustomViewStyle()) .setActions(actions.toArray(new Notification.Action[actions.size()])).build(); }
From source file:com.android.deskclock.data.StopwatchModel.java
/** * Updates the notification to reflect the latest state of the stopwatch and recorded laps. *//*w w w. j a v a 2 s .com*/ void updateNotification() { final Stopwatch stopwatch = getStopwatch(); // Notification should be hidden if the stopwatch has no time or the app is open. if (stopwatch.isReset() || mNotificationModel.isApplicationInForeground()) { mNotificationManager.cancel(mNotificationModel.getStopwatchNotificationId()); return; } @StringRes final int eventLabel = R.string.label_notification; // Intent to load the app when the notification is tapped. final Intent showApp = new Intent(mContext, HandleDeskClockApiCalls.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final PendingIntent pendingShowApp = PendingIntent.getActivity(mContext, 0, showApp, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); // Compute some values required below. final boolean running = stopwatch.isRunning(); final String pname = mContext.getPackageName(); final Resources res = mContext.getResources(); final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime(); final RemoteViews collapsed = new RemoteViews(pname, R.layout.stopwatch_notif_collapsed); collapsed.setChronometer(R.id.swn_collapsed_chronometer, base, null, running); collapsed.setOnClickPendingIntent(R.id.swn_collapsed_hitspace, pendingShowApp); collapsed.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch); final RemoteViews expanded = new RemoteViews(pname, R.layout.stopwatch_notif_expanded); expanded.setChronometer(R.id.swn_expanded_chronometer, base, null, running); expanded.setOnClickPendingIntent(R.id.swn_expanded_hitspace, pendingShowApp); expanded.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch); @IdRes final int leftButtonId = R.id.swn_left_button; @IdRes final int rightButtonId = R.id.swn_right_button; if (running) { // Left button: Pause expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_pause_button)); setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_pause_24dp); final Intent pause = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(leftButtonId, pendingServiceIntent(pause)); // Right button: Add Lap if (canAddMoreLaps()) { expanded.setTextViewText(rightButtonId, res.getText(R.string.sw_lap_button)); setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_sw_lap_24dp); final Intent lap = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(rightButtonId, pendingServiceIntent(lap)); expanded.setViewVisibility(rightButtonId, VISIBLE); } else { expanded.setViewVisibility(rightButtonId, INVISIBLE); } // Show the current lap number if any laps have been recorded. final int lapCount = getLaps().size(); if (lapCount > 0) { final int lapNumber = lapCount + 1; final String lap = res.getString(R.string.sw_notification_lap_number, lapNumber); collapsed.setTextViewText(R.id.swn_collapsed_laps, lap); collapsed.setViewVisibility(R.id.swn_collapsed_laps, VISIBLE); expanded.setTextViewText(R.id.swn_expanded_laps, lap); expanded.setViewVisibility(R.id.swn_expanded_laps, VISIBLE); } else { collapsed.setViewVisibility(R.id.swn_collapsed_laps, GONE); expanded.setViewVisibility(R.id.swn_expanded_laps, GONE); } } else { // Left button: Start expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_start_button)); setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_start_24dp); final Intent start = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(leftButtonId, pendingServiceIntent(start)); // Right button: Reset (HandleDeskClockApiCalls will also bring forward the app) expanded.setViewVisibility(rightButtonId, VISIBLE); expanded.setTextViewText(rightButtonId, res.getText(R.string.sw_reset_button)); setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_reset_24dp); final Intent reset = new Intent(mContext, HandleDeskClockApiCalls.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); expanded.setOnClickPendingIntent(rightButtonId, pendingActivityIntent(reset)); // Indicate the stopwatch is paused. collapsed.setTextViewText(R.id.swn_collapsed_laps, res.getString(R.string.swn_paused)); collapsed.setViewVisibility(R.id.swn_collapsed_laps, VISIBLE); expanded.setTextViewText(R.id.swn_expanded_laps, res.getString(R.string.swn_paused)); expanded.setViewVisibility(R.id.swn_expanded_laps, VISIBLE); } // Swipe away will reset the stopwatch without bringing forward the app. final Intent reset = new Intent(mContext, StopwatchService.class) .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH) .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel); final Notification notification = new NotificationCompat.Builder(mContext).setLocalOnly(true) .setOngoing(running).setContent(collapsed).setAutoCancel(stopwatch.isPaused()) .setPriority(Notification.PRIORITY_MAX).setDeleteIntent(pendingServiceIntent(reset)) .setSmallIcon(R.drawable.ic_tab_stopwatch_activated).build(); notification.bigContentView = expanded; mNotificationManager.notify(mNotificationModel.getStopwatchNotificationId(), notification); }