- 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:
		| @@ -7,6 +7,7 @@ import android.media.MediaPlayer; | |||||||
| import android.media.RingtoneManager; | import android.media.RingtoneManager; | ||||||
| import android.net.Uri; | import android.net.Uri; | ||||||
| import android.os.Vibrator; | import android.os.Vibrator; | ||||||
|  | import android.telephony.PhoneStateListener; | ||||||
| import android.telephony.TelephonyManager; | import android.telephony.TelephonyManager; | ||||||
| import android.util.Log; | import android.util.Log; | ||||||
|  |  | ||||||
| @@ -18,33 +19,47 @@ public class AlarmKlaxon { | |||||||
|     private static final float IN_CALL_VOLUME = 0.125f; |     private static final float IN_CALL_VOLUME = 0.125f; | ||||||
|     private static MediaPlayer mediaPlayer = null; |     private static MediaPlayer mediaPlayer = null; | ||||||
|     private static TelephonyManager telephonyManager; |     private static TelephonyManager telephonyManager; | ||||||
|  |     private static PhoneStateListener phoneStateListener; | ||||||
|  |     private static int initialCallState; | ||||||
|     private static Vibrator vibrator; |     private static Vibrator vibrator; | ||||||
|  |  | ||||||
|     public static void start(final Context context) { |     public static void start(final Context context) { | ||||||
|         /** |         /** | ||||||
|          * |          * | ||||||
|          * TODO add raw ring tone to use as fallback |          * TODO allow user to select alarm tone | ||||||
|          * TODO add in-call ring tone |          * TODO add raw alarm tone to use as fallback | ||||||
|  |          * TODO add in-call alarm tone | ||||||
|          * TODO lower volume if in a call |          * 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 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 = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); | ||||||
|         vibrator.cancel(); |         vibrator.cancel(); | ||||||
|         vibrator.vibrate(vPattern, 0); |         vibrator.vibrate(vPattern, 0); | ||||||
|  |  | ||||||
|         if (true) |  | ||||||
|             return; // TODO remove after testing -- is mediaplayer responsible for delays? |  | ||||||
|  |  | ||||||
|         telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |         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? |         // TODO select alarm tone? | ||||||
|         // Use the default alarm tone... |         // Use the default alarm tone... | ||||||
|         Uri alarmNoise = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); |         Uri alarmNoise = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); | ||||||
|  |  | ||||||
|         stopAudio(context); |         stopAudio(context); | ||||||
|         mediaPlayer = new MediaPlayer(); |         mediaPlayer = new MediaPlayer(); | ||||||
|         mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { |         mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { | ||||||
| @@ -57,24 +72,21 @@ public class AlarmKlaxon { | |||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             /* |  | ||||||
|             * TODO find out if we're in a call |  | ||||||
|             if (inTelephoneCall) { |             if (inTelephoneCall) { | ||||||
|                     Log.v("Using the in-call alarm"); |                     Log.d("AlarmKlaxon", "Using the in-call alarm"); | ||||||
|                     sMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME); |                     mediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME); | ||||||
|                     setDataSourceFromResource(context, sMediaPlayer, R.raw.in_call_alarm); |                     setDataSourceFromResource(context, mediaPlayer, R.raw.in_call_alarm); | ||||||
|             } else { |             } else { | ||||||
|             */ |  | ||||||
|                 mediaPlayer.setDataSource(context, alarmNoise); |                 mediaPlayer.setDataSource(context, alarmNoise); | ||||||
|                 startAudio(context); |                 startAudio(context); | ||||||
|             //} |             } | ||||||
|         } catch (Exception ex) { |         } catch (Exception ex) { | ||||||
|             // The alarmNoise may be on the sd card which could be busy right |             // The alarmNoise may be on the sd card which could be busy right | ||||||
|             // now. Use the fallback ringtone. |             // now. Use the fallback ringtone. | ||||||
|             try { |             try { | ||||||
|                 // Reset the media player to clear the error state. |                 // Reset the media player to clear the error state. | ||||||
|                 mediaPlayer.reset(); |                 mediaPlayer.reset(); | ||||||
|                 //setDataSourceFromResource(this, mediaPlayer, R.raw.fallbackring); |                 setDataSourceFromResource(context, mediaPlayer, R.raw.fallbackring); | ||||||
|                 startAudio(context); |                 startAudio(context); | ||||||
|             } catch (Exception ex2) { |             } catch (Exception ex2) { | ||||||
|                 // At this point we just don't play anything. |                 // At this point we just don't play anything. | ||||||
| @@ -86,9 +98,8 @@ public class AlarmKlaxon { | |||||||
|  |  | ||||||
|     public static void stop(final Context context) { |     public static void stop(final Context context) { | ||||||
|         vibrator.cancel(); |         vibrator.cancel(); | ||||||
|         if (true) |  | ||||||
|             return; // TODO remove after testing |  | ||||||
|         stopAudio(context); |         stopAudio(context); | ||||||
|  |         telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private static void startAudio(final Context context) throws IOException { |     private static void startAudio(final Context context) throws IOException { | ||||||
| @@ -118,6 +129,7 @@ public class AlarmKlaxon { | |||||||
|             mediaPlayer = null; |             mediaPlayer = null; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // Load ringtone from a resource |     // Load ringtone from a resource | ||||||
|     private static void setDataSourceFromResource(Context context, MediaPlayer player, int res) throws IOException { |     private static void setDataSourceFromResource(Context context, MediaPlayer player, int res) throws IOException { | ||||||
|         AssetFileDescriptor afd = context.getResources().openRawResourceFd(res); |         AssetFileDescriptor afd = context.getResources().openRawResourceFd(res); | ||||||
| @@ -126,4 +138,5 @@ public class AlarmKlaxon { | |||||||
|             afd.close(); |             afd.close(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -74,7 +74,9 @@ public class GraceReceiver extends BroadcastReceiver { | |||||||
|     private void sendText(Context context) { |     private void sendText(Context context) { | ||||||
|         SmsManager sms = SmsManager.getDefault(); |         SmsManager sms = SmsManager.getDefault(); | ||||||
|         // TODO uncomment sendTextMessage |         // 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(); |         Toast.makeText(context, message, Toast.LENGTH_LONG).show(); | ||||||
|         Log.d("GraceReceiver", "Sending sms to " + phoneNumber + " with message: " + message); |         Log.d("GraceReceiver", "Sending sms to " + phoneNumber + " with message: " + message); | ||||||
|     } |     } | ||||||
|   | |||||||
							
								
								
									
										
											BIN
										
									
								
								HypoAlarm/src/main/res/raw/fallbackring.ogg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								HypoAlarm/src/main/res/raw/fallbackring.ogg
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								HypoAlarm/src/main/res/raw/in_call_alarm.ogg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								HypoAlarm/src/main/res/raw/in_call_alarm.ogg
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user