mirror of
https://github.com/M66B/FairEmail.git
synced 2026-04-03 15:46:34 +02:00
Added alarm duration option
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user