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 17 package com.android.loganalysis.rule; 18 19 import com.android.loganalysis.item.BugreportItem; 20 import com.android.loganalysis.item.WakelockItem; 21 import com.android.loganalysis.item.WakelockItem.WakelockInfoItem; 22 import com.android.loganalysis.util.NumberFormattingUtil; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 27 import org.json.JSONException; 28 import org.json.JSONObject; 29 30 /** 31 * Rules definition for wakelock 32 */ 33 public class WakelockRule extends AbstractPowerRule { 34 35 private static final String WAKELOCK_ANALYSIS = "WAKELOCK_ANALYSIS"; 36 private static final float WAKELOCK_HELD_TIME_THRESHOLD_PERCENTAGE = 0.1f; // 10% 37 38 private List<WakelockInfoItem> mOffendingWakelockList; 39 WakelockRule(BugreportItem bugreportItem)40 public WakelockRule (BugreportItem bugreportItem) { 41 super(bugreportItem); 42 } 43 44 @Override applyRule()45 public void applyRule() { 46 mOffendingWakelockList = new ArrayList<WakelockInfoItem>(); 47 WakelockItem wakelockItem = getDetailedAnalysisItem().getWakelockItem(); 48 if (wakelockItem != null && getTimeOnBattery() > 0) { 49 long wakelockThreshold = (long)(getTimeOnBattery() 50 * WAKELOCK_HELD_TIME_THRESHOLD_PERCENTAGE); 51 52 for (WakelockInfoItem wakelocks : wakelockItem.getWakeLocks()) { 53 if (wakelocks.getHeldTime() > wakelockThreshold) { 54 mOffendingWakelockList.add(wakelocks); 55 } 56 } 57 } 58 } 59 60 @Override getAnalysis()61 public JSONObject getAnalysis() { 62 JSONObject wakelockAnalysis = new JSONObject(); 63 64 StringBuilder analysis = new StringBuilder(); 65 if (mOffendingWakelockList == null || mOffendingWakelockList.size() <= 0) { 66 analysis.append("No wakelocks were held for more than 10% of time on battery."); 67 } else { 68 for (WakelockInfoItem wakelocks : mOffendingWakelockList) { 69 analysis.append(String.format("%s %s is held for %s. ", wakelocks.getName(), 70 wakelocks.getCategory(), 71 NumberFormattingUtil.getDuration(wakelocks.getHeldTime()))); 72 } 73 } 74 try { 75 wakelockAnalysis.put(WAKELOCK_ANALYSIS, analysis.toString().trim()); 76 } catch (JSONException e) { 77 // do nothing 78 } 79 return wakelockAnalysis; 80 } 81 } 82