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.timer;
18
19import android.content.Context;
20import android.net.Uri;
21
22import com.android.deskclock.AsyncRingtonePlayer;
23import com.android.deskclock.LogUtils;
24import com.android.deskclock.data.DataModel;
25import com.android.deskclock.settings.SettingsActivity;
26
27public abstract class TimerKlaxon {
28
29    private static boolean sStarted = false;
30    private static AsyncRingtonePlayer sAsyncRingtonePlayer;
31
32    private TimerKlaxon() {
33    }
34
35    public static void stop(Context context) {
36        if (sStarted) {
37            LogUtils.i("TimerKlaxon.stop()");
38            sStarted = false;
39            getAsyncRingtonePlayer(context).stop();
40        }
41    }
42
43    public static void start(Context context) {
44        // Make sure we are stopped before starting
45        stop(context);
46        LogUtils.i("TimerKlaxon.start()");
47
48        // Look up user-selected timer ringtone.
49        if (DataModel.getDataModel().isTimerRingtoneSilent()) {
50            // Special case: Silent ringtone
51            LogUtils.i("Playing silent ringtone for timer");
52        } else {
53            final Uri uri = DataModel.getDataModel().getTimerRingtoneUri();
54            getAsyncRingtonePlayer(context).play(uri);
55        }
56        sStarted = true;
57    }
58
59    private static synchronized AsyncRingtonePlayer getAsyncRingtonePlayer(Context context) {
60        if (sAsyncRingtonePlayer == null) {
61            sAsyncRingtonePlayer = new AsyncRingtonePlayer(context.getApplicationContext(),
62                    SettingsActivity.KEY_TIMER_CRESCENDO);
63        }
64
65        return sAsyncRingtonePlayer;
66    }
67}