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 
17 package android.ui.cts;
18 
19 import android.app.ListActivity;
20 import android.os.Bundle;
21 import android.widget.AbsListView;
22 import android.widget.AbsListView.OnScrollListener;
23 import android.widget.ArrayAdapter;
24 import android.widget.ListView;
25 
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.TimeUnit;
28 
29 /**
30  * Activity for measuring scrolling time of long list.
31  */
32 public class ScrollingActivity extends ListActivity implements OnScrollListener {
33     static final String TAG = "ScrollingActivity";
34     private static final String NUM_ELEMENTS_EXTRA = "num_elements";
35     private static final int NUM_ELEMENTS_DEFAULT = 10000;
36     private static final int SCROLL_TIME_IN_MS = 1;
37     private static final int WAIT_TIMEOUT_IN_SECS = 5 * 60;
38     private String[] mItems;
39     private CountDownLatch mLatchStop = null;
40     private int mTargetLoc;
41     private int mNumElements;
42 
onCreate(Bundle icicle)43     public void onCreate(Bundle icicle) {
44         super.onCreate(icicle);
45         mNumElements = getIntent().getIntExtra(NUM_ELEMENTS_EXTRA, NUM_ELEMENTS_DEFAULT);
46         mItems = new String[mNumElements];
47         for (int i = 0; i < mNumElements; i++) {
48             mItems[i] = Integer.toString(i);
49         }
50         setListAdapter(new ArrayAdapter<String>(this,
51                 android.R.layout.simple_list_item_1, mItems));
52         ListView view = getListView();
53         view.setOnScrollListener(this);
54     }
55 
scrollToTop()56     public boolean scrollToTop() {
57         return doScroll(0);
58     }
scrollToBottom()59     public boolean scrollToBottom() {
60         return doScroll(mNumElements - 1);
61     }
62 
doScroll(final int loc)63     private boolean doScroll(final int loc) {
64         mLatchStop = new CountDownLatch(1);
65         mTargetLoc = loc;
66         final ListView view = getListView();
67         runOnUiThread( new Runnable() {
68             @Override
69             public void run() {
70                 view.smoothScrollToPositionFromTop(loc, 0, SCROLL_TIME_IN_MS);
71             }
72         });
73         boolean result = false;
74         try {
75             result = mLatchStop.await(WAIT_TIMEOUT_IN_SECS, TimeUnit.SECONDS);
76         } catch (InterruptedException e) {
77             // ignore
78         }
79         mLatchStop = null;
80         return result;
81     }
82 
83     @Override
onScrollStateChanged(AbsListView view, int scrollState)84     public void onScrollStateChanged(AbsListView view, int scrollState) {
85 
86     }
87 
88     @Override
onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)89     public void onScroll(AbsListView view, int firstVisibleItem,
90             int visibleItemCount, int totalItemCount) {
91         //Log.i(TAG, "onScroll " + firstVisibleItem + " " + visibleItemCount);
92         if ((mTargetLoc >= firstVisibleItem) &&
93                 (mTargetLoc <= (firstVisibleItem + visibleItemCount))) {
94             if (mLatchStop != null) {
95                 mLatchStop.countDown();
96             }
97         }
98     }
99 }
100