Home | History | Annotate | Download | only in item
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.loganalysis.item;
     17 
     18 import org.json.JSONArray;
     19 import org.json.JSONException;
     20 import org.json.JSONObject;
     21 
     22 import java.util.Arrays;
     23 import java.util.Date;
     24 import java.util.HashSet;
     25 import java.util.Set;
     26 
     27 /**
     28  * An {@link IItem} used to store monkey log info.
     29  */
     30 public class MonkeyLogItem extends GenericItem {
     31     @SuppressWarnings("serial")
     32     private class StringSet extends HashSet<String> {}
     33 
     34     public enum DroppedCategory {
     35         KEYS,
     36         POINTERS,
     37         TRACKBALLS,
     38         FLIPS,
     39         ROTATIONS
     40     }
     41 
     42     /** Constant for JSON output */
     43     public static final String START_TIME = "START_TIME";
     44     /** Constant for JSON output */
     45     public static final String STOP_TIME = "STOP_TIME";
     46     /** Constant for JSON output */
     47     public static final String PACKAGES = "PACKAGES";
     48     /** Constant for JSON output */
     49     public static final String CATEGORIES = "CATEGORIES";
     50     /** Constant for JSON output */
     51     public static final String THROTTLE = "THROTTLE";
     52     /** Constant for JSON output */
     53     public static final String SEED = "SEED";
     54     /** Constant for JSON output */
     55     public static final String TARGET_COUNT = "TARGET_COUNT";
     56     /** Constant for JSON output */
     57     public static final String IGNORE_SECURITY_EXCEPTIONS = "IGNORE_SECURITY_EXCEPTIONS";
     58     /** Constant for JSON output */
     59     public static final String TOTAL_DURATION = "TOTAL_TIME";
     60     /** Constant for JSON output */
     61     public static final String START_UPTIME_DURATION = "START_UPTIME";
     62     /** Constant for JSON output */
     63     public static final String STOP_UPTIME_DURATION = "STOP_UPTIME";
     64     /** Constant for JSON output */
     65     public static final String IS_FINISHED = "IS_FINISHED";
     66     /** Constant for JSON output */
     67     public static final String NO_ACTIVITIES = "NO_ACTIVITIES";
     68     /** Constant for JSON output */
     69     public static final String INTERMEDIATE_COUNT = "INTERMEDIATE_COUNT";
     70     /** Constant for JSON output */
     71     public static final String FINAL_COUNT = "FINAL_COUNT";
     72     /** Constant for JSON output */
     73     public static final String CRASH = "CRASH";
     74 
     75     private static final Set<String> ATTRIBUTES = new HashSet<String>(Arrays.asList(
     76             START_TIME, STOP_TIME, PACKAGES, CATEGORIES, THROTTLE, SEED, TARGET_COUNT,
     77             IGNORE_SECURITY_EXCEPTIONS, TOTAL_DURATION, START_UPTIME_DURATION, STOP_UPTIME_DURATION,
     78             IS_FINISHED, NO_ACTIVITIES, INTERMEDIATE_COUNT, FINAL_COUNT, CRASH,
     79             DroppedCategory.KEYS.toString(),
     80             DroppedCategory.POINTERS.toString(),
     81             DroppedCategory.TRACKBALLS.toString(),
     82             DroppedCategory.FLIPS.toString(),
     83             DroppedCategory.ROTATIONS.toString()));
     84 
     85     /**
     86      * The constructor for {@link MonkeyLogItem}.
     87      */
     88     public MonkeyLogItem() {
     89         super(ATTRIBUTES);
     90 
     91         setAttribute(PACKAGES, new StringSet());
     92         setAttribute(CATEGORIES, new StringSet());
     93         setAttribute(THROTTLE, 0);
     94         setAttribute(IGNORE_SECURITY_EXCEPTIONS, false);
     95         setAttribute(IS_FINISHED, false);
     96         setAttribute(NO_ACTIVITIES, false);
     97         setAttribute(INTERMEDIATE_COUNT, 0);
     98     }
     99 
    100     /**
    101      * Get the start time of the monkey log.
    102      */
    103     public Date getStartTime() {
    104         return (Date) getAttribute(START_TIME);
    105     }
    106 
    107     /**
    108      * Set the start time of the monkey log.
    109      */
    110     public void setStartTime(Date time) {
    111         setAttribute(START_TIME, time);
    112     }
    113 
    114     /**
    115      * Get the stop time of the monkey log.
    116      */
    117     public Date getStopTime() {
    118         return (Date) getAttribute(STOP_TIME);
    119     }
    120 
    121     /**
    122      * Set the stop time of the monkey log.
    123      */
    124     public void setStopTime(Date time) {
    125         setAttribute(STOP_TIME, time);
    126     }
    127 
    128     /**
    129      * Get the set of packages that the monkey is run on.
    130      */
    131     public Set<String> getPackages() {
    132         return (StringSet) getAttribute(PACKAGES);
    133     }
    134 
    135     /**
    136      * Add a package to the set that the monkey is run on.
    137      */
    138     public void addPackage(String thePackage) {
    139         ((StringSet) getAttribute(PACKAGES)).add(thePackage);
    140     }
    141 
    142     /**
    143      * Get the set of categories that the monkey is run on.
    144      */
    145     public Set<String> getCategories() {
    146         return (StringSet) getAttribute(CATEGORIES);
    147     }
    148 
    149     /**
    150      * Add a category to the set that the monkey is run on.
    151      */
    152     public void addCategory(String category) {
    153         ((StringSet) getAttribute(CATEGORIES)).add(category);
    154     }
    155 
    156     /**
    157      * Get the throttle for the monkey run.
    158      */
    159     public int getThrottle() {
    160         return (Integer) getAttribute(THROTTLE);
    161     }
    162 
    163     /**
    164      * Set the throttle for the monkey run.
    165      */
    166     public void setThrottle(int throttle) {
    167         setAttribute(THROTTLE, throttle);
    168     }
    169 
    170     /**
    171      * Get the seed for the monkey run.
    172      */
    173     public Long getSeed() {
    174         return (Long) getAttribute(SEED);
    175     }
    176 
    177     /**
    178      * Set the seed for the monkey run.
    179      */
    180     public void setSeed(long seed) {
    181         setAttribute(SEED, seed);
    182     }
    183 
    184     /**
    185      * Get the target count for the monkey run.
    186      */
    187     public Integer getTargetCount() {
    188         return (Integer) getAttribute(TARGET_COUNT);
    189     }
    190 
    191     /**
    192      * Set the target count for the monkey run.
    193      */
    194     public void setTargetCount(int count) {
    195         setAttribute(TARGET_COUNT, count);
    196     }
    197 
    198     /**
    199      * Get if the ignore security exceptions flag is set for the monkey run.
    200      */
    201     public boolean getIgnoreSecurityExceptions() {
    202         return (Boolean) getAttribute(IGNORE_SECURITY_EXCEPTIONS);
    203     }
    204 
    205     /**
    206      * Set if the ignore security exceptions flag is set for the monkey run.
    207      */
    208     public void setIgnoreSecurityExceptions(boolean ignore) {
    209         setAttribute(IGNORE_SECURITY_EXCEPTIONS, ignore);
    210     }
    211 
    212     /**
    213      * Get the total duration of the monkey run in milliseconds.
    214      */
    215     public Long getTotalDuration() {
    216         return (Long) getAttribute(TOTAL_DURATION);
    217     }
    218 
    219     /**
    220      * Set the total duration of the monkey run in milliseconds.
    221      */
    222     public void setTotalDuration(long time) {
    223         setAttribute(TOTAL_DURATION, time);
    224     }
    225 
    226     /**
    227      * Get the start uptime duration of the monkey run in milliseconds.
    228      */
    229     public Long getStartUptimeDuration() {
    230         return (Long) getAttribute(START_UPTIME_DURATION);
    231     }
    232 
    233     /**
    234      * Set the start uptime duration of the monkey run in milliseconds.
    235      */
    236     public void setStartUptimeDuration(long uptime) {
    237         setAttribute(START_UPTIME_DURATION, uptime);
    238     }
    239 
    240     /**
    241      * Get the stop uptime duration of the monkey run in milliseconds.
    242      */
    243     public Long getStopUptimeDuration() {
    244         return (Long) getAttribute(STOP_UPTIME_DURATION);
    245     }
    246 
    247     /**
    248      * Set the stop uptime duration of the monkey run in milliseconds.
    249      */
    250     public void setStopUptimeDuration(long uptime) {
    251         setAttribute(STOP_UPTIME_DURATION, uptime);
    252     }
    253 
    254     /**
    255      * Get if the monkey run finished without crashing.
    256      */
    257     public boolean getIsFinished() {
    258         return (Boolean) getAttribute(IS_FINISHED);
    259     }
    260 
    261     /**
    262      * Set if the monkey run finished without crashing.
    263      */
    264     public void setIsFinished(boolean finished) {
    265         setAttribute(IS_FINISHED, finished);
    266     }
    267 
    268     /**
    269      * Get if the monkey run aborted due to no activies to run.
    270      */
    271     public boolean getNoActivities() {
    272         return (Boolean) getAttribute(NO_ACTIVITIES);
    273     }
    274 
    275     /**
    276      * Set if the monkey run aborted due to no activies to run.
    277      */
    278     public void setNoActivities(boolean noActivities) {
    279         setAttribute(NO_ACTIVITIES, noActivities);
    280     }
    281 
    282 
    283     /**
    284      * Get the intermediate count for the monkey run.
    285      * <p>
    286      * This count starts at 0 and increments every 100 events. This number should be within 100 of
    287      * the final count.
    288      * </p>
    289      */
    290     public int getIntermediateCount() {
    291         return (Integer) getAttribute(INTERMEDIATE_COUNT);
    292     }
    293 
    294     /**
    295      * Set the intermediate count for the monkey run.
    296      * <p>
    297      * This count starts at 0 and increments every 100 events. This number should be within 100 of
    298      * the final count.
    299      * </p>
    300      */
    301     public void setIntermediateCount(int count) {
    302         setAttribute(INTERMEDIATE_COUNT, count);
    303     }
    304 
    305     /**
    306      * Get the final count for the monkey run.
    307      */
    308     public Integer getFinalCount() {
    309         return (Integer) getAttribute(FINAL_COUNT);
    310     }
    311 
    312     /**
    313      * Set the final count for the monkey run.
    314      */
    315     public void setFinalCount(int count) {
    316         setAttribute(FINAL_COUNT, count);
    317     }
    318 
    319     /**
    320      * Get the dropped events count for a {@link DroppedCategory} for the monkey run.
    321      */
    322     public Integer getDroppedCount(DroppedCategory category) {
    323         return (Integer) getAttribute(category.toString());
    324     }
    325 
    326     /**
    327      * Set the dropped events count for a {@link DroppedCategory} for the monkey run.
    328      */
    329     public void setDroppedCount(DroppedCategory category, int count) {
    330         setAttribute(category.toString(), count);
    331     }
    332 
    333     /**
    334      * Get the {@link AnrItem}, {@link JavaCrashItem}, or {@link NativeCrashItem} for the monkey run
    335      * or null if there was no crash.
    336      */
    337     public MiscLogcatItem getCrash() {
    338         return (MiscLogcatItem) getAttribute(CRASH);
    339     }
    340 
    341     /**
    342      * Set the {@link AnrItem}, {@link JavaCrashItem}, or {@link NativeCrashItem} for the monkey
    343      * run.
    344      */
    345     public void setCrash(MiscLogcatItem crash) {
    346         setAttribute(CRASH, crash);
    347     }
    348 
    349     /**
    350      * {@inheritDoc}
    351      */
    352     @Override
    353     public JSONObject toJson() {
    354         JSONObject object = super.toJson();
    355 
    356         // Override packages and categories
    357         put(object, PACKAGES, new JSONArray(getPackages()));
    358         put(object, CATEGORIES, new JSONArray(getCategories()));
    359 
    360         return object;
    361     }
    362 
    363     /**
    364      * Try to put an {@link Object} in a {@link JSONObject} and remove the existing key if it fails.
    365      */
    366     private static void put(JSONObject object, String key, Object value) {
    367         try {
    368             object.put(key, value);
    369         } catch (JSONException e) {
    370             object.remove(key);
    371         }
    372     }
    373 }
    374