Home | History | Annotate | Download | only in item
      1 /*
      2  * Copyright (C) 2015 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.Calendar;
     24 import java.util.Collection;
     25 import java.util.HashSet;
     26 import java.util.LinkedList;
     27 import java.util.Set;
     28 
     29 /**
     30  * An {@link IItem} used to store information related to Battery discharge
     31  */
     32 public class BatteryDischargeItem implements IItem {
     33 
     34     /** Constant for JSON output */
     35     public static final String BATTERY_DISCHARGE = "BATTERY_DISCHARGE";
     36 
     37     private Collection<BatteryDischargeInfoItem> mBatteryDischargeInfo =
     38             new LinkedList<BatteryDischargeInfoItem>();
     39 
     40     public static class BatteryDischargeInfoItem extends GenericItem {
     41         /** Constant for JSON output */
     42         public static final String CLOCK_TIME_OF_DISCHARGE = "CLOCK_TIME_OF_DISCHARGE";
     43         /** Constant for JSON output */
     44         public static final String DISCHARGE_ELAPSED_TIME = "DISCHARGE_ELAPSED_TIME";
     45         /** Constant for JSON output */
     46         public static final String BATTERY_LEVEL = "BATTERY_LEVEL";
     47 
     48         private static final Set<String> ATTRIBUTES = new HashSet<String>(Arrays.asList(
     49                 CLOCK_TIME_OF_DISCHARGE, DISCHARGE_ELAPSED_TIME, BATTERY_LEVEL));
     50 
     51         /**
     52          * The constructor for {@link BatteryDischargeInfoItem}
     53          *
     54          * @param clockTime Clock time when the battery discharge happened
     55          * @param elapsedTime Time it took to discharge to the current battery level
     56          * @param batteryLevel Current battery level
     57          */
     58         public BatteryDischargeInfoItem(Calendar clockTime, long elapsedTime, int batteryLevel) {
     59             super(ATTRIBUTES);
     60 
     61             setAttribute(CLOCK_TIME_OF_DISCHARGE, clockTime);
     62             setAttribute(DISCHARGE_ELAPSED_TIME, elapsedTime);
     63             setAttribute(BATTERY_LEVEL, batteryLevel);
     64         }
     65 
     66         /**
     67          * Get the clock time when the battery level dropped
     68          */
     69         public Calendar getClockTime() {
     70             return (Calendar) getAttribute(CLOCK_TIME_OF_DISCHARGE);
     71         }
     72 
     73         /**
     74          * Get the time elapsed to discharge to the current battery level
     75          */
     76         public long getElapsedTime() {
     77             return (long) getAttribute(DISCHARGE_ELAPSED_TIME);
     78         }
     79 
     80         /**
     81          * Get the current battery level
     82          */
     83         public int getBatteryLevel() {
     84             return (int) getAttribute(BATTERY_LEVEL);
     85         }
     86     }
     87 
     88     /**
     89      * Add a battery discharge step from battery stats
     90      *
     91      * @param clockTime Clock time when the battery discharge happened
     92      * @param elapsedTime Time it took to discharge to the current battery level
     93      * @param batteryLevel Current battery level
     94      */
     95     public void addBatteryDischargeInfo(Calendar clockTime, long elapsedTime, int batteryLevel) {
     96         mBatteryDischargeInfo.add(new BatteryDischargeInfoItem(clockTime,
     97                 elapsedTime, batteryLevel));
     98     }
     99 
    100     public Collection<BatteryDischargeInfoItem> getDischargeStepsInfo() {
    101         return mBatteryDischargeInfo;
    102     }
    103 
    104     /**
    105      * {@inheritDoc}
    106      */
    107     @Override
    108     public IItem merge(IItem other) throws ConflictingItemException {
    109         throw new ConflictingItemException("Wakelock items cannot be merged");
    110     }
    111 
    112     /**
    113      * {@inheritDoc}
    114      */
    115     @Override
    116     public boolean isConsistent(IItem other) {
    117         return false;
    118     }
    119 
    120     /**
    121      * {@inheritDoc}
    122      */
    123     @Override
    124     public JSONObject toJson() {
    125         JSONObject object = new JSONObject();
    126         try {
    127             JSONArray batteryDischargeSteps = new JSONArray();
    128             for (BatteryDischargeInfoItem batteryDischargeStep : mBatteryDischargeInfo) {
    129                 batteryDischargeSteps.put(batteryDischargeStep.toJson());
    130             }
    131             object.put(BATTERY_DISCHARGE, batteryDischargeSteps);
    132         } catch (JSONException e) {
    133             // Ignore
    134         }
    135         return object;
    136     }
    137 }
    138