| 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.view.translation; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.icu.util.ULocale; |
| 21 | |
| 22 | import java.util.concurrent.Executor; |
| 23 | |
| 24 | /** |
| 25 | * Callback for listening to UI Translation state changes. See {@link |
| 26 | * UiTranslationManager#registerUiTranslationStateCallback(Executor, UiTranslationStateCallback)}. |
| 27 | * <p> |
| 28 | * Prior to Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}: |
| 29 | * <ul> |
| 30 | * <li>Callback methods <em>without</em> {@code packageName} are invoked. Apps with |
| 31 | * minSdkVersion lower than {@link android.os.Build.VERSION_CODES#TIRAMISU} <em>must</em> |
| 32 | * implement those methods if they want to handle the events.</li> |
| 33 | * <li>Callback methods for a particular event <em>may</em> be called multiple times |
| 34 | * consecutively, even when the translation state has not changed (e.g., |
| 35 | * {@link #onStarted(ULocale, ULocale, String)} may be called multiple times even after |
| 36 | * translation has already started).</li> |
| 37 | * </ul> |
| 38 | * <p> |
| 39 | * In Android version {@link android.os.Build.VERSION_CODES#TIRAMISU} and later: |
| 40 | * <ul> |
| 41 | * <li>If both methods with and without {@code packageName} are implemented (e.g., |
| 42 | * {@link #onFinished()} and {@link #onFinished(String)}, only the one <em>with</em> {@code |
| 43 | * packageName} will be called.</li> |
| 44 | * <li>Callback methods for a particular event will <em>not</em> be called multiple times |
| 45 | * consecutively. They will only be called when the translation state has actually changed |
| 46 | * (e.g., from "started" to "paused"). Note: "resumed" is not considered a separate state |
| 47 | * from "started", so {@link #onResumed(ULocale, ULocale, String)} will never be called after |
| 48 | * {@link #onStarted(ULocale, ULocale, String)}.<</li> |
| 49 | * </ul> |
| 50 | */ |
| 51 | public interface UiTranslationStateCallback { |
| 52 | |
| 53 | /** |
| 54 | * @removed use {@link #onStarted(ULocale, ULocale)} or {@link #onStarted(ULocale, ULocale, |
| 55 | * String)} instead. |
| 56 | */ |
| 57 | @Deprecated |
| 58 | default void onStarted(@NonNull String sourceLocale, @NonNull String targetLocale) { |
| 59 | // no-op |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * The system is requesting translation of the UI from {@code sourceLocale} to {@code |
| 64 | * targetLocale}. |
| 65 | * <p> |
| 66 | * This is also called if either the requested {@code sourceLocale} or {@code targetLocale} has |
| 67 | * changed. |
| 68 | * <p> |
| 69 | * Apps should implement {@link #onStarted(ULocale, ULocale, String)} instead if they need the |
| 70 | * name of the package that owns the activity being translated. |
| 71 | * <p> |
| 72 | * Apps with minSdkVersion lower than {@link android.os.Build.VERSION_CODES#TIRAMISU} |
| 73 | * <em>must</em> implement this method if they want to handle the "started" event. |
| 74 | * |
| 75 | * @param sourceLocale {@link ULocale} the UI is being translated from. |
| 76 | * @param targetLocale {@link ULocale} the UI is being translated to. |
| 77 | */ |
| 78 | default void onStarted(@NonNull ULocale sourceLocale, @NonNull ULocale targetLocale) { |
| 79 | onStarted(sourceLocale.getLanguage(), targetLocale.getLanguage()); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * The system is requesting translation of the UI from {@code sourceLocale} to {@code |
| 84 | * targetLocale}. |
| 85 | * <p> |
| 86 | * This is also called if either the requested {@code sourceLocale} or {@code targetLocale} has |
| 87 | * changed. |
| 88 | * <p> |
| 89 | * Apps <em>may</em> implement {@link #onStarted(ULocale, ULocale)} instead if they don't need |
| 90 | * the name of the package that owns the activity being translated. |
| 91 | * <p> |
| 92 | * Apps with minSdkVersion lower than {@link android.os.Build.VERSION_CODES#TIRAMISU} |
| 93 | * <em>must</em> implement {@link #onStarted(ULocale, ULocale)} if they want to handle the |
| 94 | * "started" event. |
| 95 | * |
| 96 | * @param sourceLocale {@link ULocale} the UI is being translated from. |
| 97 | * @param targetLocale {@link ULocale} the UI is being translated to. |
| 98 | * @param packageName The name of the package that owns the activity being translated. |
| 99 | */ |
| 100 | default void onStarted(@NonNull ULocale sourceLocale, @NonNull ULocale targetLocale, |
| 101 | @NonNull String packageName) { |
| 102 | onStarted(sourceLocale, targetLocale); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * The system is requesting that the application temporarily show the UI contents in their |
| 107 | * original language. |
| 108 | * <p> |
| 109 | * Apps should implement {@link #onPaused(String)} as well if they need the name of the |
| 110 | * package that owns the activity being translated. |
| 111 | */ |
| 112 | void onPaused(); |
| 113 | |
| 114 | /** |
| 115 | * The system is requesting that the application temporarily show the UI contents in their |
| 116 | * original language. |
| 117 | * <p> |
| 118 | * Apps <em>may</em> implement {@link #onPaused()} instead if they don't need the name of the |
| 119 | * package that owns the activity being translated. |
| 120 | * <p> |
| 121 | * Apps with minSdkVersion lower than {@link android.os.Build.VERSION_CODES#TIRAMISU} |
| 122 | * <em>must</em> implement {@link #onPaused()} if they want to handle the "paused" event. |
| 123 | */ |
| 124 | default void onPaused(@NonNull String packageName) { |
| 125 | onPaused(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * The system is requesting that the application restore from the temporarily paused state and |
| 130 | * show the content in the translated language. |
| 131 | * <p> |
| 132 | * Apps should implement {@link #onResumed(ULocale, ULocale, String)} instead if they need the |
| 133 | * name of the package that owns the activity being translated. |
| 134 | * <p> |
| 135 | * Apps with minSdkVersion lower than {@link android.os.Build.VERSION_CODES#TIRAMISU} |
| 136 | * <em>must</em> implement this method if they want to handle the "resumed" event. |
| 137 | * |
| 138 | * @param sourceLocale {@link ULocale} the UI is being translated from. |
| 139 | * @param targetLocale {@link ULocale} the UI is being translated to. |
| 140 | */ |
| 141 | default void onResumed(@NonNull ULocale sourceLocale, @NonNull ULocale targetLocale) { |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * The system is requesting that the application restore from the temporarily paused state and |
| 146 | * show the content in the translated language. |
| 147 | * <p> |
| 148 | * Apps <em>may</em> implement {@link #onResumed(ULocale, ULocale)} instead if they don't need |
| 149 | * the name of the package that owns the activity being translated. |
| 150 | * <p> |
| 151 | * Apps with minSdkVersion lower than {@link android.os.Build.VERSION_CODES#TIRAMISU} |
| 152 | * <em>must</em> implement {@link #onResumed(ULocale, ULocale)} if they want to handle the |
| 153 | * "resumed" event. |
| 154 | * |
| 155 | * @param sourceLocale {@link ULocale} the UI is being translated from. |
| 156 | * @param targetLocale {@link ULocale} the UI is being translated to. |
| 157 | * @param packageName The name of the package that owns the activity being translated. |
| 158 | */ |
| 159 | default void onResumed(@NonNull ULocale sourceLocale, @NonNull ULocale targetLocale, |
| 160 | @NonNull String packageName) { |
| 161 | onResumed(sourceLocale, targetLocale); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * The UI Translation session has ended. |
| 166 | * <p> |
| 167 | * Apps should implement {@link #onFinished(String)} as well if they need the name of the |
| 168 | * package that owns the activity being translated. |
| 169 | */ |
| 170 | void onFinished(); |
| 171 | |
| 172 | /** |
| 173 | * The UI Translation session has ended. |
| 174 | * <p> |
| 175 | * Apps <em>may</em> implement {@link #onFinished()} instead if they don't need the name of the |
| 176 | * package that owns the activity being translated. |
| 177 | * <p> |
| 178 | * Apps with minSdkVersion lower than {@link android.os.Build.VERSION_CODES#TIRAMISU} |
| 179 | * <em>must</em> implement {@link #onFinished()} if they want to handle the "finished" event. |
| 180 | * |
| 181 | * @param packageName The name of the package that owns the activity being translated. |
| 182 | */ |
| 183 | default void onFinished(@NonNull String packageName) { |
| 184 | onFinished(); |
| 185 | } |
| 186 | } |