Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.WorkerThread; |
| 22 | import android.content.Context; |
| 23 | import android.os.Looper; |
| 24 | import android.os.RemoteException; |
| 25 | import android.os.ServiceManager; |
| 26 | import android.service.textclassifier.ITextClassificationCallback; |
| 27 | import android.service.textclassifier.ITextClassifierService; |
| 28 | import android.service.textclassifier.ITextLinksCallback; |
| 29 | import android.service.textclassifier.ITextSelectionCallback; |
| 30 | |
Abodunrinwa Toki | 6563833 | 2018-03-16 21:08:50 +0000 | [diff] [blame] | 31 | import com.android.internal.annotations.VisibleForTesting; |
| 32 | import com.android.internal.annotations.VisibleForTesting.Visibility; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 33 | import com.android.internal.util.Preconditions; |
| 34 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 35 | import java.util.concurrent.CountDownLatch; |
| 36 | import java.util.concurrent.TimeUnit; |
| 37 | |
| 38 | /** |
| 39 | * Proxy to the system's default TextClassifier. |
Abodunrinwa Toki | 6563833 | 2018-03-16 21:08:50 +0000 | [diff] [blame] | 40 | * @hide |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 41 | */ |
Abodunrinwa Toki | 6563833 | 2018-03-16 21:08:50 +0000 | [diff] [blame] | 42 | @VisibleForTesting(visibility = Visibility.PACKAGE) |
| 43 | public final class SystemTextClassifier implements TextClassifier { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 44 | |
| 45 | private static final String LOG_TAG = "SystemTextClassifier"; |
| 46 | |
| 47 | private final ITextClassifierService mManagerService; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 48 | private final TextClassificationConstants mSettings; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 49 | private final TextClassifier mFallback; |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 50 | private final String mPackageName; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 51 | private TextClassificationSessionId mSessionId; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 52 | |
Abodunrinwa Toki | 6563833 | 2018-03-16 21:08:50 +0000 | [diff] [blame] | 53 | public SystemTextClassifier(Context context, TextClassificationConstants settings) |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 54 | throws ServiceManager.ServiceNotFoundException { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 55 | mManagerService = ITextClassifierService.Stub.asInterface( |
| 56 | ServiceManager.getServiceOrThrow(Context.TEXT_CLASSIFICATION_SERVICE)); |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 57 | mSettings = Preconditions.checkNotNull(settings); |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 58 | mFallback = context.getSystemService(TextClassificationManager.class) |
| 59 | .getTextClassifier(TextClassifier.LOCAL); |
Abodunrinwa Toki | 6563833 | 2018-03-16 21:08:50 +0000 | [diff] [blame] | 60 | mPackageName = Preconditions.checkNotNull(context.getPackageName()); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @inheritDoc |
| 65 | */ |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 66 | @Override |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 67 | @WorkerThread |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 68 | public TextSelection suggestSelection(TextSelection.Request request) { |
| 69 | Preconditions.checkNotNull(request); |
| 70 | Utils.checkMainThread(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 71 | try { |
| 72 | final TextSelectionCallback callback = new TextSelectionCallback(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 73 | mManagerService.onSuggestSelection(mSessionId, request, callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 74 | final TextSelection selection = callback.mReceiver.get(); |
| 75 | if (selection != null) { |
| 76 | return selection; |
| 77 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 78 | } catch (RemoteException | InterruptedException e) { |
| 79 | Log.e(LOG_TAG, "Error suggesting selection for text. Using fallback.", e); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 80 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 81 | return mFallback.suggestSelection(request); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @inheritDoc |
| 86 | */ |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 87 | @Override |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 88 | @WorkerThread |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 89 | public TextClassification classifyText(TextClassification.Request request) { |
| 90 | Preconditions.checkNotNull(request); |
| 91 | Utils.checkMainThread(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 92 | try { |
| 93 | final TextClassificationCallback callback = new TextClassificationCallback(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 94 | mManagerService.onClassifyText(mSessionId, request, callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 95 | final TextClassification classification = callback.mReceiver.get(); |
| 96 | if (classification != null) { |
| 97 | return classification; |
| 98 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 99 | } catch (RemoteException | InterruptedException e) { |
| 100 | Log.e(LOG_TAG, "Error classifying text. Using fallback.", e); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 101 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 102 | return mFallback.classifyText(request); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @inheritDoc |
| 107 | */ |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 108 | @Override |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 109 | @WorkerThread |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 110 | public TextLinks generateLinks(@NonNull TextLinks.Request request) { |
| 111 | Preconditions.checkNotNull(request); |
| 112 | Utils.checkMainThread(); |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 113 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 114 | if (!mSettings.isSmartLinkifyEnabled() && request.isLegacyFallback()) { |
| 115 | return Utils.generateLegacyLinks(request); |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 118 | try { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 119 | request.setCallingPackageName(mPackageName); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 120 | final TextLinksCallback callback = new TextLinksCallback(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 121 | mManagerService.onGenerateLinks(mSessionId, request, callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 122 | final TextLinks links = callback.mReceiver.get(); |
| 123 | if (links != null) { |
| 124 | return links; |
| 125 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 126 | } catch (RemoteException | InterruptedException e) { |
| 127 | Log.e(LOG_TAG, "Error generating links. Using fallback.", e); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 128 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 129 | return mFallback.generateLinks(request); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 132 | /** |
| 133 | * @inheritDoc |
| 134 | */ |
| 135 | @Override |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 136 | @WorkerThread |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 137 | public int getMaxGenerateLinksTextLength() { |
| 138 | // TODO: retrieve this from the bound service. |
| 139 | return mFallback.getMaxGenerateLinksTextLength(); |
| 140 | } |
| 141 | |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 142 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 143 | public void destroy() { |
| 144 | try { |
| 145 | if (mSessionId != null) { |
| 146 | mManagerService.onDestroyTextClassificationSession(mSessionId); |
| 147 | } |
| 148 | } catch (RemoteException e) { |
| 149 | Log.e(LOG_TAG, "Error destroying classification session.", e); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Attempts to initialize a new classification session. |
| 155 | * |
| 156 | * @param classificationContext the classification context |
| 157 | * @param sessionId the session's id |
| 158 | */ |
| 159 | void initializeRemoteSession( |
| 160 | @NonNull TextClassificationContext classificationContext, |
| 161 | @NonNull TextClassificationSessionId sessionId) { |
| 162 | mSessionId = Preconditions.checkNotNull(sessionId); |
| 163 | try { |
| 164 | mManagerService.onCreateTextClassificationSession(classificationContext, mSessionId); |
| 165 | } catch (RemoteException e) { |
| 166 | Log.e(LOG_TAG, "Error starting a new classification session.", e); |
| 167 | } |
| 168 | } |
| 169 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 170 | private static final class TextSelectionCallback extends ITextSelectionCallback.Stub { |
| 171 | |
| 172 | final ResponseReceiver<TextSelection> mReceiver = new ResponseReceiver<>(); |
| 173 | |
| 174 | @Override |
| 175 | public void onSuccess(TextSelection selection) { |
| 176 | mReceiver.onSuccess(selection); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void onFailure() { |
| 181 | mReceiver.onFailure(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | private static final class TextClassificationCallback extends ITextClassificationCallback.Stub { |
| 186 | |
| 187 | final ResponseReceiver<TextClassification> mReceiver = new ResponseReceiver<>(); |
| 188 | |
| 189 | @Override |
| 190 | public void onSuccess(TextClassification classification) { |
| 191 | mReceiver.onSuccess(classification); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void onFailure() { |
| 196 | mReceiver.onFailure(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private static final class TextLinksCallback extends ITextLinksCallback.Stub { |
| 201 | |
| 202 | final ResponseReceiver<TextLinks> mReceiver = new ResponseReceiver<>(); |
| 203 | |
| 204 | @Override |
| 205 | public void onSuccess(TextLinks links) { |
| 206 | mReceiver.onSuccess(links); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void onFailure() { |
| 211 | mReceiver.onFailure(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private static final class ResponseReceiver<T> { |
| 216 | |
| 217 | private final CountDownLatch mLatch = new CountDownLatch(1); |
| 218 | |
| 219 | private T mResponse; |
| 220 | |
| 221 | public void onSuccess(T response) { |
| 222 | mResponse = response; |
| 223 | mLatch.countDown(); |
| 224 | } |
| 225 | |
| 226 | public void onFailure() { |
| 227 | Log.e(LOG_TAG, "Request failed.", null); |
| 228 | mLatch.countDown(); |
| 229 | } |
| 230 | |
| 231 | @Nullable |
| 232 | public T get() throws InterruptedException { |
| 233 | // If this is running on the main thread, do not block for a response. |
| 234 | // The response will unfortunately be null and the TextClassifier should depend on its |
| 235 | // fallback. |
| 236 | // NOTE that TextClassifier calls should preferably always be called on a worker thread. |
| 237 | if (Looper.myLooper() != Looper.getMainLooper()) { |
| 238 | mLatch.await(2, TimeUnit.SECONDS); |
| 239 | } |
| 240 | return mResponse; |
| 241 | } |
| 242 | } |
| 243 | } |