/* Copyright Statement:
*
* This software/firmware and related documentation ("MediaTek Software") are
* protected under relevant copyright laws. The information contained herein
* is confidential and proprietary to MediaTek Inc. and/or its licensors.
* Without the prior written permission of MediaTek inc. and/or its licensors,
* any reproduction, modification, use or disclosure of MediaTek Software,
* and information contained herein, in whole or in part, shall be strictly prohibited.
*/
/* MediaTek Inc. (C) 2010. All rights reserved.
*
* BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
* THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
* CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
* SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
* STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
* CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* The following software/firmware and/or related documentation ("MediaTek Software")
* have been modified by MediaTek Inc. All revisions are subject to any receiver's
* applicable license agreements with MediaTek Inc.
*/
package com.mediatek.schpwronoff;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
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.os.UserHandle;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.util.Log;
import java.util.Calendar;
/**
* The Alarms provider supplies info about Alarm Clock settings
*/
public class Alarms {
private static final String TAG = "Settings/Alarms";
// This string is used to indicate a silent alarm in the db.
public static final String ALARM_ALERT_SILENT = "silent";
// 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";
static final String PREF_SNOOZE_ID = "snooze_id";
static final String PREF_SNOOZE_TIME = "snooze_time";
private static final String M12 = "h:mm aa";
static final String M24 = "kk:mm";
/**
* Queries all alarms
* @param contentResolver ContentResolver
* @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);
}
private static Cursor getFilteredAlarmsCursor(ContentResolver contentResolver, int alarmId) {
Log.d("@M_" + TAG, "User Id = " + UserHandle.myUserId());
return contentResolver.query(
ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, alarmId),
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.
* @param contentResolver ContentResolver
* @param alarmId id
* @return Alarm object
*/
public static Alarm getAlarm(ContentResolver contentResolver, int alarmId) {
Cursor cursor = null;
Alarm alarm = null;
try {
cursor = contentResolver.query(ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI,
alarmId), Alarm.Columns.ALARM_QUERY_COLUMNS, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
alarm = new Alarm(cursor);
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return alarm;
}
/**
* A convenience method to set an alarm in the Alarms content provider.
* @param context Context
* @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 vibrate corresponds to the VIBRATE column
* @param message corresponds to the MESSAGE column
* @param alert corresponds to the ALERT column
*/
public static void setAlarm(Context context, int id, boolean enabled, int hour, int minutes,
Alarm.DaysOfWeek daysOfWeek, boolean vibrate, String message, String alert) {
final int initSize = 8;
ContentValues values = new ContentValues(initSize);
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();
}
Log.d("@M_" + TAG, "** 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);
if (id == 1) {
// power on
setNextAlertPowerOn(context);
} else if (id == 2) {
// power off
setNextAlertPowerOff(context);
}
}
/**
* A convenience method to enable or disable an alarm.
* @param context Context
* @param id corresponds to the _id column
* @param enabled corresponds to the ENABLED column
*/
public static void enableAlarm(final Context context, final int id, boolean enabled) {