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 */ 16 package com.android.test.uibench; 17 18 import android.content.Context; 19 import android.os.Trace; 20 import android.support.v7.widget.GridLayoutManager; 21 import android.support.v7.widget.RecyclerView; 22 import com.android.test.uibench.recyclerview.RvBoxAdapter; 23 import com.android.test.uibench.recyclerview.RvCompatListActivity; 24 25 import java.util.concurrent.TimeUnit; 26 27 public 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 */ spinWaitMs(long ms)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 createLayoutManager(Context context)38 protected RecyclerView.LayoutManager createLayoutManager(Context context) { 39 return new GridLayoutManager(context, 3); 40 } 41 42 @Override createAdapter()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