Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view.textclassifier; |
| 18 | |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 19 | import static java.time.temporal.ChronoUnit.MILLIS; |
| 20 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Abodunrinwa Toki | 6b76675 | 2017-01-17 16:25:38 -0800 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 23 | import android.annotation.WorkerThread; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 24 | import android.app.RemoteAction; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 25 | import android.app.SearchManager; |
Abodunrinwa Toki | fafdb73 | 2017-02-02 11:07:05 +0000 | [diff] [blame] | 26 | import android.content.ComponentName; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 27 | import android.content.ContentUris; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.pm.PackageManager; |
| 31 | import android.content.pm.ResolveInfo; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 32 | import android.graphics.drawable.Icon; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 33 | import android.net.Uri; |
Jan Althaus | 05e0051 | 2018-02-02 15:28:34 +0100 | [diff] [blame] | 34 | import android.os.Bundle; |
Abodunrinwa Toki | 4cfda0b | 2017-02-28 18:56:47 +0000 | [diff] [blame] | 35 | import android.os.LocaleList; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 36 | import android.os.ParcelFileDescriptor; |
Jan Althaus | 05e0051 | 2018-02-02 15:28:34 +0100 | [diff] [blame] | 37 | import android.os.UserManager; |
Abodunrinwa Toki | 9b4c82a | 2017-02-06 20:29:36 +0000 | [diff] [blame] | 38 | import android.provider.Browser; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 39 | import android.provider.CalendarContract; |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 40 | import android.provider.ContactsContract; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 41 | |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 42 | import com.android.internal.annotations.GuardedBy; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 43 | import com.android.internal.util.Preconditions; |
| 44 | |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 45 | import java.io.File; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 46 | import java.io.FileNotFoundException; |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 47 | import java.io.IOException; |
Jan Althaus | eaff57e | 2018-02-12 12:47:27 +0100 | [diff] [blame] | 48 | import java.io.UnsupportedEncodingException; |
Jan Althaus | eaff57e | 2018-02-12 12:47:27 +0100 | [diff] [blame] | 49 | import java.net.URLEncoder; |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 50 | import java.time.Instant; |
| 51 | import java.time.ZonedDateTime; |
Abodunrinwa Toki | 6b76675 | 2017-01-17 16:25:38 -0800 | [diff] [blame] | 52 | import java.util.ArrayList; |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 53 | import java.util.Arrays; |
| 54 | import java.util.Collection; |
| 55 | import java.util.Collections; |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 56 | import java.util.HashMap; |
Abodunrinwa Toki | 6b76675 | 2017-01-17 16:25:38 -0800 | [diff] [blame] | 57 | import java.util.List; |
Abodunrinwa Toki | 9b4c82a | 2017-02-06 20:29:36 +0000 | [diff] [blame] | 58 | import java.util.Locale; |
Abodunrinwa Toki | 6b76675 | 2017-01-17 16:25:38 -0800 | [diff] [blame] | 59 | import java.util.Map; |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 60 | import java.util.Objects; |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 61 | import java.util.StringJoiner; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 62 | import java.util.concurrent.TimeUnit; |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 63 | import java.util.regex.Matcher; |
| 64 | import java.util.regex.Pattern; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Default implementation of the {@link TextClassifier} interface. |
| 68 | * |
| 69 | * <p>This class uses machine learning to recognize entities in text. |
| 70 | * Unless otherwise stated, methods of this class are blocking operations and should most |
| 71 | * likely not be called on the UI thread. |
| 72 | * |
| 73 | * @hide |
| 74 | */ |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 75 | public final class TextClassifierImpl implements TextClassifier { |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 76 | |
Abodunrinwa Toki | 692b196 | 2017-08-15 15:05:11 +0100 | [diff] [blame] | 77 | private static final String LOG_TAG = DEFAULT_LOG_TAG; |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 78 | private static final String MODEL_DIR = "/etc/textclassifier/"; |
Jan Althaus | abb4fc8 | 2018-01-31 11:51:34 +0100 | [diff] [blame] | 79 | private static final String MODEL_FILE_REGEX = "textclassifier\\.(.*)\\.model"; |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 80 | private static final String UPDATED_MODEL_FILE_PATH = |
Jan Althaus | 67d234d | 2018-01-26 17:53:31 +0100 | [diff] [blame] | 81 | "/data/misc/textclassifier/textclassifier.model"; |
Abodunrinwa Toki | b89cf02 | 2017-02-06 19:53:22 +0000 | [diff] [blame] | 82 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 83 | private final Context mContext; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 84 | private final TextClassifier mFallback; |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 85 | private final GenerateLinksLogger mGenerateLinksLogger; |
Abodunrinwa Toki | 1d77557 | 2017-05-08 16:03:01 +0100 | [diff] [blame] | 86 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 87 | private final Object mLock = new Object(); |
| 88 | @GuardedBy("mLock") // Do not access outside this lock. |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 89 | private List<ModelFile> mAllModelFiles; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 90 | @GuardedBy("mLock") // Do not access outside this lock. |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 91 | private ModelFile mModel; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 92 | @GuardedBy("mLock") // Do not access outside this lock. |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 93 | private TextClassifierImplNative mNative; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 94 | |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 95 | private final Object mLoggerLock = new Object(); |
| 96 | @GuardedBy("mLoggerLock") // Do not access outside this lock. |
Jan Althaus | 5a03094 | 2018-04-04 19:40:38 +0200 | [diff] [blame] | 97 | private SelectionSessionLogger mSessionLogger; |
Abodunrinwa Toki | 3bb4436 | 2017-12-05 07:33:41 +0000 | [diff] [blame] | 98 | |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 99 | private final TextClassificationConstants mSettings; |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 100 | |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 101 | public TextClassifierImpl(Context context, TextClassificationConstants settings) { |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 102 | mContext = Preconditions.checkNotNull(context); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 103 | mFallback = TextClassifier.NO_OP; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 104 | mSettings = Preconditions.checkNotNull(settings); |
| 105 | mGenerateLinksLogger = new GenerateLinksLogger(mSettings.getGenerateLinksLogSampleRate()); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 108 | /** @inheritDoc */ |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 109 | @Override |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 110 | @WorkerThread |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 111 | public TextSelection suggestSelection(TextSelection.Request request) { |
| 112 | Preconditions.checkNotNull(request); |
| 113 | Utils.checkMainThread(); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 114 | try { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 115 | final int rangeLength = request.getEndIndex() - request.getStartIndex(); |
| 116 | final String string = request.getText().toString(); |
| 117 | if (string.length() > 0 |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 118 | && rangeLength <= mSettings.getSuggestSelectionMaxRangeLength()) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 119 | final String localesString = concatenateLocales(request.getDefaultLocales()); |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 120 | final ZonedDateTime refTime = ZonedDateTime.now(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 121 | final TextClassifierImplNative nativeImpl = getNative(request.getDefaultLocales()); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 122 | final int start; |
| 123 | final int end; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 124 | if (mSettings.isModelDarkLaunchEnabled() && !request.isDarkLaunchAllowed()) { |
| 125 | start = request.getStartIndex(); |
| 126 | end = request.getEndIndex(); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 127 | } else { |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 128 | final int[] startEnd = nativeImpl.suggestSelection( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 129 | string, request.getStartIndex(), request.getEndIndex(), |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 130 | new TextClassifierImplNative.SelectionOptions(localesString)); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 131 | start = startEnd[0]; |
| 132 | end = startEnd[1]; |
| 133 | } |
Jan Althaus | b7f7d36 | 2018-01-26 13:51:31 +0100 | [diff] [blame] | 134 | if (start < end |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 135 | && start >= 0 && end <= string.length() |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 136 | && start <= request.getStartIndex() && end >= request.getEndIndex()) { |
Abodunrinwa Toki | 692b196 | 2017-08-15 15:05:11 +0100 | [diff] [blame] | 137 | final TextSelection.Builder tsBuilder = new TextSelection.Builder(start, end); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 138 | final TextClassifierImplNative.ClassificationResult[] results = |
| 139 | nativeImpl.classifyText( |
Abodunrinwa Toki | d2d1399 | 2017-03-24 21:43:13 +0000 | [diff] [blame] | 140 | string, start, end, |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 141 | new TextClassifierImplNative.ClassificationOptions( |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 142 | refTime.toInstant().toEpochMilli(), |
| 143 | refTime.getZone().getId(), |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 144 | localesString)); |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 145 | final int size = results.length; |
| 146 | for (int i = 0; i < size; i++) { |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 147 | tsBuilder.setEntityType(results[i].getCollection(), results[i].getScore()); |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 148 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 149 | return tsBuilder.setId(createId( |
| 150 | string, request.getStartIndex(), request.getEndIndex())) |
Abodunrinwa Toki | 692b196 | 2017-08-15 15:05:11 +0100 | [diff] [blame] | 151 | .build(); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 152 | } else { |
| 153 | // We can not trust the result. Log the issue and ignore the result. |
| 154 | Log.d(LOG_TAG, "Got bad indices for input text. Ignoring result."); |
| 155 | } |
| 156 | } |
| 157 | } catch (Throwable t) { |
| 158 | // Avoid throwing from this method. Log the error. |
| 159 | Log.e(LOG_TAG, |
| 160 | "Error suggesting selection for text. No changes to selection suggested.", |
| 161 | t); |
| 162 | } |
| 163 | // Getting here means something went wrong, return a NO_OP result. |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 164 | return mFallback.suggestSelection(request); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 167 | /** @inheritDoc */ |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 168 | @Override |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 169 | @WorkerThread |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 170 | public TextClassification classifyText(TextClassification.Request request) { |
| 171 | Preconditions.checkNotNull(request); |
| 172 | Utils.checkMainThread(); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 173 | try { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 174 | final int rangeLength = request.getEndIndex() - request.getStartIndex(); |
| 175 | final String string = request.getText().toString(); |
| 176 | if (string.length() > 0 && rangeLength <= mSettings.getClassifyTextMaxRangeLength()) { |
| 177 | final String localesString = concatenateLocales(request.getDefaultLocales()); |
| 178 | final ZonedDateTime refTime = request.getReferenceTime() != null |
| 179 | ? request.getReferenceTime() : ZonedDateTime.now(); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 180 | final TextClassifierImplNative.ClassificationResult[] results = |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 181 | getNative(request.getDefaultLocales()) |
| 182 | .classifyText( |
| 183 | string, request.getStartIndex(), request.getEndIndex(), |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 184 | new TextClassifierImplNative.ClassificationOptions( |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 185 | refTime.toInstant().toEpochMilli(), |
| 186 | refTime.getZone().getId(), |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 187 | localesString)); |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 188 | if (results.length > 0) { |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 189 | return createClassificationResult( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 190 | results, string, |
| 191 | request.getStartIndex(), request.getEndIndex(), refTime.toInstant()); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } catch (Throwable t) { |
| 195 | // Avoid throwing from this method. Log the error. |
Abodunrinwa Toki | 7861885 | 2017-10-17 15:31:39 +0100 | [diff] [blame] | 196 | Log.e(LOG_TAG, "Error getting text classification info.", t); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 197 | } |
| 198 | // Getting here means something went wrong, return a NO_OP result. |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 199 | return mFallback.classifyText(request); |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 202 | /** @inheritDoc */ |
Abodunrinwa Toki | 2b6020f | 2017-10-28 02:28:45 +0100 | [diff] [blame] | 203 | @Override |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 204 | @WorkerThread |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 205 | public TextLinks generateLinks(@NonNull TextLinks.Request request) { |
| 206 | Preconditions.checkNotNull(request); |
| 207 | Utils.checkTextLength(request.getText(), getMaxGenerateLinksTextLength()); |
| 208 | Utils.checkMainThread(); |
Abodunrinwa Toki | 6563833 | 2018-03-16 21:08:50 +0000 | [diff] [blame] | 209 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 210 | if (!mSettings.isSmartLinkifyEnabled() && request.isLegacyFallback()) { |
| 211 | return Utils.generateLegacyLinks(request); |
Abodunrinwa Toki | 6563833 | 2018-03-16 21:08:50 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 214 | final String textString = request.getText().toString(); |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 215 | final TextLinks.Builder builder = new TextLinks.Builder(textString); |
Richard Ledley | 9cfa606 | 2018-01-15 13:13:29 +0000 | [diff] [blame] | 216 | |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 217 | try { |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 218 | final long startTimeMs = System.currentTimeMillis(); |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 219 | final ZonedDateTime refTime = ZonedDateTime.now(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 220 | final Collection<String> entitiesToIdentify = request.getEntityConfig() != null |
| 221 | ? request.getEntityConfig().resolveEntityListModifications( |
| 222 | getEntitiesForHints(request.getEntityConfig().getHints())) |
| 223 | : mSettings.getEntityListDefault(); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 224 | final TextClassifierImplNative nativeImpl = |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 225 | getNative(request.getDefaultLocales()); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 226 | final TextClassifierImplNative.AnnotatedSpan[] annotations = |
| 227 | nativeImpl.annotate( |
| 228 | textString, |
| 229 | new TextClassifierImplNative.AnnotationOptions( |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 230 | refTime.toInstant().toEpochMilli(), |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 231 | refTime.getZone().getId(), |
| 232 | concatenateLocales(request.getDefaultLocales()))); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 233 | for (TextClassifierImplNative.AnnotatedSpan span : annotations) { |
| 234 | final TextClassifierImplNative.ClassificationResult[] results = |
| 235 | span.getClassification(); |
| 236 | if (results.length == 0 |
| 237 | || !entitiesToIdentify.contains(results[0].getCollection())) { |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 238 | continue; |
| 239 | } |
| 240 | final Map<String, Float> entityScores = new HashMap<>(); |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 241 | for (int i = 0; i < results.length; i++) { |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 242 | entityScores.put(results[i].getCollection(), results[i].getScore()); |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 243 | } |
Abodunrinwa Toki | fe20cdd | 2017-12-12 02:31:25 +0000 | [diff] [blame] | 244 | builder.addLink(span.getStartIndex(), span.getEndIndex(), entityScores); |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 245 | } |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 246 | final TextLinks links = builder.build(); |
| 247 | final long endTimeMs = System.currentTimeMillis(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 248 | final String callingPackageName = request.getCallingPackageName() == null |
| 249 | ? mContext.getPackageName() // local (in process) TC. |
| 250 | : request.getCallingPackageName(); |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 251 | mGenerateLinksLogger.logGenerateLinks( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 252 | request.getText(), links, callingPackageName, endTimeMs - startTimeMs); |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 253 | return links; |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 254 | } catch (Throwable t) { |
| 255 | // Avoid throwing from this method. Log the error. |
| 256 | Log.e(LOG_TAG, "Error getting links info.", t); |
| 257 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 258 | return mFallback.generateLinks(request); |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 259 | } |
| 260 | |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 261 | /** @inheritDoc */ |
| 262 | @Override |
| 263 | public int getMaxGenerateLinksTextLength() { |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 264 | return mSettings.getGenerateLinksMaxTextLength(); |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Richard Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 267 | private Collection<String> getEntitiesForHints(Collection<String> hints) { |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 268 | final boolean editable = hints.contains(HINT_TEXT_IS_EDITABLE); |
| 269 | final boolean notEditable = hints.contains(HINT_TEXT_IS_NOT_EDITABLE); |
| 270 | |
| 271 | // Use the default if there is no hint, or conflicting ones. |
| 272 | final boolean useDefault = editable == notEditable; |
| 273 | if (useDefault) { |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 274 | return mSettings.getEntityListDefault(); |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 275 | } else if (editable) { |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 276 | return mSettings.getEntityListEditable(); |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 277 | } else { // notEditable |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 278 | return mSettings.getEntityListNotEditable(); |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 279 | } |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 282 | @Override |
| 283 | public void onSelectionEvent(SelectionEvent event) { |
| 284 | Preconditions.checkNotNull(event); |
| 285 | synchronized (mLoggerLock) { |
Jan Althaus | 5a03094 | 2018-04-04 19:40:38 +0200 | [diff] [blame] | 286 | if (mSessionLogger == null) { |
| 287 | mSessionLogger = new SelectionSessionLogger(); |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 288 | } |
Jan Althaus | 5a03094 | 2018-04-04 19:40:38 +0200 | [diff] [blame] | 289 | mSessionLogger.writeEvent(event); |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 293 | private TextClassifierImplNative getNative(LocaleList localeList) |
| 294 | throws FileNotFoundException { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 295 | synchronized (mLock) { |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 296 | localeList = localeList == null ? LocaleList.getEmptyLocaleList() : localeList; |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 297 | final ModelFile bestModel = findBestModelLocked(localeList); |
| 298 | if (bestModel == null) { |
| 299 | throw new FileNotFoundException("No model for " + localeList.toLanguageTags()); |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 300 | } |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 301 | if (mNative == null || !Objects.equals(mModel, bestModel)) { |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 302 | Log.d(DEFAULT_LOG_TAG, "Loading " + bestModel); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 303 | destroyNativeIfExistsLocked(); |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 304 | final ParcelFileDescriptor fd = ParcelFileDescriptor.open( |
| 305 | new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 306 | mNative = new TextClassifierImplNative(fd.getFd()); |
Abodunrinwa Toki | 6ace893 | 2017-04-28 19:25:24 +0100 | [diff] [blame] | 307 | closeAndLogError(fd); |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 308 | mModel = bestModel; |
Abodunrinwa Toki | b89cf02 | 2017-02-06 19:53:22 +0000 | [diff] [blame] | 309 | } |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 310 | return mNative; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 311 | } |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 314 | private String createId(String text, int start, int end) { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 315 | synchronized (mLock) { |
Jan Althaus | 5a03094 | 2018-04-04 19:40:38 +0200 | [diff] [blame] | 316 | return SelectionSessionLogger.createId(text, start, end, mContext, mModel.getVersion(), |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 317 | mModel.getSupportedLocales()); |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 321 | @GuardedBy("mLock") // Do not call outside this lock. |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 322 | private void destroyNativeIfExistsLocked() { |
| 323 | if (mNative != null) { |
| 324 | mNative.close(); |
| 325 | mNative = null; |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 329 | private static String concatenateLocales(@Nullable LocaleList locales) { |
| 330 | return (locales == null) ? "" : locales.toLanguageTags(); |
| 331 | } |
| 332 | |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 333 | /** |
| 334 | * Finds the most appropriate model to use for the given target locale list. |
| 335 | * |
| 336 | * The basic logic is: we ignore all models that don't support any of the target locales. For |
| 337 | * the remaining candidates, we take the update model unless its version number is lower than |
| 338 | * the factory version. It's assumed that factory models do not have overlapping locale ranges |
| 339 | * and conflict resolution between these models hence doesn't matter. |
| 340 | */ |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 341 | @GuardedBy("mLock") // Do not call outside this lock. |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 342 | @Nullable |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 343 | private ModelFile findBestModelLocked(LocaleList localeList) { |
Abodunrinwa Toki | a2df6e5 | 2017-04-13 09:56:52 +0100 | [diff] [blame] | 344 | // Specified localeList takes priority over the system default, so it is listed first. |
| 345 | final String languages = localeList.isEmpty() |
| 346 | ? LocaleList.getDefault().toLanguageTags() |
| 347 | : localeList.toLanguageTags() + "," + LocaleList.getDefault().toLanguageTags(); |
| 348 | final List<Locale.LanguageRange> languageRangeList = Locale.LanguageRange.parse(languages); |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 349 | |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 350 | ModelFile bestModel = null; |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 351 | for (ModelFile model : listAllModelsLocked()) { |
| 352 | if (model.isAnyLanguageSupported(languageRangeList)) { |
Lukas Zilka | 0fcacdd | 2018-03-14 11:06:26 +0100 | [diff] [blame] | 353 | if (model.isPreferredTo(bestModel)) { |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 354 | bestModel = model; |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 355 | } |
| 356 | } |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 357 | } |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 358 | return bestModel; |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 361 | /** Returns a list of all model files available, in order of precedence. */ |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 362 | @GuardedBy("mLock") // Do not call outside this lock. |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 363 | private List<ModelFile> listAllModelsLocked() { |
| 364 | if (mAllModelFiles == null) { |
| 365 | final List<ModelFile> allModels = new ArrayList<>(); |
| 366 | // The update model has the highest precedence. |
| 367 | if (new File(UPDATED_MODEL_FILE_PATH).exists()) { |
| 368 | final ModelFile updatedModel = ModelFile.fromPath(UPDATED_MODEL_FILE_PATH); |
| 369 | if (updatedModel != null) { |
| 370 | allModels.add(updatedModel); |
| 371 | } |
| 372 | } |
| 373 | // Factory models should never have overlapping locales, so the order doesn't matter. |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 374 | final File modelsDir = new File(MODEL_DIR); |
| 375 | if (modelsDir.exists() && modelsDir.isDirectory()) { |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 376 | final File[] modelFiles = modelsDir.listFiles(); |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 377 | final Pattern modelFilenamePattern = Pattern.compile(MODEL_FILE_REGEX); |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 378 | for (File modelFile : modelFiles) { |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 379 | final Matcher matcher = modelFilenamePattern.matcher(modelFile.getName()); |
| 380 | if (matcher.matches() && modelFile.isFile()) { |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 381 | final ModelFile model = ModelFile.fromPath(modelFile.getAbsolutePath()); |
| 382 | if (model != null) { |
| 383 | allModels.add(model); |
| 384 | } |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 388 | mAllModelFiles = allModels; |
Abodunrinwa Toki | c39006a1 | 2017-03-29 01:25:23 +0100 | [diff] [blame] | 389 | } |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 390 | return mAllModelFiles; |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 393 | private TextClassification createClassificationResult( |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 394 | TextClassifierImplNative.ClassificationResult[] classifications, |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 395 | String text, int start, int end, @Nullable Instant referenceTime) { |
Abodunrinwa Toki | 008f387 | 2017-11-27 19:32:35 +0000 | [diff] [blame] | 396 | final String classifiedText = text.substring(start, end); |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 397 | final TextClassification.Builder builder = new TextClassification.Builder() |
Abodunrinwa Toki | 008f387 | 2017-11-27 19:32:35 +0000 | [diff] [blame] | 398 | .setText(classifiedText); |
Abodunrinwa Toki | 9b4c82a | 2017-02-06 20:29:36 +0000 | [diff] [blame] | 399 | |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 400 | final int size = classifications.length; |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 401 | TextClassifierImplNative.ClassificationResult highestScoringResult = null; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 402 | float highestScore = Float.MIN_VALUE; |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 403 | for (int i = 0; i < size; i++) { |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 404 | builder.setEntityType(classifications[i].getCollection(), |
| 405 | classifications[i].getScore()); |
| 406 | if (classifications[i].getScore() > highestScore) { |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 407 | highestScoringResult = classifications[i]; |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 408 | highestScore = classifications[i].getScore(); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 409 | } |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 412 | boolean isPrimaryAction = true; |
| 413 | for (LabeledIntent labeledIntent : IntentFactory.create( |
| 414 | mContext, referenceTime, highestScoringResult, classifiedText)) { |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 415 | final RemoteAction action = labeledIntent.asRemoteAction(mContext); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 416 | if (isPrimaryAction) { |
| 417 | // For O backwards compatibility, the first RemoteAction is also written to the |
| 418 | // legacy API fields. |
| 419 | builder.setIcon(action.getIcon().loadDrawable(mContext)); |
| 420 | builder.setLabel(action.getTitle().toString()); |
| 421 | builder.setIntent(labeledIntent.getIntent()); |
| 422 | builder.setOnClickListener(TextClassification.createIntentOnClickListener( |
| 423 | TextClassification.createPendingIntent(mContext, |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 424 | labeledIntent.getIntent(), labeledIntent.getRequestCode()))); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 425 | isPrimaryAction = false; |
| 426 | } |
| 427 | builder.addAction(action); |
| 428 | } |
Abodunrinwa Toki | 54486c1 | 2017-04-19 21:02:36 +0100 | [diff] [blame] | 429 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 430 | return builder.setId(createId(text, start, end)).build(); |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 433 | /** |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 434 | * Closes the ParcelFileDescriptor and logs any errors that occur. |
| 435 | */ |
| 436 | private static void closeAndLogError(ParcelFileDescriptor fd) { |
| 437 | try { |
| 438 | fd.close(); |
| 439 | } catch (IOException e) { |
| 440 | Log.e(LOG_TAG, "Error closing file.", e); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 445 | * Describes TextClassifier model files on disk. |
| 446 | */ |
| 447 | private static final class ModelFile { |
| 448 | |
| 449 | private final String mPath; |
| 450 | private final String mName; |
| 451 | private final int mVersion; |
| 452 | private final List<Locale> mSupportedLocales; |
Lukas Zilka | 0fcacdd | 2018-03-14 11:06:26 +0100 | [diff] [blame] | 453 | private final boolean mLanguageIndependent; |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 454 | |
| 455 | /** Returns null if the path did not point to a compatible model. */ |
| 456 | static @Nullable ModelFile fromPath(String path) { |
| 457 | final File file = new File(path); |
| 458 | try { |
| 459 | final ParcelFileDescriptor modelFd = ParcelFileDescriptor.open( |
| 460 | file, ParcelFileDescriptor.MODE_READ_ONLY); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 461 | final int version = TextClassifierImplNative.getVersion(modelFd.getFd()); |
| 462 | final String supportedLocalesStr = |
| 463 | TextClassifierImplNative.getLocales(modelFd.getFd()); |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 464 | if (supportedLocalesStr.isEmpty()) { |
| 465 | Log.d(DEFAULT_LOG_TAG, "Ignoring " + file.getAbsolutePath()); |
| 466 | return null; |
| 467 | } |
Lukas Zilka | 0fcacdd | 2018-03-14 11:06:26 +0100 | [diff] [blame] | 468 | final boolean languageIndependent = supportedLocalesStr.equals("*"); |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 469 | final List<Locale> supportedLocales = new ArrayList<>(); |
| 470 | for (String langTag : supportedLocalesStr.split(",")) { |
| 471 | supportedLocales.add(Locale.forLanguageTag(langTag)); |
| 472 | } |
| 473 | closeAndLogError(modelFd); |
Lukas Zilka | 0fcacdd | 2018-03-14 11:06:26 +0100 | [diff] [blame] | 474 | return new ModelFile(path, file.getName(), version, supportedLocales, |
| 475 | languageIndependent); |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 476 | } catch (FileNotFoundException e) { |
| 477 | Log.e(DEFAULT_LOG_TAG, "Failed to peek " + file.getAbsolutePath(), e); |
| 478 | return null; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /** The absolute path to the model file. */ |
| 483 | String getPath() { |
| 484 | return mPath; |
| 485 | } |
| 486 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 487 | /** A name to use for id generation. Effectively the name of the model file. */ |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 488 | String getName() { |
| 489 | return mName; |
| 490 | } |
| 491 | |
| 492 | /** Returns the version tag in the model's metadata. */ |
| 493 | int getVersion() { |
| 494 | return mVersion; |
| 495 | } |
| 496 | |
| 497 | /** Returns whether the language supports any language in the given ranges. */ |
| 498 | boolean isAnyLanguageSupported(List<Locale.LanguageRange> languageRanges) { |
Lukas Zilka | 0fcacdd | 2018-03-14 11:06:26 +0100 | [diff] [blame] | 499 | return mLanguageIndependent || Locale.lookup(languageRanges, mSupportedLocales) != null; |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /** All locales supported by the model. */ |
| 503 | List<Locale> getSupportedLocales() { |
| 504 | return Collections.unmodifiableList(mSupportedLocales); |
| 505 | } |
| 506 | |
Lukas Zilka | 0fcacdd | 2018-03-14 11:06:26 +0100 | [diff] [blame] | 507 | public boolean isPreferredTo(ModelFile model) { |
| 508 | // A model is preferred to no model. |
| 509 | if (model == null) { |
| 510 | return true; |
| 511 | } |
| 512 | |
| 513 | // A language-specific model is preferred to a language independent |
| 514 | // model. |
| 515 | if (!mLanguageIndependent && model.mLanguageIndependent) { |
| 516 | return true; |
| 517 | } |
| 518 | |
| 519 | // A higher-version model is preferred. |
| 520 | if (getVersion() > model.getVersion()) { |
| 521 | return true; |
| 522 | } |
| 523 | return false; |
| 524 | } |
| 525 | |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 526 | @Override |
| 527 | public boolean equals(Object other) { |
| 528 | if (this == other) { |
| 529 | return true; |
| 530 | } else if (other == null || !ModelFile.class.isAssignableFrom(other.getClass())) { |
| 531 | return false; |
| 532 | } else { |
| 533 | final ModelFile otherModel = (ModelFile) other; |
| 534 | return mPath.equals(otherModel.mPath); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | @Override |
| 539 | public String toString() { |
| 540 | final StringJoiner localesJoiner = new StringJoiner(","); |
| 541 | for (Locale locale : mSupportedLocales) { |
| 542 | localesJoiner.add(locale.toLanguageTag()); |
| 543 | } |
| 544 | return String.format(Locale.US, "ModelFile { path=%s name=%s version=%d locales=%s }", |
| 545 | mPath, mName, mVersion, localesJoiner.toString()); |
| 546 | } |
| 547 | |
Lukas Zilka | 0fcacdd | 2018-03-14 11:06:26 +0100 | [diff] [blame] | 548 | private ModelFile(String path, String name, int version, List<Locale> supportedLocales, |
| 549 | boolean languageIndependent) { |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 550 | mPath = path; |
| 551 | mName = name; |
| 552 | mVersion = version; |
| 553 | mSupportedLocales = supportedLocales; |
Lukas Zilka | 0fcacdd | 2018-03-14 11:06:26 +0100 | [diff] [blame] | 554 | mLanguageIndependent = languageIndependent; |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
| 558 | /** |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 559 | * Helper class to store the information from which RemoteActions are built. |
| 560 | */ |
| 561 | private static final class LabeledIntent { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 562 | |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 563 | static final int DEFAULT_REQUEST_CODE = 0; |
| 564 | |
| 565 | private final String mTitle; |
| 566 | private final String mDescription; |
| 567 | private final Intent mIntent; |
| 568 | private final int mRequestCode; |
| 569 | |
| 570 | /** |
| 571 | * Initializes a LabeledIntent. |
| 572 | * |
| 573 | * <p>NOTE: {@code reqestCode} is required to not be {@link #DEFAULT_REQUEST_CODE} |
| 574 | * if distinguishing info (e.g. the classified text) is represented in intent extras only. |
| 575 | * In such circumstances, the request code should represent the distinguishing info |
| 576 | * (e.g. by generating a hashcode) so that the generated PendingIntent is (somewhat) |
| 577 | * unique. To be correct, the PendingIntent should be definitely unique but we try a |
| 578 | * best effort approach that avoids spamming the system with PendingIntents. |
| 579 | */ |
| 580 | // TODO: Fix the issue mentioned above so the behaviour is correct. |
| 581 | LabeledIntent(String title, String description, Intent intent, int requestCode) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 582 | mTitle = title; |
| 583 | mDescription = description; |
| 584 | mIntent = intent; |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 585 | mRequestCode = requestCode; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | String getTitle() { |
| 589 | return mTitle; |
| 590 | } |
| 591 | |
| 592 | String getDescription() { |
| 593 | return mDescription; |
| 594 | } |
| 595 | |
| 596 | Intent getIntent() { |
| 597 | return mIntent; |
| 598 | } |
| 599 | |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 600 | int getRequestCode() { |
| 601 | return mRequestCode; |
| 602 | } |
| 603 | |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 604 | RemoteAction asRemoteAction(Context context) { |
| 605 | final PackageManager pm = context.getPackageManager(); |
| 606 | final ResolveInfo resolveInfo = pm.resolveActivity(mIntent, 0); |
| 607 | final String packageName = resolveInfo != null && resolveInfo.activityInfo != null |
| 608 | ? resolveInfo.activityInfo.packageName : null; |
| 609 | Icon icon = null; |
| 610 | boolean shouldShowIcon = false; |
| 611 | if (packageName != null && !"android".equals(packageName)) { |
| 612 | // There is a default activity handling the intent. |
| 613 | mIntent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name)); |
| 614 | if (resolveInfo.activityInfo.getIconResource() != 0) { |
| 615 | icon = Icon.createWithResource( |
| 616 | packageName, resolveInfo.activityInfo.getIconResource()); |
| 617 | shouldShowIcon = true; |
| 618 | } |
| 619 | } |
| 620 | if (icon == null) { |
| 621 | // RemoteAction requires that there be an icon. |
| 622 | icon = Icon.createWithResource("android", |
| 623 | com.android.internal.R.drawable.ic_more_items); |
| 624 | } |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 625 | final RemoteAction action = new RemoteAction(icon, mTitle, mDescription, |
| 626 | TextClassification.createPendingIntent(context, mIntent, mRequestCode)); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 627 | action.setShouldShowIcon(shouldShowIcon); |
| 628 | return action; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | /** |
Abodunrinwa Toki | 6b76675 | 2017-01-17 16:25:38 -0800 | [diff] [blame] | 633 | * Creates intents based on the classification type. |
| 634 | */ |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 635 | static final class IntentFactory { |
| 636 | |
| 637 | private static final long MIN_EVENT_FUTURE_MILLIS = TimeUnit.MINUTES.toMillis(5); |
| 638 | private static final long DEFAULT_EVENT_DURATION = TimeUnit.HOURS.toMillis(1); |
Abodunrinwa Toki | 6b76675 | 2017-01-17 16:25:38 -0800 | [diff] [blame] | 639 | |
| 640 | private IntentFactory() {} |
| 641 | |
Jan Althaus | 92d7683 | 2017-09-27 18:14:35 +0200 | [diff] [blame] | 642 | @NonNull |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 643 | public static List<LabeledIntent> create( |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 644 | Context context, |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 645 | @Nullable Instant referenceTime, |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 646 | TextClassifierImplNative.ClassificationResult classification, |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 647 | String text) { |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 648 | final String type = classification.getCollection().trim().toLowerCase(Locale.ENGLISH); |
Abodunrinwa Toki | 70d41cd | 2017-05-02 21:43:41 +0100 | [diff] [blame] | 649 | text = text.trim(); |
Abodunrinwa Toki | 6b76675 | 2017-01-17 16:25:38 -0800 | [diff] [blame] | 650 | switch (type) { |
| 651 | case TextClassifier.TYPE_EMAIL: |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 652 | return createForEmail(context, text); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 653 | case TextClassifier.TYPE_PHONE: |
Jan Althaus | 05e0051 | 2018-02-02 15:28:34 +0100 | [diff] [blame] | 654 | return createForPhone(context, text); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 655 | case TextClassifier.TYPE_ADDRESS: |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 656 | return createForAddress(context, text); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 657 | case TextClassifier.TYPE_URL: |
| 658 | return createForUrl(context, text); |
| 659 | case TextClassifier.TYPE_DATE: |
| 660 | case TextClassifier.TYPE_DATE_TIME: |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 661 | if (classification.getDatetimeResult() != null) { |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 662 | final Instant parsedTime = Instant.ofEpochMilli( |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 663 | classification.getDatetimeResult().getTimeMsUtc()); |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 664 | return createForDatetime(context, type, referenceTime, parsedTime); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 665 | } else { |
| 666 | return new ArrayList<>(); |
| 667 | } |
| 668 | case TextClassifier.TYPE_FLIGHT_NUMBER: |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 669 | return createForFlight(context, text); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 670 | default: |
| 671 | return new ArrayList<>(); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | @NonNull |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 676 | private static List<LabeledIntent> createForEmail(Context context, String text) { |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 677 | return Arrays.asList( |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 678 | new LabeledIntent( |
| 679 | context.getString(com.android.internal.R.string.email), |
| 680 | context.getString(com.android.internal.R.string.email_desc), |
| 681 | new Intent(Intent.ACTION_SENDTO) |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 682 | .setData(Uri.parse(String.format("mailto:%s", text))), |
| 683 | LabeledIntent.DEFAULT_REQUEST_CODE), |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 684 | new LabeledIntent( |
| 685 | context.getString(com.android.internal.R.string.add_contact), |
| 686 | context.getString(com.android.internal.R.string.add_contact_desc), |
| 687 | new Intent(Intent.ACTION_INSERT_OR_EDIT) |
| 688 | .setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE) |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 689 | .putExtra(ContactsContract.Intents.Insert.EMAIL, text), |
| 690 | text.hashCode())); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | @NonNull |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 694 | private static List<LabeledIntent> createForPhone(Context context, String text) { |
| 695 | final List<LabeledIntent> actions = new ArrayList<>(); |
Jan Althaus | 05e0051 | 2018-02-02 15:28:34 +0100 | [diff] [blame] | 696 | final UserManager userManager = context.getSystemService(UserManager.class); |
| 697 | final Bundle userRestrictions = userManager != null |
| 698 | ? userManager.getUserRestrictions() : new Bundle(); |
| 699 | if (!userRestrictions.getBoolean(UserManager.DISALLOW_OUTGOING_CALLS, false)) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 700 | actions.add(new LabeledIntent( |
| 701 | context.getString(com.android.internal.R.string.dial), |
| 702 | context.getString(com.android.internal.R.string.dial_desc), |
| 703 | new Intent(Intent.ACTION_DIAL).setData( |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 704 | Uri.parse(String.format("tel:%s", text))), |
| 705 | LabeledIntent.DEFAULT_REQUEST_CODE)); |
Jan Althaus | 05e0051 | 2018-02-02 15:28:34 +0100 | [diff] [blame] | 706 | } |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 707 | actions.add(new LabeledIntent( |
| 708 | context.getString(com.android.internal.R.string.add_contact), |
| 709 | context.getString(com.android.internal.R.string.add_contact_desc), |
| 710 | new Intent(Intent.ACTION_INSERT_OR_EDIT) |
| 711 | .setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE) |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 712 | .putExtra(ContactsContract.Intents.Insert.PHONE, text), |
| 713 | text.hashCode())); |
Jan Althaus | 05e0051 | 2018-02-02 15:28:34 +0100 | [diff] [blame] | 714 | if (!userRestrictions.getBoolean(UserManager.DISALLOW_SMS, false)) { |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 715 | actions.add(new LabeledIntent( |
| 716 | context.getString(com.android.internal.R.string.sms), |
| 717 | context.getString(com.android.internal.R.string.sms_desc), |
| 718 | new Intent(Intent.ACTION_SENDTO) |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 719 | .setData(Uri.parse(String.format("smsto:%s", text))), |
| 720 | LabeledIntent.DEFAULT_REQUEST_CODE)); |
Jan Althaus | 05e0051 | 2018-02-02 15:28:34 +0100 | [diff] [blame] | 721 | } |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 722 | return actions; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | @NonNull |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 726 | private static List<LabeledIntent> createForAddress(Context context, String text) { |
| 727 | final List<LabeledIntent> actions = new ArrayList<>(); |
Jan Althaus | eaff57e | 2018-02-12 12:47:27 +0100 | [diff] [blame] | 728 | try { |
| 729 | final String encText = URLEncoder.encode(text, "UTF-8"); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 730 | actions.add(new LabeledIntent( |
| 731 | context.getString(com.android.internal.R.string.map), |
| 732 | context.getString(com.android.internal.R.string.map_desc), |
| 733 | new Intent(Intent.ACTION_VIEW) |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 734 | .setData(Uri.parse(String.format("geo:0,0?q=%s", encText))), |
| 735 | LabeledIntent.DEFAULT_REQUEST_CODE)); |
Jan Althaus | eaff57e | 2018-02-12 12:47:27 +0100 | [diff] [blame] | 736 | } catch (UnsupportedEncodingException e) { |
| 737 | Log.e(LOG_TAG, "Could not encode address", e); |
| 738 | } |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 739 | return actions; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | @NonNull |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 743 | private static List<LabeledIntent> createForUrl(Context context, String text) { |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 744 | final String httpPrefix = "http://"; |
| 745 | final String httpsPrefix = "https://"; |
| 746 | if (text.toLowerCase().startsWith(httpPrefix)) { |
| 747 | text = httpPrefix + text.substring(httpPrefix.length()); |
| 748 | } else if (text.toLowerCase().startsWith(httpsPrefix)) { |
| 749 | text = httpsPrefix + text.substring(httpsPrefix.length()); |
| 750 | } else { |
| 751 | text = httpPrefix + text; |
| 752 | } |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 753 | return Arrays.asList(new LabeledIntent( |
| 754 | context.getString(com.android.internal.R.string.browse), |
| 755 | context.getString(com.android.internal.R.string.browse_desc), |
| 756 | new Intent(Intent.ACTION_VIEW, Uri.parse(text)) |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 757 | .putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()), |
| 758 | LabeledIntent.DEFAULT_REQUEST_CODE)); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | @NonNull |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 762 | private static List<LabeledIntent> createForDatetime( |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 763 | Context context, String type, @Nullable Instant referenceTime, |
| 764 | Instant parsedTime) { |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 765 | if (referenceTime == null) { |
| 766 | // If no reference time was given, use now. |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 767 | referenceTime = Instant.now(); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 768 | } |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 769 | List<LabeledIntent> actions = new ArrayList<>(); |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 770 | actions.add(createCalendarViewIntent(context, parsedTime)); |
| 771 | final long millisUntilEvent = referenceTime.until(parsedTime, MILLIS); |
| 772 | if (millisUntilEvent > MIN_EVENT_FUTURE_MILLIS) { |
| 773 | actions.add(createCalendarCreateEventIntent(context, parsedTime, type)); |
Abodunrinwa Toki | 6b76675 | 2017-01-17 16:25:38 -0800 | [diff] [blame] | 774 | } |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 775 | return actions; |
Abodunrinwa Toki | 6b76675 | 2017-01-17 16:25:38 -0800 | [diff] [blame] | 776 | } |
| 777 | |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 778 | @NonNull |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 779 | private static List<LabeledIntent> createForFlight(Context context, String text) { |
| 780 | return Arrays.asList(new LabeledIntent( |
| 781 | context.getString(com.android.internal.R.string.view_flight), |
| 782 | context.getString(com.android.internal.R.string.view_flight_desc), |
| 783 | new Intent(Intent.ACTION_WEB_SEARCH) |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 784 | .putExtra(SearchManager.QUERY, text), |
| 785 | text.hashCode())); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | @NonNull |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 789 | private static LabeledIntent createCalendarViewIntent(Context context, Instant parsedTime) { |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 790 | Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon(); |
| 791 | builder.appendPath("time"); |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 792 | ContentUris.appendId(builder, parsedTime.toEpochMilli()); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 793 | return new LabeledIntent( |
| 794 | context.getString(com.android.internal.R.string.view_calendar), |
| 795 | context.getString(com.android.internal.R.string.view_calendar_desc), |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 796 | new Intent(Intent.ACTION_VIEW).setData(builder.build()), |
| 797 | LabeledIntent.DEFAULT_REQUEST_CODE); |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | @NonNull |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 801 | private static LabeledIntent createCalendarCreateEventIntent( |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 802 | Context context, Instant parsedTime, @EntityType String type) { |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 803 | final boolean isAllDay = TextClassifier.TYPE_DATE.equals(type); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 804 | return new LabeledIntent( |
| 805 | context.getString(com.android.internal.R.string.add_calendar_event), |
| 806 | context.getString(com.android.internal.R.string.add_calendar_event_desc), |
| 807 | new Intent(Intent.ACTION_INSERT) |
| 808 | .setData(CalendarContract.Events.CONTENT_URI) |
| 809 | .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, isAllDay) |
| 810 | .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, |
Jan Althaus | a1652cf | 2018-03-29 17:51:57 +0200 | [diff] [blame] | 811 | parsedTime.toEpochMilli()) |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 812 | .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, |
Abodunrinwa Toki | 904a931 | 2018-04-18 21:21:27 +0100 | [diff] [blame] | 813 | parsedTime.toEpochMilli() + DEFAULT_EVENT_DURATION), |
| 814 | parsedTime.hashCode()); |
Abodunrinwa Toki | 6b76675 | 2017-01-17 16:25:38 -0800 | [diff] [blame] | 815 | } |
| 816 | } |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 817 | } |