Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6a6cd46e authored by Ajinkya Chalke's avatar Ajinkya Chalke
Browse files

Adds debug logging to Backlinks

- These logs would be removed from release/non-userdebug builds.
- Also, added JvmName logcatMessage to debug build as it was only
  added to release builds earlier making it difficult to use the
  function in Java.
- Made an unrelated small small logic change so that Backlinks is
  only sent when App Clips status code is success.

Bug: 300307759
Flag: com.android.systemui.app_clips_backlinks
Test: Manually verified the logcat output
Change-Id: I6fbecdbb8f26f28529c783a7d4866a4854016de1
parent 51c415c0
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -62,6 +62,8 @@ object DebugLogger {
     * @param error: a [Throwable] to log.
     * @param message: a lazily evaluated message you wish to log.
     */
    @JvmOverloads
    @JvmName("logcatMessage")
    inline fun Any.debugLog(
        enabled: Boolean = Build.IS_DEBUGGABLE,
        priority: Int = Log.DEBUG,
+1 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.util.Log
/** An empty logger for release builds. */
object DebugLogger {

    @JvmOverloads
    @JvmName("logcatMessage")
    inline fun Any.debugLog(
        enabled: Boolean = Build.IS_DEBUGGABLE,
+6 −1
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ import com.android.internal.logging.UiEventLogger;
import com.android.internal.logging.UiEventLogger.UiEventEnum;
import com.android.settingslib.Utils;
import com.android.systemui.Flags;
import com.android.systemui.log.DebugLogger;
import com.android.systemui.res.R;
import com.android.systemui.screenshot.scroll.CropView;
import com.android.systemui.settings.UserTracker;
@@ -307,13 +308,16 @@ public class AppClipsActivity extends ComponentActivity {
                && mViewModel.getBacklinksLiveData().getValue() != null) {
            ClipData backlinksData = mViewModel.getBacklinksLiveData().getValue().getClipData();
            data.putParcelable(EXTRA_CLIP_DATA, backlinksData);

            DebugLogger.INSTANCE.logcatMessage(this,
                    () -> "setResultThenFinish: sending notes app ClipData");
        }

        try {
            mResultReceiver.send(Activity.RESULT_OK, data);
            logUiEvent(SCREENSHOT_FOR_NOTE_ACCEPTED);
        } catch (Exception e) {
            Log.e(TAG, "Error while returning data to trampoline activity", e);
            Log.e(TAG, "Error while sending data to trampoline activity", e);
        }

        // Nullify the ResultReceiver before finishing to avoid resending the result.
@@ -354,6 +358,7 @@ public class AppClipsActivity extends ComponentActivity {
            }
        } catch (Exception e) {
            // Do nothing.
            Log.e(TAG, "Error while sending trampoline activity error code: " + errorCode, e);
        }

        // Nullify the ResultReceiver to avoid resending the result.
+9 −4
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ import com.android.systemui.broadcast.BroadcastSender;
import com.android.systemui.dagger.qualifiers.Application;
import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.log.DebugLogger;
import com.android.systemui.notetask.NoteTaskController;
import com.android.systemui.notetask.NoteTaskEntryPoint;
import com.android.systemui.res.R;
@@ -265,11 +266,15 @@ public class AppClipsTrampolineActivity extends Activity {
            if (statusCode == CAPTURE_CONTENT_FOR_NOTE_SUCCESS) {
                Uri uri = resultData.getParcelable(EXTRA_SCREENSHOT_URI, Uri.class);
                convertedData.setData(uri);
            }

                if (resultData.containsKey(EXTRA_CLIP_DATA)) {
                ClipData backlinksData = resultData.getParcelable(EXTRA_CLIP_DATA, ClipData.class);
                    ClipData backlinksData = resultData.getParcelable(EXTRA_CLIP_DATA,
                            ClipData.class);
                    convertedData.setClipData(backlinksData);

                    DebugLogger.INSTANCE.logcatMessage(this,
                            () -> "onReceiveResult: sending notes app ClipData");
                }
            }

            // Broadcast no longer required, setting it to null.
+26 −0
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import androidx.lifecycle.ViewModelProvider;

import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.log.DebugLogger;
import com.android.systemui.screenshot.AssistContentRequester;
import com.android.systemui.screenshot.ImageExporter;

@@ -143,6 +144,7 @@ final class AppClipsViewModel extends ViewModel {
     * @param displayId       id of the display to query tasks for Backlinks data
     */
    void triggerBacklinks(Set<Integer> taskIdsToIgnore, int displayId) {
        DebugLogger.INSTANCE.logcatMessage(this, () -> "Backlinks triggered");
        mBgExecutor.execute(() -> {
            ListenableFuture<InternalBacklinksData> backlinksData = getBacklinksData(
                    taskIdsToIgnore, displayId);
@@ -247,6 +249,10 @@ final class AppClipsViewModel extends ViewModel {
    }

    private boolean shouldIncludeTask(RootTaskInfo taskInfo, Set<Integer> taskIdsToIgnore) {
        DebugLogger.INSTANCE.logcatMessage(this,
                () -> String.format("shouldIncludeTask taskId %d; topActivity %s", taskInfo.taskId,
                        taskInfo.topActivity));

        // Only consider tasks that shouldn't be ignored, are visible, running, and have a launcher
        // icon. Furthermore, types such as launcher/home/dock/assistant are ignored.
        return !taskIdsToIgnore.contains(taskInfo.taskId)
@@ -267,6 +273,10 @@ final class AppClipsViewModel extends ViewModel {

    private ListenableFuture<InternalBacklinksData> getBacklinksDataForTaskId(
            RootTaskInfo taskInfo) {
        DebugLogger.INSTANCE.logcatMessage(this,
                () -> String.format("getBacklinksDataForTaskId for taskId %d; topActivity %s",
                        taskInfo.taskId, taskInfo.topActivity));

        SettableFuture<InternalBacklinksData> backlinksData = SettableFuture.create();
        int taskId = taskInfo.taskId;
        mAssistContentRequester.requestAssistContent(taskId, assistContent ->
@@ -295,6 +305,10 @@ final class AppClipsViewModel extends ViewModel {
     */
    private InternalBacklinksData getBacklinksDataFromAssistContent(RootTaskInfo taskInfo,
            @Nullable AssistContent content) {
        DebugLogger.INSTANCE.logcatMessage(this,
                () -> String.format("getBacklinksDataFromAssistContent taskId %d; topActivity %s",
                        taskInfo.taskId, taskInfo.topActivity));

        String appName = getAppNameOfTask(taskInfo);
        String packageName = taskInfo.topActivity.getPackageName();
        Drawable appIcon = taskInfo.topActivityInfo.loadIcon(mPackageManager);
@@ -307,22 +321,34 @@ final class AppClipsViewModel extends ViewModel {

        // First preference is given to app provided uri.
        if (content.isAppProvidedWebUri()) {
            DebugLogger.INSTANCE.logcatMessage(this,
                    () -> "getBacklinksDataFromAssistContent: app has provided a uri");

            Uri uri = content.getWebUri();
            Intent backlinksIntent = new Intent(ACTION_VIEW).setData(uri);
            if (doesIntentResolveToSamePackage(backlinksIntent, packageName)) {
                DebugLogger.INSTANCE.logcatMessage(this,
                        () -> "getBacklinksDataFromAssistContent: using app provided uri");
                return new InternalBacklinksData(ClipData.newRawUri(appName, uri), appIcon);
            }
        }

        // Second preference is given to app provided, hopefully deep-linking, intent.
        if (content.isAppProvidedIntent()) {
            DebugLogger.INSTANCE.logcatMessage(this,
                    () -> "getBacklinksDataFromAssistContent: app has provided an intent");

            Intent backlinksIntent = content.getIntent();
            if (doesIntentResolveToSamePackage(backlinksIntent, packageName)) {
                DebugLogger.INSTANCE.logcatMessage(this,
                        () -> "getBacklinksDataFromAssistContent: using app provided intent");
                return new InternalBacklinksData(ClipData.newIntent(appName, backlinksIntent),
                        appIcon);
            }
        }

        DebugLogger.INSTANCE.logcatMessage(this,
                () -> "getBacklinksDataFromAssistContent: using fallback");
        return fallback;
    }