TTS foreground service

pull/217/head
M66B 1 year ago
parent e48dbe668e
commit e1dea071b8

@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="com.android.vending.BILLING" />
@ -630,6 +631,11 @@
android:name=".ServiceUI"
android:exported="false" />
<service
android:name=".ServiceTTS"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
<service
android:name=".ServiceExternal"
android:exported="true"

@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.TURN_SCREEN_ON" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
@ -637,6 +638,11 @@
android:name=".ServiceUI"
android:exported="false" />
<service
android:name=".ServiceTTS"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
<service
android:name=".ServiceExternal"
android:exported="true"

@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
@ -636,6 +637,11 @@
android:name=".ServiceUI"
android:exported="false" />
<service
android:name=".ServiceTTS"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
<service
android:name=".ServiceExternal"
android:exported="true"

@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
@ -636,6 +637,11 @@
android:name=".ServiceUI"
android:exported="false" />
<service
android:name=".ServiceTTS"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
<service
android:name=".ServiceExternal"
android:exported="true"

@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="com.android.vending.BILLING" />
@ -631,6 +632,11 @@
android:name=".ServiceUI"
android:exported="false" />
<service
android:name=".ServiceTTS"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
<service
android:name=".ServiceExternal"
android:exported="true"

@ -1315,7 +1315,12 @@ public class EntityRule {
sb.append(context.getString(R.string.title_rule_tts_content))
.append(' ').append(preview);
TTSHelper.speak(context, "rule:" + message.id, sb.toString(), message.language, false, null);
Intent intent = new Intent(context, ServiceTTS.class);
intent.putExtra(ServiceTTS.EXTRA_FLUSH, false);
intent.putExtra(ServiceTTS.EXTRA_TEXT, sb.toString());
intent.putExtra(ServiceTTS.EXTRA_LANGUAGE, message.language);
intent.putExtra(ServiceTTS.EXTRA_UTTERANCE_ID, "rule:" + message.id);
context.startService(intent);
}
private boolean onActionSnooze(Context context, EntityMessage message, JSONObject jargs) throws JSONException {

@ -2303,7 +2303,9 @@ public class FragmentMessages extends FragmentBase
}
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
IntentFilter iff = new IntentFilter(SimpleTask.ACTION_TASK_COUNT);
IntentFilter iff = new IntentFilter();
iff.addAction(SimpleTask.ACTION_TASK_COUNT);
iff.addAction(ServiceTTS.ACTION_TTS_COMPLETED);
lbm.registerReceiver(treceiver, iff);
return view;
@ -3634,7 +3636,12 @@ public class FragmentMessages extends FragmentBase
iProperties.setValue("tts", message.id, !tts);
if (tts) {
TTSHelper.speak(getContext(), "tts:" + message.id, "", message.language, true, null);
Intent intent = new Intent(getContext(), ServiceTTS.class);
intent.putExtra(ServiceTTS.EXTRA_FLUSH, true);
intent.putExtra(ServiceTTS.EXTRA_TEXT, "");
intent.putExtra(ServiceTTS.EXTRA_LANGUAGE, message.language);
intent.putExtra(ServiceTTS.EXTRA_UTTERANCE_ID, "tts:" + message.id);
getContext().startService(intent);
return;
}
@ -3670,7 +3677,7 @@ public class FragmentMessages extends FragmentBase
String text = HtmlHelper.getFullText(context, body);
// Avoid: Not enough namespace quota ... for ...
text = HtmlHelper.truncate(text, TTSHelper.getMaxTextSize() / 3);
text = HtmlHelper.truncate(text, ServiceTTS.getMaxTextSize() / 3);
if (!TextUtils.isEmpty(text))
sb.append(context.getString(R.string.title_rule_tts_content))
@ -3681,14 +3688,15 @@ public class FragmentMessages extends FragmentBase
@Override
protected void onExecuted(Bundle args, String text) {
if (text != null)
TTSHelper.speak(getContext(), "tts:" + message.id, text, message.language, true,
new Runnable() {
@Override
public void run() {
iProperties.setValue("tts", message.id, false);
}
});
if (text == null)
return;
Intent intent = new Intent(getContext(), ServiceTTS.class);
intent.putExtra(ServiceTTS.EXTRA_FLUSH, true);
intent.putExtra(ServiceTTS.EXTRA_TEXT, text);
intent.putExtra(ServiceTTS.EXTRA_LANGUAGE, message.language);
intent.putExtra(ServiceTTS.EXTRA_UTTERANCE_ID, "tts:" + message.id);
getContext().startService(intent);
}
@Override
@ -9016,7 +9024,11 @@ public class FragmentMessages extends FragmentBase
private BroadcastReceiver treceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
onTaskCount(intent);
String action = intent.getAction();
if (SimpleTask.ACTION_TASK_COUNT.equals(action))
onTaskCount(intent);
else if (ServiceTTS.ACTION_TTS_COMPLETED.equals(action))
onTTSCompleted(intent);
}
};
@ -9039,6 +9051,18 @@ public class FragmentMessages extends FragmentBase
updateListState("Tasks", intent.getIntExtra("count", 0), adapter.getItemCount());
}
private void onTTSCompleted(Intent intent) {
String utteranceId = intent.getStringExtra(ServiceTTS.EXTRA_UTTERANCE_ID);
if (utteranceId != null && utteranceId.startsWith("tts:"))
try {
long id = Long.parseLong(utteranceId.substring("tts:".length()));
Log.i("TTS completed id=" + id);
iProperties.setValue("tts", id, false);
} catch (Throwable ex) {
Log.e(ex);
}
}
private void onStoreRaw(Intent intent) {
getArguments().putLong("selected_message", intent.getLongExtra("id", -1));
String subject = intent.getStringExtra("subject");

@ -77,6 +77,7 @@ class NotificationHelper {
static final int NOTIFICATION_EXTERNAL = 300;
static final int NOTIFICATION_UPDATE = 400;
static final int NOTIFICATION_TAGGED = 500;
static final int NOTIFICATION_TTS = 600;
private static final int MAX_NOTIFICATION_DISPLAY = 10; // per group
private static final int MAX_NOTIFICATION_COUNT = 100; // per group

@ -0,0 +1,236 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2024 by Marcel Bokhorst (M66B)
*/
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.OperationCanceledException;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class ServiceTTS extends ServiceBase {
private Integer status = null;
private TextToSpeech instance = null;
private final List<Runnable> queue = new ArrayList<>();
private final Object lock = new Object();
static final String EXTRA_FLUSH = "flush";
static final String EXTRA_TEXT = "text";
static final String EXTRA_LANGUAGE = "language";
static final String EXTRA_UTTERANCE_ID = "utterance";
static final String ACTION_TTS_COMPLETED = BuildConfig.APPLICATION_ID + ".TTS";
@Override
public void onCreate() {
Log.i("Service TTS create");
super.onCreate();
try {
startForeground(NotificationHelper.NOTIFICATION_TTS, getNotification());
} catch (Throwable ex) {
if (Helper.isPlayStoreInstall())
Log.i(ex);
else
Log.e(ex);
}
}
@Override
public void onTimeout(int startId) {
String msg = "onTimeout" +
" class=" + this.getClass().getName() +
" ignoring=" + Helper.isIgnoringOptimizations(this);
Log.e(new Throwable(msg));
EntityLog.log(this, EntityLog.Type.Debug3, msg);
stopSelf();
}
@Override
public void onDestroy() {
Log.i("Service TTS destroy");
stopForeground(true);
super.onDestroy();
CoalMine.watch(this, this.getClass().getName() + "#onDestroy");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
EntityLog.log(this, "Service TTS intent=" + intent);
Log.logExtras(intent);
super.onStartCommand(intent, flags, startId);
try {
startForeground(NotificationHelper.NOTIFICATION_TTS, getNotification());
} catch (Throwable ex) {
if (Helper.isPlayStoreInstall())
Log.i(ex);
else
Log.e(ex);
}
if (intent == null)
return START_NOT_STICKY;
onTts(intent);
return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private Notification getNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this, "service")
.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_DEFAULT)
.setSmallIcon(R.drawable.twotone_play_arrow_24)
.setContentTitle(getString(R.string.title_rule_tts))
.setContentIntent(getPendingIntent(this))
.setAutoCancel(false)
.setShowWhen(false)
.setDefaults(0) // disable sound on pre Android 8
.setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setLocalOnly(true)
.setOngoing(true);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
return notification;
}
private static PendingIntent getPendingIntent(Context context) {
Intent view = new Intent(context, ActivityView.class);
view.setAction("unified");
view.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
return PendingIntentCompat.getActivity(
context, ActivityView.PI_UNIFIED, view, PendingIntent.FLAG_UPDATE_CURRENT);
}
void onTts(Intent intent) {
final boolean flush = intent.getBooleanExtra(EXTRA_FLUSH, false);
final String text = intent.getStringExtra(EXTRA_TEXT);
final String language = intent.getStringExtra(EXTRA_LANGUAGE);
final String utteranceId = intent.getStringExtra(EXTRA_UTTERANCE_ID);
final Locale locale = (language == null ? Locale.getDefault() : new Locale(language));
final Runnable speak = new RunnableEx("tts") {
@Override
public void delegate() {
boolean available = (instance.setLanguage(locale) >= 0);
EntityLog.log(ServiceTTS.this, "TTS queued" +
" language=" + locale +
" available=" + available +
" utterance=" + utteranceId +
" text=" + text);
int error = instance.speak(text, flush ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, null, utteranceId);
if (error != TextToSpeech.SUCCESS)
throw new OperationCanceledException("TTS error=" + error);
}
};
final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(ServiceTTS.this);
synchronized (lock) {
if (status == null) {
queue.add(speak);
if (instance == null)
try {
Log.i("TTS init");
instance = new TextToSpeech(ServiceTTS.this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int initStatus) {
instance.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.i("TTS start=" + utteranceId);
}
@Override
public void onDone(String utteranceId) {
Log.i("TTS done=" + utteranceId);
report(utteranceId);
synchronized (lock) {
if (!instance.isSpeaking())
try {
Log.i("TTS shutdown");
instance.shutdown();
} catch (Throwable ex) {
Log.e(ex);
} finally {
status = null;
instance = null;
ServiceTTS.this.stopSelf();
}
}
}
@Override
public void onError(String utteranceId) {
Log.i("TTS error=" + utteranceId);
report(utteranceId);
}
private void report(String utteranceId) {
lbm.sendBroadcast(new Intent(ACTION_TTS_COMPLETED)
.putExtra(EXTRA_UTTERANCE_ID, utteranceId));
}
});
synchronized (lock) {
status = initStatus;
Log.i("TTS status=" + status + " queued=" + queue.size());
if (status == TextToSpeech.SUCCESS)
for (Runnable speak : queue)
speak.run();
queue.clear();
}
}
});
} catch (Throwable ex) {
Log.e(ex);
status = TextToSpeech.ERROR;
}
} else if (status == TextToSpeech.SUCCESS)
speak.run();
}
}
static int getMaxTextSize() {
return TextToSpeech.getMaxSpeechInputLength();
}
}

