mirror of https://github.com/M66B/FairEmail.git
Stop alarm on screen on/off
This commit is contained in:
parent
d3e6d9668b
commit
c49d83b3eb
|
@ -1,7 +1,10 @@
|
||||||
package eu.faircode.email;
|
package eu.faircode.email;
|
||||||
|
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.media.AudioAttributes;
|
import android.media.AudioAttributes;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
|
@ -49,6 +52,8 @@ public class MediaPlayerHelper {
|
||||||
private static void play(Context context, Uri uri, boolean alarm, int duration) throws IOException {
|
private static void play(Context context, Uri uri, boolean alarm, int duration) throws IOException {
|
||||||
Semaphore sem = new Semaphore(0);
|
Semaphore sem = new Semaphore(0);
|
||||||
|
|
||||||
|
Log.i("Play uri=" + uri);
|
||||||
|
|
||||||
AudioAttributes attrs = new AudioAttributes.Builder()
|
AudioAttributes attrs = new AudioAttributes.Builder()
|
||||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||||
.setUsage(alarm ? AudioAttributes.USAGE_ALARM : AudioAttributes.USAGE_NOTIFICATION)
|
.setUsage(alarm ? AudioAttributes.USAGE_ALARM : AudioAttributes.USAGE_NOTIFICATION)
|
||||||
|
@ -74,16 +79,36 @@ public class MediaPlayerHelper {
|
||||||
});
|
});
|
||||||
mediaPlayer.prepareAsync();
|
mediaPlayer.prepareAsync();
|
||||||
|
|
||||||
|
BroadcastReceiver receiver = new BroadcastReceiver() {
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
Log.i("Received " + intent);
|
||||||
|
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) ||
|
||||||
|
intent.getAction().equals(Intent.ACTION_SCREEN_ON))
|
||||||
|
sem.release();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
IntentFilter iff = new IntentFilter();
|
||||||
|
iff.addAction(Intent.ACTION_SCREEN_OFF);
|
||||||
|
iff.addAction(Intent.ACTION_SCREEN_ON);
|
||||||
|
context.registerReceiver(receiver, iff);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
boolean acquired = sem.tryAcquire(duration, TimeUnit.SECONDS);
|
boolean acquired = sem.tryAcquire(duration, TimeUnit.SECONDS);
|
||||||
EntityLog.log(context, "Alarm acquired=" + acquired);
|
EntityLog.log(context, "Alarm acquired=" + acquired +
|
||||||
if (!acquired) {
|
" playing=" + mediaPlayer.isPlaying());
|
||||||
|
if (!acquired || mediaPlayer.isPlaying()) {
|
||||||
mediaPlayer.stop();
|
mediaPlayer.stop();
|
||||||
mediaPlayer.release();
|
mediaPlayer.release();
|
||||||
}
|
}
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
Log.w(ex);
|
Log.w(ex);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
context.unregisterReceiver(receiver);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void liveInCall(Context context, LifecycleOwner owner, IInCall intf) {
|
static void liveInCall(Context context, LifecycleOwner owner, IInCall intf) {
|
||||||
|
|
Loading…
Reference in New Issue