AlarmActivity.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Polassis: personal voice assistant for Android devices
  3. Copyright (C) 2018 Maksymilian Graczyk
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.mg.polassis.gui;
  16. import android.app.Activity;
  17. import android.content.ComponentName;
  18. import android.content.Intent;
  19. import android.content.ServiceConnection;
  20. import android.content.SharedPreferences;
  21. import android.graphics.Color;
  22. import android.media.AudioAttributes;
  23. import android.media.AudioManager;
  24. import android.media.SoundPool;
  25. import android.os.Bundle;
  26. import android.os.Handler;
  27. import android.os.IBinder;
  28. import android.os.PowerManager;
  29. import android.preference.PreferenceManager;
  30. import android.view.View;
  31. import android.view.WindowManager;
  32. import android.widget.TextView;
  33. import com.mg.polassis.R;
  34. import com.mg.polassis.misc.Assistant;
  35. import com.mg.polassis.service.TextToSpeechService;
  36. import com.mg.polassis.misc.Translations;
  37. import java.util.Calendar;
  38. import java.util.GregorianCalendar;
  39. import java.util.Timer;
  40. import java.util.TimerTask;
  41. public class AlarmActivity extends Activity {
  42. private SoundPool soundPool;
  43. private int clockStreamID;
  44. private int clockSoundID;
  45. private Timer blinkingTextTimer;
  46. private TextView timeText;
  47. private TextView titleText;
  48. private final Handler handler = new Handler();
  49. private TextToSpeechService textToSpeechService;
  50. private String toBeSaid;
  51. private final ServiceConnection textToSpeechServiceConnection = new ServiceConnection() {
  52. @Override
  53. public void onServiceConnected(ComponentName name, IBinder service) {
  54. textToSpeechService = ((TextToSpeechService.TextToSpeechBinder)service).getService();
  55. if (toBeSaid != null)
  56. {
  57. textToSpeechService.speak(toBeSaid, AudioManager.STREAM_ALARM);
  58. toBeSaid = null;
  59. }
  60. }
  61. @Override
  62. public void onServiceDisconnected(ComponentName name) {
  63. textToSpeechService = null;
  64. }
  65. };
  66. @Override
  67. protected void onCreate(Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. setContentView(R.layout.activity_timer);
  70. getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
  71. bindService(new Intent(this, TextToSpeechService.class), textToSpeechServiceConnection, BIND_AUTO_CREATE);
  72. timeText = (TextView) findViewById(R.id.noTimeRemaining);
  73. timeText.setShadowLayer(15, 4, 4, Color.BLACK);
  74. titleText = (TextView) findViewById(R.id.timeIsUp);
  75. titleText.setShadowLayer(15, 4, 4, Color.BLACK);
  76. PowerManager.WakeLock wakeLock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "WakeUp");
  77. if (!((PowerManager) getSystemService(POWER_SERVICE)).isScreenOn()) {
  78. wakeLock.acquire();
  79. wakeLock.release();
  80. }
  81. if (!Assistant.inForeground)
  82. sendBroadcast(new Intent().setAction("com.mg.polassis.ACTION_FINISH"));
  83. if (getIntent() == null) prepareForTimer();
  84. else if (getIntent().getBooleanExtra("alarm", false)) prepareForAlarmClock();
  85. else if (getIntent().getBooleanExtra("reminder", false)) prepareForReminder();
  86. else prepareForTimer();
  87. }
  88. private void startAlarmSound(final boolean continuous)
  89. {
  90. if (android.os.Build.VERSION.SDK_INT >= 21)
  91. {
  92. SoundPool.Builder builder = new SoundPool.Builder();
  93. AudioAttributes.Builder attributesBuilder = new AudioAttributes.Builder();
  94. attributesBuilder.setUsage(AudioAttributes.USAGE_ALARM);
  95. builder.setAudioAttributes(attributesBuilder.build());
  96. builder.setMaxStreams(1);
  97. soundPool = builder.build();
  98. }
  99. else soundPool = new SoundPool(1, AudioManager.STREAM_ALARM, 0);
  100. soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
  101. @Override
  102. public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
  103. clockStreamID = soundPool.play(clockSoundID, 1.0f, 1.0f, Integer.MAX_VALUE, (continuous ? -1 : 0), 1.0f);
  104. }
  105. });
  106. clockSoundID = soundPool.load(this, R.raw.timer, 1);
  107. }
  108. private void prepareForAlarmClock()
  109. {
  110. titleText.setText(Translations.getStringResource(this, "alarm_message"));
  111. timeText.setVisibility(View.INVISIBLE);
  112. SharedPreferences alarmsPreferences = getSharedPreferences("alarms", MODE_PRIVATE);
  113. GregorianCalendar calendar = new GregorianCalendar();
  114. calendar.set(Calendar.SECOND, 0);
  115. calendar.set(Calendar.MILLISECOND, 0);
  116. alarmsPreferences.edit().remove(Long.toString(calendar.getTimeInMillis())).apply();
  117. if (android.os.Build.VERSION.SDK_INT < 21 && alarmsPreferences.getAll().keySet().size() == 0)
  118. sendBroadcast(new Intent("android.intent.action.ALARM_CHANGED").putExtra("alarmSet", false));
  119. startAlarmSound(true);
  120. }
  121. private void prepareForTimer()
  122. {
  123. titleText.setText(Translations.getStringResource(this, "time_is_up"));
  124. timeText.setVisibility(View.VISIBLE);
  125. blinkingTextTimer = new Timer();
  126. blinkingTextTimer.schedule(new TimerTask() {
  127. @Override
  128. public void run() {
  129. handler.post(new Runnable() {
  130. @Override
  131. public void run() {
  132. if (timeText.getVisibility() == View.INVISIBLE)
  133. timeText.setVisibility(View.VISIBLE);
  134. else timeText.setVisibility(View.INVISIBLE);
  135. }
  136. });
  137. }
  138. }, 400, 400);
  139. startAlarmSound(true);
  140. }
  141. private void prepareForReminder()
  142. {
  143. String reminderTitle = getIntent().getStringExtra("title");
  144. titleText.setText(reminderTitle);
  145. timeText.setVisibility(View.INVISIBLE);
  146. SharedPreferences remindersPreferences = getSharedPreferences("reminders", MODE_PRIVATE);
  147. GregorianCalendar calendar = new GregorianCalendar();
  148. calendar.set(Calendar.SECOND, 0);
  149. calendar.set(Calendar.MILLISECOND, 0);
  150. remindersPreferences.edit().remove(Long.toString(calendar.getTimeInMillis())).apply();
  151. if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("text_to_speech", true))
  152. {
  153. toBeSaid = Translations.getStringResource(this, "reminder") + reminderTitle;
  154. if (textToSpeechService != null)
  155. {
  156. textToSpeechService.speak(toBeSaid, AudioManager.STREAM_ALARM);
  157. toBeSaid = null;
  158. }
  159. }
  160. else startAlarmSound(false);
  161. }
  162. public void onStopClick(View v)
  163. {
  164. finish();
  165. }
  166. public void onStop()
  167. {
  168. super.onStop();
  169. if (isFinishing()) {
  170. unbindService(textToSpeechServiceConnection);
  171. if (blinkingTextTimer != null) {
  172. blinkingTextTimer.cancel();
  173. blinkingTextTimer = null;
  174. }
  175. if (soundPool != null)
  176. {
  177. soundPool.stop(clockStreamID);
  178. soundPool.unload(clockSoundID);
  179. soundPool.release();
  180. soundPool = null;
  181. }
  182. }
  183. }
  184. }