AlarmInstance.java revision 8188813bc869d3df4885f9c2972f9cc85745b59b
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.deskclock.provider; 18 19import android.content.ContentResolver; 20import android.content.ContentUris; 21import android.content.ContentValues; 22import android.database.Cursor; 23import android.net.Uri; 24 25import java.util.Calendar; 26import java.util.LinkedList; 27import java.util.List; 28 29public final class AlarmInstance implements ClockContract.InstancesColumns { 30 /** 31 * AlarmInstances start with an invalid id when it hasn't been saved to the database. 32 */ 33 public static final long INVALID_ID = -1; 34 35 /** 36 * Alarm state when alarm is ready to be fired. 37 */ 38 public static final int ACTIVE_STATE = 0; 39 40 /** 41 * Alarm state when alarm is being fired. 42 */ 43 public static final int FIRED_STATE = 1; 44 45 /** 46 * Alarm state when alarm is in snooze. 47 */ 48 public static final int SNOOZE_STATE = 2; 49 50 private static final String[] QUERY_COLUMNS = { 51 _ID, 52 YEAR, 53 MONTH, 54 DAY, 55 HOUR, 56 MINUTES, 57 ALARM_ID, 58 ALARM_STATE 59 }; 60 61 /** 62 * These save calls to cursor.getColumnIndexOrThrow() 63 * THEY MUST BE KEPT IN SYNC WITH ABOVE QUERY COLUMNS 64 */ 65 private static final int ID_INDEX = 0; 66 private static final int YEAR_INDEX = 1; 67 private static final int MONTH_INDEX = 2; 68 private static final int DAY_INDEX = 3; 69 private static final int HOUR_INDEX = 4; 70 private static final int MINUTES_INDEX = 5; 71 private static final int ALARM_ID_INDEX = 6; 72 private static final int ALARM_STATE_INDEX = 7; 73 74 private static final int COLUMN_COUNT = ALARM_STATE_INDEX + 1; 75 76 public static ContentValues createContentValues(AlarmInstance instance) { 77 ContentValues values = new ContentValues(COLUMN_COUNT); 78 if (instance.mId != INVALID_ID) { 79 values.put(_ID, instance.mId); 80 } 81 82 values.put(YEAR, instance.mYear); 83 values.put(MONTH, instance.mMonth); 84 values.put(DAY, instance.mDay); 85 values.put(HOUR, instance.mHour); 86 values.put(MINUTES, instance.mMinute); 87 values.put(ALARM_ID, instance.mAlarmId); 88 values.put(ALARM_STATE, instance.mAlarmState); 89 return values; 90 } 91 92 public static long getId(Uri contentUri) { 93 return ContentUris.parseId(contentUri); 94 } 95 96 /** 97 * Get alarm instance from instanceId. 98 * 99 * @param contentResolver to perform the query on. 100 * @param instanceId for the desired instance. 101 * @return instance if found, null otherwise 102 */ 103 public static AlarmInstance getInstance(ContentResolver contentResolver, long instanceId) { 104 Cursor cursor = contentResolver.query(ContentUris.withAppendedId(CONTENT_URI, instanceId), 105 QUERY_COLUMNS, null, null, null); 106 AlarmInstance result = null; 107 if (cursor == null) { 108 return result; 109 } 110 111 try { 112 if (cursor.moveToFirst()) { 113 result = new AlarmInstance(cursor); 114 } 115 } finally { 116 cursor.close(); 117 } 118 119 return result; 120 } 121 122 /** 123 * Get an alarm instance by alarmId. 124 * 125 * @param contentResolver to perform the query on. 126 * @param alarmId for the desired instance. 127 * @return instance if found, null otherwise 128 */ 129 public static AlarmInstance getInstanceByAlarmId(ContentResolver contentResolver, long alarmId) { 130 List<AlarmInstance> result = getInstances(contentResolver, ALARM_ID + "=" + alarmId); 131 return !result.isEmpty() ? result.get(0) : null; 132 } 133 134 /** 135 * Get a list of instances given selection. 136 * 137 * @param contentResolver to perform the query on. 138 * @param selection A filter declaring which rows to return, formatted as an 139 * SQL WHERE clause (excluding the WHERE itself). Passing null will 140 * return all rows for the given URI. 141 * @param selectionArgs You may include ?s in selection, which will be 142 * replaced by the values from selectionArgs, in the order that they 143 * appear in the selection. The values will be bound as Strings. 144 * @return list of alarms matching where clause or empty list if none found. 145 */ 146 public static List<AlarmInstance> getInstances(ContentResolver contentResolver, 147 String selection, String ... selectionArgs) { 148 Cursor cursor = contentResolver.query(CONTENT_URI, QUERY_COLUMNS, 149 selection, selectionArgs, null); 150 List<AlarmInstance> result = new LinkedList<AlarmInstance>(); 151 if (cursor == null) { 152 return result; 153 } 154 155 try { 156 if (cursor.moveToFirst()) { 157 do { 158 result.add(new AlarmInstance(cursor)); 159 } while (cursor.moveToNext()); 160 } 161 } finally { 162 cursor.close(); 163 } 164 165 return result; 166 } 167 168 // Public fields 169 public long mId; 170 public int mYear; 171 public int mMonth; 172 public int mDay; 173 public int mHour; 174 public int mMinute; 175 public long mAlarmId; 176 public int mAlarmState; 177 178 // Creates a default alarm at the current time. 179 public AlarmInstance(Alarm alarm) { 180 //TODO: Find better way to get year and day 181 this(Calendar.getInstance(), alarm.hour, alarm.minutes, alarm.id); 182 } 183 184 public AlarmInstance(Calendar calendar, int hour, int minute, long alarmId) { 185 mId = INVALID_ID; 186 mYear = calendar.get(Calendar.YEAR); 187 mMonth = calendar.get(Calendar.MONTH); 188 mDay = calendar.get(Calendar.DAY_OF_MONTH); 189 mHour = hour; 190 mMinute = minute; 191 mAlarmId = alarmId; 192 mAlarmState = 0; 193 } 194 195 public AlarmInstance(Cursor c) { 196 mId = c.getLong(ID_INDEX); 197 mYear = c.getInt(YEAR_INDEX); 198 mMonth = c.getInt(MONTH_INDEX); 199 mDay = c.getInt(DAY_INDEX); 200 mHour = c.getInt(HOUR_INDEX); 201 mMinute = c.getInt(MINUTES_INDEX); 202 mAlarmId = c.getLong(ALARM_ID_INDEX); 203 mAlarmState = c.getInt(ALARM_STATE_INDEX); 204 } 205 206 public long calculateTime() { 207 Calendar calendar = Calendar.getInstance(); 208 calendar.set(Calendar.YEAR, mYear); 209 calendar.set(Calendar.DAY_OF_MONTH, mDay); 210 calendar.set(Calendar.HOUR, mHour); 211 calendar.set(Calendar.MINUTE, mMinute); 212 return calendar.getTimeInMillis(); 213 } 214 215 @Override 216 public boolean equals(Object o) { 217 if (!(o instanceof AlarmInstance)) return false; 218 final AlarmInstance other = (AlarmInstance) o; 219 return mId == other.mId; 220 } 221 222 @Override 223 public int hashCode() { 224 return Long.valueOf(mId).hashCode(); 225 } 226 227 @Override 228 public String toString() { 229 return "AlarmInstance{" + 230 "mId=" + mId + 231 ", mYear=" + mYear + 232 ", mMonth=" + mMonth + 233 ", mDay=" + mDay + 234 ", mHour=" + mHour + 235 ", mMinute=" + mMinute + 236 ", mAlarmId=" + mAlarmId + 237 ", mAlarmState=" + mAlarmState + 238 '}'; 239 } 240} 241