- Check for incoming phone calls and mute alarm

- Run alarm at lower volume if in a phone call
- Re-enable alarm noises.
This commit is contained in:
Timothy Allen 2014-04-13 22:53:33 +02:00
parent a0c2f37b07
commit c36a1ab907
4 changed files with 38 additions and 23 deletions

View File

@ -7,6 +7,7 @@ import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
@ -18,33 +19,47 @@ public class AlarmKlaxon {
private static final float IN_CALL_VOLUME = 0.125f;
private static MediaPlayer mediaPlayer = null;
private static TelephonyManager telephonyManager;
private static PhoneStateListener phoneStateListener;
private static int initialCallState;
private static Vibrator vibrator;
public static void start(final Context context) {
/**
*
* TODO add raw ring tone to use as fallback
* TODO add in-call ring tone
* TODO allow user to select alarm tone
* TODO add raw alarm tone to use as fallback
* TODO add in-call alarm tone
* TODO lower volume if in a call
* TODO cancel noise if a call comes in (add TelephonyManager listener which cancels the alert but calls the notification)
*
* TODO start telephony listener
*
* TODO snooze 5 minutes on press back button or home button (new runnable)
* TODO remove back/home button and most top icons
* TODO fix glowpad/seekarc
*/
vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.cancel();
vibrator.vibrate(vPattern, 0);
if (true)
return; // TODO remove after testing -- is mediaplayer responsible for delays?
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
initialCallState = telephonyManager.getCallState();
phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String ignored) {
// The user might already be in a call when the alarm fires. When
// we register onCallStateChanged, we get the initial in-call state
// which kills the alarm. Check against the initial call state so
// we don't kill the alarm during a call.
if (state != TelephonyManager.CALL_STATE_IDLE && state != initialCallState) {
stopAudio(context);
// TODO stop alarm from snoozing or turning off?
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
boolean inTelephoneCall = initialCallState != TelephonyManager.CALL_STATE_IDLE;
// TODO select alarm tone?
// Use the default alarm tone...
Uri alarmNoise = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
stopAudio(context);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@ -57,24 +72,21 @@ public class AlarmKlaxon {
});
try {
/*
* TODO find out if we're in a call
if (inTelephoneCall) {
Log.v("Using the in-call alarm");
sMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);
setDataSourceFromResource(context, sMediaPlayer, R.raw.in_call_alarm);
Log.d("AlarmKlaxon", "Using the in-call alarm");
mediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);
setDataSourceFromResource(context, mediaPlayer, R.raw.in_call_alarm);
} else {
*/
mediaPlayer.setDataSource(context, alarmNoise);
startAudio(context);
//}
mediaPlayer.setDataSource(context, alarmNoise);
startAudio(context);
}
} catch (Exception ex) {
// The alarmNoise may be on the sd card which could be busy right
// now. Use the fallback ringtone.
try {
// Reset the media player to clear the error state.
mediaPlayer.reset();
//setDataSourceFromResource(this, mediaPlayer, R.raw.fallbackring);
setDataSourceFromResource(context, mediaPlayer, R.raw.fallbackring);
startAudio(context);
} catch (Exception ex2) {
// At this point we just don't play anything.
@ -86,9 +98,8 @@ public class AlarmKlaxon {
public static void stop(final Context context) {
vibrator.cancel();
if (true)
return; // TODO remove after testing
stopAudio(context);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}
private static void startAudio(final Context context) throws IOException {
@ -118,6 +129,7 @@ public class AlarmKlaxon {
mediaPlayer = null;
}
}
// Load ringtone from a resource
private static void setDataSourceFromResource(Context context, MediaPlayer player, int res) throws IOException {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(res);
@ -126,4 +138,5 @@ public class AlarmKlaxon {
afd.close();
}
}
}

View File

@ -74,7 +74,9 @@ public class GraceReceiver extends BroadcastReceiver {
private void sendText(Context context) {
SmsManager sms = SmsManager.getDefault();
// TODO uncomment sendTextMessage
//sms.sendTextMessage(phoneNumber, null, message, null, null);
if (!MainActivity.HYPOALARM_DEBUG) {
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
Log.d("GraceReceiver", "Sending sms to " + phoneNumber + " with message: " + message);
}

Binary file not shown.

Binary file not shown.