mirror of
https://github.com/oxen-io/session-android.git
synced 2025-06-08 16:38:34 +00:00
Fix wakelock release exception.
This commit is contained in:
parent
e766b9737e
commit
b182f73415
@ -89,7 +89,7 @@ public class VoiceNotePlaybackService extends MediaBrowserServiceCompat {
|
||||
VoiceNoteMediaSourceFactory mediaSourceFactory = new VoiceNoteMediaSourceFactory(this);
|
||||
|
||||
voiceNotePlaybackPreparer = new VoiceNotePlaybackPreparer(this, player, queueDataAdapter, mediaSourceFactory);
|
||||
voiceNoteProximityManager = new VoiceNoteProximityManager(this, player);
|
||||
voiceNoteProximityManager = new VoiceNoteProximityManager(this, player, queueDataAdapter);
|
||||
|
||||
mediaSession.setPlaybackState(stateBuilder.build());
|
||||
|
||||
|
@ -8,6 +8,7 @@ import android.hardware.SensorManager;
|
||||
import android.media.AudioManager;
|
||||
import android.os.Build;
|
||||
import android.os.PowerManager;
|
||||
import android.support.v4.media.MediaDescriptionCompat;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
@ -17,6 +18,7 @@ import com.google.android.exoplayer2.SimpleExoPlayer;
|
||||
import com.google.android.exoplayer2.audio.AudioAttributes;
|
||||
import com.google.android.exoplayer2.util.Util;
|
||||
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.util.ServiceUtil;
|
||||
|
||||
@ -33,14 +35,19 @@ class VoiceNoteProximityManager implements SensorEventListener {
|
||||
private final SensorManager sensorManager;
|
||||
private final Sensor proximitySensor;
|
||||
private final PowerManager.WakeLock wakeLock;
|
||||
private final VoiceNoteQueueDataAdapter queueDataAdapter;
|
||||
|
||||
private long startTime;
|
||||
|
||||
VoiceNoteProximityManager(@NonNull Context context, @NonNull SimpleExoPlayer player) {
|
||||
VoiceNoteProximityManager(@NonNull Context context,
|
||||
@NonNull SimpleExoPlayer player,
|
||||
@NonNull VoiceNoteQueueDataAdapter queueDataAdapter)
|
||||
{
|
||||
this.player = player;
|
||||
this.audioManager = ServiceUtil.getAudioManager(context);
|
||||
this.sensorManager = ServiceUtil.getSensorManager(context);
|
||||
this.proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
|
||||
this.queueDataAdapter = queueDataAdapter;
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
this.wakeLock = ServiceUtil.getPowerManager(context).newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
|
||||
@ -81,11 +88,24 @@ class VoiceNoteProximityManager implements SensorEventListener {
|
||||
|
||||
final int currentStreamType = Util.getStreamTypeForAudioUsage(player.getAudioAttributes().usage);
|
||||
|
||||
final long threadId;
|
||||
final int windowIndex = player.getCurrentWindowIndex();
|
||||
|
||||
if (queueDataAdapter.isEmpty() || windowIndex == C.INDEX_UNSET) {
|
||||
threadId = -1;
|
||||
} else {
|
||||
MediaDescriptionCompat mediaDescriptionCompat = queueDataAdapter.getMediaDescription(windowIndex);
|
||||
|
||||
threadId = mediaDescriptionCompat.getExtras().getLong(VoiceNoteMediaDescriptionCompatFactory.EXTRA_THREAD_ID, -1);
|
||||
}
|
||||
|
||||
if (desiredStreamType == AudioManager.STREAM_VOICE_CALL &&
|
||||
desiredStreamType != currentStreamType &&
|
||||
!audioManager.isWiredHeadsetOn())
|
||||
!audioManager.isWiredHeadsetOn() &&
|
||||
threadId != -1 &&
|
||||
ApplicationDependencies.getMessageNotifier().getVisibleThread() == threadId)
|
||||
{
|
||||
if (wakeLock != null) {
|
||||
if (wakeLock != null && !wakeLock.isHeld()) {
|
||||
wakeLock.acquire(TimeUnit.MINUTES.toMillis(30));
|
||||
}
|
||||
|
||||
@ -102,7 +122,10 @@ class VoiceNoteProximityManager implements SensorEventListener {
|
||||
System.currentTimeMillis() - startTime > 500)
|
||||
{
|
||||
if (wakeLock != null) {
|
||||
if (wakeLock.isHeld()) {
|
||||
wakeLock.release();
|
||||
}
|
||||
|
||||
player.setPlayWhenReady(false);
|
||||
player.setAudioAttributes(new AudioAttributes.Builder()
|
||||
.setContentType(C.CONTENT_TYPE_MUSIC)
|
||||
|
@ -109,6 +109,11 @@ public class DefaultMessageNotifier implements MessageNotifier {
|
||||
visibleThread = threadId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getVisibleThread() {
|
||||
return visibleThread;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearVisibleThread() {
|
||||
setVisibleThread(-1);
|
||||
|
@ -12,6 +12,7 @@ import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
|
||||
|
||||
public interface MessageNotifier {
|
||||
void setVisibleThread(long threadId);
|
||||
long getVisibleThread();
|
||||
void clearVisibleThread();
|
||||
void setLastDesktopActivityTimestamp(long timestamp);
|
||||
void notifyMessageDeliveryFailed(Context context, Recipient recipient, long threadId);
|
||||
|
@ -30,6 +30,11 @@ public class OptimizedMessageNotifier implements MessageNotifier {
|
||||
wrapped.setVisibleThread(threadId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getVisibleThread() {
|
||||
return wrapped.getVisibleThread();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearVisibleThread() {
|
||||
wrapped.clearVisibleThread();
|
||||
|
Loading…
x
Reference in New Issue
Block a user