PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/services/usage/java/com/android/server/usage/UsageStatsXmlV1.java

https://gitlab.com/amardeep434/nitro_base
Java | 271 lines | 181 code | 48 blank | 42 comment | 31 complexity | 8042414e609bdbcac3d5bc57105ffec8 MD5 | raw file
  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. package com.android.server.usage;
  17. import com.android.internal.util.XmlUtils;
  18. import org.xmlpull.v1.XmlPullParser;
  19. import org.xmlpull.v1.XmlPullParserException;
  20. import org.xmlpull.v1.XmlSerializer;
  21. import android.app.usage.ConfigurationStats;
  22. import android.app.usage.TimeSparseArray;
  23. import android.app.usage.UsageEvents;
  24. import android.app.usage.UsageStats;
  25. import android.content.res.Configuration;
  26. import java.io.IOException;
  27. import java.net.ProtocolException;
  28. /**
  29. * UsageStats reader/writer for version 1 of the XML format.
  30. */
  31. final class UsageStatsXmlV1 {
  32. private static final String PACKAGES_TAG = "packages";
  33. private static final String PACKAGE_TAG = "package";
  34. private static final String CONFIGURATIONS_TAG = "configurations";
  35. private static final String CONFIG_TAG = "config";
  36. private static final String EVENT_LOG_TAG = "event-log";
  37. private static final String EVENT_TAG = "event";
  38. // Attributes
  39. private static final String PACKAGE_ATTR = "package";
  40. private static final String CLASS_ATTR = "class";
  41. private static final String TOTAL_TIME_ACTIVE_ATTR = "timeActive";
  42. private static final String COUNT_ATTR = "count";
  43. private static final String ACTIVE_ATTR = "active";
  44. private static final String LAST_EVENT_ATTR = "lastEvent";
  45. private static final String TYPE_ATTR = "type";
  46. private static final String SHORTCUT_ID_ATTR = "shortcutId";
  47. // Time attributes stored as an offset of the beginTime.
  48. private static final String LAST_TIME_ACTIVE_ATTR = "lastTimeActive";
  49. private static final String END_TIME_ATTR = "endTime";
  50. private static final String TIME_ATTR = "time";
  51. private static void loadUsageStats(XmlPullParser parser, IntervalStats statsOut)
  52. throws IOException {
  53. final String pkg = parser.getAttributeValue(null, PACKAGE_ATTR);
  54. if (pkg == null) {
  55. throw new ProtocolException("no " + PACKAGE_ATTR + " attribute present");
  56. }
  57. final UsageStats stats = statsOut.getOrCreateUsageStats(pkg);
  58. // Apply the offset to the beginTime to find the absolute time.
  59. stats.mLastTimeUsed = statsOut.beginTime + XmlUtils.readLongAttribute(
  60. parser, LAST_TIME_ACTIVE_ATTR);
  61. stats.mTotalTimeInForeground = XmlUtils.readLongAttribute(parser, TOTAL_TIME_ACTIVE_ATTR);
  62. stats.mLastEvent = XmlUtils.readIntAttribute(parser, LAST_EVENT_ATTR);
  63. }
  64. private static void loadConfigStats(XmlPullParser parser, IntervalStats statsOut)
  65. throws XmlPullParserException, IOException {
  66. final Configuration config = new Configuration();
  67. Configuration.readXmlAttrs(parser, config);
  68. final ConfigurationStats configStats = statsOut.getOrCreateConfigurationStats(config);
  69. // Apply the offset to the beginTime to find the absolute time.
  70. configStats.mLastTimeActive = statsOut.beginTime + XmlUtils.readLongAttribute(
  71. parser, LAST_TIME_ACTIVE_ATTR);
  72. configStats.mTotalTimeActive = XmlUtils.readLongAttribute(parser, TOTAL_TIME_ACTIVE_ATTR);
  73. configStats.mActivationCount = XmlUtils.readIntAttribute(parser, COUNT_ATTR);
  74. if (XmlUtils.readBooleanAttribute(parser, ACTIVE_ATTR)) {
  75. statsOut.activeConfiguration = configStats.mConfiguration;
  76. }
  77. }
  78. private static void loadEvent(XmlPullParser parser, IntervalStats statsOut)
  79. throws XmlPullParserException, IOException {
  80. final String packageName = XmlUtils.readStringAttribute(parser, PACKAGE_ATTR);
  81. if (packageName == null) {
  82. throw new ProtocolException("no " + PACKAGE_ATTR + " attribute present");
  83. }
  84. final String className = XmlUtils.readStringAttribute(parser, CLASS_ATTR);
  85. final UsageEvents.Event event = statsOut.buildEvent(packageName, className);
  86. // Apply the offset to the beginTime to find the absolute time of this event.
  87. event.mTimeStamp = statsOut.beginTime + XmlUtils.readLongAttribute(parser, TIME_ATTR);
  88. event.mEventType = XmlUtils.readIntAttribute(parser, TYPE_ATTR);
  89. switch (event.mEventType) {
  90. case UsageEvents.Event.CONFIGURATION_CHANGE:
  91. event.mConfiguration = new Configuration();
  92. Configuration.readXmlAttrs(parser, event.mConfiguration);
  93. break;
  94. case UsageEvents.Event.SHORTCUT_INVOCATION:
  95. final String id = XmlUtils.readStringAttribute(parser, SHORTCUT_ID_ATTR);
  96. event.mShortcutId = (id != null) ? id.intern() : null;
  97. break;
  98. }
  99. if (statsOut.events == null) {
  100. statsOut.events = new TimeSparseArray<>();
  101. }
  102. statsOut.events.put(event.mTimeStamp, event);
  103. }
  104. private static void writeUsageStats(XmlSerializer xml, final IntervalStats stats,
  105. final UsageStats usageStats) throws IOException {
  106. xml.startTag(null, PACKAGE_TAG);
  107. // Write the time offset.
  108. XmlUtils.writeLongAttribute(xml, LAST_TIME_ACTIVE_ATTR,
  109. usageStats.mLastTimeUsed - stats.beginTime);
  110. XmlUtils.writeStringAttribute(xml, PACKAGE_ATTR, usageStats.mPackageName);
  111. XmlUtils.writeLongAttribute(xml, TOTAL_TIME_ACTIVE_ATTR, usageStats.mTotalTimeInForeground);
  112. XmlUtils.writeIntAttribute(xml, LAST_EVENT_ATTR, usageStats.mLastEvent);
  113. xml.endTag(null, PACKAGE_TAG);
  114. }
  115. private static void writeConfigStats(XmlSerializer xml, final IntervalStats stats,
  116. final ConfigurationStats configStats, boolean isActive) throws IOException {
  117. xml.startTag(null, CONFIG_TAG);
  118. // Write the time offset.
  119. XmlUtils.writeLongAttribute(xml, LAST_TIME_ACTIVE_ATTR,
  120. configStats.mLastTimeActive - stats.beginTime);
  121. XmlUtils.writeLongAttribute(xml, TOTAL_TIME_ACTIVE_ATTR, configStats.mTotalTimeActive);
  122. XmlUtils.writeIntAttribute(xml, COUNT_ATTR, configStats.mActivationCount);
  123. if (isActive) {
  124. XmlUtils.writeBooleanAttribute(xml, ACTIVE_ATTR, true);
  125. }
  126. // Now write the attributes representing the configuration object.
  127. Configuration.writeXmlAttrs(xml, configStats.mConfiguration);
  128. xml.endTag(null, CONFIG_TAG);
  129. }
  130. private static void writeEvent(XmlSerializer xml, final IntervalStats stats,
  131. final UsageEvents.Event event) throws IOException {
  132. xml.startTag(null, EVENT_TAG);
  133. // Store the time offset.
  134. XmlUtils.writeLongAttribute(xml, TIME_ATTR, event.mTimeStamp - stats.beginTime);
  135. XmlUtils.writeStringAttribute(xml, PACKAGE_ATTR, event.mPackage);
  136. if (event.mClass != null) {
  137. XmlUtils.writeStringAttribute(xml, CLASS_ATTR, event.mClass);
  138. }
  139. XmlUtils.writeIntAttribute(xml, TYPE_ATTR, event.mEventType);
  140. switch (event.mEventType) {
  141. case UsageEvents.Event.CONFIGURATION_CHANGE:
  142. if (event.mConfiguration != null) {
  143. Configuration.writeXmlAttrs(xml, event.mConfiguration);
  144. }
  145. break;
  146. case UsageEvents.Event.SHORTCUT_INVOCATION:
  147. if (event.mShortcutId != null) {
  148. XmlUtils.writeStringAttribute(xml, SHORTCUT_ID_ATTR, event.mShortcutId);
  149. }
  150. break;
  151. }
  152. xml.endTag(null, EVENT_TAG);
  153. }
  154. /**
  155. * Reads from the {@link XmlPullParser}, assuming that it is already on the
  156. * <code><usagestats></code> tag.
  157. *
  158. * @param parser The parser from which to read events.
  159. * @param statsOut The stats object to populate with the data from the XML file.
  160. */
  161. public static void read(XmlPullParser parser, IntervalStats statsOut)
  162. throws XmlPullParserException, IOException {
  163. statsOut.packageStats.clear();
  164. statsOut.configurations.clear();
  165. statsOut.activeConfiguration = null;
  166. if (statsOut.events != null) {
  167. statsOut.events.clear();
  168. }
  169. statsOut.endTime = statsOut.beginTime + XmlUtils.readLongAttribute(parser, END_TIME_ATTR);
  170. int eventCode;
  171. int outerDepth = parser.getDepth();
  172. while ((eventCode = parser.next()) != XmlPullParser.END_DOCUMENT
  173. && (eventCode != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
  174. if (eventCode != XmlPullParser.START_TAG) {
  175. continue;
  176. }
  177. final String tag = parser.getName();
  178. switch (tag) {
  179. case PACKAGE_TAG:
  180. loadUsageStats(parser, statsOut);
  181. break;
  182. case CONFIG_TAG:
  183. loadConfigStats(parser, statsOut);
  184. break;
  185. case EVENT_TAG:
  186. loadEvent(parser, statsOut);
  187. break;
  188. }
  189. }
  190. }
  191. /**
  192. * Writes the stats object to an XML file. The {@link XmlSerializer}
  193. * has already written the <code><usagestats></code> tag, but attributes may still
  194. * be added.
  195. *
  196. * @param xml The serializer to which to write the packageStats data.
  197. * @param stats The stats object to write to the XML file.
  198. */
  199. public static void write(XmlSerializer xml, IntervalStats stats) throws IOException {
  200. XmlUtils.writeLongAttribute(xml, END_TIME_ATTR, stats.endTime - stats.beginTime);
  201. xml.startTag(null, PACKAGES_TAG);
  202. final int statsCount = stats.packageStats.size();
  203. for (int i = 0; i < statsCount; i++) {
  204. writeUsageStats(xml, stats, stats.packageStats.valueAt(i));
  205. }
  206. xml.endTag(null, PACKAGES_TAG);
  207. xml.startTag(null, CONFIGURATIONS_TAG);
  208. final int configCount = stats.configurations.size();
  209. for (int i = 0; i < configCount; i++) {
  210. boolean active = stats.activeConfiguration.equals(stats.configurations.keyAt(i));
  211. writeConfigStats(xml, stats, stats.configurations.valueAt(i), active);
  212. }
  213. xml.endTag(null, CONFIGURATIONS_TAG);
  214. xml.startTag(null, EVENT_LOG_TAG);
  215. final int eventCount = stats.events != null ? stats.events.size() : 0;
  216. for (int i = 0; i < eventCount; i++) {
  217. writeEvent(xml, stats, stats.events.valueAt(i));
  218. }
  219. xml.endTag(null, EVENT_LOG_TAG);
  220. }
  221. private UsageStatsXmlV1() {
  222. }
  223. }