@Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { // Inflate the menu from xml. getMenuInflater().inflate(R.menu.context_menu, menu); // Use the current item to create a custom view for the header. final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; final Cursor c = (Cursor) mAlarmsList.getAdapter().getItem(info.position); final Alarm alarm = new Alarm(c); // Construct the Calendar to compute the time. final Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, alarm.hour); cal.set(Calendar.MINUTE, alarm.minutes); final String time = Alarms.formatTime(this, cal); // Inflate the custom view and set each TextView's text. final View v = mFactory.inflate(R.layout.context_menu_header, null); TextView textView = (TextView) v.findViewById(R.id.header_time); textView.setText(time); textView = (TextView) v.findViewById(R.id.header_label); textView.setText(alarm.label); // Set the custom view on the menu. menu.setHeaderView(v); // Change the text based on the state of the alarm. if (alarm.enabled) { menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm); } }
@Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); mFactory = LayoutInflater.from(this); mPrefs = getSharedPreferences(PREFERENCES, 0); mCursor = Alarms.getAlarmsCursor(getContentResolver()); updateLayout(); }
@Override public boolean onContextItemSelected(final MenuItem item) { final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); final int id = (int) info.id; // Error check just in case. if (id == -1) { return super.onContextItemSelected(item); } switch (item.getItemId()) { case R.id.delete_alarm: { // Confirm that the alarm will be deleted. new AlertDialog.Builder(this) .setTitle(getString(R.string.delete_alarm)) .setMessage(getString(R.string.delete_alarm_confirm)) .setPositiveButton( android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface d, int w) { Alarms.deleteAlarm(AlarmClock.this, id); } }) .setNegativeButton(android.R.string.cancel, null) .show(); return true; } case R.id.enable_alarm: { final Cursor c = (Cursor) mAlarmsList.getAdapter().getItem(info.position); final Alarm alarm = new Alarm(c); Alarms.enableAlarm(this, alarm.id, !alarm.enabled); if (!alarm.enabled) { SetAlarm.popAlarmSetToast( this, alarm.hour, alarm.minutes, alarm.daysOfWeek, alarm.weekType); } return true; } case R.id.edit_alarm: { final Cursor c = (Cursor) mAlarmsList.getAdapter().getItem(info.position); final Alarm alarm = new Alarm(c); Intent intent = new Intent(this, SetAlarm.class); intent.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm); startActivity(intent); return true; } default: break; } return super.onContextItemSelected(item); }
/** * Get the nearest alarm from preference file. * * @param context * @return the nearest alarm object, if not set, return null. */ public static Alarm getNearestAlarm(final Context context) { SharedPreferences prefs = context.getSharedPreferences(NEAREST_ALARM_PREFERENCES, 0); int alarmId = prefs.getInt(PREF_NEAREST_ALARM_ID, -1); if (alarmId == -1) { return null; } ContentResolver cr = context.getContentResolver(); return Alarms.getAlarm(cr, alarmId); }
/** @} */ 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")); }
private long saveAlarm() { Alarm alarm = new Alarm(); alarm.id = mId; alarm.enabled = mEnabledPref.isChecked(); alarm.hour = mHour; alarm.minutes = mMinutes; alarm.daysOfWeek = mRepeatPref.getDaysOfWeek(); alarm.vibrate = mVibratePref.isChecked(); alarm.label = mLabel.getText(); alarm.alert = mAlarmPref.getAlert(); long time; if (alarm.id == -1) { time = Alarms.addAlarm(this, alarm); // addAlarm populates the alarm with the new id. Update mId so that // changes to other preferences update the new alarm. mId = alarm.id; } else { time = Alarms.setAlarm(this, alarm); } return time; }
/** * M: get the next will play alarm, whose ringtone is from external storage * * @param cr * @return @{ */ private static String getNearestAlarmWithExternalRingtone(Context ctx) { Cursor c = null; String alert = null; try { Alarm nextAlarm = Alarms.getNearestAlarm(ctx); if (nextAlarm != null && nextAlarm.alert != null && nextAlarm.alert.toString().contains("external")) { alert = nextAlarm.alert.toString(); } } catch (Exception e) { e.printStackTrace(); } return alert; }
private void updateAlarm(boolean enabled, Alarm alarm) { Alarms.enableAlarm(this, alarm.id, enabled); if (enabled) { SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes, alarm.daysOfWeek, alarm.weekType); } }
private void setDateFormat() { mFormat = Alarms.get24HourMode(getContext()) ? Alarms.M24 : M12; mAmPm.setShowAmPm(mFormat == M12); }
/** * Set an alarm. Requires an Alarms.ALARM_ID to be passed in as an extra. FIXME: Pass an Alarm * object like every other Activity. */ @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); // Override the default content view. setContentView(R.layout.set_alarm); addPreferencesFromResource(R.xml.alarm_prefs); // Get each preference so we can retrieve the value later. mLabel = (EditTextPreference) findPreference("label"); mLabel.setOnPreferenceChangeListener( new Preference.OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference p, Object newValue) { String val = (String) newValue; // Set the summary based on the new label. p.setSummary(val); if (val != null && !val.equals(mLabel.getText())) { // Call through to the generic listener. return SetAlarm.this.onPreferenceChange(p, newValue); } return true; } }); mEnabledPref = (CheckBoxPreference) findPreference("enabled"); mEnabledPref.setOnPreferenceChangeListener( new Preference.OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference p, Object newValue) { // Pop a toast when enabling alarms. if (!mEnabledPref.isChecked()) { popAlarmSetToast(SetAlarm.this, mHour, mMinutes, mRepeatPref.getDaysOfWeek()); } return SetAlarm.this.onPreferenceChange(p, newValue); } }); mTimePref = findPreference("time"); mAlarmPref = (AlarmPreference) findPreference("alarm"); mAlarmPref.setOnPreferenceChangeListener(this); mVibratePref = (CheckBoxPreference) findPreference("vibrate"); mVibratePref.setOnPreferenceChangeListener(this); mRepeatPref = (RepeatPreference) findPreference("setRepeat"); mRepeatPref.setOnPreferenceChangeListener(this); Intent i = getIntent(); mId = i.getIntExtra(Alarms.ALARM_ID, -1); if (Log.LOGV) { Log.v("In SetAlarm, alarm id = " + mId); } Alarm alarm = null; if (mId == -1) { // No alarm id means create a new alarm. alarm = new Alarm(); } else { /* load alarm details from database */ alarm = Alarms.getAlarm(getContentResolver(), mId); // Bad alarm, bail to avoid a NPE. if (alarm == null) { finish(); return; } } mOriginalAlarm = alarm; updatePrefs(mOriginalAlarm); // We have to do this to get the save/cancel buttons to highlight on // their own. getListView().setItemsCanFocus(true); // Attach actions to each button. Button b = (Button) findViewById(R.id.alarm_save); b.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { saveAlarm(); finish(); } }); final Button revert = (Button) findViewById(R.id.alarm_revert); revert.setEnabled(false); revert.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { int newId = mId; updatePrefs(mOriginalAlarm); // "Revert" on a newly created alarm should delete it. if (mOriginalAlarm.id == -1) { Alarms.deleteAlarm(SetAlarm.this, newId); } else { saveAlarm(); } revert.setEnabled(false); } }); b = (Button) findViewById(R.id.alarm_delete); if (mId == -1) { b.setEnabled(false); } else { b.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { deleteAlarm(); } }); } // The last thing we do is pop the time picker if this is a new alarm. if (mId == -1) { // Assume the user hit cancel mTimePickerCancelled = true; showTimePicker(); } }
/** * Display a toast that tells the user how long until the alarm goes off. This helps prevent * "am/pm" mistakes. */ static void popAlarmSetToast(Context context, int hour, int minute, Alarm.DaysOfWeek daysOfWeek) { popAlarmSetToast(context, Alarms.calculateAlarm(hour, minute, daysOfWeek).getTimeInMillis()); }
private void updateTime() { if (Log.LOGV) { Log.v("updateTime " + mId); } mTimePref.setSummary(Alarms.formatTime(this, mHour, mMinutes, mRepeatPref.getDaysOfWeek())); }
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { return Alarms.getAlarmsCursorLoader(this); }