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.Collection;
20 import java.util.EnumSet;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 
25 import com.google.common.collect.ImmutableList;
26 
27 /**
28  * ActivityReport contains the summary of the activity that consumes power
29  * as reported by batterystats or statsd.
30  */
31 public class ActivityReport {
32     private AppList<AppActivity> mApps;
33 
getAllApps()34     public ImmutableList<AppActivity> getAllApps() {
35         return mApps.getAllApps();
36     }
37 
getRegularApps()38     public ImmutableList<AppActivity> getRegularApps() {
39         return mApps.getRegularApps();
40     }
41 
findApp(String pkg)42     public List<AppActivity> findApp(String pkg) {
43         return mApps.findApp(pkg);
44     }
45 
findApp(SpecialApp specialApp)46     public AppActivity findApp(SpecialApp specialApp) {
47         return mApps.findApp(specialApp);
48     }
49 
50     /**
51      * Find a component in the GLOBAL app.
52      * <p>
53      * Returns null if either the global app doesn't exist (bad data?) or the component
54      * doesn't exist in the global app.
55      */
findGlobalComponent(Component component)56     public ComponentActivity findGlobalComponent(Component component) {
57          final AppActivity global = mApps.findApp(SpecialApp.GLOBAL);
58          if (global == null) {
59              return null;
60          }
61          return global.getComponentActivity(component);
62     }
63 
64     public static class Builder {
65         private AppList.Builder<AppActivity,AppActivity.Builder> mApps = new AppList.Builder();
66 
Builder()67         public Builder() {
68         }
69 
build()70         public ActivityReport build() {
71             final ActivityReport result = new ActivityReport();
72             result.mApps = mApps.build();
73             return result;
74         }
75 
addActivity(Component component, Collection<ComponentActivity> activities)76         public void addActivity(Component component, Collection<ComponentActivity> activities) {
77             for (final ComponentActivity activity: activities) {
78                 addActivity(component, activity);
79             }
80         }
81 
addActivity(Component component, ComponentActivity activity)82         public void addActivity(Component component, ComponentActivity activity) {
83             AppActivity.Builder app = mApps.get(activity.attribution);
84             if (app == null) {
85                 app = new AppActivity.Builder();
86                 app.setAttribution(activity.attribution);
87                 mApps.put(activity.attribution, app);
88             }
89             app.addComponentActivity(component, activity);
90         }
91     }
92 }
93