1 /* 2 * Copyright (C) 2015 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.tools.idea.editors.theme.widgets; 18 19 import com.android.ide.common.rendering.api.LayoutLog; 20 import com.android.layoutlib.bridge.Bridge; 21 22 import android.content.Context; 23 import android.graphics.Canvas; 24 import android.util.AttributeSet; 25 import android.view.View; 26 import android.view.ViewGroup; 27 28 /** 29 * {@link ViewGroup} that wraps another view and catches any possible exceptions that the child view 30 * might generate. 31 * This is used by the theme editor to stop custom views from breaking the preview. 32 */ 33 // TODO: This view is just a temporary solution that will be replaced by adding a try / catch 34 // for custom views in the ClassConverter 35 public class ErrorCatcher extends ViewGroup { 36 public ErrorCatcher(Context context) { 37 super(context); 38 } 39 40 public ErrorCatcher(Context context, AttributeSet attrs) { 41 super(context, attrs); 42 } 43 44 public ErrorCatcher(Context context, AttributeSet attrs, int defStyleAttr) { 45 super(context, attrs, defStyleAttr); 46 } 47 48 public ErrorCatcher(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 49 super(context, attrs, defStyleAttr, defStyleRes); 50 } 51 52 @Override 53 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 54 assert getChildCount() == 1 : "ErrorCatcher can only have one child"; 55 56 View child = getChildAt(0); 57 try { 58 measureChild(child, widthMeasureSpec, heightMeasureSpec); 59 60 setMeasuredDimension(resolveSize(child.getMeasuredWidth(), widthMeasureSpec), 61 resolveSize(child.getMeasuredHeight(), heightMeasureSpec)); 62 } catch (Throwable t) { 63 Bridge.getLog().warning(LayoutLog.TAG_BROKEN, "Failed to do onMeasure for view " + 64 child.getClass().getCanonicalName(), t); 65 setMeasuredDimension(resolveSize(0, widthMeasureSpec), 66 resolveSize(0, heightMeasureSpec)); 67 } 68 } 69 70 @Override 71 protected boolean drawChild(Canvas canvas, View child, long drawingTime) { 72 try { 73 return super.drawChild(canvas, child, drawingTime); 74 } catch (Throwable t) { 75 Bridge.getLog().warning(LayoutLog.TAG_BROKEN, "Failed to draw for view " + 76 child.getClass().getCanonicalName(), t); 77 } 78 79 return false; 80 } 81 82 @Override 83 protected void onLayout(boolean changed, int l, int t, int r, int b) { 84 assert getChildCount() == 1 : "ErrorCatcher can only have one child"; 85 86 View child = getChildAt(0); 87 try { 88 child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight()); 89 } catch (Throwable e) { 90 Bridge.getLog().warning(LayoutLog.TAG_BROKEN, "Failed to do onLayout for view " + 91 child.getClass().getCanonicalName(), e); 92 } 93 } 94 } 95