[go: nahoru, domu]

1/**
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy
6 * of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16
17package android.app.usage;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * Contains usage statistics for an app package for a specific
24 * time range.
25 */
26public final class UsageStats implements Parcelable {
27
28    /**
29     * {@hide}
30     */
31    public String mPackageName;
32
33    /**
34     * {@hide}
35     */
36    public long mBeginTimeStamp;
37
38    /**
39     * {@hide}
40     */
41    public long mEndTimeStamp;
42
43    /**
44     * Last time used by the user with an explicit action (notification, activity launch).
45     * {@hide}
46     */
47    public long mLastTimeUsed;
48
49    /**
50     * {@hide}
51     */
52    public long mTotalTimeInForeground;
53
54    /**
55     * {@hide}
56     */
57    public int mLaunchCount;
58
59    /**
60     * {@hide}
61     */
62    public int mLastEvent;
63
64    /**
65     * {@hide}
66     */
67    public UsageStats() {
68    }
69
70    public UsageStats(UsageStats stats) {
71        mPackageName = stats.mPackageName;
72        mBeginTimeStamp = stats.mBeginTimeStamp;
73        mEndTimeStamp = stats.mEndTimeStamp;
74        mLastTimeUsed = stats.mLastTimeUsed;
75        mTotalTimeInForeground = stats.mTotalTimeInForeground;
76        mLaunchCount = stats.mLaunchCount;
77        mLastEvent = stats.mLastEvent;
78    }
79
80    public String getPackageName() {
81        return mPackageName;
82    }
83
84    /**
85     * Get the beginning of the time range this {@link android.app.usage.UsageStats} represents,
86     * measured in milliseconds since the epoch.
87     * <p/>
88     * See {@link System#currentTimeMillis()}.
89     */
90    public long getFirstTimeStamp() {
91        return mBeginTimeStamp;
92    }
93
94    /**
95     * Get the end of the time range this {@link android.app.usage.UsageStats} represents,
96     * measured in milliseconds since the epoch.
97     * <p/>
98     * See {@link System#currentTimeMillis()}.
99     */
100    public long getLastTimeStamp() {
101        return mEndTimeStamp;
102    }
103
104    /**
105     * Get the last time this package was used, measured in milliseconds since the epoch.
106     * <p/>
107     * See {@link System#currentTimeMillis()}.
108     */
109    public long getLastTimeUsed() {
110        return mLastTimeUsed;
111    }
112
113    /**
114     * Get the total time this package spent in the foreground, measured in milliseconds.
115     */
116    public long getTotalTimeInForeground() {
117        return mTotalTimeInForeground;
118    }
119
120    /**
121     * Add the statistics from the right {@link UsageStats} to the left. The package name for
122     * both {@link UsageStats} objects must be the same.
123     * @param right The {@link UsageStats} object to merge into this one.
124     * @throws java.lang.IllegalArgumentException if the package names of the two
125     *         {@link UsageStats} objects are different.
126     */
127    public void add(UsageStats right) {
128        if (!mPackageName.equals(right.mPackageName)) {
129            throw new IllegalArgumentException("Can't merge UsageStats for package '" +
130                    mPackageName + "' with UsageStats for package '" + right.mPackageName + "'.");
131        }
132
133        if (right.mBeginTimeStamp > mBeginTimeStamp) {
134            // The incoming UsageStat begins after this one, so use its last time used fields
135            // as the source of truth.
136            // We use the mBeginTimeStamp due to a bug where UsageStats files can overlap with
137            // regards to their mEndTimeStamp.
138            mLastEvent = right.mLastEvent;
139            mLastTimeUsed = right.mLastTimeUsed;
140        }
141        mBeginTimeStamp = Math.min(mBeginTimeStamp, right.mBeginTimeStamp);
142        mEndTimeStamp = Math.max(mEndTimeStamp, right.mEndTimeStamp);
143        mTotalTimeInForeground += right.mTotalTimeInForeground;
144        mLaunchCount += right.mLaunchCount;
145    }
146
147    @Override
148    public int describeContents() {
149        return 0;
150    }
151
152    @Override
153    public void writeToParcel(Parcel dest, int flags) {
154        dest.writeString(mPackageName);
155        dest.writeLong(mBeginTimeStamp);
156        dest.writeLong(mEndTimeStamp);
157        dest.writeLong(mLastTimeUsed);
158        dest.writeLong(mTotalTimeInForeground);
159        dest.writeInt(mLaunchCount);
160        dest.writeInt(mLastEvent);
161    }
162
163    public static final Creator<UsageStats> CREATOR = new Creator<UsageStats>() {
164        @Override
165        public UsageStats createFromParcel(Parcel in) {
166            UsageStats stats = new UsageStats();
167            stats.mPackageName = in.readString();
168            stats.mBeginTimeStamp = in.readLong();
169            stats.mEndTimeStamp = in.readLong();
170            stats.mLastTimeUsed = in.readLong();
171            stats.mTotalTimeInForeground = in.readLong();
172            stats.mLaunchCount = in.readInt();
173            stats.mLastEvent = in.readInt();
174            return stats;
175        }
176
177        @Override
178        public UsageStats[] newArray(int size) {
179            return new UsageStats[size];
180        }
181    };
182}
183