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.Context
20 import android.content.SharedPreferences
21 import android.net.Uri
22 
23 import com.android.deskclock.R
24 import com.android.deskclock.Utils
25 
26 import java.util.TimeZone
27 
28 /**
29  * All settings data is accessed via this model.
30  */
31 internal class SettingsModel(
32     private val mContext: Context,
33     private val mPrefs: SharedPreferences,
34     /** The model from which time data are fetched.  */
35     private val mTimeModel: TimeModel
36 ) {
37 
38     /** The uri of the default ringtone to use for timers until the user explicitly chooses one.  */
39     private var mDefaultTimerRingtoneUri: Uri? = null
40 
41     init {
42         // Set the user's default display seconds preference if one has not yet been chosen.
43         SettingsDAO.setDefaultDisplayClockSeconds(mContext, mPrefs)
44     }
45 
46     val globalIntentId: Int
47         get() = SettingsDAO.getGlobalIntentId(mPrefs)
48 
updateGlobalIntentIdnull49     fun updateGlobalIntentId() {
50         SettingsDAO.updateGlobalIntentId(mPrefs)
51     }
52 
53     val citySort: DataModel.CitySort
54         get() = SettingsDAO.getCitySort(mPrefs)
55 
toggleCitySortnull56     fun toggleCitySort() {
57         SettingsDAO.toggleCitySort(mPrefs)
58     }
59 
60     val homeTimeZone: TimeZone
61         get() = SettingsDAO.getHomeTimeZone(mContext, mPrefs, TimeZone.getDefault())
62 
63     val clockStyle: DataModel.ClockStyle
64         get() = SettingsDAO.getClockStyle(mContext, mPrefs)
65 
66     var displayClockSeconds: Boolean
67         get() = SettingsDAO.getDisplayClockSeconds(mPrefs)
68         set(shouldDisplaySeconds) {
69             SettingsDAO.setDisplayClockSeconds(mPrefs, shouldDisplaySeconds)
70         }
71 
72     val screensaverClockStyle: DataModel.ClockStyle
73         get() = SettingsDAO.getScreensaverClockStyle(mContext, mPrefs)
74 
75     val screensaverNightModeOn: Boolean
76         get() = SettingsDAO.getScreensaverNightModeOn(mPrefs)
77 
78     val showHomeClock: Boolean
79         get() {
80             if (!SettingsDAO.getAutoShowHomeClock(mPrefs)) {
81                 return false
82             }
83 
84             // Show the home clock if the current time and home time differ.
85             // (By using UTC offset for this comparison the various DST rules are considered)
86             val defaultTZ = TimeZone.getDefault()
87             val homeTimeZone = SettingsDAO.getHomeTimeZone(mContext, mPrefs, defaultTZ)
88             val now = System.currentTimeMillis()
89             return homeTimeZone.getOffset(now) != defaultTZ.getOffset(now)
90         }
91 
92     val defaultTimerRingtoneUri: Uri
93         get() {
94             if (mDefaultTimerRingtoneUri == null) {
95                 mDefaultTimerRingtoneUri = Utils.getResourceUri(mContext, R.raw.timer_expire)
96             }
97 
98             return mDefaultTimerRingtoneUri!!
99         }
100 
101     var timerRingtoneUri: Uri
102         get() = SettingsDAO.getTimerRingtoneUri(mPrefs, defaultTimerRingtoneUri)
103         set(uri) {
104             SettingsDAO.setTimerRingtoneUri(mPrefs, uri)
105         }
106 
107     val alarmVolumeButtonBehavior: DataModel.AlarmVolumeButtonBehavior
108         get() = SettingsDAO.getAlarmVolumeButtonBehavior(mPrefs)
109 
110     val alarmTimeout: Int
111         get() = SettingsDAO.getAlarmTimeout(mPrefs)
112 
113     val snoozeLength: Int
114         get() = SettingsDAO.getSnoozeLength(mPrefs)
115 
116     var defaultAlarmRingtoneUri: Uri
117         get() = SettingsDAO.getDefaultAlarmRingtoneUri(mPrefs)
118         set(uri) {
119             SettingsDAO.setDefaultAlarmRingtoneUri(mPrefs, uri)
120         }
121 
122     val alarmCrescendoDuration: Long
123         get() = SettingsDAO.getAlarmCrescendoDuration(mPrefs)
124 
125     val timerCrescendoDuration: Long
126         get() = SettingsDAO.getTimerCrescendoDuration(mPrefs)
127 
128     val weekdayOrder: Weekdays.Order
129         get() = SettingsDAO.getWeekdayOrder(mPrefs)
130 
131     var isRestoreBackupFinished: Boolean
132         get() = SettingsDAO.isRestoreBackupFinished(mPrefs)
133         set(finished) {
134             SettingsDAO.setRestoreBackupFinished(mPrefs, finished)
135         }
136 
137     var timerVibrate: Boolean
138         get() = SettingsDAO.getTimerVibrate(mPrefs)
139         set(enabled) {
140             SettingsDAO.setTimerVibrate(mPrefs, enabled)
141         }
142 
143     val timeZones: TimeZones
144         get() = SettingsDAO.getTimeZones(mContext, mTimeModel.currentTimeMillis())
145 }