1 /*
2  * Copyright (C) 2011 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.cts.verifier;
18 
19 import com.android.cts.verifier.TestListAdapter.TestListItem;
20 
21 import android.app.ListActivity;
22 import android.content.Intent;
23 import android.content.res.Configuration;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.view.Window;
27 import android.widget.ListView;
28 
29 /** {@link ListActivity} that displays a list of manual tests. */
30 public abstract class AbstractTestListActivity extends ListActivity {
31     private static final int LAUNCH_TEST_REQUEST_CODE = 9001;
32 
33     protected TestListAdapter mAdapter;
34 
setTestListAdapter(TestListAdapter adapter)35     protected void setTestListAdapter(TestListAdapter adapter) {
36         mAdapter = adapter;
37         setListAdapter(mAdapter);
38         mAdapter.loadTestResults();
39     }
40 
getIntent(int position)41     private Intent getIntent(int position) {
42         TestListItem item = mAdapter.getItem(position);
43         return item.intent;
44     }
45 
46     @Override
onActivityResult(int requestCode, int resultCode, Intent data)47     protected final void onActivityResult(int requestCode, int resultCode, Intent data) {
48         super.onActivityResult(requestCode, resultCode, data);
49         handleActivityResult(requestCode, resultCode, data);
50     }
51 
52     /** Override this in subclasses instead of onActivityResult */
handleActivityResult(int requestCode, int resultCode, Intent data)53     protected void handleActivityResult(int requestCode, int resultCode, Intent data) {
54         switch (requestCode) {
55             case LAUNCH_TEST_REQUEST_CODE:
56                 handleLaunchTestResult(resultCode, data);
57                 break;
58 
59             default:
60                 throw new IllegalArgumentException("Unknown request code: " + requestCode);
61         }
62     }
63 
64     @Override
onCreate(Bundle savedInstanceState)65     protected void onCreate(Bundle savedInstanceState) {
66         super.onCreate(savedInstanceState);
67         if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_TYPE_MASK)
68               == Configuration.UI_MODE_TYPE_TELEVISION) {
69             getWindow().requestFeature(Window.FEATURE_OPTIONS_PANEL);
70         }
71         setContentView(R.layout.list_content);
72     }
73 
handleLaunchTestResult(int resultCode, Intent data)74     protected void handleLaunchTestResult(int resultCode, Intent data) {
75         if (resultCode == RESULT_OK) {
76             TestResult testResult = TestResult.fromActivityResult(resultCode, data);
77             mAdapter.setTestResult(testResult);
78         }
79     }
80 
81     /** Launch the activity when its {@link ListView} item is clicked. */
82     @Override
onListItemClick(ListView listView, View view, int position, long id)83     protected final void onListItemClick(ListView listView, View view, int position, long id) {
84         super.onListItemClick(listView, view, position, id);
85         handleItemClick(listView, view, position, id);
86     }
87 
88     /** Override this in subclasses instead of onListItemClick */
handleItemClick(ListView listView, View view, int position, long id)89     protected void handleItemClick(ListView listView, View view, int position, long id) {
90         Intent intent = getIntent(position);
91         startActivityForResult(intent, LAUNCH_TEST_REQUEST_CODE);
92     }
93 }
94