AlarmInstance.java revision 6a9e04e0443ee22d4e1745a1f118e7b23c37e138
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.content.Context; 23import android.content.Intent; 24import android.database.Cursor; 25import android.media.RingtoneManager; 26import android.net.Uri; 27import android.preference.PreferenceManager; 28 29import com.android.deskclock.Log; 30import com.android.deskclock.R; 31import com.android.deskclock.SettingsActivity; 32 33import java.util.Calendar; 34import java.util.LinkedList; 35import java.util.List; 36 37public final class AlarmInstance implements ClockContract.InstancesColumns { 38 /** 39 * Offset from alarm time to show low priority notification 40 */ 41 public static final int LOW_NOTIFICATION_HOUR_OFFSET = -2; 42 43 /** 44 * Offset from alarm time to show high priority notification 45 */ 46 public static final int HIGH_NOTIFICATION_MINUTE_OFFSET = -30; 47 48 /** 49 * Offset from alarm time to stop showing missed notification. 50 */ 51 private static final int MISSED_TIME_TO_LIVE_HOUR_OFFSET = 12; 52 53 /** 54 * Default timeout for alarms in minutes. 55 */ 56 private static final String DEFAULT_ALARM_TIMEOUT_SETTING = "10"; 57 58 /** 59 * AlarmInstances start with an invalid id when it hasn't been saved to the database. 60 */ 61 public static final long INVALID_ID = -1; 62 63 private static final String[] QUERY_COLUMNS = { 64 _ID, 65 YEAR, 66 MONTH, 67 DAY, 68 HOUR, 69 MINUTES, 70 LABEL, 71 VIBRATE, 72 RINGTONE, 73 ALARM_ID, 74 ALARM_STATE 75 }; 76 77 /** 78 * These save calls to cursor.getColumnIndexOrThrow() 79 * THEY MUST BE KEPT IN SYNC WITH ABOVE QUERY COLUMNS 80 */ 81 private static final int ID_INDEX = 0; 82 private static final int YEAR_INDEX = 1; 83 private static final int MONTH_INDEX = 2; 84 private static final int DAY_INDEX = 3; 85 private static final int HOUR_INDEX = 4; 86 private static final int MINUTES_INDEX = 5; 87 private static final int LABEL_INDEX = 6; 88 private static final int VIBRATE_INDEX = 7; 89 private static final int RINGTONE_INDEX = 8; 90 private static final int ALARM_ID_INDEX = 9; 91 private static final int ALARM_STATE_INDEX = 10; 92 93 private static final int COLUMN_COUNT = ALARM_STATE_INDEX + 1; 94 private Calendar mTimeout; 95 96 public static ContentValues createContentValues(AlarmInstance instance) { 97 ContentValues values = new ContentValues(COLUMN_COUNT); 98 if (instance.mId != INVALID_ID) { 99 values.put(_ID, instance.mId); 100 } 101 102 values.put(YEAR, instance.mYear); 103 values.put(MONTH, instance.mMonth); 104 values.put(DAY, instance.mDay); 105 values.put(HOUR, instance.mHour); 106 values.put(MINUTES, instance.mMinute); 107 values.put(LABEL, instance.mLabel); 108 values.put(VIBRATE, instance.mVibrate ? 1 : 0); 109 if (instance.mRingtone == null) { 110 // We want to put null in the database, so we'll be able 111 // to pick up on changes to the default alarm 112 values.putNull(RINGTONE); 113 } else { 114 values.put(RINGTONE, instance.mRingtone.toString()); 115 } 116 values.put(ALARM_ID, instance.mAlarmId); 117 values.put(ALARM_STATE, instance.mAlarmState); 118 return values; 119 } 120 121 public static Intent createIntent(String action, long instanceId) { 122 return new Intent(action).setData(getUri(instanceId)); 123 } 124 125 public static Intent createIntent(Context context, Class<?> cls, long instanceId) { 126 return new Intent(context, cls).setData(getUri(instanceId)); 127 } 128 129 public static long getId(Uri contentUri) { 130 return ContentUris.parseId(contentUri); 131 } 132 133 public static Uri getUri(long instanceId) { 134 return ContentUris.withAppendedId(CONTENT_URI, instanceId); 135 } 136 137 /** 138 * Get alarm instance from instanceId. 139 * 140 * @param contentResolver to perform the query on. 141 * @param instanceId for the desired instance. 142 * @return instance if found, null otherwise 143 */ 144 public static AlarmInstance getInstance(ContentResolver contentResolver, long instanceId) { 145 Cursor cursor = contentResolver.query(getUri(instanceId), QUERY_COLUMNS, null, null, null); 146 AlarmInstance result = null; 147 if (cursor == null) { 148 return result; 149 } 150 151 try { 152 if (cursor.moveToFirst()) { 153 result = new AlarmInstance(cursor); 154 } 155 } finally { 156 cursor.close(); 157 } 158 159 return result; 160 } 161 162 /** 163 * Get an alarm instances by alarmId. 164 * 165 * @param contentResolver to perform the query on. 166 * @param alarmId of instances desired. 167 * @return list of alarms instances that are owned by alarmId. 168 */ 169 public static List<AlarmInstance> getInstancesByAlarmId(ContentResolver contentResolver, 170 long alarmId) { 171 return getInstances(contentResolver, ALARM_ID + "=" + alarmId); 172 } 173 174 /** 175 * Get a list of instances given selection. 176 * 177 * @param contentResolver to perform the query on. 178 * @param selection A filter declaring which rows to return, formatted as an 179 * SQL WHERE clause (excluding the WHERE itself). Passing null will 180 * return all rows for the given URI. 181 * @param selectionArgs You may include ?s in selection, which will be 182 * replaced by the values from selectionArgs, in the order that they 183 * appear in the selection. The values will be bound as Strings. 184 * @return list of alarms matching where clause or empty list if none found. 185 */ 186 public static List<AlarmInstance> getInstances(ContentResolver contentResolver, 187 String selection, String ... selectionArgs) { 188 Cursor cursor = contentResolver.query(CONTENT_URI, QUERY_COLUMNS, 189 selection, selectionArgs, null); 190 List<AlarmInstance> result = new LinkedList<AlarmInstance>(); 191 if (cursor == null) { 192 return result; 193 } 194 195 try { 196 if (cursor.moveToFirst()) { 197 do { 198 result.add(new AlarmInstance(cursor)); 199 } while (cursor.moveToNext()); 200 } 201 } finally { 202 cursor.close(); 203 } 204 205 return result; 206 } 207 208 public static AlarmInstance addInstance(ContentResolver contentResolver, 209 AlarmInstance instance) { 210 // Make sure we are not adding a duplicate instances. This is not a 211 // fix and should never happen. This is only a safe guard against bad code, and you 212 // should fix the root issue if you see the error message. 213 String dupSelector = AlarmInstance.ALARM_ID + " = " + instance.mAlarmId; 214 for (AlarmInstance otherInstances : getInstances(contentResolver, dupSelector)) { 215 if (otherInstances.getAlarmTime().equals(instance.getAlarmTime())) { 216 Log.e("Trying to install a duplicate alarm instance, updating old one instead"); 217 // Copy over the new instance values and update the db 218 instance.mId = otherInstances.mId; 219 updateInstance(contentResolver, instance); 220 return instance; 221 } 222 } 223 224 ContentValues values = createContentValues(instance); 225 Uri uri = contentResolver.insert(CONTENT_URI, values); 226 instance.mId = getId(uri); 227 return instance; 228 } 229 230 public static boolean updateInstance(ContentResolver contentResolver, AlarmInstance instance) { 231 if (instance.mId == INVALID_ID) return false; 232 ContentValues values = createContentValues(instance); 233 long rowsUpdated = contentResolver.update(getUri(instance.mId), values, null, null); 234 return rowsUpdated == 1; 235 } 236 237 public static boolean deleteInstance(ContentResolver contentResolver, long instanceId) { 238 if (instanceId == INVALID_ID) return false; 239 int deletedRows = contentResolver.delete(getUri(instanceId), "", null); 240 return deletedRows == 1; 241 } 242 243 // Public fields 244 public long mId; 245 public int mYear; 246 public int mMonth; 247 public int mDay; 248 public int mHour; 249 public int mMinute; 250 public String mLabel; 251 public boolean mVibrate; 252 public Uri mRingtone; 253 public Long mAlarmId; 254 public int mAlarmState; 255 256 public AlarmInstance(Calendar calendar, Long alarmId) { 257 this(calendar); 258 mAlarmId = alarmId; 259 } 260 261 public AlarmInstance(Calendar calendar) { 262 mId = INVALID_ID; 263 setAlarmTime(calendar); 264 mLabel = ""; 265 mVibrate = false; 266 mRingtone = null; 267 mAlarmState = SILENT_STATE; 268 } 269 270 public AlarmInstance(Cursor c) { 271 mId = c.getLong(ID_INDEX); 272 mYear = c.getInt(YEAR_INDEX); 273 mMonth = c.getInt(MONTH_INDEX); 274 mDay = c.getInt(DAY_INDEX); 275 mHour = c.getInt(HOUR_INDEX); 276 mMinute = c.getInt(MINUTES_INDEX); 277 mLabel = c.getString(LABEL_INDEX); 278 mVibrate = c.getInt(VIBRATE_INDEX) == 1; 279 if (c.isNull(RINGTONE_INDEX)) { 280 // Should we be saving this with the current ringtone or leave it null 281 // so it changes when user changes default ringtone? 282 mRingtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 283 } else { 284 mRingtone = Uri.parse(c.getString(RINGTONE_INDEX)); 285 } 286 287 if (!c.isNull(ALARM_ID_INDEX)) { 288 mAlarmId = c.getLong(ALARM_ID_INDEX); 289 } 290 mAlarmState = c.getInt(ALARM_STATE_INDEX); 291 } 292 293 public String getLabelOrDefault(Context context) { 294 return mLabel.isEmpty() ? context.getString(R.string.default_label) : mLabel; 295 } 296 297 public void setAlarmTime(Calendar calendar) { 298 mYear = calendar.get(Calendar.YEAR); 299 mMonth = calendar.get(Calendar.MONTH); 300 mDay = calendar.get(Calendar.DAY_OF_MONTH); 301 mHour = calendar.get(Calendar.HOUR_OF_DAY); 302 mMinute = calendar.get(Calendar.MINUTE); 303 } 304 305 /** 306 * Return the time when a alarm should fire. 307 * 308 * @return the time 309 */ 310 public Calendar getAlarmTime() { 311 Calendar calendar = Calendar.getInstance(); 312 calendar.set(Calendar.YEAR, mYear); 313 calendar.set(Calendar.DAY_OF_MONTH, mDay); 314 calendar.set(Calendar.HOUR_OF_DAY, mHour); 315 calendar.set(Calendar.MINUTE, mMinute); 316 calendar.set(Calendar.SECOND, 0); 317 calendar.set(Calendar.MILLISECOND, 0); 318 return calendar; 319 } 320 321 /** 322 * Return the time when a low priority notification should be shown. 323 * 324 * @return the time 325 */ 326 public Calendar getLowNotificationTime() { 327 Calendar calendar = getAlarmTime(); 328 calendar.add(Calendar.HOUR_OF_DAY, LOW_NOTIFICATION_HOUR_OFFSET); 329 return calendar; 330 } 331 332 /** 333 * Return the time when a high priority notification should be shown. 334 * 335 * @return the time 336 */ 337 public Calendar getHighNotificationTime() { 338 Calendar calendar = getAlarmTime(); 339 calendar.add(Calendar.MINUTE, HIGH_NOTIFICATION_MINUTE_OFFSET); 340 return calendar; 341 } 342 343 /** 344 * Return the time when a missed notification should be removed. 345 * 346 * @return the time 347 */ 348 public Calendar getMissedTimeToLive() { 349 Calendar calendar = getAlarmTime(); 350 calendar.add(Calendar.HOUR, MISSED_TIME_TO_LIVE_HOUR_OFFSET); 351 return calendar; 352 } 353 354 /** 355 * Return the time when the alarm should stop firing and be marked as missed. 356 * 357 * @param context to figure out the timeout setting 358 * @return the time when alarm should be silence, or null if never 359 */ 360 public Calendar getTimeout(Context context) { 361 String timeoutSetting = PreferenceManager.getDefaultSharedPreferences(context) 362 .getString(SettingsActivity.KEY_AUTO_SILENCE, DEFAULT_ALARM_TIMEOUT_SETTING); 363 int timeoutMinutes = Integer.parseInt(timeoutSetting); 364 365 // Alarm silence has been set to "None" 366 if (timeoutMinutes < 0) { 367 return null; 368 } 369 370 Calendar calendar = getAlarmTime(); 371 calendar.add(Calendar.MINUTE, timeoutMinutes); 372 return calendar; 373 } 374 375 @Override 376 public boolean equals(Object o) { 377 if (!(o instanceof AlarmInstance)) return false; 378 final AlarmInstance other = (AlarmInstance) o; 379 return mId == other.mId; 380 } 381 382 @Override 383 public int hashCode() { 384 return Long.valueOf(mId).hashCode(); 385 } 386 387 @Override 388 public String toString() { 389 return "AlarmInstance{" + 390 "mId=" + mId + 391 ", mYear=" + mYear + 392 ", mMonth=" + mMonth + 393 ", mDay=" + mDay + 394 ", mHour=" + mHour + 395 ", mMinute=" + mMinute + 396 ", mLabel=" + mLabel + 397 ", mVibrate=" + mVibrate + 398 ", mRingtone=" + mRingtone + 399 ", mAlarmId=" + mAlarmId + 400 ", mAlarmState=" + mAlarmState + 401 '}'; 402 } 403} 404