private void updateNotification(Context context, Alarm alarm, int timeout) { NotificationManager nm = getNotificationManager(context); // If the alarm is null, just cancel the notification. if (alarm == null) { if (Log.LOGV) { Log.v("Cannot update notification for killer callback"); } return; } // Launch SetAlarm when clicked. Intent viewAlarm = new Intent(context, SetAlarm.class); viewAlarm.putExtra(Alarms.ALARM_ID, alarm.id); PendingIntent intent = PendingIntent.getActivity(context, alarm.id, viewAlarm, 0); // Update the notification to indicate that the alert has been // silenced. String label = alarm.getLabelOrDefault(context); Notification n = new Notification(R.drawable.stat_notify_alarm, label, alarm.time); n.setLatestEventInfo( context, label, context.getString(R.string.alarm_alert_alert_silenced, timeout), intent); n.flags |= Notification.FLAG_AUTO_CANCEL; // We have to cancel the original notification since it is in the // ongoing section and we want the "killed" notification to be a plain // notification. nm.cancel(alarm.id); nm.notify(alarm.id, n); }
/** @} */ static void snooze(Context context, Alarm alarm) { final String snooze = PreferenceManager.getDefaultSharedPreferences(context) .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE); int snoozeMinutes = Integer.parseInt(snooze); final long snoozeTime = System.currentTimeMillis() + ((long) 1000 * 60 * snoozeMinutes); saveSnoozeAlert(context, alarm.id, snoozeTime); // Get the display time for the snooze and update the notification. final Calendar c = Calendar.getInstance(); c.setTimeInMillis(snoozeTime); String snoozeTimeStr = Alarms.formatTime(context, c); String label = alarm.getLabelOrDefault(context); // Notify the user that the alarm has been snoozed. Intent dismissIntent = new Intent(context, AlarmReceiver.class); dismissIntent.setAction(Alarms.CANCEL_SNOOZE); dismissIntent.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm); Intent openAlarm = new Intent(context, DeskClock.class); openAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); openAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm); openAlarm.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.CLOCK_TAB_INDEX); NotificationManager nm = getNotificationManager(context); Notification notif = new Notification.Builder(context.getApplicationContext()) .setContentTitle(label) .setContentText( context.getResources().getString(R.string.alarm_alert_snooze_until, snoozeTimeStr)) .setSmallIcon(R.drawable.stat_notify_alarm) .setOngoing(true) .setAutoCancel(false) .setPriority(Notification.PRIORITY_MAX) .setWhen(0) .addAction( android.R.drawable.ic_menu_close_clear_cancel, context.getResources().getString(R.string.alarm_alert_dismiss_text), PendingIntent.getBroadcast(context, alarm.id, dismissIntent, 0)) .build(); notif.contentIntent = PendingIntent.getActivity(context, alarm.id, openAlarm, 0); nm.notify(alarm.id, notif); /// M: @} String displayTime = context.getString(R.string.alarm_alert_snooze_set, snoozeMinutes); // Intentionally log the snooze time for debugging. Log.v(displayTime); // Display the snooze minutes in a toast. Toast.makeText(context, displayTime, Toast.LENGTH_LONG).show(); context.stopService(new Intent(Alarms.ALARM_ALERT_ACTION)); context.stopService(new Intent("com.android.deskclock.ALARM_PHONE_LISTENER")); }
// Attempt to snooze this alert. private void snooze() { // Do not snooze if the snooze button is disabled. if (!findViewById(R.id.snooze).isEnabled()) { dismiss(false); return; } // final String snooze = // PreferenceManager.getDefaultSharedPreferences(this) // .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE); final String snooze = PreferenceManager.getDefaultSharedPreferences(this).getString("", DEFAULT_SNOOZE); int snoozeMinutes = Integer.parseInt(snooze); final long snoozeTime = System.currentTimeMillis() + (1000 * 60 * snoozeMinutes); Alarms.saveSnoozeAlert(AlarmAlertFullScreen.this, mAlarm.id, snoozeTime); // Get the display time for the snooze and update the notification. final Calendar c = Calendar.getInstance(); c.setTimeInMillis(snoozeTime); // Append (snoozed) to the label. String label = mAlarm.getLabelOrDefault(this); label = getString(R.string.alarm_notify_snooze_label, label); // Notify the user that the alarm has been snoozed. Intent cancelSnooze = new Intent(this, AlarmReceiver.class); cancelSnooze.setAction(Alarms.CANCEL_SNOOZE); cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id); PendingIntent broadcast = PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0); NotificationManager nm = getNotificationManager(); Notification n = new Notification(R.drawable.stat_notify_alarm, label, 0); n.setLatestEventInfo( this, label, getString(R.string.alarm_notify_snooze_text, Alarms.formatTime(this, c)), broadcast); n.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT; nm.notify(mAlarm.id, n); String displayTime = getString(R.string.alarm_alert_snooze_set, snoozeMinutes); // Intentionally log the snooze time for debugging. Log.v(TAG, displayTime); // Display the snooze minutes in a toast. Toast.makeText(AlarmAlertFullScreen.this, displayTime, Toast.LENGTH_LONG).show(); stopService(new Intent(Alarms.ALARM_ALERT_ACTION)); finish(); }
@Override public void onReceive(Context context, Intent intent) { if (Alarms.ALARM_KILLED.equals(intent.getAction())) { // The alarm has been killed, update the notification updateNotification( context, (Alarm) intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA), intent.getIntExtra(Alarms.ALARM_KILLED_TIMEOUT, -1)); return; } else if (Alarms.CANCEL_SNOOZE.equals(intent.getAction())) { Alarms.saveSnoozeAlert(context, -1, -1); return; } Alarm alarm = null; // Grab the alarm from the intent. Since the remote AlarmManagerService // fills in the Intent to add some extra data, it must unparcel the // Alarm object. It throws a ClassNotFoundException when unparcelling. // To avoid this, do the marshalling ourselves. final byte[] data = intent.getByteArrayExtra(Alarms.ALARM_RAW_DATA); if (data != null) { Parcel in = Parcel.obtain(); in.unmarshall(data, 0, data.length); in.setDataPosition(0); alarm = Alarm.CREATOR.createFromParcel(in); } if (alarm == null) { Log.v("AlarmReceiver failed to parse the alarm from the intent"); return; } // Intentionally verbose: always log the alarm time to provide useful // information in bug reports. long now = System.currentTimeMillis(); SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS aaa"); Log.v( "AlarmReceiver.onReceive() id " + alarm.id + " setFor " + format.format(new Date(alarm.time))); if (now > alarm.time + STALE_WINDOW * 1000) { if (Log.LOGV) { Log.v("AlarmReceiver ignoring stale alarm"); } return; } // Maintain a cpu wake lock until the AlarmAlert and AlarmKlaxon can // pick it up. AlarmAlertWakeLock.acquireCpuWakeLock(context); /* Close dialogs and window shade */ Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); context.sendBroadcast(closeDialogs); // Decide which activity to start based on the state of the keyguard. Class c = AlarmAlert.class; KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); if (km.inKeyguardRestrictedInputMode()) { // Use the full screen activity for security. c = AlarmAlertFullScreen.class; } /* * launch UI, explicitly stating that this is not due to user action so * that the current app's notification management is not disturbed */ // ÐÞ¸Ä // Intent alarmAlert = new Intent(context, c); Intent test = new Intent(context, CalcScreen.class); test.putExtra(Alarms.ALARM_RAW_DATA, data); test.putExtra(Alarms.ALARM_ID, alarm.id); // test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // alarmAlert.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm); // alarmAlert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK // | Intent.FLAG_ACTIVITY_NO_USER_ACTION); // context.startActivity(alarmAlert); test.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm); test.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION); context.startActivity(test); // Disable the snooze alert if this alarm is the snooze. Alarms.disableSnoozeAlert(context, alarm.id); // Disable this alarm if it does not repeat. if (!alarm.daysOfWeek.isRepeatSet()) { // ÐÞ¸Ä Alarms.enableAlarm(context, alarm.id, false); } else { // Enable the next alert if there is one. The above call to // enableAlarm will call setNextAlert so avoid calling it twice. Alarms.setNextAlert(context); } // Play the alarm alert and vibrate the device. Intent playAlarm = new Intent(Alarms.ALARM_ALERT_ACTION); playAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm); context.startService(playAlarm); // Trigger a notification that, when clicked, will show the alarm alert // dialog. No need to check for fullscreen since this will always be // launched from a user action. Intent notify = new Intent(context, AlarmAlert.class); notify.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm); PendingIntent pendingNotify = PendingIntent.getActivity(context, alarm.id, notify, 0); // Use the alarm's label or the default label as the ticker text and // main text of the notification. String label = alarm.getLabelOrDefault(context); Notification n = new Notification(R.drawable.stat_notify_alarm, label, alarm.time); n.setLatestEventInfo( context, label, context.getString(R.string.alarm_notify_text), pendingNotify); n.flags |= Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONGOING_EVENT; n.defaults |= Notification.DEFAULT_LIGHTS; // Send the notification using the alarm id to easily identify the // correct notification. NotificationManager nm = getNotificationManager(context); nm.notify(alarm.id, n); }
private void setTitle() { Log.d(TAG, "=====>setTitle"); String label = mAlarm.getLabelOrDefault(this); TextView title = (TextView) findViewById(R.id.alertTitle); title.setText(label); }