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

Commit f0fdef22 authored by Aleksei Vetrov's avatar Aleksei Vetrov Committed by Android (Google) Code Review
Browse files

Merge "Revert "UsageStats: New event query API with specific event types."" into main

parents 2aea2fec cf12661e
Loading
Loading
Loading
Loading
+0 −16
Original line number Diff line number Diff line
@@ -9321,21 +9321,6 @@ package android.app.usage {
    field public static final int USER_INTERACTION = 7; // 0x7
  }
  @FlaggedApi("android.app.usage.filter_based_event_query_api") public final class UsageEventsQuery implements android.os.Parcelable {
    method public int describeContents();
    method public long getBeginTimeMillis();
    method public long getEndTimeMillis();
    method @NonNull public java.util.Set<java.lang.Integer> getEventTypes();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.app.usage.UsageEventsQuery> CREATOR;
  }
  public static final class UsageEventsQuery.Builder {
    ctor public UsageEventsQuery.Builder(long, long);
    method @NonNull public android.app.usage.UsageEventsQuery.Builder addEventTypes(@NonNull int...);
    method @NonNull public android.app.usage.UsageEventsQuery build();
  }
  public final class UsageStats implements android.os.Parcelable {
    ctor public UsageStats(android.app.usage.UsageStats);
    method public void add(android.app.usage.UsageStats);
@@ -9360,7 +9345,6 @@ package android.app.usage {
    method public java.util.List<android.app.usage.ConfigurationStats> queryConfigurations(int, long, long);
    method public java.util.List<android.app.usage.EventStats> queryEventStats(int, long, long);
    method public android.app.usage.UsageEvents queryEvents(long, long);
    method @FlaggedApi("android.app.usage.filter_based_event_query_api") @NonNull @RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) public android.app.usage.UsageEvents queryEvents(@NonNull android.app.usage.UsageEventsQuery);
    method public android.app.usage.UsageEvents queryEventsForSelf(long, long);
    method public java.util.List<android.app.usage.UsageStats> queryUsageStats(int, long, long);
    field public static final int INTERVAL_BEST = 4; // 0x4
+2 −2
Original line number Diff line number Diff line
@@ -20,9 +20,10 @@ import android.app.PendingIntent;
import android.app.usage.BroadcastResponseStats;
import android.app.usage.BroadcastResponseStatsList;
import android.app.usage.UsageEvents;
import android.app.usage.UsageEventsQuery;
import android.content.pm.ParceledListSlice;

import java.util.Map;

/**
 * System private API for talking with the UsageStatsManagerService.
 *
@@ -41,7 +42,6 @@ interface IUsageStatsManager {
    UsageEvents queryEventsForPackage(long beginTime, long endTime, String callingPackage);
    UsageEvents queryEventsForUser(long beginTime, long endTime, int userId, String callingPackage);
    UsageEvents queryEventsForPackageForUser(long beginTime, long endTime, int userId, String pkg, String callingPackage);
    UsageEvents queryEventsWithFilter(in UsageEventsQuery query, String callingPackage);
    @UnsupportedAppUsage(maxTargetSdk = 30, trackingBug = 170729553)
    void setAppInactive(String packageName, boolean inactive, int userId);
    boolean isAppStandbyEnabled();
+0 −41
Original line number Diff line number Diff line
@@ -349,47 +349,6 @@ public final class UsageEvents implements Parcelable {
         */
        public static final int MAX_EVENT_TYPE = 31;

        /**
         * Keep in sync with the event types defined above.
         * @hide
         */
        @IntDef(flag = false, value = {
                NONE,
                ACTIVITY_RESUMED,
                ACTIVITY_PAUSED,
                END_OF_DAY,
                CONTINUE_PREVIOUS_DAY,
                CONFIGURATION_CHANGE,
                SYSTEM_INTERACTION,
                USER_INTERACTION,
                SHORTCUT_INVOCATION,
                CHOOSER_ACTION,
                NOTIFICATION_SEEN,
                STANDBY_BUCKET_CHANGED,
                NOTIFICATION_INTERRUPTION,
                SLICE_PINNED_PRIV,
                SLICE_PINNED,
                SCREEN_INTERACTIVE,
                SCREEN_NON_INTERACTIVE,
                KEYGUARD_SHOWN,
                KEYGUARD_HIDDEN,
                FOREGROUND_SERVICE_START,
                FOREGROUND_SERVICE_STOP,
                CONTINUING_FOREGROUND_SERVICE,
                ROLLOVER_FOREGROUND_SERVICE,
                ACTIVITY_STOPPED,
                ACTIVITY_DESTROYED,
                FLUSH_TO_DISK,
                DEVICE_SHUTDOWN,
                DEVICE_STARTUP,
                USER_UNLOCKED,
                USER_STOPPED,
                LOCUS_ID_SET,
                APP_COMPONENT_USED,
        })
        @Retention(RetentionPolicy.SOURCE)
        public @interface EventType {}

        /** @hide */
        public static final int FLAG_IS_PACKAGE_INSTANT_APP = 1 << 0;

+0 −19
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.usage;

parcelable UsageEventsQuery;
 No newline at end of file
+0 −173
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.usage;

import android.annotation.CurrentTimeMillisLong;
import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.app.usage.UsageEvents.Event;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.ArraySet;

import com.android.internal.util.ArrayUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * An Object-Oriented representation for a {@link UsageEvents} query.
 * Used by {@link UsageStatsManager#queryEvents(UsageEventsQuery)} call.
 */
@FlaggedApi(Flags.FLAG_FILTER_BASED_EVENT_QUERY_API)
public final class UsageEventsQuery implements Parcelable {
    private final @CurrentTimeMillisLong long mBeginTimeMillis;
    private final @CurrentTimeMillisLong long mEndTimeMillis;
    private final @Event.EventType int[] mEventTypes;

    private UsageEventsQuery(@NonNull Builder builder) {
        mBeginTimeMillis = builder.mBeginTimeMillis;
        mEndTimeMillis = builder.mEndTimeMillis;
        mEventTypes = ArrayUtils.convertToIntArray(builder.mEventTypes);
    }

    private UsageEventsQuery(Parcel in) {
        mBeginTimeMillis = in.readLong();
        mEndTimeMillis = in.readLong();
        int eventTypesLength = in.readInt();
        mEventTypes = new int[eventTypesLength];
        in.readIntArray(mEventTypes);
    }

    /**
     * Returns the inclusive timestamp to indicate the beginning of the range of events.
     * Defined in terms of "Unix time", see {@link java.lang.System#currentTimeMillis}.
     */
    public @CurrentTimeMillisLong long getBeginTimeMillis() {
        return mBeginTimeMillis;
    }

    /**
     * Returns the exclusive timpstamp to indicate the end of the range of events.
     * Defined in terms of "Unix time", see {@link java.lang.System#currentTimeMillis}.
     */
    public @CurrentTimeMillisLong long getEndTimeMillis() {
        return mEndTimeMillis;
    }

    /**
     * Returns the set of usage event types for the query.
     * <em>Note: An empty set indicates query for all usage events. </em>
     */
    public @NonNull Set<Integer> getEventTypes() {
        if (ArrayUtils.isEmpty(mEventTypes)) {
            return Collections.emptySet();
        }

        HashSet<Integer> eventTypeSet = new HashSet<>();
        for (int eventType : mEventTypes) {
            eventTypeSet.add(eventType);
        }
        return eventTypeSet;
    }

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

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeLong(mBeginTimeMillis);
        dest.writeLong(mEndTimeMillis);
        dest.writeInt(mEventTypes.length);
        dest.writeIntArray(mEventTypes);
    }

    @NonNull
    public static final Creator<UsageEventsQuery> CREATOR =
            new Creator<UsageEventsQuery>() {
                @Override
                public UsageEventsQuery createFromParcel(Parcel in) {
                    return new UsageEventsQuery(in);
                }

                @Override
                public UsageEventsQuery[] newArray(int size) {
                    return new UsageEventsQuery[size];
                }
            };

    /** @hide */
    public int[] getEventTypeFilter() {
        return Arrays.copyOf(mEventTypes, mEventTypes.length);
    }

    /**
     * Builder for UsageEventsQuery.
     */
    public static final class Builder {
        private final @CurrentTimeMillisLong long mBeginTimeMillis;
        private final @CurrentTimeMillisLong long mEndTimeMillis;
        private final ArraySet<Integer> mEventTypes = new ArraySet<>();

        /**
         * Constructor that specifies the period for which to return events.
         * @param beginTimeMillis Inclusive beginning timestamp, as per
         *                        {@link java.lang.System#currentTimeMillis()}
         * @param endTimeMillis Exclusive ending timestamp, as per
         *                        {@link java.lang.System#currentTimeMillis()}
         *
         * @throws IllegalArgumentException if {@code beginTimeMillis} &lt;
         *                                  {@code endTimeMillis}
         */
        public Builder(@CurrentTimeMillisLong long beginTimeMillis,
                @CurrentTimeMillisLong long endTimeMillis) {
            if (beginTimeMillis < 0 || endTimeMillis < beginTimeMillis) {
                throw new IllegalArgumentException("Invalid period");
            }
            mBeginTimeMillis = beginTimeMillis;
            mEndTimeMillis = endTimeMillis;
        }

        /**
         * Builds a read-only UsageEventsQuery object.
         */
        public @NonNull UsageEventsQuery build() {
            return new UsageEventsQuery(this);
        }

        /**
         * Specifies the list of usage event types to be included in the query.
         * @param eventTypes List of the usage event types. See {@link UsageEvents.Event}
         *
         * @throws llegalArgumentException if the event type is not valid.
         */
        public @NonNull Builder addEventTypes(@NonNull @Event.EventType int... eventTypes) {
            for (int i = 0; i < eventTypes.length; i++) {
                final int eventType = eventTypes[i];
                if (eventType < Event.NONE || eventType > Event.MAX_EVENT_TYPE) {
                    throw new IllegalArgumentException("Invalid usage event type: " + eventType);
                }
                mEventTypes.add(eventType);
            }
            return this;
        }
    }
}
Loading