Home | History | Annotate | Download | only in verifier
      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.view.View;
     24 import android.widget.ListView;
     25 
     26 /** {@link ListActivity} that displays a list of manual tests. */
     27 public abstract class AbstractTestListActivity extends ListActivity {
     28     private static final int LAUNCH_TEST_REQUEST_CODE = 9001;
     29 
     30     protected TestListAdapter mAdapter;
     31 
     32     protected void setTestListAdapter(TestListAdapter adapter) {
     33         mAdapter = adapter;
     34         setListAdapter(mAdapter);
     35         mAdapter.loadTestResults();
     36     }
     37 
     38     private Intent getIntent(int position) {
     39         TestListItem item = mAdapter.getItem(position);
     40         return item.intent;
     41     }
     42 
     43     @Override
     44     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     45         super.onActivityResult(requestCode, resultCode, data);
     46         switch (requestCode) {
     47             case LAUNCH_TEST_REQUEST_CODE:
     48                 handleLaunchTestResult(resultCode, data);
     49                 break;
     50 
     51             default:
     52                 throw new IllegalArgumentException("Unknown request code: " + requestCode);
     53         }
     54     }
     55 
     56     private void handleLaunchTestResult(int resultCode, Intent data) {
     57         if (resultCode == RESULT_OK) {
     58             TestResult testResult = TestResult.fromActivityResult(resultCode, data);
     59             mAdapter.setTestResult(testResult);
     60         }
     61     }
     62 
     63     /** Launch the activity when its {@link ListView} item is clicked. */
     64     @Override
     65     protected void onListItemClick(ListView listView, View view, int position, long id) {
     66         super.onListItemClick(listView, view, position, id);
     67         Intent intent = getIntent(position);
     68         startActivityForResult(intent, LAUNCH_TEST_REQUEST_CODE);
     69     }
     70 }
     71