1/*
2 * Copyright (C) 2015 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.data;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.database.ContentObserver;
22import android.net.Uri;
23import android.os.Handler;
24import android.provider.Settings;
25
26import com.android.deskclock.data.DataModel.AlarmVolumeButtonBehavior;
27import com.android.deskclock.provider.Alarm;
28
29/**
30 * All alarm data will eventually be accessed via this model.
31 */
32final class AlarmModel {
33
34    /** The model from which settings are fetched. */
35    private final SettingsModel mSettingsModel;
36
37    /** The uri of the default ringtone to play for new alarms; mirrors last selection. */
38    private Uri mDefaultAlarmRingtoneUri;
39
40    AlarmModel(Context context, SettingsModel settingsModel) {
41        mSettingsModel = settingsModel;
42
43        // Clear caches affected by system settings when system settings change.
44        final ContentResolver cr = context.getContentResolver();
45        final ContentObserver observer = new SystemAlarmAlertChangeObserver();
46        cr.registerContentObserver(Settings.System.DEFAULT_ALARM_ALERT_URI, false, observer);
47    }
48
49    Uri getDefaultAlarmRingtoneUri() {
50        if (mDefaultAlarmRingtoneUri == null) {
51            mDefaultAlarmRingtoneUri = mSettingsModel.getDefaultAlarmRingtoneUri();
52        }
53
54        return mDefaultAlarmRingtoneUri;
55    }
56
57    void setDefaultAlarmRingtoneUri(Uri uri) {
58        // Never set the silent ringtone as default; new alarms should always make sound by default.
59        if (!Alarm.NO_RINGTONE_URI.equals(uri)) {
60            mSettingsModel.setDefaultAlarmRingtoneUri(uri);
61            mDefaultAlarmRingtoneUri = uri;
62        }
63    }
64
65    long getAlarmCrescendoDuration() {
66        return mSettingsModel.getAlarmCrescendoDuration();
67    }
68
69    AlarmVolumeButtonBehavior getAlarmVolumeButtonBehavior() {
70        return mSettingsModel.getAlarmVolumeButtonBehavior();
71    }
72
73    int getAlarmTimeout() {
74        return mSettingsModel.getAlarmTimeout();
75    }
76
77    int getSnoozeLength() {
78        return mSettingsModel.getSnoozeLength();
79    }
80
81    /**
82     * This receiver is notified when system settings change. Cached information built on
83     * those system settings must be cleared.
84     */
85    private final class SystemAlarmAlertChangeObserver extends ContentObserver {
86
87        private SystemAlarmAlertChangeObserver() {
88            super(new Handler());
89        }
90
91        @Override
92        public void onChange(boolean selfChange) {
93            super.onChange(selfChange);
94            mDefaultAlarmRingtoneUri = null;
95        }
96    }
97}