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

Commit 001d1fa5 authored by Joe Antonetti's avatar Joe Antonetti Committed by Android (Google) Code Review
Browse files

Merge "[Handoff][7/N] Properly Serialize Application Name" into main

parents fc188a82 d8bbb08e
Loading
Loading
Loading
Loading
+44 −3
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ import android.companion.AssociationInfo;
import android.companion.CompanionDeviceManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.RemoteException;
import android.util.Slog;

@@ -60,6 +62,7 @@ class TaskBroadcaster
    private final ActivityTaskManager mActivityTaskManager;
    private final CompanionDeviceManager mCompanionDeviceManager;
    private final ConnectedAssociationStore mConnectedAssociationStore;
    private final PackageManager mPackageManager;

    private boolean mIsBroadcasting = false;

@@ -75,6 +78,8 @@ class TaskBroadcaster

        mCompanionDeviceManager
            = context.getSystemService(CompanionDeviceManager.class);

        mPackageManager = context.getPackageManager();
    }

    void startBroadcasting(){
@@ -127,7 +132,12 @@ class TaskBroadcaster
        ActivityManager.RunningTaskInfo taskInfo = getRunningTask(taskId);

        if (taskInfo != null) {
            RemoteTaskInfo remoteTaskInfo = new RemoteTaskInfo(taskInfo);
            RemoteTaskInfo remoteTaskInfo = createRemoteTaskInfo(taskInfo);
            if (remoteTaskInfo == null) {
                Slog.w(TAG, "Could not create RemoteTaskInfo for task: " + taskInfo.taskId);
                return;
            }

            RemoteTaskAddedMessage taskAddedMessage
                = new RemoteTaskAddedMessage(remoteTaskInfo);

@@ -149,7 +159,12 @@ class TaskBroadcaster
    public void onTaskMovedToFront(RunningTaskInfo taskInfo) throws RemoteException {
        Slog.v(TAG, "onTaskMovedToFront: taskId=" + taskInfo.taskId);

        RemoteTaskInfo remoteTaskInfo = new RemoteTaskInfo(taskInfo);
        RemoteTaskInfo remoteTaskInfo = createRemoteTaskInfo(taskInfo);
        if (remoteTaskInfo == null) {
            Slog.w(TAG, "Could not create RemoteTaskInfo for task: " + taskInfo.taskId);
            return;
        }

        RemoteTaskUpdatedMessage taskUpdatedMessage = new RemoteTaskUpdatedMessage(remoteTaskInfo);
        sendMessageToAllConnectedAssociations(taskUpdatedMessage);
    }
@@ -169,7 +184,12 @@ class TaskBroadcaster

        List<RemoteTaskInfo> remoteTasks = new ArrayList<>();
        for (ActivityManager.RunningTaskInfo taskInfo : runningTasks) {
            remoteTasks.add(new RemoteTaskInfo(taskInfo));
            RemoteTaskInfo remoteTaskInfo = createRemoteTaskInfo(taskInfo);
            if (remoteTaskInfo != null) {
                remoteTasks.add(remoteTaskInfo);
            } else {
                Slog.w(TAG, "Could not create RemoteTaskInfo for task: " + taskInfo.taskId);
            }
        }

        ContinuityDeviceConnected deviceConnectedMessage =
@@ -232,4 +252,25 @@ class TaskBroadcaster
    private List<ActivityManager.RunningTaskInfo> getRunningTasks() {
        return mActivityTaskManager.getTasks(Integer.MAX_VALUE, true);
    }

    private RemoteTaskInfo createRemoteTaskInfo(ActivityManager.RunningTaskInfo taskInfo) {
        PackageInfo packageInfo;
        try {
            packageInfo = mPackageManager.getPackageInfo(
                taskInfo.baseActivity.getPackageName(),
                PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            Slog.e(TAG, "Failed to get package info for task: " + taskInfo.taskId, e);
            return null;
        }

        String baseApplicationLabel = mPackageManager.getApplicationLabel(
            packageInfo.applicationInfo).toString();

        return new RemoteTaskInfo(
            taskInfo.taskId,
            baseApplicationLabel,
            taskInfo.lastActiveTime,
            new byte[0]);
    }
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ public class ContinuityDeviceConnected implements TaskContinuityMessageData {
                case (int) android.companion.ContinuityDeviceConnected.REMOTE_TASKS:
                    final long remoteTasksToken = pis.start(
                        android.companion.ContinuityDeviceConnected.REMOTE_TASKS);
                    remoteTasks.add(new RemoteTaskInfo(pis));
                    remoteTasks.add(RemoteTaskInfo.fromProto(pis));
                    pis.end(remoteTasksToken);
                    break;
            }
+1 −1
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ public class RemoteTaskAddedMessage implements TaskContinuityMessageData {
                case (int) android.companion.RemoteTaskAddedMessage.TASK:
                    final long taskToken = pis.start(
                        android.companion.RemoteTaskAddedMessage.TASK);
                    task = new RemoteTaskInfo(pis);
                    task = RemoteTaskInfo.fromProto(pis);
                    pis.end(taskToken);
                    break;
            }
+38 −46
Original line number Diff line number Diff line
@@ -16,92 +16,84 @@

package com.android.server.companion.datatransfer.continuity.messages;

import android.app.TaskInfo;
import android.companion.datatransfer.continuity.RemoteTask;
import android.util.proto.ProtoInputStream;
import android.util.proto.ProtoOutputStream;

import java.util.Arrays;
import java.util.Objects;
import java.io.IOException;

public class RemoteTaskInfo {
public record RemoteTaskInfo(int id, String label, long lastUsedTimeMillis, byte[] taskIcon) {

    private int mId = 0;
    private String mLabel = "";
    private long mLastUsedTimeMillis = 0;
    private byte[] mTaskIcon = new byte[0];

    public RemoteTaskInfo(TaskInfo taskInfo) {
        mId = taskInfo.taskId;

        // TODO: joeantonetti - Proper fallback if task description is null.
        if (taskInfo.taskDescription != null && taskInfo.taskDescription.getLabel() != null) {
            mLabel = taskInfo.taskDescription.getLabel().toString();
        }

        mLastUsedTimeMillis = taskInfo.lastActiveTime;
    }

    public RemoteTaskInfo(ProtoInputStream protoInputStream)
    public static RemoteTaskInfo fromProto(ProtoInputStream protoInputStream)
        throws IOException {

        int id = 0;
        String label = "";
        long lastUsedTimeMillis = 0;
        byte[] taskIcon = new byte[0];
        while (protoInputStream.nextField() != ProtoInputStream.NO_MORE_FIELDS) {
            switch (protoInputStream.getFieldNumber()) {
              case (int) android.companion.RemoteTaskInfo.ID:
                    mId = protoInputStream.readInt(
                    id = protoInputStream.readInt(
                        android.companion.RemoteTaskInfo.ID);

                    break;
                case (int) android.companion.RemoteTaskInfo.LABEL:
                    mLabel = protoInputStream.readString(
                    label = protoInputStream.readString(
                        android.companion.RemoteTaskInfo.LABEL);

                    break;
                case (int) android.companion.RemoteTaskInfo.LAST_USED_TIME_MILLIS:
                    mLastUsedTimeMillis = protoInputStream.readLong(
                    lastUsedTimeMillis = protoInputStream.readLong(
                        android.companion.RemoteTaskInfo.LAST_USED_TIME_MILLIS);
                    break;
                case (int) android.companion.RemoteTaskInfo.TASK_ICON:
                    mTaskIcon = protoInputStream
                    taskIcon = protoInputStream
                        .readBytes(android.companion.RemoteTaskInfo.TASK_ICON);
                    break;
            }
       }
   }

    public int getId() {
        return mId;
    }

    public String getLabel() {
        return mLabel;
    }

    public long getLastUsedTimeMillis() {
        return mLastUsedTimeMillis;
    }

    public byte[] getTaskIcon() {
        return mTaskIcon;
        return new RemoteTaskInfo(id, label, lastUsedTimeMillis, taskIcon);
   }

    public void writeToProto(ProtoOutputStream protoOutputStream) {
        protoOutputStream
            .writeInt32(android.companion.RemoteTaskInfo.ID, mId);
            .writeInt32(android.companion.RemoteTaskInfo.ID, id());
        protoOutputStream
            .writeString(android.companion.RemoteTaskInfo.LABEL, mLabel);
            .writeString(android.companion.RemoteTaskInfo.LABEL, label());
        protoOutputStream
            .writeInt64(
                android.companion.RemoteTaskInfo.LAST_USED_TIME_MILLIS,
                mLastUsedTimeMillis);
                lastUsedTimeMillis());
        protoOutputStream
            .writeBytes(android.companion.RemoteTaskInfo.TASK_ICON, mTaskIcon);
            .writeBytes(android.companion.RemoteTaskInfo.TASK_ICON, taskIcon());
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof RemoteTaskInfo) {
            RemoteTaskInfo other = (RemoteTaskInfo) o;
            return id() == other.id()
                && label().equals(other.label())
                && lastUsedTimeMillis() == other.lastUsedTimeMillis()
                && Arrays.equals(taskIcon(), other.taskIcon());
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id(), label(), lastUsedTimeMillis(), Arrays.hashCode(taskIcon()));
    }

    public RemoteTask toRemoteTask(int deviceId, String deviceName) {
        return new RemoteTask.Builder(getId())
                .setLabel(mLabel)
        return new RemoteTask.Builder(id())
                .setLabel(label())
                .setDeviceId(deviceId)
                .setLastUsedTimestampMillis((int) mLastUsedTimeMillis)
                .setLastUsedTimestampMillis((int) lastUsedTimeMillis())
                .setSourceDeviceName(deviceName)
                .build();
    }
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ public class RemoteTaskUpdatedMessage implements TaskContinuityMessageData {
                case (int) android.companion.RemoteTaskUpdatedMessage.TASK:
                    final long taskToken = pis.start(
                        android.companion.RemoteTaskUpdatedMessage.TASK);
                    mTask = new RemoteTaskInfo(pis);
                    mTask = RemoteTaskInfo.fromProto(pis);
                    pis.end(taskToken);
                    break;
            }
Loading