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.HashMap; 20 import java.util.Set; 21 22 import com.google.common.collect.ImmutableMap; 23 24 public class AppPower extends AppInfo { 25 private ImmutableMap<Component, ComponentPower> mComponents; 26 27 private double mAppPowerMah; 28 29 AppPower()30 private AppPower() { 31 } 32 33 /** 34 * Returns the {@link ComponentPower} for the {@link Component} provided, 35 * or null if this AppPower does not have that component. 36 * @more 37 * If the component was in the power profile for this device, there 38 * will be a component for it, even if there was no power used 39 * by that component. In that case, the 40 * {@link ComponentPower.getUsage() ComponentPower.getUsage()} 41 * method will return 0. 42 */ getComponentPower(Component component)43 public ComponentPower getComponentPower(Component component) { 44 return mComponents.get(component); 45 } 46 getComponents()47 public Set<Component> getComponents() { 48 return mComponents.keySet(); 49 } 50 51 /** 52 * Return the total power used by this app. 53 */ getAppPowerMah()54 public double getAppPowerMah() { 55 return mAppPowerMah; 56 } 57 58 /** 59 * Builder class for {@link AppPower} 60 */ 61 public static class Builder extends AppInfo.Builder<AppPower> { 62 private HashMap<Component, ComponentPower> mComponents = new HashMap(); 63 Builder()64 public Builder() { 65 } 66 build()67 public AppPower build() { 68 final AppPower result = new AppPower(); 69 init(result); 70 result.mComponents = ImmutableMap.copyOf(mComponents); 71 72 // Add up the components 73 double appPowerMah = 0; 74 for (final ComponentPower componentPower: mComponents.values()) { 75 appPowerMah += componentPower.powerMah; 76 } 77 result.mAppPowerMah = appPowerMah; 78 79 return result; 80 } 81 addComponentPower(Component component, ComponentPower componentPower)82 public void addComponentPower(Component component, ComponentPower componentPower) { 83 mComponents.put(component, componentPower); 84 } 85 } 86 } 87