From 577886e17fe13b7dc7b947efdb25031d533dad59 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 4 Jan 2022 19:40:27 +0100 Subject: [PATCH] Added alarm duration option --- .../java/eu/faircode/email/EntityRule.java | 3 +- .../java/eu/faircode/email/FragmentRule.java | 28 +++++++++++++++++-- .../eu/faircode/email/MediaPlayerHelper.java | 6 ++-- app/src/main/res/layout/fragment_rule.xml | 28 +++++++++++++++++-- app/src/main/res/values/strings.xml | 1 + 5 files changed, 58 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index f947d9df38..7367d70b82 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -954,6 +954,7 @@ public class EntityRule { Uri uri = Uri.parse(jargs.getString("uri")); boolean alarm = jargs.getBoolean("alarm"); + int duration = jargs.optInt("duration", MediaPlayerHelper.DEFAULT_ALARM_DURATION); DB db = DB.getInstance(context); @@ -964,7 +965,7 @@ public class EntityRule { @Override public void run() { try { - MediaPlayerHelper.play(context, uri, alarm); + MediaPlayerHelper.play(context, uri, alarm, duration); } catch (Throwable ex) { Log.e(ex); } diff --git a/app/src/main/java/eu/faircode/email/FragmentRule.java b/app/src/main/java/eu/faircode/email/FragmentRule.java index 2c7322eaf8..d3bdd4fa9d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentRule.java +++ b/app/src/main/java/eu/faircode/email/FragmentRule.java @@ -149,6 +149,7 @@ public class FragmentRule extends FragmentBase { private Button btnSound; private CheckBox cbAlarm; + private EditText etAlarmDuration; private TextView tvAutomation; @@ -297,6 +298,7 @@ public class FragmentRule extends FragmentBase { btnSound = view.findViewById(R.id.btnSound); cbAlarm = view.findViewById(R.id.cbAlarm); + etAlarmDuration = view.findViewById(R.id.etAlarmDuration); tvAutomation = view.findViewById(R.id.tvAutomation); @@ -517,6 +519,16 @@ public class FragmentRule extends FragmentBase { } }); + cbAlarm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + etAlarmDuration.setEnabled(isChecked); + } + }); + + etAlarmDuration.setHint(Integer.toString(MediaPlayerHelper.DEFAULT_ALARM_DURATION)); + etAlarmDuration.setEnabled(false); + npDuration.setMinValue(0); npDuration.setMaxValue(999); @@ -1043,7 +1055,11 @@ public class FragmentRule extends FragmentBase { case EntityRule.TYPE_SOUND: if (jaction.has("uri")) FragmentRule.this.sound = Uri.parse(jaction.getString("uri")); - cbAlarm.setChecked(jaction.optBoolean("alarm")); + boolean alarm = jaction.optBoolean("alarm"); + int duration = jaction.optInt("duration", 0); + cbAlarm.setChecked(alarm); + etAlarmDuration.setEnabled(alarm); + etAlarmDuration.setText(duration == 0 ? null : Integer.toString(duration)); break; } @@ -1375,8 +1391,16 @@ public class FragmentRule extends FragmentBase { break; case EntityRule.TYPE_SOUND: + boolean alarm = cbAlarm.isChecked(); + String duration = etAlarmDuration.getText().toString(); jaction.put("uri", sound); - jaction.put("alarm", cbAlarm.isChecked()); + jaction.put("alarm", alarm); + if (alarm && !TextUtils.isEmpty(duration)) + try { + jaction.put("duration", Integer.parseInt(duration)); + } catch (NumberFormatException ex) { + Log.e(ex); + } break; } } diff --git a/app/src/main/java/eu/faircode/email/MediaPlayerHelper.java b/app/src/main/java/eu/faircode/email/MediaPlayerHelper.java index 3f8663b2ed..629a4ac8fe 100644 --- a/app/src/main/java/eu/faircode/email/MediaPlayerHelper.java +++ b/app/src/main/java/eu/faircode/email/MediaPlayerHelper.java @@ -10,9 +10,9 @@ import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; public class MediaPlayerHelper { - private static final int MAX_DURATION = 30; // seconds + static final int DEFAULT_ALARM_DURATION = 30; // seconds - static void play(Context context, Uri uri, boolean alarm) throws IOException { + static void play(Context context, Uri uri, boolean alarm, int duration) throws IOException { Semaphore sem = new Semaphore(0); AudioAttributes attrs = new AudioAttributes.Builder() @@ -41,7 +41,7 @@ public class MediaPlayerHelper { mediaPlayer.prepareAsync(); try { - if (!sem.tryAcquire(MAX_DURATION, TimeUnit.SECONDS)) { + if (!sem.tryAcquire(duration, TimeUnit.SECONDS)) { mediaPlayer.stop(); mediaPlayer.release(); } diff --git a/app/src/main/res/layout/fragment_rule.xml b/app/src/main/res/layout/fragment_rule.xml index 134ff399d8..dd025ccf25 100644 --- a/app/src/main/res/layout/fragment_rule.xml +++ b/app/src/main/res/layout/fragment_rule.xml @@ -947,6 +947,30 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/cbAlarm" /> + + + + + app:layout_constraintTop_toBottomOf="@+id/etAlarmDuration" /> + app:constraint_referenced_ids="btnSound,cbAlarm,tvAlarm,tvAlarmDuration,etAlarmDuration" /> Select sound Use as alarm This will ignore "do not disturb" rules + Maximum alarm duration (seconds) Synchronize Folders