@ -1,159 +0,0 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2024 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.os.OperationCanceledException;
import android.os.PowerManager;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
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
static void speak(
@NonNull final Context context,
@NonNull final String utteranceId,
@NonNull final String text,
final String language,
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() {
@Override
public void run() {
try {
boolean available = (instance.setLanguage(locale) >= 0);
EntityLog.log(context, "TTS queued" +
" language=" + locale +
" available=" + available +
" utterance=" + utteranceId +
" text=" + text);
int error = instance.speak(text, flush ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, null, utteranceId);
if (error != TextToSpeech.SUCCESS)
throw new OperationCanceledException("TTS error=" + error);
} catch (Throwable ex) {
Log.e(ex);
}
}
};
synchronized (lock) {
if (listener != null)
listeners.put(utteranceId, listener);
if (status == null) {
queue.add(speak);
if (instance == null)
try {
Log.i("TTS init");
instance = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int initStatus) {
instance.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.i("TTS start=" + utteranceId);
}
@Override
public void onDone(String utteranceId) {
Log.i("TTS done=" + utteranceId);
report(utteranceId);
synchronized (lock) {
if (queue.isEmpty())
try {
Log.i("TTS shutdown");
instance.shutdown();
} catch (Throwable ex) {
Log.e(ex);
} finally {
status = null;
instance = null;
wl.release();
}
}
}
@Override
public void onError(String utteranceId) {
Log.i("TTS error=" + utteranceId);
report(utteranceId);
}
private void report(String utteranceId) {
synchronized (lock) {
Runnable listener = listeners.remove(utteranceId);
if (listener != null)
ApplicationEx.getMainHandler().post(listener);
}
}
});
synchronized (lock) {
status = initStatus;
boolean ok = (status == TextToSpeech.SUCCESS);
Log.i("TTS status=" + status + " ok=" + ok + " queued=" + queue.size());
if (ok)
for (Runnable speak : queue)
speak.run();
queue.clear();
}
}
});
wl.acquire(MAX_WAKELOCK_DURATION);
} catch (Throwable ex) {
Log.e(ex);
status = TextToSpeech.ERROR;
}
} else if (status == TextToSpeech.SUCCESS)
speak.run();
}
}
static int getMaxTextSize() {
return TextToSpeech.getMaxSpeechInputLength();
}
}

@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="com.android.vending.BILLING" />
@ -630,6 +631,11 @@
android:name=".ServiceUI"
android:exported="false" />
<service
android:name=".ServiceTTS"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
<service
android:name=".ServiceExternal"
android:exported="true"

Loading…
Cancel
Save