1 /*
2  * Copyright (C) 2019 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 package android.textclassifier.cts2;
17 
18 import android.app.Activity;
19 import android.app.PendingIntent;
20 import android.app.PendingIntent.CanceledException;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.util.Log;
24 import android.view.textclassifier.TextClassificationManager;
25 import android.view.textclassifier.TextClassifier;
26 import android.view.textclassifier.TextLanguage;
27 
28 /**
29  * An activity that queries the device's TextClassifierService when started and immediately
30  * terminates itself.
31  */
32 public final class QueryTextClassifierServiceActivity extends Activity {
33 
34     private static final String TAG = QueryTextClassifierServiceActivity.class.getSimpleName();
35     private PendingIntent mPendingIntent;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         final Intent intent = getIntent();
41         mPendingIntent = intent.getParcelableExtra("finishBroadcast");
42     }
43 
44     @Override
onResume()45     protected void onResume() {
46         super.onResume();
47         // Do a TextClassifier call
48         detectLanguageAndFinish();
49     }
50 
detectLanguageAndFinish()51     private void detectLanguageAndFinish() {
52         final String text = "An email address is [email protected].";
53         new Thread(() -> {
54             final TextLanguage.Request textLanguageRequest =
55                     new TextLanguage.Request.Builder(text)
56                             .build();
57             TextClassifier textClassifier = getClassifier();
58             textClassifier.detectLanguage(textLanguageRequest);
59             // Send finish broadcast
60             if (mPendingIntent != null) {
61                 try {
62                     mPendingIntent.send();
63                 } catch (CanceledException e) {
64                     Log.w(TAG, "Pending intent " + mPendingIntent + " canceled");
65                 }
66             }
67             finish();
68         }).start();
69     }
70 
getClassifier()71     private TextClassifier getClassifier() {
72         final TextClassificationManager tcm = getSystemService(TextClassificationManager.class);
73         return tcm != null ? tcm.getTextClassifier() : TextClassifier.NO_OP;
74     }
75 }
76