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
17package com.android.benchmark.app;
18
19import android.graphics.Typeface;
20import android.view.LayoutInflater;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.BaseExpandableListAdapter;
24import android.widget.CheckBox;
25import android.widget.TextView;
26
27import com.android.benchmark.registry.BenchmarkGroup;
28import com.android.benchmark.registry.BenchmarkRegistry;
29import com.android.benchmark.R;
30
31/**
32 *
33 */
34public class BenchmarkListAdapter extends BaseExpandableListAdapter {
35
36    private final LayoutInflater mInflater;
37    private final BenchmarkRegistry mRegistry;
38
39    BenchmarkListAdapter(LayoutInflater inflater,
40                         BenchmarkRegistry registry) {
41        mInflater = inflater;
42        mRegistry = registry;
43    }
44
45    @Override
46    public int getGroupCount() {
47        return mRegistry.getGroupCount();
48    }
49
50    @Override
51    public int getChildrenCount(int groupPosition) {
52        return mRegistry.getBenchmarkCount(groupPosition);
53    }
54
55    @Override
56    public Object getGroup(int groupPosition) {
57        return mRegistry.getBenchmarkGroup(groupPosition);
58    }
59
60    @Override
61    public Object getChild(int groupPosition, int childPosition) {
62        BenchmarkGroup benchmarkGroup = mRegistry.getBenchmarkGroup(groupPosition);
63
64        if (benchmarkGroup != null) {
65           return benchmarkGroup.getBenchmarks()[childPosition];
66        }
67
68        return null;
69    }
70
71    @Override
72    public long getGroupId(int groupPosition) {
73        return groupPosition;
74    }
75
76    @Override
77    public long getChildId(int groupPosition, int childPosition) {
78        return childPosition;
79    }
80
81    @Override
82    public boolean hasStableIds() {
83        return false;
84    }
85
86    @Override
87    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
88        BenchmarkGroup group = (BenchmarkGroup) getGroup(groupPosition);
89        if (convertView == null) {
90            convertView = mInflater.inflate(R.layout.benchmark_list_group_row, null);
91        }
92
93        TextView title = (TextView) convertView.findViewById(R.id.group_name);
94        title.setTypeface(null, Typeface.BOLD);
95        title.setText(group.getTitle());
96        return convertView;
97    }
98
99    @Override
100    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
101                             View convertView, ViewGroup parent) {
102        BenchmarkGroup.Benchmark benchmark =
103                (BenchmarkGroup.Benchmark) getChild(groupPosition, childPosition);
104        if (convertView == null) {
105            convertView = mInflater.inflate(R.layout.benchmark_list_item, null);
106        }
107
108        TextView name = (TextView) convertView.findViewById(R.id.benchmark_name);
109        name.setText(benchmark.getName());
110        CheckBox enabledBox = (CheckBox) convertView.findViewById(R.id.benchmark_enable_checkbox);
111        enabledBox.setOnClickListener(benchmark);
112        enabledBox.setChecked(benchmark.isEnabled());
113
114        return convertView;
115    }
116
117    @Override
118    public boolean isChildSelectable(int groupPosition, int childPosition) {
119        return true;
120    }
121
122    public int getChildrenHeight() {
123        // TODO
124        return 1024;
125    }
126}
127