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