mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-28 16:55:56 +00:00
TTS: wakelock
This commit is contained in:
parent
1e157e95d6
commit
e48dbe668e
1 changed files with 11 additions and 0 deletions
|
@ -21,6 +21,7 @@ package eu.faircode.email;
|
|||
|
||||
import android.content.Context;
|
||||
import android.os.OperationCanceledException;
|
||||
import android.os.PowerManager;
|
||||
import android.speech.tts.TextToSpeech;
|
||||
import android.speech.tts.UtteranceProgressListener;
|
||||
|
||||
|
@ -35,10 +36,13 @@ import java.util.Map;
|
|||
public class TTSHelper {
|
||||
private static Integer status = null;
|
||||
private static TextToSpeech instance = null;
|
||||
private static PowerManager.WakeLock wl = null;
|
||||
private static final List<Runnable> queue = new ArrayList<>();
|
||||
private static final Map<String, Runnable> listeners = new HashMap<>();
|
||||
private static final Object lock = new Object();
|
||||
|
||||
private static final long MAX_WAKELOCK_DURATION = 3 * 60 * 1000L; // milliseconds
|
||||
|
||||
// https://developer.android.com/reference/android/speech/tts/TextToSpeech
|
||||
// https://android-developers.googleblog.com/2009/09/introduction-to-text-to-speech-in.html
|
||||
|
||||
|
@ -50,6 +54,11 @@ public class TTSHelper {
|
|||
final boolean flush,
|
||||
final Runnable listener) {
|
||||
|
||||
if (wl == null) {
|
||||
PowerManager pm = Helper.getSystemService(context, PowerManager.class);
|
||||
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":tts");
|
||||
}
|
||||
|
||||
Locale locale = (language == null ? Locale.getDefault() : new Locale(language));
|
||||
|
||||
final Runnable speak = new Runnable() {
|
||||
|
@ -103,6 +112,7 @@ public class TTSHelper {
|
|||
} finally {
|
||||
status = null;
|
||||
instance = null;
|
||||
wl.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -133,6 +143,7 @@ public class TTSHelper {
|
|||
}
|
||||
}
|
||||
});
|
||||
wl.acquire(MAX_WAKELOCK_DURATION);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
status = TextToSpeech.ERROR;
|
||||
|
|
Loading…
Reference in a new issue