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.JSONException;
19 import org.json.JSONObject;
20 
21 /**
22  * An {@link IItem} used to store BatteryStats Info
23  */
24 public class DumpsysBatteryStatsItem implements IItem {
25 
26     /** Constant for JSON output */
27     public static final String SUMMARY = "SUMMARY";
28     /** Constant for JSON output */
29     public static final String DETAILED_STATS = "DETAILED_STATS";
30     /** Constant for JSON output */
31     public static final String DISCHARGE_STATS = "DISCHARGE_STATS";
32 
33     private BatteryStatsSummaryInfoItem mBatteryStatsSummaryItem;
34     private BatteryStatsDetailedInfoItem mDetailedBatteryStatsItem;
35     private BatteryDischargeStatsInfoItem mDischargeStatsItem;
36 
37     /**
38      * Set the battery stats summary {@link BatteryStatsSummaryInfoItem}
39      */
setBatteryStatsSummarytem(BatteryStatsSummaryInfoItem summaryItem)40     public void setBatteryStatsSummarytem(BatteryStatsSummaryInfoItem summaryItem) {
41         mBatteryStatsSummaryItem = summaryItem;
42     }
43 
44     /**
45      * Set the detailed battery stats item {@link BatteryStatsDetailedInfoItem}
46      */
setDetailedBatteryStatsItem(BatteryStatsDetailedInfoItem detailedItem)47     public void setDetailedBatteryStatsItem(BatteryStatsDetailedInfoItem detailedItem) {
48         mDetailedBatteryStatsItem = detailedItem;
49     }
50 
51     /**
52      * Set the battery steps info item {@link BatteryDischargeStatsInfoItem}
53      */
setBatteryDischargeStatsItem(BatteryDischargeStatsInfoItem item)54     public void setBatteryDischargeStatsItem(BatteryDischargeStatsInfoItem item){
55         mDischargeStatsItem = item;
56     }
57 
58     /**
59      * Get the battery stats summary {@link BatteryStatsSummaryInfoItem}
60      */
getBatteryStatsSummaryItem()61     public BatteryStatsSummaryInfoItem getBatteryStatsSummaryItem() {
62         return mBatteryStatsSummaryItem;
63     }
64 
65     /**
66      * Get the detailed battery stats item {@link BatteryStatsDetailedInfoItem}
67      */
getDetailedBatteryStatsItem()68     public BatteryStatsDetailedInfoItem getDetailedBatteryStatsItem() {
69         return mDetailedBatteryStatsItem;
70     }
71 
72     /**
73      * Get the battery steps info item {@link BatteryDischargeStatsInfoItem}
74      */
getBatteryDischargeStatsItem()75     public BatteryDischargeStatsInfoItem getBatteryDischargeStatsItem() {
76         return mDischargeStatsItem;
77     }
78 
79     /**
80      * {@inheritDoc}
81      */
82     @Override
merge(IItem other)83     public IItem merge(IItem other) throws ConflictingItemException {
84         throw new ConflictingItemException("Dumpsys battery info items cannot be merged");
85     }
86 
87     /**
88      * {@inheritDoc}
89      */
90     @Override
isConsistent(IItem other)91     public boolean isConsistent(IItem other) {
92         return false;
93     }
94 
95     /**
96      * {@inheritDoc}
97      */
98     @Override
toJson()99     public JSONObject toJson() {
100         JSONObject batteryStatsComponent = new JSONObject();
101         try {
102             if (mBatteryStatsSummaryItem != null) {
103                 batteryStatsComponent.put(SUMMARY, mBatteryStatsSummaryItem.toJson());
104             }
105             if (mDetailedBatteryStatsItem != null) {
106                 batteryStatsComponent.put(DETAILED_STATS, mDetailedBatteryStatsItem.toJson());
107             }
108             if (mDischargeStatsItem != null) {
109                 batteryStatsComponent.put(DISCHARGE_STATS, mDischargeStatsItem.toJson());
110             }
111         } catch (JSONException e) {
112             // ignore
113         }
114         return batteryStatsComponent;
115     }
116 }
117