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 interrupts 31 */ 32 public class InterruptItem implements IItem { 33 /** Constant for JSON output */ 34 public static final String INTERRUPTS = "INTERRUPT_INFO"; 35 36 private Collection<InterruptInfoItem> mInterrupts = new LinkedList<InterruptInfoItem>(); 37 38 /** 39 * Enum for describing the type of interrupt 40 */ 41 public enum InterruptCategory { 42 WIFI_INTERRUPT, 43 MODEM_INTERRUPT, 44 ALARM_INTERRUPT, 45 ADSP_INTERRUPT, 46 UNKNOWN_INTERRUPT, 47 } 48 49 public static class InterruptInfoItem extends GenericItem { 50 /** Constant for JSON output */ 51 public static final String NAME = "NAME"; 52 /** Constant for JSON output */ 53 public static final String CATEGORY = "CATEGORY"; 54 /** Constant for JSON output */ 55 public static final String INTERRUPT_COUNT = "INTERRUPT_COUNT"; 56 57 private static final Set<String> ATTRIBUTES = new HashSet<String>(Arrays.asList( 58 NAME, INTERRUPT_COUNT, CATEGORY)); 59 60 /** 61 * The constructor for {@link InterruptItem} 62 * 63 * @param name The name of the wake lock 64 * @param interruptCount The number of times the interrupt woke up the AP 65 * @param category The {@link InterruptCategory} of the interrupt 66 */ InterruptInfoItem(String name, int interruptCount, InterruptCategory category)67 public InterruptInfoItem(String name, int interruptCount, 68 InterruptCategory category) { 69 super(ATTRIBUTES); 70 71 setAttribute(NAME, name); 72 setAttribute(INTERRUPT_COUNT, interruptCount); 73 setAttribute(CATEGORY, category); 74 } 75 76 /** 77 * Get the name of the interrupt 78 */ getName()79 public String getName() { 80 return (String) getAttribute(NAME); 81 } 82 83 /** 84 * Get the interrupt count. 85 */ getInterruptCount()86 public int getInterruptCount() { 87 return (Integer) getAttribute(INTERRUPT_COUNT); 88 } 89 90 /** 91 * Get the {@link InterruptCategory} of the wake lock. 92 */ getCategory()93 public InterruptCategory getCategory() { 94 return (InterruptCategory) getAttribute(CATEGORY); 95 } 96 } 97 98 /** 99 * Add an interrupt from the battery info section. 100 * 101 * @param name The name of the interrupt 102 * @param interruptCount Number of interrupts 103 * @param category The {@link InterruptCategory} of the interrupt. 104 */ addInterrupt(String name, int interruptCount, InterruptCategory category)105 public void addInterrupt(String name, int interruptCount, 106 InterruptCategory category) { 107 mInterrupts.add(new InterruptInfoItem(name, interruptCount, category)); 108 } 109 110 /** 111 * Get a list of {@link InterruptInfoItem} objects matching a given {@link InterruptCategory}. 112 */ getInterrupts(InterruptCategory category)113 public List<InterruptInfoItem> getInterrupts(InterruptCategory category) { 114 LinkedList<InterruptInfoItem> interrupts = new LinkedList<InterruptInfoItem>(); 115 if (category == null) { 116 return interrupts; 117 } 118 119 for (InterruptInfoItem interrupt : mInterrupts) { 120 if (category.equals(interrupt.getCategory())) { 121 interrupts.add(interrupt); 122 } 123 } 124 return interrupts; 125 } 126 127 /** 128 * Get a list of {@link InterruptInfoItem} objects 129 */ getInterrupts()130 public List<InterruptInfoItem> getInterrupts() { 131 return (List<InterruptInfoItem>) mInterrupts; 132 } 133 134 /** 135 * {@inheritDoc} 136 */ 137 @Override merge(IItem other)138 public IItem merge(IItem other) throws ConflictingItemException { 139 throw new ConflictingItemException("Wakelock items cannot be merged"); 140 } 141 142 /** 143 * {@inheritDoc} 144 */ 145 @Override isConsistent(IItem other)146 public boolean isConsistent(IItem other) { 147 return false; 148 } 149 150 /** 151 * {@inheritDoc} 152 */ 153 @Override toJson()154 public JSONObject toJson() { 155 JSONObject object = new JSONObject(); 156 if (mInterrupts != null) { 157 try { 158 JSONArray interrupts = new JSONArray(); 159 for (InterruptInfoItem interrupt : mInterrupts) { 160 interrupts.put(interrupt.toJson()); 161 } 162 object.put(INTERRUPTS, interrupts); 163 } catch (JSONException e) { 164 // Ignore 165 } 166 } 167 return object; 168 } 169 } 170