• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.views;
18 
19 import static android.translation.cts.Helper.CUSTOM_TRANSLATION_ID_MY_TAG;
20 
21 import android.content.Context;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.view.translation.TranslationRequestValue;
25 import android.view.translation.ViewTranslationRequest;
26 import android.view.translation.ViewTranslationResponse;
27 import android.widget.TextView;
28 
29 import java.util.function.Consumer;
30 
31 /**
32  * A custom TextView that overrides translation {@link View#onCreateViewTranslationRequest} and
33  * {@link View#onViewTranslationResponse} and the {@link ViewTranslationResponse} is set.
34  */
35 public class CustomTextView extends TextView {
36 
37     private boolean mMyTagTranslationSupported;
38 
CustomTextView(Context context, AttributeSet attrs)39     public CustomTextView(Context context, AttributeSet attrs) {
40         super(context, attrs);
41     }
42 
43     @Override
onCreateViewTranslationRequest(int[] supportedFormats, Consumer<ViewTranslationRequest> requestsCollector)44     public void onCreateViewTranslationRequest(int[] supportedFormats,
45             Consumer<ViewTranslationRequest> requestsCollector) {
46         mMyTagTranslationSupported = false;
47         ViewTranslationRequest.Builder requestBuilder =
48                 new ViewTranslationRequest.Builder(getAutofillId());
49         requestBuilder.setValue(ViewTranslationRequest.ID_TEXT,
50                 TranslationRequestValue.forText(getText()));
51         requestBuilder.setValue(CUSTOM_TRANSLATION_ID_MY_TAG,
52                 TranslationRequestValue.forText(getText()));
53         requestsCollector.accept(requestBuilder.build());
54     }
55 
56     @Override
onViewTranslationResponse(ViewTranslationResponse response)57     public void onViewTranslationResponse(ViewTranslationResponse response) {
58         super.onViewTranslationResponse(response);
59 
60         if (response.getKeys().contains(CUSTOM_TRANSLATION_ID_MY_TAG)) {
61             mMyTagTranslationSupported = true;
62         }
63     }
64 
isMyTagTranslationSupported()65     public boolean isMyTagTranslationSupported() {
66         return  mMyTagTranslationSupported;
67     }
68 }
69