AlarmInstance.java revision 47807edd241c17c52b33722356c13fba2bbdaea0
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.LogUtils; 30import com.android.deskclock.R; 31import com.android.deskclock.alarms.AlarmStateManager; 32import com.android.deskclock.settings.SettingsActivity; 33 34import java.util.Calendar; 35import java.util.LinkedList; 36import java.util.List; 37 38public final class AlarmInstance implements ClockContract.InstancesColumns { 39 /** 40 * Offset from alarm time to show low priority notification 41 */ 42 public static final int LOW_NOTIFICATION_HOUR_OFFSET = -2; 43 44 /** 45 * Offset from alarm time to show high priority notification 46 */ 47 public static final int HIGH_NOTIFICATION_MINUTE_OFFSET = -30; 48 49 /** 50 * Offset from alarm time to stop showing missed notification. 51 */ 52 private static final int MISSED_TIME_TO_LIVE_HOUR_OFFSET = 12; 53 54 /** 55 * Default timeout for alarms in minutes. 56 */ 57 private static final String DEFAULT_ALARM_TIMEOUT_SETTING = "10"; 58 59 /** 60 * AlarmInstances start with an invalid id when it hasn't been saved to the database. 61 */ 62 public static final long INVALID_ID = -1; 63 64 private static final String[] QUERY_COLUMNS = { 65 _ID, 66 YEAR, 67 MONTH, 68 DAY, 69 HOUR, 70 MINUTES, 71 LABEL, 72 VIBRATE, 73 RINGTONE, 74 ALARM_ID, 75 ALARM_STATE 76 }; 77 78 /** 79 * These save calls to cursor.getColumnIndexOrThrow() 80 * THEY MUST BE KEPT IN SYNC WITH ABOVE QUERY COLUMNS 81 */ 82 private static final int ID_INDEX = 0; 83 private static final int YEAR_INDEX = 1; 84 private static final int MONTH_INDEX = 2; 85 private static final int DAY_INDEX = 3; 86 private static final int HOUR_INDEX = 4; 87 private static final int MINUTES_INDEX = 5; 88 private static final int LABEL_INDEX = 6; 89 private static final int VIBRATE_INDEX = 7; 90 private static final int RINGTONE_INDEX = 8; 91 private static final int ALARM_ID_INDEX = 9; 92 private static final int ALARM_STATE_INDEX = 10; 93 94 private static final int COLUMN_COUNT = ALARM_STATE_INDEX + 1; 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 cr 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 cr, long instanceId) { 145 try (Cursor cursor = cr.query(getUri(instanceId), QUERY_COLUMNS, null, null, null)) { 146 if (cursor != null && cursor.moveToFirst()) { 147 return new AlarmInstance(cursor, false /* joinedTable */); 148 } 149 } 150 151 return null; 152 } 153 154 /** 155 * Get an alarm instances by alarmId. 156 * 157 * @param contentResolver to perform the query on. 158 * @param alarmId of instances desired. 159 * @return list of alarms instances that are owned by alarmId. 160 */ 161 public static List<AlarmInstance> getInstancesByAlarmId(ContentResolver contentResolver, 162 long alarmId) { 163 return getInstances(contentResolver, ALARM_ID + "=" + alarmId); 164 } 165 166 /** 167 * Get the next instance of an alarm given its alarmId 168 * @param contentResolver to perform query on 169 * @param alarmId of instance desired 170 * @return the next instance of an alarm by alarmId. 171 */ 172 public static AlarmInstance getNextUpcomingInstanceByAlarmId(ContentResolver contentResolver, 173 long alarmId) { 174 final List<AlarmInstance> alarmInstances = getInstancesByAlarmId(contentResolver, alarmId); 175 if (alarmInstances.isEmpty()) { 176 return null; 177 } 178 AlarmInstance nextAlarmInstance = alarmInstances.get(0); 179 for (AlarmInstance instance : alarmInstances) { 180 if (instance.getAlarmTime().before(nextAlarmInstance.getAlarmTime())) { 181 nextAlarmInstance = instance; 182 } 183 } 184 return nextAlarmInstance; 185 } 186 187 /** 188 * Get alarm instance by id and state. 189 */ 190 public static List<AlarmInstance> getInstancesByInstanceIdAndState( 191 ContentResolver contentResolver, long alarmInstanceId, int state) { 192 return getInstances(contentResolver, _ID + "=" + alarmInstanceId + " AND " + ALARM_STATE + 193 "=" + state); 194 } 195 196 /** 197 * Get alarm instances in the specified state. 198 */ 199 public static List<AlarmInstance> getInstancesByState( 200 ContentResolver contentResolver, int state) { 201 return getInstances(contentResolver, ALARM_STATE + "=" + state); 202 } 203 204 /** 205 * Get a list of instances given selection. 206 * 207 * @param cr to perform the query on. 208 * @param selection A filter declaring which rows to return, formatted as an 209 * SQL WHERE clause (excluding the WHERE itself). Passing null will 210 * return all rows for the given URI. 211 * @param selectionArgs You may include ?s in selection, which will be 212 * replaced by the values from selectionArgs, in the order that they 213 * appear in the selection. The values will be bound as Strings. 214 * @return list of alarms matching where clause or empty list if none found. 215 */ 216 public static List<AlarmInstance> getInstances(ContentResolver cr, String selection, 217 String... selectionArgs) { 218 final List<AlarmInstance> result = new LinkedList<>(); 219 try (Cursor cursor = cr.query(CONTENT_URI, QUERY_COLUMNS, selection, selectionArgs, null)) { 220 if (cursor.moveToFirst()) { 221 do { 222 result.add(new AlarmInstance(cursor, false /* joinedTable */)); 223 } while (cursor.moveToNext()); 224 } 225 } 226 227 return result; 228 } 229 230 public static AlarmInstance addInstance(ContentResolver contentResolver, 231 AlarmInstance instance) { 232 // Make sure we are not adding a duplicate instances. This is not a 233 // fix and should never happen. This is only a safe guard against bad code, and you 234 // should fix the root issue if you see the error message. 235 String dupSelector = AlarmInstance.ALARM_ID + " = " + instance.mAlarmId; 236 for (AlarmInstance otherInstances : getInstances(contentResolver, dupSelector)) { 237 if (otherInstances.getAlarmTime().equals(instance.getAlarmTime())) { 238 LogUtils.i("Detected duplicate instance in DB. Updating " + otherInstances + " to " 239 + instance); 240 // Copy over the new instance values and update the db 241 instance.mId = otherInstances.mId; 242 updateInstance(contentResolver, instance); 243 return instance; 244 } 245 } 246 247 ContentValues values = createContentValues(instance); 248 Uri uri = contentResolver.insert(CONTENT_URI, values); 249 instance.mId = getId(uri); 250 return instance; 251 } 252 253 public static boolean updateInstance(ContentResolver contentResolver, AlarmInstance instance) { 254 if (instance.mId == INVALID_ID) return false; 255 ContentValues values = createContentValues(instance); 256 long rowsUpdated = contentResolver.update(getUri(instance.mId), values, null, null); 257 return rowsUpdated == 1; 258 } 259 260 public static boolean deleteInstance(ContentResolver contentResolver, long instanceId) { 261 if (instanceId == INVALID_ID) return false; 262 int deletedRows = contentResolver.delete(getUri(instanceId), "", null); 263 return deletedRows == 1; 264 } 265 266 /** 267 * @param context 268 * @param contentResolver to access the content provider 269 * @param alarmId identifies the alarm in question 270 * @param instanceId identifies the instance to keep; all other instances will be removed 271 */ 272 public static void deleteOtherInstances(Context context, ContentResolver contentResolver, 273 long alarmId, long instanceId) { 274 final List<AlarmInstance> instances = getInstancesByAlarmId(contentResolver, alarmId); 275 for (AlarmInstance instance : instances) { 276 if (instance.mId != instanceId) { 277 AlarmStateManager.unregisterInstance(context, instance); 278 deleteInstance(contentResolver, instance.mId); 279 } 280 } 281 } 282 283 // Public fields 284 public long mId; 285 public int mYear; 286 public int mMonth; 287 public int mDay; 288 public int mHour; 289 public int mMinute; 290 public String mLabel; 291 public boolean mVibrate; 292 public Uri mRingtone; 293 public Long mAlarmId; 294 public int mAlarmState; 295 296 public AlarmInstance(Calendar calendar, Long alarmId) { 297 this(calendar); 298 mAlarmId = alarmId; 299 } 300 301 public AlarmInstance(Calendar calendar) { 302 mId = INVALID_ID; 303 setAlarmTime(calendar); 304 mLabel = ""; 305 mVibrate = false; 306 mRingtone = null; 307 mAlarmState = SILENT_STATE; 308 } 309 310 public AlarmInstance(AlarmInstance instance) { 311 this.mId = instance.mId; 312 this.mYear = instance.mYear; 313 this.mMonth = instance.mMonth; 314 this.mDay = instance.mDay; 315 this.mHour = instance.mHour; 316 this.mMinute = instance.mMinute; 317 this.mLabel = instance.mLabel; 318 this.mVibrate = instance.mVibrate; 319 this.mRingtone = instance.mRingtone; 320 this.mAlarmId = instance.mAlarmId; 321 this.mAlarmState = instance.mAlarmState; 322 } 323 324 public AlarmInstance(Cursor c, boolean joinedTable) { 325 if (joinedTable) { 326 mId = c.getLong(Alarm.INSTANCE_ID_INDEX); 327 mYear = c.getInt(Alarm.INSTANCE_YEAR_INDEX); 328 mMonth = c.getInt(Alarm.INSTANCE_MONTH_INDEX); 329 mDay = c.getInt(Alarm.INSTANCE_DAY_INDEX); 330 mHour = c.getInt(Alarm.INSTANCE_HOUR_INDEX); 331 mMinute = c.getInt(Alarm.INSTANCE_MINUTE_INDEX); 332 mLabel = c.getString(Alarm.INSTANCE_LABEL_INDEX); 333 mVibrate = c.getInt(Alarm.INSTANCE_VIBRATE_INDEX) == 1; 334 } else { 335 mId = c.getLong(ID_INDEX); 336 mYear = c.getInt(YEAR_INDEX); 337 mMonth = c.getInt(MONTH_INDEX); 338 mDay = c.getInt(DAY_INDEX); 339 mHour = c.getInt(HOUR_INDEX); 340 mMinute = c.getInt(MINUTES_INDEX); 341 mLabel = c.getString(LABEL_INDEX); 342 mVibrate = c.getInt(VIBRATE_INDEX) == 1; 343 } 344 if (c.isNull(RINGTONE_INDEX)) { 345 // Should we be saving this with the current ringtone or leave it null 346 // so it changes when user changes default ringtone? 347 mRingtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 348 } else { 349 mRingtone = Uri.parse(c.getString(RINGTONE_INDEX)); 350 } 351 352 if (!c.isNull(ALARM_ID_INDEX)) { 353 mAlarmId = c.getLong(ALARM_ID_INDEX); 354 } 355 mAlarmState = c.getInt(ALARM_STATE_INDEX); 356 } 357 358 public String getLabelOrDefault(Context context) { 359 return mLabel.isEmpty() ? context.getString(R.string.default_label) : mLabel; 360 } 361 362 public void setAlarmTime(Calendar calendar) { 363 mYear = calendar.get(Calendar.YEAR); 364 mMonth = calendar.get(Calendar.MONTH); 365 mDay = calendar.get(Calendar.DAY_OF_MONTH); 366 mHour = calendar.get(Calendar.HOUR_OF_DAY); 367 mMinute = calendar.get(Calendar.MINUTE); 368 } 369 370 /** 371 * Return the time when a alarm should fire. 372 * 373 * @return the time 374 */ 375 public Calendar getAlarmTime() { 376 Calendar calendar = Calendar.getInstance(); 377 calendar.set(Calendar.YEAR, mYear); 378 calendar.set(Calendar.MONTH, mMonth); 379 calendar.set(Calendar.DAY_OF_MONTH, mDay); 380 calendar.set(Calendar.HOUR_OF_DAY, mHour); 381 calendar.set(Calendar.MINUTE, mMinute); 382 calendar.set(Calendar.SECOND, 0); 383 calendar.set(Calendar.MILLISECOND, 0); 384 return calendar; 385 } 386 387 /** 388 * Return the time when a low priority notification should be shown. 389 * 390 * @return the time 391 */ 392 public Calendar getLowNotificationTime() { 393 Calendar calendar = getAlarmTime(); 394 calendar.add(Calendar.HOUR_OF_DAY, LOW_NOTIFICATION_HOUR_OFFSET); 395 return calendar; 396 } 397 398 /** 399 * Return the time when a high priority notification should be shown. 400 * 401 * @return the time 402 */ 403 public Calendar getHighNotificationTime() { 404 Calendar calendar = getAlarmTime(); 405 calendar.add(Calendar.MINUTE, HIGH_NOTIFICATION_MINUTE_OFFSET); 406 return calendar; 407 } 408 409 /** 410 * Return the time when a missed notification should be removed. 411 * 412 * @return the time 413 */ 414 public Calendar getMissedTimeToLive() { 415 Calendar calendar = getAlarmTime(); 416 calendar.add(Calendar.HOUR, MISSED_TIME_TO_LIVE_HOUR_OFFSET); 417 return calendar; 418 } 419 420 /** 421 * Return the time when the alarm should stop firing and be marked as missed. 422 * 423 * @param context to figure out the timeout setting 424 * @return the time when alarm should be silence, or null if never 425 */ 426 public Calendar getTimeout(Context context) { 427 String timeoutSetting = PreferenceManager.getDefaultSharedPreferences(context) 428 .getString(SettingsActivity.KEY_AUTO_SILENCE, DEFAULT_ALARM_TIMEOUT_SETTING); 429 int timeoutMinutes = Integer.parseInt(timeoutSetting); 430 431 // Alarm silence has been set to "None" 432 if (timeoutMinutes < 0) { 433 return null; 434 } 435 436 Calendar calendar = getAlarmTime(); 437 calendar.add(Calendar.MINUTE, timeoutMinutes); 438 return calendar; 439 } 440 441 @Override 442 public boolean equals(Object o) { 443 if (!(o instanceof AlarmInstance)) return false; 444 final AlarmInstance other = (AlarmInstance) o; 445 return mId == other.mId; 446 } 447 448 @Override 449 public int hashCode() { 450 return Long.valueOf(mId).hashCode(); 451 } 452 453 @Override 454 public String toString() { 455 return "AlarmInstance{" + 456 "mId=" + mId + 457 ", mYear=" + mYear + 458 ", mMonth=" + mMonth + 459 ", mDay=" + mDay + 460 ", mHour=" + mHour + 461 ", mMinute=" + mMinute + 462 ", mLabel=" + mLabel + 463 ", mVibrate=" + mVibrate + 464 ", mRingtone=" + mRingtone + 465 ", mAlarmId=" + mAlarmId + 466 ", mAlarmState=" + mAlarmState + 467 '}'; 468 } 469} 470