From 07c8db824e87c9926797233a489763099196adcf Mon Sep 17 00:00:00 2001 From: FeuRenard Date: Sun, 26 Mar 2017 16:55:43 +0200 Subject: [PATCH] Disable screen during earpiece voice note playback Fixes #6437 Closes #6445 // FREEBIE --- .../securesms/audio/AudioSlidePlayer.java | 29 ++++++++++++++----- .../securesms/util/ServiceUtil.java | 5 ++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java b/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java index fedb44b7cc..0245ae1ba2 100644 --- a/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java +++ b/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java @@ -7,8 +7,11 @@ import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.media.AudioManager; import android.media.MediaPlayer; +import android.os.Build; import android.os.Handler; import android.os.Message; +import android.os.PowerManager; +import android.os.PowerManager.WakeLock; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; @@ -19,6 +22,7 @@ import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.attachments.AttachmentServer; import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.mms.AudioSlide; +import org.thoughtcrime.securesms.util.ServiceUtil; import org.thoughtcrime.securesms.util.Util; import org.whispersystems.libsignal.util.guava.Optional; @@ -31,13 +35,14 @@ public class AudioSlidePlayer implements SensorEventListener { private static @NonNull Optional playing = Optional.absent(); - private final @NonNull Context context; - private final @NonNull MasterSecret masterSecret; - private final @NonNull AudioSlide slide; - private final @NonNull Handler progressEventHandler; - private final @NonNull AudioManager audioManager; - private final @NonNull SensorManager sensorManager; - private final @NonNull Sensor proximitySensor; + private final @NonNull Context context; + private final @NonNull MasterSecret masterSecret; + private final @NonNull AudioSlide slide; + private final @NonNull Handler progressEventHandler; + private final @NonNull AudioManager audioManager; + private final @NonNull SensorManager sensorManager; + private final @NonNull Sensor proximitySensor; + private final @Nullable WakeLock wakeLock; private @NonNull WeakReference listener; private @Nullable MediaPlayerWrapper mediaPlayer; @@ -70,6 +75,12 @@ public class AudioSlidePlayer implements SensorEventListener { this.audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); this.sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); this.proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); + + if (Build.VERSION.SDK_INT >= 21) { + this.wakeLock = ServiceUtil.getPowerManager(context).newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG); + } else { + this.wakeLock = null; + } } public void play(final double progress) throws IOException { @@ -122,6 +133,7 @@ public class AudioSlidePlayer implements SensorEventListener { } sensorManager.unregisterListener(AudioSlidePlayer.this); + if (wakeLock != null && wakeLock.isHeld()) wakeLock.release(); } notifyOnStop(); @@ -145,6 +157,7 @@ public class AudioSlidePlayer implements SensorEventListener { } sensorManager.unregisterListener(AudioSlidePlayer.this); + if (wakeLock != null && wakeLock.isHeld()) wakeLock.release(); } notifyOnStop(); @@ -281,6 +294,7 @@ public class AudioSlidePlayer implements SensorEventListener { double duration = mediaPlayer.getDuration(); double progress = position / duration; + if (wakeLock != null) wakeLock.acquire(); stop(); try { play(progress, true); @@ -291,6 +305,7 @@ public class AudioSlidePlayer implements SensorEventListener { mediaPlayer.getAudioStreamType() != streamType && System.currentTimeMillis() - startTime > 500) { + if (wakeLock != null) wakeLock.release(); stop(); notifyOnStop(); } diff --git a/src/org/thoughtcrime/securesms/util/ServiceUtil.java b/src/org/thoughtcrime/securesms/util/ServiceUtil.java index d01a3d6ca6..ce06b8fd31 100644 --- a/src/org/thoughtcrime/securesms/util/ServiceUtil.java +++ b/src/org/thoughtcrime/securesms/util/ServiceUtil.java @@ -5,6 +5,7 @@ import android.app.NotificationManager; import android.content.Context; import android.media.AudioManager; import android.net.ConnectivityManager; +import android.os.PowerManager; import android.telephony.TelephonyManager; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; @@ -33,4 +34,8 @@ public class ServiceUtil { public static AudioManager getAudioManager(Context context) { return (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); } + + public static PowerManager getPowerManager(Context context) { + return (PowerManager)context.getSystemService(Context.POWER_SERVICE); + } }