/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.alarmclock;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.provider.Settings;
import android.text.format.DateFormat;
import java.util.Calendar;
import java.text.DateFormatSymbols;
import java.util.Date;
/**
* The Alarms provider supplies info about Alarm Clock settings
*/
public class Alarms {
// This action triggers the AlarmReceiver as well as the AlarmKlaxon. It
// is a public action used in the manifest for receiving Alarm broadcasts
// from the alarm manager.
public static final String ALARM_ALERT_ACTION = "com.android.alarmclock.ALARM_ALERT";
// A public action sent by AlarmKlaxon when the alarm has stopped sounding
// for any reason (e.g. because it has been dismissed from AlarmAlertFullScreen,
// or killed due to an incoming phone call, etc).
public static final String ALARM_DONE_ACTION = "com.android.alarmclock.ALARM_DONE";
// This is a private action used by the AlarmKlaxon to update the UI to
// show the alarm has been killed.
public static final String ALARM_KILLED = "alarm_killed";
// Extra in the ALARM_KILLED intent to indicate to the user how long the
// alarm played before being killed.
public static final String ALARM_KILLED_TIMEOUT = "alarm_killed_timeout";
// This string is used to indicate a silent alarm in the db.
public static final String ALARM_ALERT_SILENT = "silent";
// This intent is sent from the notification when the user cancels the
// snooze alert.
public static final String CANCEL_SNOOZE = "cancel_snooze";
// This string is used when passing an Alarm object through an intent.
public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";
// This extra is the raw Alarm object data. It is used in the
// AlarmManagerService to avoid a ClassNotFoundException when filling in
// the Intent extras.
public static final String ALARM_RAW_DATA = "intent.extra.alarm_raw";
// This string is used to identify the alarm id passed to SetAlarm from the
// list of alarms.
public static final String ALARM_ID = "alarm_id";
final static String PREF_SNOOZE_ID = "snooze_id";
final static String PREF_SNOOZE_TIME = "snooze_time";
private final static String DM12 = "E h:mm aa";
private final static String DM24 = "E k:mm";
//private final static String M12 = "h:mm aa";
private final static String M12 = "aa h:mm";
// Shared with DigitalClock
final static String M24 = "kk:mm";
/**
* Creates a new Alarm.
*/
public static Uri addAlarm(ContentResolver contentResolver) {
ContentValues values = new ContentValues();
//T603T_P000436 xushaofei 110613
Date curDate = new Date(System.currentTimeMillis());
values.put(Alarm.Columns.HOUR,curDate.getHours());
values.put(Alarm.Columns.MINUTES, curDate.getMinutes());
return contentResolver.insert(Alarm.Columns.CONTENT_URI, values);
}
/**
* Removes an existing Alarm. If this alarm is snoozing, disables
* snooze. Sets next alert.
*/
public static void deleteAlarm(
Context context, int alarmId) {
ContentResolver contentResolver = context.getContentResolver();
/* If alarm is snoozing, lose it */
disableSnoozeAlert(context, alarmId);
Uri uri = ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, alarmId);
contentResolver.delete(uri, "", null);
setNextAlert(context);
}
/**
* Queries all alarms
* @return cursor over all alarms
*/
public static Cursor getAlarmsCursor(ContentResolver contentResolver) {
return contentResolver.query(
Alarm.Columns.CONTENT_URI, Alarm.Columns.ALARM_QUERY_COLUMNS,
null, null, Alarm.Columns.DEFAULT_SORT_ORDER);
}
// Private method to get a more limited set of alarms from the database.
private static Cursor getFilteredAlarmsCursor(
ContentResolver contentResolver) {
return contentResolver.query(Alarm.Columns.CONTENT_URI,
Alarm.Columns.ALARM_QUERY_COLUMNS, Alarm.Columns.WHERE_ENABLED,
null, null);
}
/**
* Return an Alarm object representing the alarm id in the database.
* Returns null if no alarm exists.
*/
public static Alarm getAlarm(ContentResolver contentResolver, int alarmId) {
Cursor cursor = contentResolver.query(
ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, alarmId),
Alarm.Columns.ALARM_QUERY_COLUMNS,
null, null, null);
Alarm alarm = null;
if (cursor != null) {
if (cursor.moveToFirst()) {
alarm = new Alarm(cursor);
}
cursor.close();
}
return alarm;
}
/**
* A convenience method to set an alarm in the Alarms
* content provider.
*
* @param id corresponds to the _id column
* @param enabled corresponds to the ENABLED column
* @param hour corresponds to the HOUR column
* @param minutes corresponds to the MINUTES column
* @param daysOfWeek corresponds to the DAYS_OF_WEEK column
* @param time corresponds to the ALARM_TIME column
* @param vibrate corresponds to the VIBRATE column
* @param message corresponds to the MESSAGE column
* @param alert corresponds to the ALERT column
* @return Time when the alarm will fire.
*/
public static long setAlarm(
Context context, int id, boolean enabled, int hour, int minutes,
Alarm.DaysOfWeek daysOfWeek, boolean vibrate, String message,
String alert) {
ContentValues values = new ContentValues(8);
ContentResolver resolver = context.getContentResolver();
// Set the alarm_time value if this alarm does not repeat. This will be
// used later to disable expired alarms.
long time = 0;
if (!daysOfWeek.isRepeatSet()) {
time = calculateAlarm(hour, minutes, daysOfWeek).getTimeInMillis();
}
if (Log.LOGV) Log.v(
"** setAlarm * idx " + id + " hour " + hour + " minutes " +
minutes + " enabled " + enabled + " time " + time);
values.put(Alarm.Columns.ENABLED, enabled ? 1 : 0);
values.put(Alarm.Columns.HOUR, hour);
values.put(Alarm.Columns.MINUTES, minutes);
values.put(Alarm.Columns.ALARM_TIME, time);
values.put(Alarm.Columns.DAYS_OF_WEEK, daysOfWeek.getCoded());
values.put(Alarm.Columns.VIBRATE, vibrate);
values.put(Alarm.Columns.MESSAGE, message);
values.put(Alarm.Columns.ALERT, alert);
resolver.update(ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, id),
values, null, null);
long timeInMillis =
calculateAlarm(hour, minutes, daysOfWeek).getTimeInMillis();