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

Commit 7f3d63f9 authored by sandeepbandaru's avatar sandeepbandaru Committed by Sandeep Bandaru
Browse files

Expose InferenceInfo via Hidden API for V to use in Settings App.

- Parse InferenceInfo based on a proto definition to be reused in the remote implementation and passed as byte[] in the result Bundle incase of success and Base64 string in the PersistableBundle incase of failures.
- Add a in-memory list of inference info as received from the remote implementation.
- Evict entries based on max-age of configured 3 hours, as settings app
  will query this data every hour.

Bug: 335390745

Change-Id: I92883c1009ffcda4f499e439928ab4528f6483a5
parent f43c3308
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@
 import android.os.Bundle;
 import android.app.ondeviceintelligence.Feature;
 import android.app.ondeviceintelligence.FeatureDetails;
 import android.app.ondeviceintelligence.InferenceInfo;
 import java.util.List;
 import android.app.ondeviceintelligence.IDownloadCallback;
 import android.app.ondeviceintelligence.IListFeaturesCallback;
 import android.app.ondeviceintelligence.IFeatureCallback;
@@ -72,4 +74,6 @@ interface IOnDeviceIntelligenceManager {
                    in IStreamingResponseCallback streamingCallback) = 8;

      String getRemoteServicePackageName() = 9;

      List<InferenceInfo> getLatestInferenceInfo(long startTimeEpochMillis) = 10;
 }
+22 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.ondeviceintelligence;

/**
  * @hide
  */
parcelable InferenceInfo;
+211 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.ondeviceintelligence;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * This class represents the information related to an inference event to track the resource usage
 * as a function of inference time.
 *
 * @hide
 */
public class InferenceInfo implements Parcelable {

    /**
     * Uid for the caller app.
     */
    private final int uid;

    /**
     * Inference start time (milliseconds from the epoch time).
     */
    private final long startTimeMs;

    /**
     * Inference end time (milliseconds from the epoch time).
     */
    private final long endTimeMs;

    /**
     * Suspended time in milliseconds.
     */
    private final long suspendedTimeMs;

    /**
     * Constructs an InferenceInfo object with the specified parameters.
     *
     * @param uid             Uid for the caller app.
     * @param startTimeMs     Inference start time (milliseconds from the epoch time).
     * @param endTimeMs       Inference end time (milliseconds from the epoch time).
     * @param suspendedTimeMs Suspended time in milliseconds.
     */
    public InferenceInfo(int uid, long startTimeMs, long endTimeMs,
            long suspendedTimeMs) {
        this.uid = uid;
        this.startTimeMs = startTimeMs;
        this.endTimeMs = endTimeMs;
        this.suspendedTimeMs = suspendedTimeMs;
    }

    /**
     * Constructs an InferenceInfo object from a Parcel.
     *
     * @param in The Parcel to read the object's data from.
     */
    protected InferenceInfo(Parcel in) {
        uid = in.readInt();
        startTimeMs = in.readLong();
        endTimeMs = in.readLong();
        suspendedTimeMs = in.readLong();
    }


    /**
     * Writes the object's data to the provided Parcel.
     *
     * @param dest The Parcel to write the object's data to.
     * @param flags Additional flags about how the object should be written.
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(uid);
        dest.writeLong(startTimeMs);
        dest.writeLong(endTimeMs);
        dest.writeLong(suspendedTimeMs);
    }

    /**
     * Returns the UID for the caller app.
     *
     * @return the UID for the caller app.
     */
    public int getUid() {
        return uid;
    }

    /**
     * Returns the inference start time in milliseconds from the epoch time.
     *
     * @return the inference start time in milliseconds from the epoch time.
     */
    public long getStartTimeMs() {
        return startTimeMs;
    }

    /**
     * Returns the inference end time in milliseconds from the epoch time.
     *
     * @return the inference end time in milliseconds from the epoch time.
     */
    public long getEndTimeMs() {
        return endTimeMs;
    }

    /**
     * Returns the suspended time in milliseconds.
     *
     * @return the suspended time in milliseconds.
     */
    public long getSuspendedTimeMs() {
        return suspendedTimeMs;
    }

    @Override
    public int describeContents() {
        return 0;
    }


    public static final @android.annotation.NonNull Parcelable.Creator<InferenceInfo> CREATOR
            = new Parcelable.Creator<InferenceInfo>() {
        @Override
        public InferenceInfo[] newArray(int size) {
            return new InferenceInfo[size];
        }

        @Override
        public InferenceInfo createFromParcel(@android.annotation.NonNull Parcel in) {
            return new InferenceInfo(in);
        }
    };

    /**
     * Builder class for creating instances of {@link InferenceInfo}.
     */
    public static class Builder {
        private int uid;
        private long startTimeMs;
        private long endTimeMs;
        private long suspendedTimeMs;

        /**
         * Sets the UID for the caller app.
         *
         * @param uid the UID for the caller app.
         * @return the Builder instance.
         */
        public Builder setUid(int uid) {
            this.uid = uid;
            return this;
        }

        /**
         * Sets the inference start time in milliseconds from the epoch time.
         *
         * @param startTimeMs the inference start time in milliseconds from the epoch time.
         * @return the Builder instance.
         */
        public Builder setStartTimeMs(long startTimeMs) {
            this.startTimeMs = startTimeMs;
            return this;
        }

        /**
         * Sets the inference end time in milliseconds from the epoch time.
         *
         * @param endTimeMs the inference end time in milliseconds from the epoch time.
         * @return the Builder instance.
         */
        public Builder setEndTimeMs(long endTimeMs) {
            this.endTimeMs = endTimeMs;
            return this;
        }

        /**
         * Sets the suspended time in milliseconds.
         *
         * @param suspendedTimeMs the suspended time in milliseconds.
         * @return the Builder instance.
         */
        public Builder setSuspendedTimeMs(long suspendedTimeMs) {
            this.suspendedTimeMs = suspendedTimeMs;
            return this;
        }

        /**
         * Builds and returns an instance of {@link InferenceInfo}.
         *
         * @return an instance of {@link InferenceInfo}.
         */
        public InferenceInfo build() {
            return new InferenceInfo(uid, startTimeMs, endTimeMs,
                    suspendedTimeMs);
        }
    }
}
+18 −0
Original line number Diff line number Diff line
@@ -496,6 +496,24 @@ public final class OnDeviceIntelligenceManager {
        }
    }

    /**
     * This is primarily intended to be used to attribute/blame on-device intelligence power usage,
     * via the configured remote implementation, to its actual caller.
     *
     * @param startTimeEpochMillis epoch millis used to filter the InferenceInfo events.
     * @return InferenceInfo events since the passed in startTimeEpochMillis.
     *
     * @hide
     */
    @RequiresPermission(Manifest.permission.DUMP)
    public List<InferenceInfo> getLatestInferenceInfo(long startTimeEpochMillis) {
        try {
            return mService.getLatestInferenceInfo(startTimeEpochMillis);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }


    /** Request inference with provided Bundle and Params. */
    public static final int REQUEST_TYPE_INFERENCE = 0;
+5 −0
Original line number Diff line number Diff line
@@ -100,6 +100,11 @@ import java.util.function.Consumer;
public abstract class OnDeviceSandboxedInferenceService extends Service {
    private static final String TAG = OnDeviceSandboxedInferenceService.class.getSimpleName();

    /**
     * @hide
     */
    public static final String INFERENCE_INFO_BUNDLE_KEY = "inference_info";

    /**
     * The {@link Intent} that must be declared as handled by the service. To be supported, the
     * service must also require the
Loading