1 /* 2 * Copyright (C) 2014 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 android.uirendering.cts.testinfrastructure; 17 18 import android.annotation.Nullable; 19 import android.app.Activity; 20 import android.content.res.Configuration; 21 import android.graphics.Point; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.os.Message; 25 import android.view.View; 26 import android.view.ViewStub; 27 import android.view.ViewTreeObserver; 28 import android.webkit.WebView; 29 30 import com.android.cts.uirendering.R; 31 32 /** 33 * A generic activity that uses a view specified by the user. 34 */ 35 public class DrawActivity extends Activity { 36 private final static long TIME_OUT_MS = 10000; 37 private final Point mLock = new Point(); 38 public static final int MIN_NUMBER_OF_DRAWS = 20; 39 40 private Handler mHandler; 41 private View mView; 42 private View mViewWrapper; 43 private boolean mOnTv; 44 onCreate(Bundle bundle)45 public void onCreate(Bundle bundle){ 46 super.onCreate(bundle); 47 getWindow().getDecorView().setSystemUiVisibility( 48 View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN); 49 mHandler = new RenderSpecHandler(); 50 int uiMode = getResources().getConfiguration().uiMode; 51 mOnTv = (uiMode & Configuration.UI_MODE_TYPE_TELEVISION) == Configuration.UI_MODE_TYPE_TELEVISION; 52 } 53 getOnTv()54 public boolean getOnTv() { 55 return mOnTv; 56 } 57 enqueueRenderSpecAndWait(int layoutId, CanvasClient canvasClient, String webViewUrl, @Nullable ViewInitializer viewInitializer, boolean useHardware)58 public Point enqueueRenderSpecAndWait(int layoutId, CanvasClient canvasClient, String webViewUrl, 59 @Nullable ViewInitializer viewInitializer, boolean useHardware) { 60 ((RenderSpecHandler) mHandler).setViewInitializer(viewInitializer); 61 int arg2 = (useHardware ? View.LAYER_TYPE_NONE : View.LAYER_TYPE_SOFTWARE); 62 if (canvasClient != null) { 63 mHandler.obtainMessage(RenderSpecHandler.CANVAS_MSG, 0, arg2, canvasClient).sendToTarget(); 64 } else if (webViewUrl != null) { 65 mHandler.obtainMessage(RenderSpecHandler.WEB_VIEW_MSG, 0, arg2, webViewUrl).sendToTarget(); 66 } else { 67 mHandler.obtainMessage(RenderSpecHandler.LAYOUT_MSG, layoutId, arg2).sendToTarget(); 68 } 69 70 Point point = new Point(); 71 synchronized (mLock) { 72 try { 73 mLock.wait(TIME_OUT_MS); 74 point.set(mLock.x, mLock.y); 75 } catch (InterruptedException e) { 76 e.printStackTrace(); 77 } 78 } 79 return point; 80 } 81 82 private class RenderSpecHandler extends Handler { 83 public static final int LAYOUT_MSG = 1; 84 public static final int CANVAS_MSG = 2; 85 public static final int WEB_VIEW_MSG = 3; 86 87 private ViewInitializer mViewInitializer; 88 setViewInitializer(ViewInitializer viewInitializer)89 public void setViewInitializer(ViewInitializer viewInitializer) { 90 mViewInitializer = viewInitializer; 91 } 92 handleMessage(Message message)93 public void handleMessage(Message message) { 94 int drawCountDelay = 0; 95 setContentView(R.layout.test_container); 96 ViewStub stub = (ViewStub) findViewById(R.id.test_content_stub); 97 mViewWrapper = findViewById(R.id.test_content_wrapper); 98 switch (message.what) { 99 case LAYOUT_MSG: { 100 stub.setLayoutResource(message.arg1); 101 mView = stub.inflate(); 102 } break; 103 104 case CANVAS_MSG: { 105 stub.setLayoutResource(R.layout.test_content_canvasclientview); 106 mView = stub.inflate(); 107 ((CanvasClientView) mView).setCanvasClient((CanvasClient) (message.obj)); 108 } break; 109 110 case WEB_VIEW_MSG: { 111 stub.setLayoutResource(R.layout.test_content_webview); 112 mView = stub.inflate(); 113 ((WebView) mView).loadUrl((String) message.obj); 114 ((WebView) mView).setInitialScale(100); 115 drawCountDelay = 10; 116 } break; 117 } 118 119 if (mView == null) { 120 throw new IllegalStateException("failed to inflate test content"); 121 } 122 123 if (mViewInitializer != null) { 124 mViewInitializer.initializeView(mView); 125 } 126 127 // set layer on wrapper parent of view, so view initializer 128 // can control layer type of View under test. 129 mViewWrapper.setLayerType(message.arg2, null); 130 131 DrawCounterListener onDrawListener = new DrawCounterListener(drawCountDelay); 132 133 mView.getViewTreeObserver().addOnPreDrawListener(onDrawListener); 134 135 mView.postInvalidate(); 136 } 137 } 138 139 private class DrawCounterListener implements ViewTreeObserver.OnPreDrawListener { 140 private int mCurrentDraws = 0; 141 private int mExtraDraws; 142 DrawCounterListener(int extraDraws)143 public DrawCounterListener(int extraDraws) { 144 mExtraDraws = extraDraws; 145 } 146 147 @Override onPreDraw()148 public boolean onPreDraw() { 149 150 mCurrentDraws++; 151 if (mCurrentDraws < MIN_NUMBER_OF_DRAWS + mExtraDraws) { 152 mView.postInvalidate(); 153 } else { 154 synchronized (mLock) { 155 mLock.set(mViewWrapper.getLeft(), mViewWrapper.getTop()); 156 mLock.notify(); 157 } 158 mView.getViewTreeObserver().removeOnPreDrawListener(this); 159 } 160 return true; 161 } 162 } 163 } 164