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