Apply proximity wake lock in locked audio recording mode.

Fixes #10098
This commit is contained in:
Alex Hart
2021-06-07 16:55:26 -03:00
committed by GitHub
parent 02f0301f25
commit 36443c59f9
2 changed files with 56 additions and 0 deletions

View File

@@ -400,6 +400,7 @@ public class ConversationActivity extends PassphraseRequiredActivity
private ConversationGroupViewModel groupViewModel;
private MentionsPickerViewModel mentionsViewModel;
private GroupCallViewModel groupCallViewModel;
private VoiceRecorderWakeLock voiceRecorderWakeLock;
private LiveRecipient recipient;
private long threadId;
@@ -432,6 +433,8 @@ public class ConversationActivity extends PassphraseRequiredActivity
return;
}
voiceRecorderWakeLock = new VoiceRecorderWakeLock(this);
new FullscreenHelper(this).showSystemUI();
ConversationIntents.Args args = ConversationIntents.Args.from(getIntent());
@@ -3033,12 +3036,14 @@ public class ConversationActivity extends PassphraseRequiredActivity
@Override
public void onRecorderLocked() {
voiceRecorderWakeLock.acquire();
updateToggleButtonState();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
@Override
public void onRecorderFinished() {
voiceRecorderWakeLock.release();
updateToggleButtonState();
Vibrator vibrator = ServiceUtil.getVibrator(this);
vibrator.vibrate(20);
@@ -3095,6 +3100,7 @@ public class ConversationActivity extends PassphraseRequiredActivity
@Override
public void onRecorderCanceled() {
voiceRecorderWakeLock.release();
updateToggleButtonState();
Vibrator vibrator = ServiceUtil.getVibrator(this);
vibrator.vibrate(50);

View File

@@ -0,0 +1,50 @@
package org.thoughtcrime.securesms.conversation
import android.os.Build
import android.os.PowerManager
import androidx.activity.ComponentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import org.thoughtcrime.securesms.util.WakeLockUtil
import java.util.concurrent.TimeUnit
/**
* Holds on to and manages a wake-lock for the device proximity sensor.
*
* This class will register itself as an observe of the given activity's lifecycle and automatically
* release the lock if it holds one in onPause
*/
class VoiceRecorderWakeLock(
private val activity: ComponentActivity
) : DefaultLifecycleObserver {
private var wakeLock: PowerManager.WakeLock? = null
init {
activity.lifecycle.addObserver(this)
}
fun acquire() {
synchronized(this) {
if (wakeLock?.isHeld == true) {
return
}
if (Build.VERSION.SDK_INT >= 21) {
wakeLock = WakeLockUtil.acquire(activity, PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TimeUnit.HOURS.toMillis(1), "voiceRecorder")
}
}
}
fun release() {
synchronized(this) {
if (wakeLock?.isHeld == true) {
wakeLock?.release()
}
}
}
override fun onPause(owner: LifecycleOwner) {
release()
}
}