📄 alarms.java
字号:
/* * 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.Notification;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.provider.BaseColumns;import android.provider.Settings;import android.text.format.DateFormat;import java.util.Calendar;/** * The Alarms provider supplies info about Alarm Clock settings */public class Alarms { public final static String ALARM_ALERT_ACTION = "com.android.alarmclock.ALARM_ALERT"; public final static String ID = "alarm_id"; public final static String TIME = "alarm_time"; 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 M24 = "k:mm"; static class DaysOfWeek { int mDays; /** * Days of week coded as single int, convenient for DB * storage: * * 0x00: no day * 0x01: Monday * 0x02: Tuesday * 0x04: Wednesday * 0x08: Thursday * 0x10: Friday * 0x20: Saturday * 0x40: Sunday */ DaysOfWeek() { this(0); } DaysOfWeek(int days) { mDays = days; } public String toString(Context context, boolean showNever) { StringBuilder ret = new StringBuilder(); /* no days */ if (mDays == 0) return showNever ? context.getText( R.string.never).toString() : ""; /* every day */ if (mDays == 0x7f) { return context.getText(R.string.every_day).toString(); } /* count selected days */ int dayCount = 0, days = mDays; while (days > 0) { if ((days & 1) == 1) dayCount++; days >>= 1; } /* short or long form? */ CharSequence[] strings = context.getResources().getTextArray( (dayCount > 1) ? R.array.days_of_week_short : R.array.days_of_week); /* selected days */ for (int i = 0; i < 7; i++) { if ((mDays & (1 << i)) != 0) { ret.append(strings[i]); dayCount -= 1; if (dayCount > 0) ret.append( context.getText(R.string.day_concat)); } } return ret.toString(); } /** * @param day Mon=0 ... Sun=6 * @return true if given day is set */ public boolean isSet(int day) { return ((mDays & (1 << day)) > 0); } public void set(int day, boolean set) { if (set) { mDays |= (1 << day); } else { mDays &= ~(1 << day); } } public void set(DaysOfWeek dow) { mDays = dow.mDays; } public int getCoded() { return mDays; } public boolean equals(DaysOfWeek dow) { return mDays == dow.mDays; } // Returns days of week encoded in an array of booleans. public boolean[] getBooleanArray() { boolean[] ret = new boolean[7]; for (int i = 0; i < 7; i++) { ret[i] = isSet(i); } return ret; } public void setCoded(int days) { mDays = days; } /** * @return true if alarm is set to repeat */ public boolean isRepeatSet() { return mDays != 0; } /** * @return true if alarm is set to repeat every day */ public boolean isEveryDaySet() { return mDays == 0x7f; } /** * returns number of days from today until next alarm * @param c must be set to today */ public int getNextAlarm(Calendar c) { if (mDays == 0) return -1; int today = (c.get(Calendar.DAY_OF_WEEK) + 5) % 7; int day, dayCount; for (dayCount = 0; dayCount < 7; dayCount++) { day = (today + dayCount) % 7; if ((mDays & (1 << day)) > 0) { break; } } return dayCount; } } public static class AlarmColumns implements BaseColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://com.android.alarmclock/alarm"); public static final String _ID = "_id"; /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "_id ASC"; /** * Hour in 24-hour localtime 0 - 23. * <P>Type: INTEGER</P> */ public static final String HOUR = "hour"; /** * Minutes in localtime 0 - 59 * <P>Type: INTEGER</P> */ public static final String MINUTES = "minutes"; /** * Days of week coded as integer * <P>Type: INTEGER</P> */ public static final String DAYS_OF_WEEK = "daysofweek"; /** * Alarm time in UTC milliseconds from the epoch. * <P>Type: INTEGER</P> */ public static final String ALARM_TIME = "alarmtime"; /** * True if alarm is active * <P>Type: BOOLEAN</P> */ public static final String ENABLED = "enabled"; /** * True if alarm should vibrate * <P>Type: BOOLEAN</P> */ public static final String VIBRATE = "vibrate"; /** * Message to show when alarm triggers * Note: not currently used * <P>Type: STRING</P> */ public static final String MESSAGE = "message"; /** * Audio alert to play when alarm triggers * <P>Type: STRING</P> */ public static final String ALERT = "alert"; static final String[] ALARM_QUERY_COLUMNS = { _ID, HOUR, MINUTES, DAYS_OF_WEEK, ALARM_TIME, ENABLED, VIBRATE, MESSAGE, ALERT}; /** * These save calls to cursor.getColumnIndexOrThrow() * THEY MUST BE KEPT IN SYNC WITH ABOVE QUERY COLUMNS */ public static final int ALARM_ID_INDEX = 0; public static final int ALARM_HOUR_INDEX = 1; public static final int ALARM_MINUTES_INDEX = 2; public static final int ALARM_DAYS_OF_WEEK_INDEX = 3; public static final int ALARM_TIME_INDEX = 4; public static final int ALARM_ENABLED_INDEX = 5; public static final int ALARM_VIBRATE_INDEX = 6; public static final int ALARM_MESSAGE_INDEX = 7; public static final int ALARM_ALERT_INDEX = 8; } /** * getAlarm and getAlarms call this interface to report alarms in * the database */ static interface AlarmSettings { void reportAlarm( int idx, boolean enabled, int hour, int minutes, DaysOfWeek daysOfWeek, boolean vibrate, String message, String alert); } /** * Creates a new Alarm. */ public synchronized static Uri addAlarm(ContentResolver contentResolver) { ContentValues values = new ContentValues(); values.put(Alarms.AlarmColumns.HOUR, 8); return contentResolver.insert(AlarmColumns.CONTENT_URI, values); } /** * Removes an existing Alarm. If this alarm is snoozing, disables * snooze. Sets next alert. */ public synchronized static void deleteAlarm( Context context, int alarmId) { ContentResolver contentResolver = context.getContentResolver(); /* If alarm is snoozing, lose it */ int snoozeId = getSnoozeAlarmId(context); if (snoozeId == alarmId) disableSnoozeAlert(context); Uri uri = ContentUris.withAppendedId(AlarmColumns.CONTENT_URI, alarmId); deleteAlarm(contentResolver, uri); setNextAlert(context); } private synchronized static void deleteAlarm( ContentResolver contentResolver, Uri uri) { contentResolver.delete(uri, "", null); } /** * Queries all alarms * @return cursor over all alarms */ public synchronized static Cursor getAlarmsCursor( ContentResolver contentResolver) { return contentResolver.query( AlarmColumns.CONTENT_URI, AlarmColumns.ALARM_QUERY_COLUMNS, null, null, AlarmColumns.DEFAULT_SORT_ORDER); } /** * Calls the AlarmSettings.reportAlarm interface on all alarms found in db. */ public synchronized static void getAlarms( ContentResolver contentResolver, AlarmSettings alarmSettings) { Cursor cursor = getAlarmsCursor(contentResolver); getAlarms(alarmSettings, cursor); cursor.close(); } private synchronized static void getAlarms( AlarmSettings alarmSettings, Cursor cur) { if (cur.moveToFirst()) { do { // Get the field values int id = cur.getInt(AlarmColumns.ALARM_ID_INDEX); int hour = cur.getInt(AlarmColumns.ALARM_HOUR_INDEX); int minutes = cur.getInt(AlarmColumns.ALARM_MINUTES_INDEX); int daysOfWeek = cur.getInt(AlarmColumns.ALARM_DAYS_OF_WEEK_INDEX); boolean enabled = cur.getInt(AlarmColumns.ALARM_ENABLED_INDEX) == 1 ? true : false; boolean vibrate = cur.getInt(AlarmColumns.ALARM_VIBRATE_INDEX) == 1 ? true : false; String message = cur.getString(AlarmColumns.ALARM_MESSAGE_INDEX); String alert = cur.getString(AlarmColumns.ALARM_ALERT_INDEX); alarmSettings.reportAlarm( id, enabled, hour, minutes, new DaysOfWeek(daysOfWeek), vibrate, message, alert); } while (cur.moveToNext()); } } /** * Calls the AlarmSettings.reportAlarm interface on alarm with given * alarmId */ public synchronized static void getAlarm( ContentResolver contentResolver, AlarmSettings alarmSetting, int alarmId) { Cursor cursor = contentResolver.query( ContentUris.withAppendedId(AlarmColumns.CONTENT_URI, alarmId), AlarmColumns.ALARM_QUERY_COLUMNS, null, null, AlarmColumns.DEFAULT_SORT_ORDER); getAlarms(alarmSetting, cursor); cursor.close(); } /** * A convenience method to set an alarm in the Alarms * content provider. * * @param id corresponds to the _id column
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -