Home | History | Annotate | Download | only in widget
      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 
     17 package com.example.android.supportv7.widget;
     18 
     19 import android.support.v7.widget.LinearLayoutManager;
     20 import android.support.v7.widget.RecyclerView;
     21 import android.view.ViewGroup;
     22 
     23 import com.example.android.supportv7.Cheeses;
     24 import com.example.android.supportv7.R;
     25 import com.example.android.supportv7.widget.adapter.SimpleStringAdapter;
     26 import com.example.android.supportv7.widget.util.ConfigToggle;
     27 
     28 /**
     29  * A configurably janky activity that uses {@link LinearLayoutManager}.
     30  */
     31 public class LinearLayoutManagerJankActivity extends LinearLayoutManagerActivity {
     32 
     33     private boolean mBindSlowdownEnabled = true;
     34     private boolean mInflateSlowdownEnabled = true;
     35 
     36     /**
     37      * Spin wait. Used instead of sleeping so a core is used up for the duration, and so
     38      * traces/sampled profiling show the sections as expensive, and not just a scheduling mistake.
     39      */
     40     private static void spinWaitMs(long ms) {
     41         long start = System.nanoTime();
     42         while (System.nanoTime() - start < ms * 1000L * 1000L);
     43     }
     44 
     45     @Override
     46     protected RecyclerView.Adapter createAdapter() {
     47         return new SimpleStringAdapter(this, Cheeses.sCheeseStrings) {
     48             @Override
     49             public void onBindViewHolder(ViewHolder holder, int position) {
     50                 super.onBindViewHolder(holder, position);
     51                 if (mBindSlowdownEnabled) spinWaitMs(8);
     52             }
     53 
     54             @Override
     55             public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     56                 if (mInflateSlowdownEnabled) spinWaitMs(4);
     57                 return super.onCreateViewHolder(parent, viewType);
     58             }
     59         };
     60     }
     61 
     62     @Override
     63     protected ConfigToggle[] createConfigToggles() {
     64         return new ConfigToggle[]{
     65                 new ConfigToggle(this, R.string.enable_bind_slowdown) {
     66                     @Override
     67                     public boolean isChecked() { return mBindSlowdownEnabled; }
     68 
     69                     @Override
     70                     public void onChange(boolean newValue) { mBindSlowdownEnabled = newValue; }
     71                 },
     72                 new ConfigToggle(this, R.string.enable_inflate_slowdown) {
     73                     @Override
     74                     public boolean isChecked() { return mInflateSlowdownEnabled; }
     75 
     76                     @Override
     77                     public void onChange(boolean newValue) { mInflateSlowdownEnabled = newValue; }
     78                 },
     79                 new ConfigToggle(this, R.string.enable_prefetch) {
     80                     @Override
     81                     public boolean isChecked() { return mLayoutManager.isItemPrefetchEnabled(); }
     82 
     83                     @Override
     84                     public void onChange(boolean newValue) {
     85                         mLayoutManager.setItemPrefetchEnabled(newValue);
     86                     }
     87                 },
     88         };
     89     }
     90 }
     91