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 BatteryStatsDetailedInfoItem implements IItem { 25 26 /** Constant for JSON output */ 27 public static final String TIME_ON_BATTERY = "TIME_ON_BATTERY"; 28 /** Constant for JSON output */ 29 public static final String SCREEN_ON_TIME = "SCREEN_ON_TIME"; 30 /** Constant for JSON output */ 31 public static final String BATTERY_USAGE = "BATTERY_USAGE"; 32 /** Constant for JSON output */ 33 public static final String WAKELOCKS = "WAKELOCKS"; 34 /** Constant for JSON output */ 35 public static final String INTERRUPTS = "INTERRUPTS"; 36 /** Constant for JSON output */ 37 public static final String PROCESS_USAGE = "PROCESS_USAGE"; 38 39 private long mTimeOnBattery = 0; 40 private long mScreenOnTime = 0; 41 private BatteryUsageItem mBatteryUsageItem = null; 42 private WakelockItem mWakelockItem = null; 43 private InterruptItem mInterruptItem = null; 44 private ProcessUsageItem mprocessUsageItem = null; 45 46 /** 47 * Set the time on battery 48 */ setTimeOnBattery(long timeOnBattery)49 public void setTimeOnBattery(long timeOnBattery) { 50 mTimeOnBattery = timeOnBattery; 51 } 52 53 /** 54 * Set the time on battery 55 */ setScreenOnTime(long screenOnTime)56 public void setScreenOnTime(long screenOnTime) { 57 mScreenOnTime = screenOnTime; 58 } 59 60 /** 61 * Set the wakelock summary {@link WakelockItem} 62 */ setWakelockItem(WakelockItem wakelockItem)63 public void setWakelockItem(WakelockItem wakelockItem) { 64 mWakelockItem = wakelockItem; 65 } 66 67 /** 68 * Set the interrupt summary {@link InterruptItem} 69 */ setInterruptItem(InterruptItem interruptItem)70 public void setInterruptItem(InterruptItem interruptItem) { 71 mInterruptItem = interruptItem; 72 } 73 74 /** 75 * Set the process usage {@link ProcessUsageItem} 76 */ setProcessUsageItem(ProcessUsageItem processUsageItem)77 public void setProcessUsageItem(ProcessUsageItem processUsageItem) { 78 mprocessUsageItem = processUsageItem; 79 } 80 81 /** 82 * Set the process usage {@link BatteryUsageItem} 83 */ setBatteryUsageItem(BatteryUsageItem batteryUsageItem)84 public void setBatteryUsageItem(BatteryUsageItem batteryUsageItem) { 85 mBatteryUsageItem = batteryUsageItem; 86 } 87 88 /** 89 * Get the time on battery 90 */ getTimeOnBattery()91 public long getTimeOnBattery() { 92 return mTimeOnBattery; 93 } 94 95 /** 96 * Get the screen on time 97 */ getScreenOnTime()98 public long getScreenOnTime() { 99 return mScreenOnTime; 100 } 101 102 /** 103 * Get the wakelock summary {@link WakelockItem} 104 */ getWakelockItem()105 public WakelockItem getWakelockItem() { 106 return mWakelockItem; 107 } 108 109 /** 110 * Get the interrupt summary {@link InterruptItem} 111 */ getInterruptItem()112 public InterruptItem getInterruptItem() { 113 return mInterruptItem; 114 } 115 116 /** 117 * Get the process usage summary {@link ProcessUsageItem} 118 */ getProcessUsageItem()119 public ProcessUsageItem getProcessUsageItem() { 120 return mprocessUsageItem; 121 } 122 123 /** 124 * Get the battery usage summary {@link BatteryUsageItem} 125 */ getBatteryUsageItem()126 public BatteryUsageItem getBatteryUsageItem() { 127 return mBatteryUsageItem; 128 } 129 130 /** 131 * {@inheritDoc} 132 */ 133 @Override merge(IItem other)134 public IItem merge(IItem other) throws ConflictingItemException { 135 throw new ConflictingItemException("Dumpsys battery info items cannot be merged"); 136 } 137 138 /** 139 * {@inheritDoc} 140 */ 141 @Override isConsistent(IItem other)142 public boolean isConsistent(IItem other) { 143 return false; 144 } 145 146 /** 147 * {@inheritDoc} 148 */ 149 @Override toJson()150 public JSONObject toJson() { 151 JSONObject batteryStatsComponent = new JSONObject(); 152 try { 153 if (mTimeOnBattery > 0) { 154 batteryStatsComponent.put(TIME_ON_BATTERY, getTimeOnBattery()); 155 } 156 if (mScreenOnTime > 0) { 157 batteryStatsComponent.put(SCREEN_ON_TIME, getScreenOnTime()); 158 } 159 if (mBatteryUsageItem != null) { 160 batteryStatsComponent.put(BATTERY_USAGE, mBatteryUsageItem.toJson()); 161 } 162 if (mWakelockItem != null) { 163 batteryStatsComponent.put(WAKELOCKS, mWakelockItem.toJson()); 164 } 165 if (mInterruptItem != null) { 166 batteryStatsComponent.put(INTERRUPTS, mInterruptItem.toJson()); 167 } 168 if (mprocessUsageItem != null) { 169 batteryStatsComponent.put(PROCESS_USAGE, mprocessUsageItem.toJson()); 170 } 171 } catch (JSONException e) { 172 // ignore 173 } 174 return batteryStatsComponent; 175 } 176 } 177