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.timer
18 
19 import android.annotation.TargetApi
20 import android.content.Context
21 import android.media.AudioAttributes
22 import android.net.Uri
23 import android.os.Build
24 import android.os.Vibrator
25 
26 import com.android.deskclock.AsyncRingtonePlayer
27 import com.android.deskclock.LogUtils
28 import com.android.deskclock.Utils
29 import com.android.deskclock.data.DataModel
30 
31 /**
32  * Manages playing the timer ringtone and vibrating the device.
33  */
34 object TimerKlaxon {
35     private val VIBRATE_PATTERN = longArrayOf(500, 500)
36 
37     private var sStarted = false
38     private var sAsyncRingtonePlayer: AsyncRingtonePlayer? = null
39 
40     @JvmStatic
stopnull41     fun stop(context: Context) {
42         if (sStarted) {
43             LogUtils.i("TimerKlaxon.stop()")
44             sStarted = false
45             getAsyncRingtonePlayer(context)!!.stop()
46             (context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator).cancel()
47         }
48     }
49 
50     @JvmStatic
startnull51     fun start(context: Context) {
52         // Make sure we are stopped before starting
53         stop(context)
54         LogUtils.i("TimerKlaxon.start()")
55 
56         // Look up user-selected timer ringtone.
57         if (DataModel.dataModel.isTimerRingtoneSilent) {
58             // Special case: Silent ringtone
59             LogUtils.i("Playing silent ringtone for timer")
60         } else {
61             val uri: Uri = DataModel.dataModel.timerRingtoneUri
62             val crescendoDuration: Long = DataModel.dataModel.timerCrescendoDuration
63             getAsyncRingtonePlayer(context)!!.play(uri, crescendoDuration)
64         }
65 
66         if (DataModel.dataModel.timerVibrate) {
67             val vibrator = getVibrator(context)
68             if (Utils.isLOrLater) {
69                 vibrateLOrLater(vibrator)
70             } else {
71                 vibrator.vibrate(VIBRATE_PATTERN, 0)
72             }
73         }
74         sStarted = true
75     }
76 
77     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
vibrateLOrLaternull78     private fun vibrateLOrLater(vibrator: Vibrator) {
79         vibrator.vibrate(VIBRATE_PATTERN, 0, AudioAttributes.Builder()
80                 .setUsage(AudioAttributes.USAGE_ALARM)
81                 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
82                 .build())
83     }
84 
getVibratornull85     private fun getVibrator(context: Context): Vibrator {
86         return context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
87     }
88 
89     @Synchronized
getAsyncRingtonePlayernull90     private fun getAsyncRingtonePlayer(context: Context): AsyncRingtonePlayer? {
91         if (sAsyncRingtonePlayer == null) {
92             sAsyncRingtonePlayer = AsyncRingtonePlayer(context.applicationContext)
93         }
94 
95         return sAsyncRingtonePlayer
96     }
97 }