1 /* 2 * Copyright (C) 2012 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 package com.android.cts.uiautomator; 17 18 import android.app.Activity; 19 import android.os.Bundle; 20 import android.support.v4.app.ListFragment; 21 import android.view.View; 22 import android.widget.ArrayAdapter; 23 import android.widget.ListView; 24 25 public class TestListFragment extends ListFragment { 26 27 private static final String STATE_ACTIVATED_POSITION = "activated_position"; 28 29 private Callbacks mCallbacks = sDummyCallbacks; 30 private int mActivatedPosition = ListView.INVALID_POSITION; 31 32 public interface Callbacks { 33 onItemSelected(String id)34 public void onItemSelected(String id); 35 } 36 37 private static Callbacks sDummyCallbacks = new Callbacks() { 38 @Override 39 public void onItemSelected(String id) { 40 } 41 }; 42 TestListFragment()43 public TestListFragment() { 44 } 45 46 @Override onCreate(Bundle savedInstanceState)47 public void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 setListAdapter(new ArrayAdapter<TestItems.TestItem>(getActivity(), 50 R.layout.simple_list_item_selected, R.id.label, TestItems.getTests())); 51 } 52 53 @Override onViewCreated(View view, Bundle savedState)54 public void onViewCreated(View view, Bundle savedState) { 55 super.onViewCreated(view, savedState); 56 if (savedState != null && savedState.containsKey(STATE_ACTIVATED_POSITION)) { 57 setActivatedPosition(savedState.getInt(STATE_ACTIVATED_POSITION)); 58 } 59 } 60 61 @Override onAttach(Activity activity)62 public void onAttach(Activity activity) { 63 super.onAttach(activity); 64 if (!(activity instanceof Callbacks)) { 65 throw new IllegalStateException("Activity must implement fragment's callbacks."); 66 } 67 68 mCallbacks = (Callbacks) activity; 69 } 70 71 @Override onDetach()72 public void onDetach() { 73 super.onDetach(); 74 mCallbacks = sDummyCallbacks; 75 } 76 77 @Override onListItemClick(ListView listView, View view, int position, long id)78 public void onListItemClick(ListView listView, View view, int position, long id) { 79 super.onListItemClick(listView, view, position, id); 80 mCallbacks.onItemSelected(TestItems.getTest(position).mId); 81 } 82 83 @Override onSaveInstanceState(Bundle outState)84 public void onSaveInstanceState(Bundle outState) { 85 super.onSaveInstanceState(outState); 86 if (mActivatedPosition != ListView.INVALID_POSITION) { 87 outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition); 88 } 89 } 90 setActivateOnItemClick(boolean activateOnItemClick)91 public void setActivateOnItemClick(boolean activateOnItemClick) { 92 getListView().setChoiceMode( 93 activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE); 94 } 95 setActivatedPosition(int position)96 public void setActivatedPosition(int position) { 97 if (position == ListView.INVALID_POSITION) { 98 getListView().setItemChecked(mActivatedPosition, false); 99 } else { 100 getListView().setItemChecked(position, true); 101 } 102 103 mActivatedPosition = position; 104 } 105 } 106