• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 
17 package com.android.deskclock.data
18 
19 import android.content.ContentResolver
20 import android.content.Context
21 import android.database.ContentObserver
22 import android.net.Uri
23 import android.os.Handler
24 import android.os.Looper
25 import android.provider.Settings
26 
27 import com.android.deskclock.provider.ClockContract.AlarmSettingColumns
28 
29 /**
30  * All alarm data will eventually be accessed via this model.
31  */
32 internal class AlarmModel(
33     context: Context,
34     /** The model from which settings are fetched.  */
35     private val mSettingsModel: SettingsModel
36 ) {
37 
38     /** The uri of the default ringtone to play for new alarms; mirrors last selection.  */
39     private var mDefaultAlarmRingtoneUri: Uri? = null
40 
41     init {
42         // Clear caches affected by system settings when system settings change.
43         val cr: ContentResolver = context.getContentResolver()
44         val observer: ContentObserver = SystemAlarmAlertChangeObserver()
45         cr.registerContentObserver(Settings.System.DEFAULT_ALARM_ALERT_URI, false, observer)
46     }
47 
48     // Never set the silent ringtone as default; new alarms should always make sound by default.
49     var defaultAlarmRingtoneUri: Uri
50         get() {
51             if (mDefaultAlarmRingtoneUri == null) {
52                 mDefaultAlarmRingtoneUri = mSettingsModel.defaultAlarmRingtoneUri
53             }
54 
55             return mDefaultAlarmRingtoneUri!!
56         }
57         set(uri) {
58             // Never set the silent ringtone as default; new alarms should always make sound by default.
59             if (!AlarmSettingColumns.NO_RINGTONE_URI.equals(uri)) {
60                 mSettingsModel.defaultAlarmRingtoneUri = uri
61                 mDefaultAlarmRingtoneUri = uri
62             }
63         }
64 
65     val alarmCrescendoDuration: Long
66         get() = mSettingsModel.alarmCrescendoDuration
67 
68     val alarmVolumeButtonBehavior: DataModel.AlarmVolumeButtonBehavior
69         get() = mSettingsModel.alarmVolumeButtonBehavior
70 
71     val alarmTimeout: Int
72         get() = mSettingsModel.alarmTimeout
73 
74     val snoozeLength: Int
75         get() = mSettingsModel.snoozeLength
76 
77     /**
78      * This receiver is notified when system settings change. Cached information built on
79      * those system settings must be cleared.
80      */
81     private inner class SystemAlarmAlertChangeObserver
82         : ContentObserver(Handler(Looper.myLooper()!!)) {
onChangenull83         override fun onChange(selfChange: Boolean) {
84             super.onChange(selfChange)
85             mDefaultAlarmRingtoneUri = null
86         }
87     }
88 }