• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.translation.cts;
18 
19 import static android.translation.cts.Helper.COMMAND_CREATE_TRANSLATOR;
20 
21 import android.app.Service;
22 import android.content.Intent;
23 import android.icu.util.ULocale;
24 import android.os.IBinder;
25 import android.util.Log;
26 import android.view.translation.TranslationContext;
27 import android.view.translation.TranslationManager;
28 import android.view.translation.TranslationSpec;
29 
30 /**
31  * A Service that running on otherTranslationProcess1 process for testing create translator
32  * will generate unique translation session id.
33  */
34 public class OtherProcessService1 extends Service {
35 
36     private static final String TAG = "OtherProcessService";
37 
38     static final String EXTRA_COMMAND = "android.translation.cts.extra.CREATE_TRANSLATOR";
39 
40     @Override
onBind(Intent intent)41     public IBinder onBind(Intent intent) {
42         return null;
43     }
44 
45     @Override
onStartCommand(Intent intent, int flags, int startId)46     public int onStartCommand(Intent intent, int flags, int startId) {
47         int command = intent.getIntExtra(EXTRA_COMMAND, -1);
48         if (command == COMMAND_CREATE_TRANSLATOR) {
49             createOnDeviceTranslator();
50         } else {
51             Log.e(TAG, "Unknown command: " + command);
52         }
53         return Service.START_NOT_STICKY;
54     }
55 
createOnDeviceTranslator()56     private void createOnDeviceTranslator() {
57         TranslationManager manager = getSystemService(TranslationManager.class);
58         new Thread(() -> {
59             TranslationContext translationContext = new TranslationContext.Builder(
60                     new TranslationSpec(ULocale.ENGLISH, TranslationSpec.DATA_FORMAT_TEXT),
61                     new TranslationSpec(ULocale.FRENCH, TranslationSpec.DATA_FORMAT_TEXT))
62                     .build();
63             createTranslator(manager, translationContext);
64         }).start();
65     }
66 
createTranslator(TranslationManager manager, TranslationContext translationContext)67     private void createTranslator(TranslationManager manager,
68             TranslationContext translationContext) {
69         manager.createOnDeviceTranslator(translationContext, Runnable::run,
70                 translator -> {
71                     if (translator != null) {
72                         translator.destroy();
73                     }
74                 });
75     }
76 }
77