AlarmKlaxonpublic class AlarmKlaxon extends Object implements Alarms.AlarmSettingsManages alarms and vibe. Singleton, so it can be initiated in
AlarmReceiver and shut down in the AlarmAlert activity |
Fields Summary |
---|
static final int | ALARM_TIMEOUT_SECONDSPlay alarm up to 10 minutes before silencing | private static final long[] | sVibratePattern | private int | mAlarmId | private String | mAlert | private Alarms.DaysOfWeek | mDaysOfWeek | private boolean | mVibrate | private boolean | mPlaying | private android.os.Vibrator | mVibrator | private android.media.MediaPlayer | mMediaPlayer | private KillerCallback | mKillerCallback | private static final int | KILLER | private static final int | PLAY | private android.os.Handler | mHandler | private static final float | IN_CALL_VOLUME |
Constructors Summary |
---|
AlarmKlaxon()
mVibrator = new Vibrator();
|
Methods Summary |
---|
private void | disableKiller()
mHandler.removeMessages(KILLER);
| private void | enableKiller()Kills alarm audio after ALARM_TIMEOUT_SECONDS, so the alarm
won't run all day.
This just cancels the audio, but leaves the notification
popped, so the user will know that the alarm tripped.
mHandler.sendMessageDelayed(mHandler.obtainMessage(KILLER),
1000 * ALARM_TIMEOUT_SECONDS);
| private void | play(android.content.Context context, int alarmId)
ContentResolver contentResolver = context.getContentResolver();
if (mPlaying) stop(context, false);
mAlarmId = alarmId;
/* this will call reportAlarm() callback */
Alarms.getAlarm(contentResolver, this, mAlarmId);
if (Log.LOGV) Log.v("AlarmKlaxon.play() " + mAlarmId + " alert " + mAlert);
// TODO: Reuse mMediaPlayer instead of creating a new one and/or use
// RingtoneManager.
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e("Error occurred while playing audio.");
mp.stop();
mp.release();
mMediaPlayer = null;
return true;
}
});
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(
Context.TELEPHONY_SERVICE);
// Check if we are in a call. If we are, use the in-call alarm
// resource at a low volume to not disrupt the call.
if (tm.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
Log.v("Using the in-call alarm");
mMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);
setDataSourceFromResource(context.getResources(),
mMediaPlayer, R.raw.in_call_alarm);
} else {
mMediaPlayer.setDataSource(context, Uri.parse(mAlert));
}
startAlarm(mMediaPlayer);
} catch (Exception ex) {
Log.v("Using the fallback ringtone");
// The alert may be on the sd card which could be busy right now.
// Use the fallback ringtone.
try {
// Must reset the media player to clear the error state.
mMediaPlayer.reset();
setDataSourceFromResource(context.getResources(), mMediaPlayer,
com.android.internal.R.raw.fallbackring);
startAlarm(mMediaPlayer);
} catch (Exception ex2) {
// At this point we just don't play anything.
Log.e("Failed to play fallback ringtone", ex2);
}
}
/* Start the vibrator after everything is ok with the media player */
if (mVibrate) {
mVibrator.vibrate(sVibratePattern, 0);
} else {
mVibrator.cancel();
}
enableKiller();
mPlaying = true;
| public void | postPlay(android.content.Context context, int alarmId)
mHandler.sendMessage(mHandler.obtainMessage(PLAY, alarmId, 0, context));
| public void | reportAlarm(int idx, boolean enabled, int hour, int minutes, Alarms.DaysOfWeek daysOfWeek, boolean vibrate, java.lang.String message, java.lang.String alert)
if (Log.LOGV) Log.v("AlarmKlaxon.reportAlarm: " + idx + " " + hour +
" " + minutes + " dow " + daysOfWeek);
mAlert = alert;
mDaysOfWeek = daysOfWeek;
mVibrate = vibrate;
| private void | setDataSourceFromResource(android.content.res.Resources resources, android.media.MediaPlayer player, int res)
AssetFileDescriptor afd = resources.openRawResourceFd(res);
if (afd != null) {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength());
afd.close();
}
| public void | setKillerCallback(com.android.alarmclock.AlarmKlaxon$KillerCallback killerCallback)This callback called when alarm killer times out unattended
alarm
mKillerCallback = killerCallback;
| private void | startAlarm(android.media.MediaPlayer player)
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
player.start();
| public void | stop(android.content.Context context, boolean snoozed)Stops alarm audio and disables alarm if it not snoozed and not
repeating
if (Log.LOGV) Log.v("AlarmKlaxon.stop() " + mAlarmId);
if (mPlaying) {
mPlaying = false;
// Stop audio playing
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
// Stop vibrator
mVibrator.cancel();
/* disable alarm only if it is not set to repeat */
if (!snoozed && ((mDaysOfWeek == null || !mDaysOfWeek.isRepeatSet()))) {
Alarms.enableAlarm(context, mAlarmId, false);
}
}
disableKiller();
|
|