1 /* 2 * Copyright (C) 2017 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.android.layoutlib.bridge.test.widgets; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.graphics.Color; 22 import android.graphics.Paint; 23 import android.graphics.Rect; 24 import android.util.AttributeSet; 25 import android.view.View; 26 27 @SuppressWarnings("unused") // Used from RenderTests 28 public class HookWidget extends View { 29 private static final Runnable NOP_RUNNABLE = () -> {}; 30 private static Runnable sOnPreDrawHook = NOP_RUNNABLE; 31 private static Runnable sOnPreMeasure = NOP_RUNNABLE; 32 private static Runnable sOnPreLayout = NOP_RUNNABLE; 33 private static Runnable sOnPostDrawHook = NOP_RUNNABLE; 34 private static Runnable sOnPostMeasure = NOP_RUNNABLE; 35 private static Runnable sOnPostLayout = NOP_RUNNABLE; 36 setOnPreDrawHook(Runnable runnable)37 public static void setOnPreDrawHook(Runnable runnable) { 38 sOnPreDrawHook = runnable; 39 } 40 setOnPreMeasure(Runnable runnable)41 public static void setOnPreMeasure(Runnable runnable) { 42 sOnPreMeasure = runnable; 43 } 44 setOnPreLayout(Runnable runnable)45 public static void setOnPreLayout(Runnable runnable) { 46 sOnPreLayout = runnable; 47 } 48 setOnPostDrawHook(Runnable onPostDrawHook)49 public static void setOnPostDrawHook(Runnable onPostDrawHook) { 50 sOnPostDrawHook = onPostDrawHook; 51 } 52 setOnPostMeasure(Runnable onPostMeasure)53 public static void setOnPostMeasure(Runnable onPostMeasure) { 54 sOnPostMeasure = onPostMeasure; 55 } 56 setOnPostLayout(Runnable onPostLayout)57 public static void setOnPostLayout(Runnable onPostLayout) { 58 sOnPostLayout = onPostLayout; 59 } 60 reset()61 public static void reset() { 62 sOnPreDrawHook = NOP_RUNNABLE; 63 sOnPreMeasure = NOP_RUNNABLE; 64 sOnPreLayout = NOP_RUNNABLE; 65 sOnPostDrawHook = NOP_RUNNABLE; 66 sOnPostMeasure = NOP_RUNNABLE; 67 sOnPostLayout = NOP_RUNNABLE; 68 } 69 HookWidget(Context context)70 public HookWidget(Context context) { 71 super(context); 72 } 73 HookWidget(Context context, AttributeSet attrs)74 public HookWidget(Context context, AttributeSet attrs) { 75 super(context, attrs); 76 } 77 HookWidget(Context context, AttributeSet attrs, int defStyleAttr)78 public HookWidget(Context context, AttributeSet attrs, int defStyleAttr) { 79 super(context, attrs, defStyleAttr); 80 } 81 HookWidget(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)82 public HookWidget(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 83 super(context, attrs, defStyleAttr, defStyleRes); 84 } 85 86 @Override onDraw(Canvas canvas)87 protected void onDraw(Canvas canvas) { 88 sOnPreDrawHook.run(); 89 90 super.onDraw(canvas); 91 Paint paint = new Paint(); 92 paint.setColor(Color.BLUE); 93 Rect rect = new Rect(); 94 getDrawingRect(rect); 95 canvas.drawRect(rect, paint); 96 97 sOnPostDrawHook.run(); 98 } 99 100 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)101 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 102 sOnPreMeasure.run(); 103 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 104 sOnPostMeasure.run(); 105 } 106 107 @Override onLayout(boolean changed, int left, int top, int right, int bottom)108 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 109 sOnPreLayout.run(); 110 super.onLayout(changed, left, top, right, bottom); 111 sOnPostLayout.run(); 112 } 113 } 114