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 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 public abstract class TimerKlaxon { 35 36 private static final long[] VIBRATE_PATTERN = {500, 500}; 37 38 private static boolean sStarted = false; 39 private static AsyncRingtonePlayer sAsyncRingtonePlayer; 40 TimerKlaxon()41 private TimerKlaxon() { 42 } 43 stop(Context context)44 public static void stop(Context context) { 45 if (sStarted) { 46 LogUtils.i("TimerKlaxon.stop()"); 47 sStarted = false; 48 getAsyncRingtonePlayer(context).stop(); 49 ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).cancel(); 50 } 51 } 52 start(Context context)53 public static void start(Context context) { 54 // Make sure we are stopped before starting 55 stop(context); 56 LogUtils.i("TimerKlaxon.start()"); 57 58 // Look up user-selected timer ringtone. 59 if (DataModel.getDataModel().isTimerRingtoneSilent()) { 60 // Special case: Silent ringtone 61 LogUtils.i("Playing silent ringtone for timer"); 62 } else { 63 final Uri uri = DataModel.getDataModel().getTimerRingtoneUri(); 64 final long crescendoDuration = DataModel.getDataModel().getTimerCrescendoDuration(); 65 getAsyncRingtonePlayer(context).play(uri, crescendoDuration); 66 } 67 68 if (DataModel.getDataModel().getTimerVibrate()) { 69 final Vibrator vibrator = getVibrator(context); 70 if (Utils.isLOrLater()) { 71 vibrateLOrLater(vibrator); 72 } else { 73 vibrator.vibrate(VIBRATE_PATTERN, 0); 74 } 75 } 76 sStarted = true; 77 } 78 79 @TargetApi(Build.VERSION_CODES.LOLLIPOP) vibrateLOrLater(Vibrator vibrator)80 private static void vibrateLOrLater(Vibrator vibrator) { 81 vibrator.vibrate(VIBRATE_PATTERN, 0, new AudioAttributes.Builder() 82 .setUsage(AudioAttributes.USAGE_ALARM) 83 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 84 .build()); 85 } 86 getVibrator(Context context)87 private static Vibrator getVibrator(Context context) { 88 return ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)); 89 } 90 getAsyncRingtonePlayer(Context context)91 private static synchronized AsyncRingtonePlayer getAsyncRingtonePlayer(Context context) { 92 if (sAsyncRingtonePlayer == null) { 93 sAsyncRingtonePlayer = new AsyncRingtonePlayer(context.getApplicationContext()); 94 } 95 96 return sAsyncRingtonePlayer; 97 } 98 }