1 /* 2 * Copyright (C) 2013 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.alarms; 18 19 import android.annotation.TargetApi; 20 import android.content.Context; 21 import android.media.AudioAttributes; 22 import android.os.Build; 23 import android.os.Vibrator; 24 25 import com.android.deskclock.AsyncRingtonePlayer; 26 import com.android.deskclock.LogUtils; 27 import com.android.deskclock.Utils; 28 import com.android.deskclock.data.DataModel; 29 import com.android.deskclock.provider.AlarmInstance; 30 31 /** 32 * Manages playing alarm ringtones and vibrating the device. 33 */ 34 final class AlarmKlaxon { 35 36 private static final long[] VIBRATE_PATTERN = {500, 500}; 37 38 private static boolean sStarted = false; 39 private static AsyncRingtonePlayer sAsyncRingtonePlayer; 40 AlarmKlaxon()41 private AlarmKlaxon() {} 42 stop(Context context)43 public static void stop(Context context) { 44 if (sStarted) { 45 LogUtils.v("AlarmKlaxon.stop()"); 46 sStarted = false; 47 getAsyncRingtonePlayer(context).stop(); 48 ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).cancel(); 49 } 50 } 51 start(Context context, AlarmInstance instance)52 public static void start(Context context, AlarmInstance instance) { 53 // Make sure we are stopped before starting 54 stop(context); 55 LogUtils.v("AlarmKlaxon.start()"); 56 57 if (!AlarmInstance.NO_RINGTONE_URI.equals(instance.mRingtone)) { 58 final long crescendoDuration = DataModel.getDataModel().getAlarmCrescendoDuration(); 59 getAsyncRingtonePlayer(context).play(instance.mRingtone, crescendoDuration); 60 } 61 62 if (instance.mVibrate) { 63 final Vibrator vibrator = getVibrator(context); 64 if (Utils.isLOrLater()) { 65 vibrateLOrLater(vibrator); 66 } else { 67 vibrator.vibrate(VIBRATE_PATTERN, 0); 68 } 69 } 70 71 sStarted = true; 72 } 73 74 @TargetApi(Build.VERSION_CODES.LOLLIPOP) vibrateLOrLater(Vibrator vibrator)75 private static void vibrateLOrLater(Vibrator vibrator) { 76 vibrator.vibrate(VIBRATE_PATTERN, 0, new AudioAttributes.Builder() 77 .setUsage(AudioAttributes.USAGE_ALARM) 78 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 79 .build()); 80 } 81 getVibrator(Context context)82 private static Vibrator getVibrator(Context context) { 83 return ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)); 84 } 85 getAsyncRingtonePlayer(Context context)86 private static synchronized AsyncRingtonePlayer getAsyncRingtonePlayer(Context context) { 87 if (sAsyncRingtonePlayer == null) { 88 sAsyncRingtonePlayer = new AsyncRingtonePlayer(context.getApplicationContext()); 89 } 90 91 return sAsyncRingtonePlayer; 92 } 93 }