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.Collection; 24 import java.util.HashSet; 25 import java.util.LinkedList; 26 import java.util.List; 27 import java.util.Set; 28 29 /** 30 * An {@link IItem} used to store information related to battery usage stats 31 */ 32 public class BatteryUsageItem implements IItem { 33 34 /** Constant for JSON output */ 35 public static final String BATTERY_USAGE = "BATTERY_USAGE"; 36 /** Constant for JSON output */ 37 public static final String BATTERY_CAPACITY = "BATTERY_CAPACITY"; 38 39 private Collection<BatteryUsageInfoItem> mBatteryUsage = new LinkedList<BatteryUsageInfoItem>(); 40 41 private int mBatteryCapacity = 0; 42 43 public static class BatteryUsageInfoItem extends GenericItem { 44 /** Constant for JSON output */ 45 public static final String NAME = "NAME"; 46 /** Constant for JSON output */ 47 public static final String USAGE = "USAGE"; 48 49 private static final Set<String> ATTRIBUTES = new HashSet<String>(Arrays.asList( 50 NAME, USAGE)); 51 52 /** 53 * The constructor for {@link BatteryUsageItem} 54 * 55 * @param name The name of the wake lock 56 * @param usage Usage in mAh 57 */ BatteryUsageInfoItem(String name, double usage)58 public BatteryUsageInfoItem(String name, double usage) { 59 super(ATTRIBUTES); 60 setAttribute(NAME, name); 61 setAttribute(USAGE, usage); 62 } 63 64 /** 65 * Get the name of the wake lock. 66 */ getName()67 public String getName() { 68 return (String) getAttribute(NAME); 69 } 70 71 /** 72 * Get the battery usage 73 */ getUsage()74 public double getUsage() { 75 return (double) getAttribute(USAGE); 76 } 77 } 78 79 /** 80 * Add a battery usage from the battery stats section. 81 * 82 * @param name The name of the process 83 * @param usage The usage in mAh 84 */ addBatteryUsage(String name, double usage)85 public void addBatteryUsage(String name, double usage) { 86 mBatteryUsage.add(new BatteryUsageInfoItem(name, usage)); 87 } 88 getBatteryCapacity()89 public int getBatteryCapacity() { 90 return mBatteryCapacity; 91 } 92 getBatteryUsage()93 public List<BatteryUsageInfoItem> getBatteryUsage() { 94 return (List<BatteryUsageInfoItem>)mBatteryUsage; 95 } 96 setBatteryCapacity(int capacity)97 public void setBatteryCapacity(int capacity) { 98 mBatteryCapacity = capacity; 99 } 100 101 /** 102 * {@inheritDoc} 103 */ 104 @Override merge(IItem other)105 public IItem merge(IItem other) throws ConflictingItemException { 106 throw new ConflictingItemException("Wakelock items cannot be merged"); 107 } 108 109 /** 110 * {@inheritDoc} 111 */ 112 @Override isConsistent(IItem other)113 public boolean isConsistent(IItem other) { 114 return false; 115 } 116 117 /** 118 * {@inheritDoc} 119 */ 120 @Override toJson()121 public JSONObject toJson() { 122 JSONObject object = new JSONObject(); 123 if (mBatteryUsage != null) { 124 try { 125 object.put(BATTERY_CAPACITY, mBatteryCapacity); 126 JSONArray usageInfo = new JSONArray(); 127 for (BatteryUsageInfoItem usage : mBatteryUsage) { 128 usageInfo.put(usage.toJson()); 129 } 130 object.put(BATTERY_USAGE, usageInfo); 131 } catch (JSONException e) { 132 // Ignore 133 } 134 } 135 return object; 136 } 137 } 138