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  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and limitations under the
13  * License.
14  *
15  */
16 
17 package com.android.benchmark.registry;
18 
19 import android.content.ComponentName;
20 import android.content.Intent;
21 import android.view.View;
22 import android.widget.CheckBox;
23 
24 /**
25  * Logical grouping of benchmarks
26  */
27 public class BenchmarkGroup {
28     public static final String BENCHMARK_EXTRA_ENABLED_TESTS =
29             "com.android.benchmark.EXTRA_ENABLED_BENCHMARK_IDS";
30 
31     public static final String BENCHMARK_EXTRA_RUN_COUNT =
32             "com.android.benchmark.EXTRA_RUN_COUNT";
33     public static final String BENCHMARK_EXTRA_FINISH = "com.android.benchmark.FINISH_WHEN_DONE";
34 
35     public static class Benchmark implements CheckBox.OnClickListener {
36         /** The name of this individual benchmark test */
37         private final String mName;
38 
39         /** The category of this individual benchmark test */
40         private final @BenchmarkCategory int mCategory;
41 
42         /** Human-readable description of the benchmark */
43         private final String mDescription;
44 
45         private final int mId;
46 
47         private boolean mEnabled;
48 
Benchmark(int id, String name, @BenchmarkCategory int category, String description)49         Benchmark(int id, String name, @BenchmarkCategory int category, String description) {
50             mId = id;
51             mName = name;
52             mCategory = category;
53             mDescription = description;
54             mEnabled = true;
55         }
56 
isEnabled()57         public boolean isEnabled() { return mEnabled; }
58 
setEnabled(boolean enabled)59         public void setEnabled(boolean enabled) { mEnabled = enabled; }
60 
getId()61         public int getId() { return mId; }
62 
getDescription()63         public String getDescription() { return mDescription; }
64 
getCategory()65         public @BenchmarkCategory int getCategory() { return mCategory; }
66 
getName()67         public String getName() { return mName; }
68 
69         @Override
onClick(View view)70         public void onClick(View view) {
71             setEnabled(((CheckBox)view).isChecked());
72         }
73     }
74 
75     /**
76      * Component for this benchmark group.
77      */
78     private final ComponentName mComponentName;
79 
80     /**
81      * Benchmark title, showed in the {@link android.widget.ListView}
82      */
83     private final String mTitle;
84 
85     /**
86      * List of all benchmarks exported by this group
87      */
88     private final Benchmark[] mBenchmarks;
89 
90     /**
91      * The intent to launch the benchmark
92      */
93     private final Intent mIntent;
94 
95     /** Human-readable description of the benchmark group */
96     private final String mDescription;
97 
BenchmarkGroup(ComponentName componentName, String title, String description, Benchmark[] benchmarks, Intent intent)98     BenchmarkGroup(ComponentName componentName, String title,
99                    String description, Benchmark[] benchmarks, Intent intent) {
100         mComponentName = componentName;
101         mTitle = title;
102         mBenchmarks = benchmarks;
103         mDescription = description;
104         mIntent = intent;
105     }
106 
getIntent()107     public Intent getIntent() {
108         int[] enabledBenchmarksIds = getEnabledBenchmarksIds();
109         if (enabledBenchmarksIds.length != 0) {
110             mIntent.putExtra(BENCHMARK_EXTRA_ENABLED_TESTS, enabledBenchmarksIds);
111             return mIntent;
112         }
113 
114         return null;
115     }
116 
getComponentName()117     public ComponentName getComponentName() {
118         return mComponentName;
119     }
120 
getTitle()121     public String getTitle() {
122         return mTitle;
123     }
124 
getBenchmarks()125     public Benchmark[] getBenchmarks() {
126         return mBenchmarks;
127     }
128 
getDescription()129     public String getDescription() {
130         return mDescription;
131     }
132 
getEnabledBenchmarksIds()133     private int[] getEnabledBenchmarksIds() {
134         int enabledBenchmarkCount = 0;
135         for (int i = 0; i < mBenchmarks.length; i++) {
136             if (mBenchmarks[i].isEnabled()) {
137                 enabledBenchmarkCount++;
138             }
139         }
140 
141         int writeIndex = 0;
142         int[] enabledBenchmarks = new int[enabledBenchmarkCount];
143         for (int i = 0; i < mBenchmarks.length; i++) {
144             if (mBenchmarks[i].isEnabled()) {
145                 enabledBenchmarks[writeIndex++] = mBenchmarks[i].getId();
146             }
147         }
148 
149         return enabledBenchmarks;
150     }
151 }
152