1 /* 2 * Copyright (C) 2010 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; 18 19 import com.android.ide.common.rendering.api.LayoutLog; 20 import com.android.layoutlib.bridge.Bridge; 21 import com.android.layoutlib.bridge.android.BridgeContext; 22 import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 23 24 import android.content.Context; 25 import android.graphics.Canvas; 26 import android.os.IBinder; 27 28 /** 29 * Delegate used to provide new implementation of a select few methods of {@link View} 30 * 31 * Through the layoutlib_create tool, the original methods of View have been replaced 32 * by calls to methods of the same name in this delegate class. 33 * 34 */ 35 public class View_Delegate { 36 37 @LayoutlibDelegate isInEditMode(View thisView)38 /*package*/ static boolean isInEditMode(View thisView) { 39 return true; 40 } 41 42 @LayoutlibDelegate getWindowToken(View thisView)43 /*package*/ static IBinder getWindowToken(View thisView) { 44 Context baseContext = BridgeContext.getBaseContext(thisView.getContext()); 45 if (baseContext instanceof BridgeContext) { 46 return ((BridgeContext) baseContext).getBinder(); 47 } 48 return null; 49 } 50 51 @LayoutlibDelegate draw(View thisView, Canvas canvas)52 /*package*/ static void draw(View thisView, Canvas canvas) { 53 try { 54 // This code is run within a catch to prevent misbehaving components from breaking 55 // all the layout. 56 thisView.draw_Original(canvas); 57 } catch (Throwable t) { 58 Bridge.getLog().error(LayoutLog.TAG_BROKEN, "View draw failed", t, null, null); 59 } 60 } 61 62 @LayoutlibDelegate draw( View thisView, Canvas canvas, ViewGroup parent, long drawingTime)63 /*package*/ static boolean draw( 64 View thisView, Canvas canvas, ViewGroup parent, long drawingTime) { 65 try { 66 // This code is run within a catch to prevent misbehaving components from breaking 67 // all the layout. 68 return thisView.draw_Original(canvas, parent, drawingTime); 69 } catch (Throwable t) { 70 Bridge.getLog().error(LayoutLog.TAG_BROKEN, "View draw failed", t, null, null); 71 } 72 return false; 73 } 74 75 @LayoutlibDelegate measure(View thisView, int widthMeasureSpec, int heightMeasureSpec)76 /*package*/ static void measure(View thisView, int widthMeasureSpec, int heightMeasureSpec) { 77 try { 78 // This code is run within a catch to prevent misbehaving components from breaking 79 // all the layout. 80 thisView.measure_Original(widthMeasureSpec, heightMeasureSpec); 81 } catch (Throwable t) { 82 Bridge.getLog().error(LayoutLog.TAG_BROKEN, "View measure failed", t, null, null); 83 } 84 } 85 86 @LayoutlibDelegate layout(View thisView, int l, int t, int r, int b)87 /*package*/ static void layout(View thisView, int l, int t, int r, int b) { 88 try { 89 // This code is run within a catch to prevent misbehaving components from breaking 90 // all the layout. 91 thisView.layout_Original(l, t, r, b); 92 } catch (Throwable th) { 93 Bridge.getLog().error(LayoutLog.TAG_BROKEN, "View layout failed", th, null, null); 94 } 95 } 96 97 @LayoutlibDelegate dispatchDetachedFromWindow(View thisView)98 /*package*/ static void dispatchDetachedFromWindow(View thisView) { 99 try { 100 // This code is run within a try/catch to prevent components from throwing user-visible 101 // exceptions when being disposed. 102 thisView.dispatchDetachedFromWindow_Original(); 103 } catch (Throwable t) { 104 Context context = BridgeContext.getBaseContext(thisView.getContext()); 105 if (context instanceof BridgeContext) { 106 ((BridgeContext) context).warn("Exception while detaching " + thisView.getClass(), t); 107 } 108 } 109 } 110 } 111