Refactoring

pull/189/head
M66B 4 years ago
parent a451409196
commit f0a34f78d2

@ -27,7 +27,6 @@ import android.app.Dialog;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.Person;
import android.app.RemoteAction; import android.app.RemoteAction;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.ClipData; import android.content.ClipData;
@ -94,8 +93,6 @@ import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
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.TextClassifier;
import android.webkit.WebView; import android.webkit.WebView;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
@ -160,20 +157,16 @@ import java.text.Collator;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -2415,48 +2408,17 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
if (!conversation_actions) if (!conversation_actions)
return null; return null;
TextClassificationManager tcm = (TextClassificationManager) context.getSystemService(Context.TEXT_CLASSIFICATION_SERVICE); List<String> texts = new ArrayList<>();
if (tcm == null)
return null;
Person author = isOutgoing(message)
? ConversationActions.Message.PERSON_USER_SELF
: ConversationActions.Message.PERSON_USER_OTHERS;
ZonedDateTime dt = new Date(message.received)
.toInstant()
.atZone(ZoneId.systemDefault());
List<ConversationActions.Message> input = new ArrayList<>();
if (!TextUtils.isEmpty(message.subject)) if (!TextUtils.isEmpty(message.subject))
input.add(new ConversationActions.Message.Builder(author) texts.add(message.subject);
.setReferenceTime(dt) texts.add(document.text());
.setText(message.subject)
.build()); return TextHelper.getConversationActions(
input.add(new ConversationActions.Message.Builder(author) context,
.setReferenceTime(dt) texts.toArray(new String[0]),
.setText(document.text()) conversation_actions_replies,
.build()); isOutgoing(message),
message.received);
Set<String> excluded = new HashSet<>(Arrays.asList(
ConversationAction.TYPE_OPEN_URL,
ConversationAction.TYPE_SEND_EMAIL
));
if (!conversation_actions_replies)
excluded.add(ConversationAction.TYPE_TEXT_REPLY);
TextClassifier.EntityConfig config =
new TextClassifier.EntityConfig.Builder()
.setExcludedTypes(excluded)
.build();
List<String> hints = Collections.unmodifiableList(Arrays.asList(
ConversationActions.Request.HINT_FOR_IN_APP
));
ConversationActions.Request crequest =
new ConversationActions.Request.Builder(input)
.setTypeConfig(config)
.setHints(hints)
.build();
return tcm.getTextClassifier().suggestConversationActions(crequest);
} }
}.setCount(false).execute(context, owner, args, "message:body"); }.setCount(false).execute(context, owner, args, "message:body");
} }

@ -1,13 +1,25 @@
package eu.faircode.email; package eu.faircode.email;
import android.app.Person;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.textclassifier.ConversationAction;
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 android.view.textclassifier.TextLanguage; import android.view.textclassifier.TextLanguage;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Set;
public class TextHelper { public class TextHelper {
@ -44,12 +56,58 @@ public class TextHelper {
if (tcm == null) if (tcm == null)
return null; return null;
TextLanguage.Request trequest = new TextLanguage.Request.Builder(text).build(); TextLanguage.Request request = new TextLanguage.Request.Builder(text).build();
TextClassifier tc = tcm.getTextClassifier(); TextClassifier tc = tcm.getTextClassifier();
TextLanguage tlanguage = tc.detectLanguage(trequest); TextLanguage tlanguage = tc.detectLanguage(request);
if (tlanguage.getLocaleHypothesisCount() > 0) if (tlanguage.getLocaleHypothesisCount() > 0)
return tlanguage.getLocale(0).toLocale(); return tlanguage.getLocale(0).toLocale();
return null; return null;
} }
static ConversationActions getConversationActions(
Context context, String[] texts, boolean replies, boolean outgoing, long time) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
return null;
TextClassificationManager tcm =
(TextClassificationManager) context.getSystemService(Context.TEXT_CLASSIFICATION_SERVICE);
if (tcm == null)
return null;
Person author = outgoing
? ConversationActions.Message.PERSON_USER_SELF
: ConversationActions.Message.PERSON_USER_OTHERS;
ZonedDateTime dt = new Date(time)
.toInstant()
.atZone(ZoneId.systemDefault());
List<ConversationActions.Message> input = new ArrayList<>();
for (String text : texts)
input.add(new ConversationActions.Message.Builder(author)
.setReferenceTime(dt)
.setText(text)
.build());
Set<String> excluded = new HashSet<>(Arrays.asList(
ConversationAction.TYPE_OPEN_URL,
ConversationAction.TYPE_SEND_EMAIL
));
if (!replies)
excluded.add(ConversationAction.TYPE_TEXT_REPLY);
TextClassifier.EntityConfig config =
new TextClassifier.EntityConfig.Builder()
.setExcludedTypes(excluded)
.build();
List<String> hints = Collections.unmodifiableList(Arrays.asList(
ConversationActions.Request.HINT_FOR_IN_APP
));
ConversationActions.Request request =
new ConversationActions.Request.Builder(input)
.setTypeConfig(config)
.setHints(hints)
.build();
return tcm.getTextClassifier().suggestConversationActions(request);
}
} }

Loading…
Cancel
Save