Home | History | Annotate | Download | only in alarms
      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.provider.AlarmInstance;
     29 import com.android.deskclock.settings.SettingsActivity;
     30 
     31 /**
     32  * Manages playing ringtone and vibrating the device.
     33  */
     34 public final class AlarmKlaxon {
     35     private static final long[] sVibratePattern = {500, 500};
     36 
     37     private static boolean sStarted = false;
     38     private static AsyncRingtonePlayer sAsyncRingtonePlayer;
     39 
     40     private AlarmKlaxon() {}
     41 
     42     public static void stop(Context context) {
     43         if (sStarted) {
     44             LogUtils.v("AlarmKlaxon.stop()");
     45             sStarted = false;
     46             getAsyncRingtonePlayer(context).stop();
     47             ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).cancel();
     48         }
     49     }
     50 
     51     public static void start(Context context, AlarmInstance instance) {
     52         // Make sure we are stopped before starting
     53         stop(context);
     54         LogUtils.v("AlarmKlaxon.start()");
     55 
     56         if (!AlarmInstance.NO_RINGTONE_URI.equals(instance.mRingtone)) {
     57             getAsyncRingtonePlayer(context).play(instance.mRingtone);
     58         }
     59 
     60         if (instance.mVibrate) {
     61             final Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
     62             if (Utils.isLOrLater()) {
     63                 vibrateLOrLater(vibrator);
     64             } else {
     65                 vibrator.vibrate(sVibratePattern, 0);
     66             }
     67         }
     68 
     69         sStarted = true;
     70     }
     71 
     72     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     73     private static void vibrateLOrLater(Vibrator vibrator) {
     74         vibrator.vibrate(sVibratePattern, 0, new AudioAttributes.Builder()
     75                 .setUsage(AudioAttributes.USAGE_ALARM)
     76                 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
     77                 .build());
     78     }
     79 
     80     private static synchronized AsyncRingtonePlayer getAsyncRingtonePlayer(Context context) {
     81         if (sAsyncRingtonePlayer == null) {
     82             sAsyncRingtonePlayer = new AsyncRingtonePlayer(context.getApplicationContext(),
     83                     SettingsActivity.KEY_ALARM_CRESCENDO);
     84         }
     85 
     86         return sAsyncRingtonePlayer;
     87     }
     88 }