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

Commit 430f5680 authored by Adam Lesinski's avatar Adam Lesinski Committed by Android (Google) Code Review
Browse files

Merge "Add Configuration changes to UsageStats" into lmp-dev

parents 078dede1 7f61e96d
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -5706,6 +5706,19 @@ package android.app.job {
package android.app.usage {
  public final class ConfigurationStats implements android.os.Parcelable {
    ctor public ConfigurationStats(android.app.usage.ConfigurationStats);
    method public int describeContents();
    method public int getActivationCount();
    method public android.content.res.Configuration getConfiguration();
    method public long getFirstTimeStamp();
    method public long getLastTimeActive();
    method public long getLastTimeStamp();
    method public long getTotalTimeActive();
    method public void writeToParcel(android.os.Parcel, int);
    field public static final android.os.Parcelable.Creator CREATOR;
  }
  public final class UsageEvents implements android.os.Parcelable {
    method public int describeContents();
    method public boolean getNextEvent(android.app.usage.UsageEvents.Event);
@@ -5718,9 +5731,11 @@ package android.app.usage {
  public static final class UsageEvents.Event {
    ctor public UsageEvents.Event();
    method public java.lang.String getClassName();
    method public android.content.res.Configuration getConfiguration();
    method public int getEventType();
    method public java.lang.String getPackageName();
    method public long getTimeStamp();
    field public static final int CONFIGURATION_CHANGE = 5; // 0x5
    field public static final int MOVE_TO_BACKGROUND = 2; // 0x2
    field public static final int MOVE_TO_FOREGROUND = 1; // 0x1
    field public static final int NONE = 0; // 0x0
@@ -5741,6 +5756,7 @@ package android.app.usage {
  public final class UsageStatsManager {
    method public java.util.Map<java.lang.String, android.app.usage.UsageStats> queryAndAggregateUsageStats(long, long);
    method public java.util.List<android.app.usage.ConfigurationStats> queryConfigurations(int, long, long);
    method public android.app.usage.UsageEvents queryEvents(long, long);
    method public java.util.List<android.app.usage.UsageStats> queryUsageStats(int, long, long);
    field public static final int INTERVAL_BEST = 4; // 0x4
+161 −0
Original line number Diff line number Diff line
/**
 * Copyright (C) 2014 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.content.res.Configuration;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * Represents the usage statistics of a device {@link android.content.res.Configuration} for a
 * specific time range.
 */
public final class ConfigurationStats implements Parcelable {

    /**
     * {@hide}
     */
    public Configuration mConfiguration;

    /**
     * {@hide}
     */
    public long mBeginTimeStamp;

    /**
     * {@hide}
     */
    public long mEndTimeStamp;

    /**
     * {@hide}
     */
    public long mLastTimeActive;

    /**
     * {@hide}
     */
    public long mTotalTimeActive;

    /**
     * {@hide}
     */
    public int mActivationCount;

    /**
     * {@hide}
     */
    public ConfigurationStats() {
    }

    public ConfigurationStats(ConfigurationStats stats) {
        mConfiguration = stats.mConfiguration;
        mBeginTimeStamp = stats.mBeginTimeStamp;
        mEndTimeStamp = stats.mEndTimeStamp;
        mLastTimeActive = stats.mLastTimeActive;
        mTotalTimeActive = stats.mTotalTimeActive;
        mActivationCount = stats.mActivationCount;
    }

    public Configuration getConfiguration() {
        return mConfiguration;
    }

    /**
     * Get the beginning of the time range this {@link ConfigurationStats} represents,
     * measured in milliseconds since the epoch.
     * <p/>
     * See {@link System#currentTimeMillis()}.
     */
    public long getFirstTimeStamp() {
        return mBeginTimeStamp;
    }

    /**
     * Get the end of the time range this {@link ConfigurationStats} represents,
     * measured in milliseconds since the epoch.
     * <p/>
     * See {@link System#currentTimeMillis()}.
     */
    public long getLastTimeStamp() {
        return mEndTimeStamp;
    }

    /**
     * Get the last time this configuration was active, measured in milliseconds since the epoch.
     * <p/>
     * See {@link System#currentTimeMillis()}.
     */
    public long getLastTimeActive() {
        return mLastTimeActive;
    }

    /**
     * Get the total time this configuration was active, measured in milliseconds.
     */
    public long getTotalTimeActive() {
        return mTotalTimeActive;
    }

    /**
     * Get the number of times this configuration was active.
     */
    public int getActivationCount() {
        return mActivationCount;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (mConfiguration != null) {
            dest.writeInt(1);
            mConfiguration.writeToParcel(dest, flags);
        } else {
            dest.writeInt(0);
        }

        dest.writeLong(mBeginTimeStamp);
        dest.writeLong(mEndTimeStamp);
        dest.writeLong(mLastTimeActive);
        dest.writeLong(mTotalTimeActive);
        dest.writeInt(mActivationCount);
    }

    public static final Creator<ConfigurationStats> CREATOR = new Creator<ConfigurationStats>() {
        @Override
        public ConfigurationStats createFromParcel(Parcel source) {
            ConfigurationStats stats = new ConfigurationStats();
            if (source.readInt() != 0) {
                stats.mConfiguration = Configuration.CREATOR.createFromParcel(source);
            }
            stats.mBeginTimeStamp = source.readLong();
            stats.mEndTimeStamp = source.readLong();
            stats.mLastTimeActive = source.readLong();
            stats.mTotalTimeActive = source.readLong();
            stats.mActivationCount = source.readInt();
            return stats;
        }

        @Override
        public ConfigurationStats[] newArray(int size) {
            return new ConfigurationStats[size];
        }
    };
}
+2 −0
Original line number Diff line number Diff line
@@ -27,5 +27,7 @@ import android.content.pm.ParceledListSlice;
interface IUsageStatsManager {
    ParceledListSlice queryUsageStats(int bucketType, long beginTime, long endTime,
            String callingPackage);
    ParceledListSlice queryConfigurationStats(int bucketType, long beginTime, long endTime,
            String callingPackage);
    UsageEvents queryEvents(long beginTime, long endTime, String callingPackage);
}
+83 −38
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package android.app.usage;

import android.content.ComponentName;
import android.content.res.Configuration;
import android.os.Parcel;
import android.os.Parcelable;

@@ -62,6 +63,11 @@ public final class UsageEvents implements Parcelable {
         */
        public static final int CONTINUE_PREVIOUS_DAY = 4;

        /**
         * An event type denoting that the device configuration has changed.
         */
        public static final int CONFIGURATION_CHANGE = 5;

        /**
         * {@hide}
         */
@@ -82,6 +88,12 @@ public final class UsageEvents implements Parcelable {
         */
        public int mEventType;

        /**
         * Only present for {@link #CONFIGURATION_CHANGE} event types.
         * {@hide}
         */
        public Configuration mConfiguration;

        /**
         * TODO(adamlesinski): Removed before release.
         * {@hide}
@@ -123,6 +135,14 @@ public final class UsageEvents implements Parcelable {
        public int getEventType() {
            return mEventType;
        }

        /**
         * Returns a {@link Configuration} for this event if the event is of type
         * {@link #CONFIGURATION_CHANGE}, otherwise it returns null.
         */
        public Configuration getConfiguration() {
            return mConfiguration;
        }
    }

    // Only used when creating the resulting events. Not used for reading/unparceling.
@@ -201,23 +221,9 @@ public final class UsageEvents implements Parcelable {
            return false;
        }

        final int packageIndex = mParcel.readInt();
        if (packageIndex >= 0) {
            eventOut.mPackage = mStringPool[packageIndex];
        } else {
            eventOut.mPackage = null;
        }
        readEventFromParcel(mParcel, eventOut);

        final int classIndex = mParcel.readInt();
        if (classIndex >= 0) {
            eventOut.mClass = mStringPool[classIndex];
        } else {
            eventOut.mClass = null;
        }
        eventOut.mEventType = mParcel.readInt();
        eventOut.mTimeStamp = mParcel.readLong();
        mIndex++;

        if (mIndex >= mEventCount) {
            mParcel.recycle();
            mParcel = null;
@@ -235,11 +241,6 @@ public final class UsageEvents implements Parcelable {
        }
    }

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

    private int findStringIndex(String str) {
        final int index = Arrays.binarySearch(mStringPool, str);
        if (index < 0) {
@@ -248,21 +249,10 @@ public final class UsageEvents implements Parcelable {
        return index;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mEventCount);
        dest.writeInt(mIndex);
        if (mEventCount > 0) {
            dest.writeStringArray(mStringPool);

            if (mEventsToWrite != null) {
                // Write out the events
                Parcel p = Parcel.obtain();
                try {
                    p.setDataPosition(0);
                    for (int i = 0; i < mEventCount; i++) {
                        final Event event = mEventsToWrite.get(i);

    /**
     * Writes a single event to the parcel. Modify this when updating {@link Event}.
     */
    private void writeEventToParcel(Event event, Parcel p, int flags) {
        final int packageIndex;
        if (event.mPackage != null) {
            packageIndex = findStringIndex(event.mPackage);
@@ -278,9 +268,64 @@ public final class UsageEvents implements Parcelable {
        }
        p.writeInt(packageIndex);
        p.writeInt(classIndex);
                        p.writeInt(event.getEventType());
                        p.writeLong(event.getTimeStamp());
        p.writeInt(event.mEventType);
        p.writeLong(event.mTimeStamp);

        if (event.mEventType == Event.CONFIGURATION_CHANGE) {
            event.mConfiguration.writeToParcel(p, flags);
        }
    }

    /**
     * Reads a single event from the parcel. Modify this when updating {@link Event}.
     */
    private void readEventFromParcel(Parcel p, Event eventOut) {
        final int packageIndex = p.readInt();
        if (packageIndex >= 0) {
            eventOut.mPackage = mStringPool[packageIndex];
        } else {
            eventOut.mPackage = null;
        }

        final int classIndex = p.readInt();
        if (classIndex >= 0) {
            eventOut.mClass = mStringPool[classIndex];
        } else {
            eventOut.mClass = null;
        }
        eventOut.mEventType = p.readInt();
        eventOut.mTimeStamp = p.readLong();

        // Extract the configuration for configuration change events.
        if (eventOut.mEventType == Event.CONFIGURATION_CHANGE) {
            eventOut.mConfiguration = Configuration.CREATOR.createFromParcel(p);
        } else {
            eventOut.mConfiguration = null;
        }
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mEventCount);
        dest.writeInt(mIndex);
        if (mEventCount > 0) {
            dest.writeStringArray(mStringPool);

            if (mEventsToWrite != null) {
                // Write out the events
                Parcel p = Parcel.obtain();
                try {
                    p.setDataPosition(0);
                    for (int i = 0; i < mEventCount; i++) {
                        final Event event = mEventsToWrite.get(i);
                        writeEventToParcel(event, p, flags);
                    }

                    final int listByteLength = p.dataPosition();

                    // Write the total length of the data.
+27 −2
Original line number Diff line number Diff line
@@ -125,9 +125,9 @@ public final class UsageStatsManager {
     * @see #INTERVAL_YEARLY
     * @see #INTERVAL_BEST
     */
    @SuppressWarnings("unchecked")
    public List<UsageStats> queryUsageStats(int intervalType, long beginTime, long endTime) {
        try {
            @SuppressWarnings("unchecked")
            ParceledListSlice<UsageStats> slice = mService.queryUsageStats(intervalType, beginTime,
                    endTime, mContext.getOpPackageName());
            if (slice != null) {
@@ -136,7 +136,32 @@ public final class UsageStatsManager {
        } catch (RemoteException e) {
            // fallthrough and return null.
        }
        return Collections.EMPTY_LIST;
        return Collections.emptyList();
    }

    /**
     * Gets the hardware configurations the device was in for the given time range, aggregated by
     * the specified interval. The results are ordered as in
     * {@link #queryUsageStats(int, long, long)}.
     *
     * @param intervalType The time interval by which the stats are aggregated.
     * @param beginTime The inclusive beginning of the range of stats to include in the results.
     * @param endTime The exclusive end of the range of stats to include in the results.
     * @return A list of {@link ConfigurationStats} or null if none are available.
     */
    public List<ConfigurationStats> queryConfigurations(int intervalType, long beginTime,
            long endTime) {
        try {
            @SuppressWarnings("unchecked")
            ParceledListSlice<ConfigurationStats> slice = mService.queryConfigurationStats(
                    intervalType, beginTime, endTime, mContext.getOpPackageName());
            if (slice != null) {
                return slice.getList();
            }
        } catch (RemoteException e) {
            // fallthrough and return the empty list.
        }
        return Collections.emptyList();
    }

    /**
Loading