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.content.Intent;
20import android.os.AsyncTask;
21import android.os.Bundle;
22import android.support.design.widget.FloatingActionButton;
23import android.support.v7.app.AppCompatActivity;
24import android.support.v7.widget.Toolbar;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.Menu;
28import android.view.MenuItem;
29import android.view.ViewGroup;
30import android.widget.Button;
31import android.widget.ExpandableListView;
32import android.widget.Toast;
33
34import com.android.benchmark.registry.BenchmarkRegistry;
35import com.android.benchmark.R;
36import com.android.benchmark.results.GlobalResultsStore;
37
38import java.io.IOException;
39import java.util.LinkedList;
40import java.util.Queue;
41
42public class HomeActivity extends AppCompatActivity implements Button.OnClickListener {
43
44    private FloatingActionButton mStartButton;
45    private BenchmarkRegistry mRegistry;
46    private Queue<Intent> mRunnableBenchmarks;
47
48    @Override
49    protected void onCreate(Bundle savedInstanceState) {
50        super.onCreate(savedInstanceState);
51        setContentView(R.layout.activity_home);
52
53        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
54        setSupportActionBar(toolbar);
55
56        mStartButton = (FloatingActionButton) findViewById(R.id.start_button);
57        mStartButton.setActivated(true);
58        mStartButton.setOnClickListener(this);
59
60        mRegistry = new BenchmarkRegistry(this);
61
62        mRunnableBenchmarks = new LinkedList<>();
63
64        ExpandableListView listView = (ExpandableListView) findViewById(R.id.test_list);
65        BenchmarkListAdapter adapter =
66                new BenchmarkListAdapter(LayoutInflater.from(this), mRegistry);
67        listView.setAdapter(adapter);
68
69        adapter.notifyDataSetChanged();
70        ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
71        layoutParams.height = 2048;
72        listView.setLayoutParams(layoutParams);
73        listView.requestLayout();
74        System.out.println(System.getProperties().stringPropertyNames());
75    }
76
77    @Override
78    public boolean onCreateOptionsMenu(Menu menu) {
79        // Inflate the menu; this adds items to the action bar if it is present.
80        getMenuInflater().inflate(R.menu.menu_main, menu);
81        return true;
82    }
83
84    @Override
85    public boolean onOptionsItemSelected(MenuItem item) {
86        // Handle action bar item clicks here. The action bar will
87        // automatically handle clicks on the Home/Up button, so long
88        // as you specify a parent activity in AndroidManifest.xml.
89        int id = item.getItemId();
90
91        //noinspection SimplifiableIfStatement
92        if (id == R.id.action_settings) {
93            new AsyncTask<Void, Void, Void>() {
94                @Override
95                protected Void doInBackground(Void... voids) {
96                    try {
97                        HomeActivity.this.runOnUiThread(new Runnable() {
98                            @Override
99                            public void run() {
100                                Toast.makeText(HomeActivity.this, "Exporting...", Toast.LENGTH_LONG).show();
101                            }
102                        });
103                        GlobalResultsStore.getInstance(HomeActivity.this).exportToCsv();
104                    } catch (IOException e) {
105                        e.printStackTrace();
106                    }
107                    return null;
108                }
109
110                @Override
111                protected void onPostExecute(Void aVoid) {
112                    HomeActivity.this.runOnUiThread(new Runnable() {
113                        @Override
114                        public void run() {
115                            Toast.makeText(HomeActivity.this, "Done", Toast.LENGTH_LONG).show();
116                        }
117                    });
118                }
119            }.execute();
120
121            return true;
122        }
123
124        return super.onOptionsItemSelected(item);
125    }
126
127    @Override
128    public void onClick(View v) {
129        final int groupCount = mRegistry.getGroupCount();
130        for (int i = 0; i < groupCount; i++) {
131
132            Intent intent = mRegistry.getBenchmarkGroup(i).getIntent();
133            if (intent != null) {
134                mRunnableBenchmarks.add(intent);
135            }
136        }
137
138        handleNextBenchmark();
139    }
140
141    @Override
142    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
143
144    }
145
146    private void handleNextBenchmark() {
147        Intent nextIntent = mRunnableBenchmarks.peek();
148        startActivityForResult(nextIntent, 0);
149    }
150}
151