1 /* 2 * Copyright (C) 2018 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.powermodel; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 import java.util.Set; 22 23 import com.google.common.collect.ImmutableMap; 24 25 /** 26 * PowerReport contains the summary of all power used on a device 27 * as reported by batterystats or statsd, based on the power profile. 28 */ 29 public class PowerReport { 30 private AppList<AppPower> mApps; 31 private double mTotalPowerMah; 32 PowerReport()33 private PowerReport() { 34 } 35 36 /** 37 * The total power used by this device for this PowerReport. 38 */ getTotalPowerMah()39 public double getTotalPowerMah() { 40 return mTotalPowerMah; 41 } 42 getAllApps()43 public List<AppPower> getAllApps() { 44 return mApps.getAllApps(); 45 } 46 getRegularApps()47 public List<AppPower> getRegularApps() { 48 return mApps.getRegularApps(); 49 } 50 findApp(String pkg)51 public List<AppPower> findApp(String pkg) { 52 return mApps.findApp(pkg); 53 } 54 findApp(SpecialApp specialApp)55 public AppPower findApp(SpecialApp specialApp) { 56 return mApps.findApp(specialApp); 57 } 58 createReport(PowerProfile profile, ActivityReport activityReport)59 public static PowerReport createReport(PowerProfile profile, ActivityReport activityReport) { 60 final PowerReport.Builder powerReport = new PowerReport.Builder(); 61 for (final AppActivity appActivity: activityReport.getAllApps()) { 62 final AppPower.Builder appPower = new AppPower.Builder(); 63 appPower.setAttribution(appActivity.getAttribution()); 64 65 for (final ImmutableMap.Entry<Component,ComponentActivity> entry: 66 appActivity.getComponentActivities().entrySet()) { 67 final ComponentPower componentPower = entry.getValue() 68 .applyProfile(activityReport, profile); 69 if (componentPower != null) { 70 appPower.addComponentPower(entry.getKey(), componentPower); 71 } 72 } 73 74 powerReport.add(appPower); 75 } 76 return powerReport.build(); 77 } 78 79 private static class Builder { 80 private AppList.Builder mApps = new AppList.Builder(); 81 Builder()82 public Builder() { 83 } 84 build()85 public PowerReport build() { 86 final PowerReport report = new PowerReport(); 87 88 report.mApps = mApps.build(); 89 90 for (AppPower app: report.mApps.getAllApps()) { 91 report.mTotalPowerMah += app.getAppPowerMah(); 92 } 93 94 return report; 95 } 96 add(AppPower.Builder app)97 public void add(AppPower.Builder app) { 98 mApps.put(app.getAttribution(), app); 99 } 100 } 101 } 102