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 android.view.contentcapture; 18 19 import android.app.Activity; 20 import android.graphics.drawable.Drawable; 21 import android.os.Bundle; 22 import android.os.Looper; 23 import android.os.RemoteCallback; 24 import android.view.Choreographer; 25 import android.view.View; 26 import android.widget.LinearLayout; 27 import android.widget.TextView; 28 29 import com.android.perftests.contentcapture.R; 30 31 /** 32 * A simple activity used for testing, e.g. performance of activity switching, or as a base 33 * container of testing view. 34 */ 35 public class CustomTestActivity extends Activity { 36 public static final String INTENT_EXTRA_LAYOUT_ID = "layout_id"; 37 public static final String INTENT_EXTRA_CUSTOM_VIEWS = "custom_view_number"; 38 static final String INTENT_EXTRA_FINISH_ON_IDLE = "finish"; 39 static final String INTENT_EXTRA_DRAW_CALLBACK = "draw_callback"; 40 public static final int MAX_VIEWS = 500; 41 private static final int CUSTOM_CONTAINER_LAYOUT_ID = R.layout.test_container_activity; 42 43 @Override onCreate(Bundle savedInstanceState)44 protected void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 47 if (getIntent().hasExtra(INTENT_EXTRA_LAYOUT_ID)) { 48 final int layoutId = getIntent().getIntExtra(INTENT_EXTRA_LAYOUT_ID, 49 /* defaultValue= */0); 50 setContentView(layoutId); 51 if (layoutId == CUSTOM_CONTAINER_LAYOUT_ID) { 52 createCustomViews(findViewById(R.id.root_view), 53 getIntent().getIntExtra(INTENT_EXTRA_CUSTOM_VIEWS, MAX_VIEWS)); 54 } 55 } 56 57 final RemoteCallback drawCallback = getIntent().getParcelableExtra( 58 INTENT_EXTRA_DRAW_CALLBACK, RemoteCallback.class); 59 if (drawCallback != null) { 60 getWindow().getDecorView().addOnAttachStateChangeListener( 61 new View.OnAttachStateChangeListener() { 62 @Override 63 public void onViewAttachedToWindow(View v) { 64 Choreographer.getInstance().postCallback( 65 Choreographer.CALLBACK_COMMIT, 66 // Report that the first frame is drawn. 67 () -> drawCallback.sendResult(null), null /* token */); 68 } 69 70 @Override 71 public void onViewDetachedFromWindow(View v) { 72 } 73 }); 74 } 75 76 if (getIntent().getBooleanExtra(INTENT_EXTRA_FINISH_ON_IDLE, false)) { 77 Looper.myQueue().addIdleHandler(() -> { 78 // Finish without animation. 79 finish(); 80 overridePendingTransition(0 /* enterAnim */, 0 /* exitAnim */); 81 return false; 82 }); 83 } 84 } 85 createCustomViews(LinearLayout root, int number)86 private void createCustomViews(LinearLayout root, int number) { 87 LinearLayout horizontalLayout = null; 88 for (int i = 0; i < number; i++) { 89 final int j = i % 8; 90 if (horizontalLayout != null && j == 0) { 91 root.addView(horizontalLayout); 92 horizontalLayout = null; 93 } 94 if (horizontalLayout == null) { 95 horizontalLayout = createHorizontalLayout(); 96 } 97 horizontalLayout.addView(createItem(null, i)); 98 } 99 if (horizontalLayout != null) { 100 root.addView(horizontalLayout); 101 } 102 } 103 createHorizontalLayout()104 private LinearLayout createHorizontalLayout() { 105 final LinearLayout layout = new LinearLayout(getApplicationContext()); 106 layout.setOrientation(LinearLayout.HORIZONTAL); 107 return layout; 108 } 109 createItem(Drawable drawable, int index)110 private LinearLayout createItem(Drawable drawable, int index) { 111 final LinearLayout group = new LinearLayout(getApplicationContext()); 112 group.setOrientation(LinearLayout.VERTICAL); 113 group.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 114 LinearLayout.LayoutParams.WRAP_CONTENT, /* weight= */ 1.0f)); 115 116 final TextView text = new TextView(this); 117 text.setText("i = " + index); 118 group.addView(text); 119 120 return group; 121 } 122 } 123