1 /* 2 * Copyright (C) 2008 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.ide.common.rendering.api; 18 19 import com.android.resources.ResourceType; 20 import com.android.util.Pair; 21 22 import java.net.URL; 23 24 /** 25 * Callback for project information needed by the Layout Library. 26 * Classes implementing this interface provide methods giving access to some project data, like 27 * resource resolution, namespace information, and instantiation of custom view. 28 */ 29 public interface IProjectCallback { 30 31 public enum ViewAttribute { 32 TEXT(String.class), 33 IS_CHECKED(Boolean.class), 34 SRC(URL.class), 35 COLOR(Integer.class); 36 37 private final Class<?> mClass; 38 39 private ViewAttribute(Class<?> theClass) { 40 mClass = theClass; 41 } 42 43 public Class<?> getAttributeClass() { 44 return mClass; 45 } 46 } 47 48 /** 49 * Loads a custom view with the given constructor signature and arguments. 50 * @param name The fully qualified name of the class. 51 * @param constructorSignature The signature of the class to use 52 * @param constructorArgs The arguments to use on the constructor 53 * @return A newly instantiated android.view.View object. 54 * @throws ClassNotFoundException 55 * @throws Exception 56 */ 57 @SuppressWarnings("unchecked") 58 Object loadView(String name, Class[] constructorSignature, Object[] constructorArgs) 59 throws ClassNotFoundException, Exception; 60 61 /** 62 * Returns the namespace of the application. 63 * <p/>This lets the Layout Lib load custom attributes for custom views. 64 */ 65 String getNamespace(); 66 67 /** 68 * Resolves the id of a resource Id. 69 * <p/>The resource id is the value of a <code>R.<type>.<name></code>, and 70 * this method will return both the type and name of the resource. 71 * @param id the Id to resolve. 72 * @return a Pair of {@link ResourceType} and resource name, or null if the id 73 * does not match any resource. 74 */ 75 @SuppressWarnings("deprecation") 76 Pair<ResourceType, String> resolveResourceId(int id); 77 78 /** 79 * Resolves the id of a resource Id of type int[] 80 * <p/>The resource id is the value of a R.styleable.<name>, and this method will 81 * return the name of the resource. 82 * @param id the Id to resolve. 83 * @return the name of the resource or <code>null</code> if not found. 84 */ 85 String resolveResourceId(int[] id); 86 87 /** 88 * Returns the id of a resource. 89 * <p/>The provided type and name must match an existing constant defined as 90 * <code>R.<type>.<name></code>. 91 * @param type the type of the resource 92 * @param name the name of the resource 93 * @return an Integer containing the resource Id, or <code>null</code> if not found. 94 */ 95 Integer getResourceId(ResourceType type, String name); 96 97 /** 98 * Returns a custom parser for the layout of the given name. 99 * @param layoutName the name of the layout. 100 * @return returns a custom parser or null if no custom parsers are needed. 101 * @deprecated This is replaced by {@link #getParser(ResourceValue)} but older version 102 * of the layoutlib (before API7) will still call this method. 103 */ 104 @Deprecated 105 ILayoutPullParser getParser(String layoutName); 106 107 /** 108 * Returns a custom parser for a given layout. 109 * @param layoutResource The layout. 110 * @return returns a custom parser or null if no custom parsers are needed. 111 */ 112 ILayoutPullParser getParser(ResourceValue layoutResource); 113 114 /** 115 * Returns the value of an item used by an adapter. 116 * @param adapterView The {@link ResourceReference} for the adapter view info. 117 * @param adapterCookie the view cookie for this particular view. 118 * @param itemRef the {@link ResourceReference} for the layout used by the adapter item. 119 * @param fullPosition the position of the item in the full list. 120 * @param positionPerType the position of the item if only items of the same type are 121 * considered. If there is only one type of items, this is the same as 122 * <var>fullPosition</var>. 123 * @param fullParentPosition the full position of the item's parent. This is only 124 * valid if the adapter view is an ExpandableListView. 125 * @param parentPositionPerType the position of the parent's item, only considering items 126 * of the same type. This is only valid if the adapter view is an ExpandableListView. 127 * If there is only one type of items, this is the same as <var>fullParentPosition</var>. 128 * @param viewRef The {@link ResourceReference} for the view we're trying to fill. 129 * @param ViewAttribute the attribute being queried. 130 * @param defaultValue the default value for this attribute. The object class matches the 131 * class associated with the {@link ViewAttribute}. 132 * @return the item value or null if there's no value. 133 * 134 * @see ViewAttribute#getAttributeClass() 135 */ 136 Object getAdapterItemValue(ResourceReference adapterView, Object adapterCookie, 137 ResourceReference itemRef, 138 int fullPosition, int positionPerType, 139 int fullParentPosition, int parentPositionPerType, 140 ResourceReference viewRef, ViewAttribute viewAttribute, Object defaultValue); 141 142 /** 143 * Returns an adapter binding for a given adapter view. 144 * This is only called if {@link SessionParams} does not have an {@link AdapterBinding} for 145 * the given {@link ResourceReference} already. 146 * 147 * @param adapterViewRef the reference of adapter view to return the adapter binding for. 148 * @param adapterCookie the view cookie for this particular view. 149 * @param viewObject the view object for the adapter. 150 * @return an adapter binding for the given view or null if there's no data. 151 */ 152 AdapterBinding getAdapterBinding(ResourceReference adapterViewRef, Object adapterCookie, 153 Object viewObject); 154 } 155