From 98221155d3c81612a5ea7c5b5252390082b77817 Mon Sep 17 00:00:00 2001 From: Joanne Chung Date: Tue, 11 Apr 2023 16:57:04 +0800 Subject: Improve translation debugging logging not work The debug flag is set as a static variable, it only be reset after the process restarted. We receive many bugs that the debug flag is not enabled correctly, it's hard to guide users to do the correct set up. To improve this, we check isLoggable before logging it. This should improve the debug process with lab. Bug: 253558603 Bug: 230018939 Test: adb shell setprop log.tag.UiTranslation DEBUG and start the chat apps. The debug log can be printed Change-Id: I5b3cb1c2761641346ac800b1a19fd961f88a1886 --- core/java/android/view/translation/Translator.java | 3 +-- .../view/translation/UiTranslationController.java | 26 +++++++++++++--------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/core/java/android/view/translation/Translator.java b/core/java/android/view/translation/Translator.java index 70db6e513c88..50249da08c3c 100644 --- a/core/java/android/view/translation/Translator.java +++ b/core/java/android/view/translation/Translator.java @@ -18,7 +18,6 @@ package android.view.translation; import static android.view.translation.TranslationManager.STATUS_SYNC_CALL_FAIL; import static android.view.translation.TranslationManager.SYNC_CALLS_TIMEOUT_MS; -import static android.view.translation.UiTranslationController.DEBUG; import android.annotation.CallbackExecutor; import android.annotation.NonNull; @@ -402,7 +401,7 @@ public class Translator { @Override public void onTranslationResponse(TranslationResponse response) throws RemoteException { - if (DEBUG) { + if (Log.isLoggable(UiTranslationManager.LOG_TAG, Log.DEBUG)) { Log.i(TAG, "onTranslationResponse called."); } final Runnable runnable = diff --git a/core/java/android/view/translation/UiTranslationController.java b/core/java/android/view/translation/UiTranslationController.java index 514df59f1989..140e3f1b6fdc 100644 --- a/core/java/android/view/translation/UiTranslationController.java +++ b/core/java/android/view/translation/UiTranslationController.java @@ -122,8 +122,9 @@ public class UiTranslationController implements Dumpable { Log.i(TAG, "Cannot update " + stateToString(state) + " for destroyed " + mActivity); return; } + boolean isLoggable = Log.isLoggable(UiTranslationManager.LOG_TAG, Log.DEBUG); Log.i(TAG, "updateUiTranslationState state: " + stateToString(state) - + (DEBUG ? (", views: " + views + ", spec: " + uiTranslationSpec) : "")); + + (isLoggable ? (", views: " + views + ", spec: " + uiTranslationSpec) : "")); synchronized (mLock) { mCurrentState = state; if (views != null) { @@ -237,7 +238,7 @@ public class UiTranslationController implements Dumpable { } pw.print(outerPrefix); pw.print("padded views: "); pw.println(mViewsToPadContent); } - if (DEBUG) { + if (Log.isLoggable(UiTranslationManager.LOG_TAG, Log.DEBUG)) { dumpViewByTraversal(outerPrefix, pw); } } @@ -345,6 +346,7 @@ public class UiTranslationController implements Dumpable { */ private void onVirtualViewTranslationCompleted( SparseArray> translatedResult) { + boolean isLoggable = Log.isLoggable(UiTranslationManager.LOG_TAG, Log.DEBUG); if (mActivity.isDestroyed()) { Log.v(TAG, "onTranslationCompleted:" + mActivity + "is destroyed."); return; @@ -369,7 +371,7 @@ public class UiTranslationController implements Dumpable { } final LongSparseArray virtualChildResponse = translatedResult.valueAt(i); - if (DEBUG) { + if (isLoggable) { Log.v(TAG, "onVirtualViewTranslationCompleted: received response for " + "AutofillId " + autofillId); } @@ -379,7 +381,7 @@ public class UiTranslationController implements Dumpable { } mActivity.runOnUiThread(() -> { if (view.getViewTranslationCallback() == null) { - if (DEBUG) { + if (isLoggable) { Log.d(TAG, view + " doesn't support showing translation because of " + "null ViewTranslationCallback."); } @@ -397,12 +399,13 @@ public class UiTranslationController implements Dumpable { * The method is used to handle the translation result for non-vertual views. */ private void onTranslationCompleted(SparseArray translatedResult) { + boolean isLoggable = Log.isLoggable(UiTranslationManager.LOG_TAG, Log.DEBUG); if (mActivity.isDestroyed()) { Log.v(TAG, "onTranslationCompleted:" + mActivity + "is destroyed."); return; } final int resultCount = translatedResult.size(); - if (DEBUG) { + if (isLoggable) { Log.v(TAG, "onTranslationCompleted: receive " + resultCount + " responses."); } synchronized (mLock) { @@ -413,7 +416,7 @@ public class UiTranslationController implements Dumpable { } for (int i = 0; i < resultCount; i++) { final ViewTranslationResponse response = translatedResult.valueAt(i); - if (DEBUG) { + if (isLoggable) { Log.v(TAG, "onTranslationCompleted: " + sanitizedViewTranslationResponse(response)); } @@ -443,7 +446,7 @@ public class UiTranslationController implements Dumpable { (TextViewTranslationCallback) callback; if (textViewCallback.isShowingTranslation() || textViewCallback.isAnimationRunning()) { - if (DEBUG) { + if (isLoggable) { Log.d(TAG, "Duplicate ViewTranslationResponse for " + autofillId + ". Ignoring."); } @@ -458,7 +461,7 @@ public class UiTranslationController implements Dumpable { callback = new TextViewTranslationCallback(); view.setViewTranslationCallback(callback); } else { - if (DEBUG) { + if (isLoggable) { Log.d(TAG, view + " doesn't support showing translation because of " + "null ViewTranslationCallback."); } @@ -506,7 +509,7 @@ public class UiTranslationController implements Dumpable { final TranslationRequest request = new TranslationRequest.Builder() .setViewTranslationRequests(requests) .build(); - if (DEBUG) { + if (Log.isLoggable(UiTranslationManager.LOG_TAG, Log.DEBUG)) { StringBuilder msg = new StringBuilder("sendTranslationRequest:{requests=["); for (ViewTranslationRequest viewRequest: requests) { msg.append("{request=") @@ -636,6 +639,7 @@ public class UiTranslationController implements Dumpable { private void runForEachView(BiConsumer action) { synchronized (mLock) { + boolean isLoggable = Log.isLoggable(UiTranslationManager.LOG_TAG, Log.DEBUG); final ArrayMap> views = new ArrayMap<>(mViews); if (views.size() == 0) { Log.w(TAG, "No views can be excuted for runForEachView."); @@ -644,12 +648,12 @@ public class UiTranslationController implements Dumpable { final int viewCounts = views.size(); for (int i = 0; i < viewCounts; i++) { final View view = views.valueAt(i).get(); - if (DEBUG) { + if (isLoggable) { Log.d(TAG, "runForEachView for autofillId = " + (view != null ? view.getAutofillId() : " null")); } if (view == null || view.getViewTranslationCallback() == null) { - if (DEBUG) { + if (isLoggable) { Log.d(TAG, "View was gone or ViewTranslationCallback for autofillId " + "= " + views.keyAt(i)); } -- cgit v1.2.3-59-g8ed1b