diff options
author | Adam Lesinski <adamlesinski@google.com> | 2014-08-25 14:48:12 -0700 |
---|---|---|
committer | Adam Lesinski <adamlesinski@google.com> | 2014-08-26 12:41:31 -0700 |
commit | 9d9607527f5bbf49c96565b63b90e36276b0dda7 (patch) | |
tree | f750028aa4a8c1c0c29b2f3fedac393642dc9438 /services/usage/java/com/android/server | |
parent | 4e9c07c0de199169374bded403805c92f1c1c6c1 (diff) | |
download | frameworks_base-9d9607527f5bbf49c96565b63b90e36276b0dda7.zip frameworks_base-9d9607527f5bbf49c96565b63b90e36276b0dda7.tar.gz frameworks_base-9d9607527f5bbf49c96565b63b90e36276b0dda7.tar.bz2 |
Split up ComponentName in UsageEvents.Event
Some events in the future may not have originated
from a class, so we shouldn't be using ComponentName.
Bug:17259858
Change-Id: Id7fe3245b91596cf27ae4ec51655602f01665622
Diffstat (limited to 'services/usage/java/com/android/server')
4 files changed, 57 insertions, 36 deletions
diff --git a/services/usage/java/com/android/server/usage/IntervalStats.java b/services/usage/java/com/android/server/usage/IntervalStats.java index 43027ad..dc036e2 100644 --- a/services/usage/java/com/android/server/usage/IntervalStats.java +++ b/services/usage/java/com/android/server/usage/IntervalStats.java @@ -18,8 +18,8 @@ package com.android.server.usage; import android.app.usage.TimeSparseArray; import android.app.usage.UsageEvents; import android.app.usage.UsageStats; -import android.content.ComponentName; import android.util.ArrayMap; +import android.util.ArraySet; class IntervalStats { public long beginTime; @@ -28,10 +28,11 @@ class IntervalStats { public final ArrayMap<String, UsageStats> stats = new ArrayMap<>(); public TimeSparseArray<UsageEvents.Event> events; - // Maps flattened string representations of component names to ComponentName. - // This helps save memory from using many duplicate ComponentNames and - // parse time when reading XML. - private final ArrayMap<String, ComponentName> mComponentNames = new ArrayMap<>(); + // A string cache. This is important as when we're parsing XML files, we don't want to + // keep hundreds of strings that have the same contents. We will read the string + // and only keep it if it's not in the cache. The GC will take care of the + // strings that had identical copies in the cache. + private final ArraySet<String> mStringCache = new ArraySet<>(); UsageStats getOrCreateUsageStats(String packageName) { UsageStats usageStats = stats.get(packageName); @@ -63,19 +64,21 @@ class IntervalStats { endTime = timeStamp; } - /** - * Return a ComponentName for the given string representation. This will use a cached - * copy of the ComponentName if possible, otherwise it will parse and add it to the - * internal cache. - */ - ComponentName getCachedComponentName(String str) { - ComponentName name = mComponentNames.get(str); - if (name == null) { - name = ComponentName.unflattenFromString(str); - if (name != null) { - mComponentNames.put(str, name); - } + private String getCachedStringRef(String str) { + final int index = mStringCache.indexOf(str); + if (index < 0) { + mStringCache.add(str); + return str; + } + return mStringCache.valueAt(index); + } + + UsageEvents.Event buildEvent(String packageName, String className) { + UsageEvents.Event event = new UsageEvents.Event(); + event.mPackage = getCachedStringRef(packageName); + if (className != null) { + event.mClass = getCachedStringRef(className); } - return name; + return event; } } diff --git a/services/usage/java/com/android/server/usage/UsageStatsService.java b/services/usage/java/com/android/server/usage/UsageStatsService.java index 82e837d..0e8b427 100644 --- a/services/usage/java/com/android/server/usage/UsageStatsService.java +++ b/services/usage/java/com/android/server/usage/UsageStatsService.java @@ -354,7 +354,8 @@ public class UsageStatsService extends SystemService implements } UsageEvents.Event event = new UsageEvents.Event(); - event.mComponent = component; + event.mPackage = component.getPackageName(); + event.mClass = component.getClassName(); event.mTimeStamp = timeStamp; event.mEventType = eventType; mHandler.obtainMessage(MSG_REPORT_EVENT, userId, 0, event).sendToTarget(); diff --git a/services/usage/java/com/android/server/usage/UsageStatsXmlV1.java b/services/usage/java/com/android/server/usage/UsageStatsXmlV1.java index 916601b..374429a 100644 --- a/services/usage/java/com/android/server/usage/UsageStatsXmlV1.java +++ b/services/usage/java/com/android/server/usage/UsageStatsXmlV1.java @@ -37,6 +37,8 @@ final class UsageStatsXmlV1 { private static final String END_TIME_ATTR = "endTime"; private static final String PACKAGE_TAG = "package"; private static final String NAME_ATTR = "name"; + private static final String PACKAGE_ATTR = "package"; + private static final String CLASS_ATTR = "class"; private static final String TOTAL_TIME_ACTIVE_ATTR = "totalTimeActive"; private static final String LAST_TIME_ACTIVE_ATTR = "lastTimeActive"; private static final String LAST_EVENT_ATTR = "lastEvent"; @@ -80,18 +82,27 @@ final class UsageStatsXmlV1 { return null; } - final String componentName = XmlUtils.readStringAttribute(parser, NAME_ATTR); - if (componentName == null) { - throw new ProtocolException("no " + NAME_ATTR + " attribute present"); - } + String packageName = XmlUtils.readStringAttribute(parser, PACKAGE_ATTR); + String className; + if (packageName == null) { + // Try getting the component name if it exists. + final String componentName = XmlUtils.readStringAttribute(parser, NAME_ATTR); + if (componentName == null) { + throw new ProtocolException("no " + NAME_ATTR + " or " + PACKAGE_ATTR + + " attribute present"); + } + ComponentName component = ComponentName.unflattenFromString(componentName); + if (component == null) { + throw new ProtocolException("ComponentName " + componentName + " is invalid"); + } - ComponentName component = statsOut.getCachedComponentName(componentName); - if (component == null) { - throw new ProtocolException("ComponentName " + componentName + " is invalid"); + packageName = component.getPackageName(); + className = component.getClassName(); + } else { + className = XmlUtils.readStringAttribute(parser, CLASS_ATTR); } - UsageEvents.Event event = new UsageEvents.Event(); - event.mComponent = component; + UsageEvents.Event event = statsOut.buildEvent(packageName, className); event.mEventType = XmlUtils.readIntAttribute(parser, TYPE_ATTR); event.mTimeStamp = XmlUtils.readLongAttribute(parser, TIME_ATTR); XmlUtils.skipCurrentTag(parser); @@ -112,7 +123,10 @@ final class UsageStatsXmlV1 { private static void writeEvent(FastXmlSerializer serializer, UsageEvents.Event event) throws IOException { serializer.startTag(null, EVENT_LOG_TAG); - serializer.attribute(null, NAME_ATTR, event.getComponent().flattenToString()); + serializer.attribute(null, PACKAGE_ATTR, event.mPackage); + if (event.mClass != null) { + serializer.attribute(null, CLASS_ATTR, event.mClass); + } serializer.attribute(null, TYPE_ATTR, Integer.toString(event.getEventType())); serializer.attribute(null, TIME_ATTR, Long.toString(event.getTimeStamp())); serializer.endTag(null, EVENT_LOG_TAG); diff --git a/services/usage/java/com/android/server/usage/UserUsageStatsService.java b/services/usage/java/com/android/server/usage/UserUsageStatsService.java index e489fb3..6951590 100644 --- a/services/usage/java/com/android/server/usage/UserUsageStatsService.java +++ b/services/usage/java/com/android/server/usage/UserUsageStatsService.java @@ -20,7 +20,6 @@ import android.app.usage.TimeSparseArray; import android.app.usage.UsageEvents; import android.app.usage.UsageStats; import android.app.usage.UsageStatsManager; -import android.content.ComponentName; import android.util.ArraySet; import android.util.Slog; @@ -114,7 +113,7 @@ class UserUsageStatsService { void reportEvent(UsageEvents.Event event) { if (DEBUG) { - Slog.d(TAG, mLogPrefix + "Got usage event for " + event.getComponent().getPackageName() + Slog.d(TAG, mLogPrefix + "Got usage event for " + event.mPackage + "[" + event.getTimeStamp() + "]: " + eventToString(event.getEventType())); } @@ -130,7 +129,7 @@ class UserUsageStatsService { mCurrentStats[UsageStatsManager.INTERVAL_DAILY].events.put(event.getTimeStamp(), event); for (IntervalStats stats : mCurrentStats) { - stats.update(event.getComponent().getPackageName(), event.getTimeStamp(), + stats.update(event.mPackage, event.getTimeStamp(), event.getEventType()); } @@ -203,17 +202,21 @@ class UserUsageStatsService { return null; } - ArraySet<ComponentName> names = new ArraySet<>(); + ArraySet<String> names = new ArraySet<>(); ArrayList<UsageEvents.Event> results = new ArrayList<>(); final int size = events.size(); for (int i = startIndex; i < size; i++) { if (events.keyAt(i) >= endTime) { break; } - names.add(events.valueAt(i).getComponent()); - results.add(events.valueAt(i)); + final UsageEvents.Event event = events.valueAt(i); + names.add(event.mPackage); + if (event.mClass != null) { + names.add(event.mClass); + } + results.add(event); } - ComponentName[] table = names.toArray(new ComponentName[names.size()]); + String[] table = names.toArray(new String[names.size()]); Arrays.sort(table); return new UsageEvents(results, table); } |