1/*
2 * Copyright (C) 2016 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 */
16package com.android.test.uibench;
17
18import android.content.Context;
19import android.os.Trace;
20import android.support.v7.widget.GridLayoutManager;
21import android.support.v7.widget.RecyclerView;
22import com.android.test.uibench.recyclerview.RvBoxAdapter;
23import com.android.test.uibench.recyclerview.RvCompatListActivity;
24
25import java.util.concurrent.TimeUnit;
26
27public class SlowBindRecyclerViewActivity extends RvCompatListActivity {
28    /**
29     * Spin wait. Used instead of sleeping so a core is used up for the duration, and so
30     * traces/sampled profiling show the sections as expensive, and not just a scheduling mistake.
31     */
32    private static void spinWaitMs(long ms) {
33        long start = System.nanoTime();
34        while (System.nanoTime() - start < TimeUnit.MILLISECONDS.toNanos(ms));
35    }
36
37    @Override
38    protected RecyclerView.LayoutManager createLayoutManager(Context context) {
39        return new GridLayoutManager(context, 3);
40    }
41
42    @Override
43    protected RecyclerView.Adapter createAdapter() {
44        return new RvBoxAdapter(this, TextUtils.buildSimpleStringList()) {
45            @Override
46            public void onBindViewHolder(ViewHolder holder, int position) {
47                Trace.beginSection("bind item " + position);
48
49                spinWaitMs(3);
50                super.onBindViewHolder(holder, position);
51                Trace.endSection();
52            }
53        };
54    }
55}
56