Limit conversation action duration

This commit is contained in:
M66B 2021-12-05 18:59:23 +01:00
parent 8c241598ca
commit beaa70cd05
1 changed files with 36 additions and 1 deletions

View File

@ -24,12 +24,14 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.icu.text.Transliterator; import android.icu.text.Transliterator;
import android.os.Build; import android.os.Build;
import android.os.SystemClock;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.textclassifier.ConversationAction; import android.view.textclassifier.ConversationAction;
import android.view.textclassifier.ConversationActions; import android.view.textclassifier.ConversationActions;
import android.view.textclassifier.TextClassificationManager; import android.view.textclassifier.TextClassificationManager;
import android.view.textclassifier.TextClassifier; import android.view.textclassifier.TextClassifier;
import androidx.annotation.RequiresApi;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import java.time.ZoneId; import java.time.ZoneId;
@ -42,12 +44,21 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class TextHelper { public class TextHelper {
private static final int MAX_DETECT_SAMPLE_SIZE = 8192; private static final int MAX_DETECT_SAMPLE_SIZE = 8192;
private static final float MIN_DETECT_PROBABILITY = 0.80f; private static final float MIN_DETECT_PROBABILITY = 0.80f;
private static final String TRANSLITERATOR = "Any-Latin; Latin-ASCII"; private static final String TRANSLITERATOR = "Any-Latin; Latin-ASCII";
private static final int MAX_CONVERSATION_SAMPLE_SIZE = 8192; private static final int MAX_CONVERSATION_SAMPLE_SIZE = 8192;
private static final long MAX_CONVERSATION_DURATION = 5000; // milliseconds
private static final ExecutorService executor =
Helper.getBackgroundExecutor(0, "text");
static { static {
System.loadLibrary("fairemail"); System.loadLibrary("fairemail");
@ -169,7 +180,31 @@ public class TextHelper {
.setHints(hints) .setHints(hints)
.build(); .build();
return tcm.getTextClassifier().suggestConversationActions(request); Future<ConversationActions> future = executor.submit(new Callable<ConversationActions>() {
@Override
@RequiresApi(api = Build.VERSION_CODES.Q)
public ConversationActions call() throws Exception {
long start = SystemClock.elapsedRealtime();
try {
return tcm.getTextClassifier().suggestConversationActions(request);
} finally {
long elapse = SystemClock.elapsedRealtime() - start;
Log.i("Conversation actions=" + elapse + " ms");
}
}
});
try {
return future.get(MAX_CONVERSATION_DURATION, TimeUnit.MILLISECONDS);
} catch (TimeoutException ex) {
Log.e(ex);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putBoolean("conversation_actions", false);
return null;
} catch (Throwable ex) {
Log.e(ex);
return null;
}
} }
private static class DetectResult { private static class DetectResult {