Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 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 android.view; |
| 18 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 19 | import com.android.ide.common.rendering.api.LayoutLog; |
Deepanshu Gupta | 2bc2daa | 2015-03-30 15:01:03 -0700 | [diff] [blame] | 20 | import com.android.ide.common.rendering.api.LayoutlibCallback; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 21 | import com.android.ide.common.rendering.api.MergeCookie; |
| 22 | import com.android.ide.common.rendering.api.ResourceReference; |
| 23 | import com.android.ide.common.rendering.api.ResourceValue; |
| 24 | import com.android.layoutlib.bridge.Bridge; |
Jens Ole Lauridsen | 4dfe4d4 | 2015-06-05 08:04:27 -0700 | [diff] [blame] | 25 | import com.android.layoutlib.bridge.BridgeConstants; |
Deepanshu Gupta | d30c141 | 2015-10-20 17:29:44 -0700 | [diff] [blame] | 26 | import com.android.layoutlib.bridge.MockView; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 27 | import com.android.layoutlib.bridge.android.BridgeContext; |
| 28 | import com.android.layoutlib.bridge.android.BridgeXmlBlockParser; |
Deepanshu Gupta | ccbc117 | 2015-07-10 17:38:29 -0700 | [diff] [blame] | 29 | import com.android.layoutlib.bridge.android.support.DrawerLayoutUtil; |
Deepanshu Gupta | 61f23e9 | 2015-07-06 18:31:20 -0700 | [diff] [blame] | 30 | import com.android.layoutlib.bridge.android.support.RecyclerViewUtil; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 31 | import com.android.layoutlib.bridge.impl.ParserFactory; |
Deepanshu Gupta | 61f23e9 | 2015-07-06 18:31:20 -0700 | [diff] [blame] | 32 | import com.android.layoutlib.bridge.util.ReflectionUtils; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 33 | import com.android.resources.ResourceType; |
| 34 | import com.android.util.Pair; |
| 35 | |
| 36 | import org.xmlpull.v1.XmlPullParser; |
| 37 | |
Deepanshu Gupta | ccbc117 | 2015-07-10 17:38:29 -0700 | [diff] [blame] | 38 | import android.annotation.NonNull; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 39 | import android.content.Context; |
Deepanshu Gupta | 7535177 | 2015-08-28 11:53:46 -0700 | [diff] [blame] | 40 | import android.content.res.TypedArray; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 41 | import android.util.AttributeSet; |
Jerome Gaillard | 1d8b0bb | 2016-06-02 15:35:22 +0100 | [diff] [blame] | 42 | import android.widget.NumberPicker; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 43 | |
| 44 | import java.io.File; |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 45 | import java.util.Arrays; |
| 46 | import java.util.Collections; |
Deepanshu Gupta | ccbc117 | 2015-07-10 17:38:29 -0700 | [diff] [blame] | 47 | import java.util.HashMap; |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 48 | import java.util.HashSet; |
Deepanshu Gupta | ccbc117 | 2015-07-10 17:38:29 -0700 | [diff] [blame] | 49 | import java.util.Map; |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 50 | import java.util.Set; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 51 | |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 52 | import static com.android.SdkConstants.AUTO_COMPLETE_TEXT_VIEW; |
| 53 | import static com.android.SdkConstants.BUTTON; |
| 54 | import static com.android.SdkConstants.CHECKED_TEXT_VIEW; |
| 55 | import static com.android.SdkConstants.CHECK_BOX; |
| 56 | import static com.android.SdkConstants.EDIT_TEXT; |
| 57 | import static com.android.SdkConstants.IMAGE_BUTTON; |
Diego Perez | 55656fe | 2016-07-22 10:05:32 +0100 | [diff] [blame] | 58 | import static com.android.SdkConstants.IMAGE_VIEW; |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 59 | import static com.android.SdkConstants.MULTI_AUTO_COMPLETE_TEXT_VIEW; |
| 60 | import static com.android.SdkConstants.RADIO_BUTTON; |
| 61 | import static com.android.SdkConstants.SEEK_BAR; |
| 62 | import static com.android.SdkConstants.SPINNER; |
| 63 | import static com.android.SdkConstants.TEXT_VIEW; |
Deepanshu Gupta | ada8590 | 2015-03-13 11:06:47 -0700 | [diff] [blame] | 64 | import static com.android.layoutlib.bridge.android.BridgeContext.getBaseContext; |
| 65 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Custom implementation of {@link LayoutInflater} to handle custom views. |
| 68 | */ |
| 69 | public final class BridgeInflater extends LayoutInflater { |
| 70 | |
Deepanshu Gupta | 37dbb8b | 2015-04-14 16:39:41 -0700 | [diff] [blame] | 71 | private final LayoutlibCallback mLayoutlibCallback; |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 72 | /** |
| 73 | * If true, the inflater will try to replace the framework widgets with the AppCompat versions. |
| 74 | * Ideally, this should be based on the activity being an AppCompat activity but since that is |
| 75 | * not trivial to check from layoutlib, we currently base the decision on the current theme |
| 76 | * being an AppCompat theme. |
| 77 | */ |
| 78 | private boolean mLoadAppCompatViews; |
Diego Perez | 3a6cdb1 | 2016-07-28 17:04:05 +0100 | [diff] [blame] | 79 | /** |
| 80 | * This set contains the framework views that have an AppCompat version but failed to load. |
| 81 | * This might happen because not all widgets are contained in all versions of the support |
| 82 | * library. |
| 83 | * This will help us to avoid trying to load the AppCompat version multiple times if it |
| 84 | * doesn't exist. |
| 85 | */ |
| 86 | private Set<String> mFailedAppCompatViews = new HashSet<>(); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 87 | private boolean mIsInMerge = false; |
| 88 | private ResourceReference mResourceReference; |
Deepanshu Gupta | ccbc117 | 2015-07-10 17:38:29 -0700 | [diff] [blame] | 89 | private Map<View, String> mOpenDrawerLayouts; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 90 | |
Deepanshu Gupta | 7535177 | 2015-08-28 11:53:46 -0700 | [diff] [blame] | 91 | // Keep in sync with the same value in LayoutInflater. |
| 92 | private static final int[] ATTRS_THEME = new int[] {com.android.internal.R.attr.theme }; |
| 93 | |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 94 | private static final String APPCOMPAT_WIDGET_PREFIX = "android.support.v7.widget.AppCompat"; |
| 95 | /** List of platform widgets that have an AppCompat version */ |
| 96 | private static final Set<String> APPCOMPAT_VIEWS = Collections.unmodifiableSet( |
| 97 | new HashSet<>( |
Diego Perez | 55656fe | 2016-07-22 10:05:32 +0100 | [diff] [blame] | 98 | Arrays.asList(TEXT_VIEW, IMAGE_VIEW, BUTTON, EDIT_TEXT, SPINNER, |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 99 | IMAGE_BUTTON, CHECK_BOX, RADIO_BUTTON, CHECKED_TEXT_VIEW, |
| 100 | AUTO_COMPLETE_TEXT_VIEW, MULTI_AUTO_COMPLETE_TEXT_VIEW, "RatingBar", |
| 101 | SEEK_BAR))); |
| 102 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 103 | /** |
| 104 | * List of class prefixes which are tried first by default. |
| 105 | * <p/> |
| 106 | * This should match the list in com.android.internal.policy.impl.PhoneLayoutInflater. |
| 107 | */ |
| 108 | private static final String[] sClassPrefixList = { |
| 109 | "android.widget.", |
Deepanshu Gupta | f8ea750 | 2015-05-18 18:47:07 -0700 | [diff] [blame] | 110 | "android.webkit.", |
| 111 | "android.app." |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
Deepanshu Gupta | f8ea750 | 2015-05-18 18:47:07 -0700 | [diff] [blame] | 114 | public static String[] getClassPrefixList() { |
| 115 | return sClassPrefixList; |
| 116 | } |
| 117 | |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 118 | private BridgeInflater(LayoutInflater original, Context newContext) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 119 | super(original, newContext); |
Deepanshu Gupta | ada8590 | 2015-03-13 11:06:47 -0700 | [diff] [blame] | 120 | newContext = getBaseContext(newContext); |
| 121 | if (newContext instanceof BridgeContext) { |
Deepanshu Gupta | 37dbb8b | 2015-04-14 16:39:41 -0700 | [diff] [blame] | 122 | mLayoutlibCallback = ((BridgeContext) newContext).getLayoutlibCallback(); |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 123 | mLoadAppCompatViews = ((BridgeContext) newContext).isAppCompatTheme(); |
Deepanshu Gupta | ada8590 | 2015-03-13 11:06:47 -0700 | [diff] [blame] | 124 | } else { |
Deepanshu Gupta | 37dbb8b | 2015-04-14 16:39:41 -0700 | [diff] [blame] | 125 | mLayoutlibCallback = null; |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 126 | mLoadAppCompatViews = false; |
Deepanshu Gupta | ada8590 | 2015-03-13 11:06:47 -0700 | [diff] [blame] | 127 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Deepanshu Gupta | 37dbb8b | 2015-04-14 16:39:41 -0700 | [diff] [blame] | 131 | * Instantiate a new BridgeInflater with an {@link LayoutlibCallback} object. |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 132 | * |
| 133 | * @param context The Android application context. |
Deepanshu Gupta | 37dbb8b | 2015-04-14 16:39:41 -0700 | [diff] [blame] | 134 | * @param layoutlibCallback the {@link LayoutlibCallback} object. |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 135 | */ |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 136 | public BridgeInflater(BridgeContext context, LayoutlibCallback layoutlibCallback) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 137 | super(context); |
Deepanshu Gupta | 37dbb8b | 2015-04-14 16:39:41 -0700 | [diff] [blame] | 138 | mLayoutlibCallback = layoutlibCallback; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 139 | mConstructorArgs[0] = context; |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 140 | mLoadAppCompatViews = context.isAppCompatTheme(); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException { |
| 145 | View view = null; |
| 146 | |
| 147 | try { |
Diego Perez | 3a6cdb1 | 2016-07-28 17:04:05 +0100 | [diff] [blame] | 148 | if (mLoadAppCompatViews |
| 149 | && APPCOMPAT_VIEWS.contains(name) |
| 150 | && !mFailedAppCompatViews.contains(name)) { |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 151 | // We are using an AppCompat theme so try to load the appcompat views |
Diego Perez | 3a6cdb1 | 2016-07-28 17:04:05 +0100 | [diff] [blame] | 152 | view = loadCustomView(APPCOMPAT_WIDGET_PREFIX + name, attrs, true); |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 153 | |
| 154 | if (view == null) { |
Diego Perez | 3a6cdb1 | 2016-07-28 17:04:05 +0100 | [diff] [blame] | 155 | mFailedAppCompatViews.add(name); // Do not try this one anymore |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 156 | } |
Diego Perez | 3a6cdb1 | 2016-07-28 17:04:05 +0100 | [diff] [blame] | 157 | } |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 158 | |
Diego Perez | 3a6cdb1 | 2016-07-28 17:04:05 +0100 | [diff] [blame] | 159 | if (view == null) { |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 160 | // First try to find a class using the default Android prefixes |
| 161 | for (String prefix : sClassPrefixList) { |
| 162 | try { |
| 163 | view = createView(name, prefix, attrs); |
| 164 | if (view != null) { |
| 165 | break; |
| 166 | } |
| 167 | } catch (ClassNotFoundException e) { |
| 168 | // Ignore. We'll try again using the base class below. |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Next try using the parent loader. This will most likely only work for |
| 173 | // fully-qualified class names. |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 174 | try { |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 175 | if (view == null) { |
| 176 | view = super.onCreateView(name, attrs); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 177 | } |
| 178 | } catch (ClassNotFoundException e) { |
Diego Perez | fb17566 | 2016-05-27 14:33:41 +0100 | [diff] [blame] | 179 | // Ignore. We'll try again using the custom view loader below. |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 183 | // Finally try again using the custom view loader |
Deepanshu Gupta | 61f23e9 | 2015-07-06 18:31:20 -0700 | [diff] [blame] | 184 | if (view == null) { |
| 185 | view = loadCustomView(name, attrs); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 186 | } |
Deepanshu Gupta | d30c141 | 2015-10-20 17:29:44 -0700 | [diff] [blame] | 187 | } catch (InflateException e) { |
| 188 | // Don't catch the InflateException below as that results in hiding the real cause. |
| 189 | throw e; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 190 | } catch (Exception e) { |
| 191 | // Wrap the real exception in a ClassNotFoundException, so that the calling method |
| 192 | // can deal with it. |
Deepanshu Gupta | d345f44 | 2015-03-05 18:38:37 -0800 | [diff] [blame] | 193 | throw new ClassNotFoundException("onCreateView", e); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | setupViewInContext(view, attrs); |
| 197 | |
| 198 | return view; |
| 199 | } |
| 200 | |
| 201 | @Override |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 202 | public View createViewFromTag(View parent, String name, Context context, AttributeSet attrs, |
Deepanshu Gupta | 7535177 | 2015-08-28 11:53:46 -0700 | [diff] [blame] | 203 | boolean ignoreThemeAttr) { |
Diego Perez | e852d19 | 2016-05-27 14:38:49 +0100 | [diff] [blame] | 204 | View view = null; |
| 205 | if (name.equals("view")) { |
| 206 | // This is usually done by the superclass but this allows us catching the error and |
| 207 | // reporting something useful. |
| 208 | name = attrs.getAttributeValue(null, "class"); |
| 209 | |
| 210 | if (name == null) { |
| 211 | Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to inflate view tag without " + |
| 212 | "class attribute", null); |
| 213 | // We weren't able to resolve the view so we just pass a mock View to be able to |
| 214 | // continue rendering. |
| 215 | view = new MockView(context, attrs); |
| 216 | ((MockView) view).setText("view"); |
| 217 | } |
| 218 | } |
| 219 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 220 | try { |
Diego Perez | e852d19 | 2016-05-27 14:38:49 +0100 | [diff] [blame] | 221 | if (view == null) { |
| 222 | view = super.createViewFromTag(parent, name, context, attrs, ignoreThemeAttr); |
| 223 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 224 | } catch (InflateException e) { |
Deepanshu Gupta | 7535177 | 2015-08-28 11:53:46 -0700 | [diff] [blame] | 225 | // Creation of ContextThemeWrapper code is same as in the super method. |
| 226 | // Apply a theme wrapper, if allowed and one is specified. |
| 227 | if (!ignoreThemeAttr) { |
| 228 | final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME); |
| 229 | final int themeResId = ta.getResourceId(0, 0); |
| 230 | if (themeResId != 0) { |
| 231 | context = new ContextThemeWrapper(context, themeResId); |
| 232 | } |
| 233 | ta.recycle(); |
| 234 | } |
Deepanshu Gupta | d30c141 | 2015-10-20 17:29:44 -0700 | [diff] [blame] | 235 | if (!(e.getCause() instanceof ClassNotFoundException)) { |
| 236 | // There is some unknown inflation exception in inflating a View that was found. |
| 237 | view = new MockView(context, attrs); |
| 238 | ((MockView) view).setText(name); |
| 239 | Bridge.getLog().error(LayoutLog.TAG_BROKEN, e.getMessage(), e, null); |
| 240 | } else { |
| 241 | final Object lastContext = mConstructorArgs[0]; |
| 242 | mConstructorArgs[0] = context; |
| 243 | // try to load the class from using the custom view loader |
| 244 | try { |
| 245 | view = loadCustomView(name, attrs); |
| 246 | } catch (Exception e2) { |
| 247 | // Wrap the real exception in an InflateException so that the calling |
| 248 | // method can deal with it. |
| 249 | InflateException exception = new InflateException(); |
| 250 | if (!e2.getClass().equals(ClassNotFoundException.class)) { |
| 251 | exception.initCause(e2); |
| 252 | } else { |
| 253 | exception.initCause(e); |
| 254 | } |
| 255 | throw exception; |
| 256 | } finally { |
| 257 | mConstructorArgs[0] = lastContext; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 258 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | setupViewInContext(view, attrs); |
| 263 | |
| 264 | return view; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public View inflate(int resource, ViewGroup root) { |
| 269 | Context context = getContext(); |
Deepanshu Gupta | ada8590 | 2015-03-13 11:06:47 -0700 | [diff] [blame] | 270 | context = getBaseContext(context); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 271 | if (context instanceof BridgeContext) { |
| 272 | BridgeContext bridgeContext = (BridgeContext)context; |
| 273 | |
| 274 | ResourceValue value = null; |
| 275 | |
Deepanshu Gupta | 37dbb8b | 2015-04-14 16:39:41 -0700 | [diff] [blame] | 276 | @SuppressWarnings("deprecation") |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 277 | Pair<ResourceType, String> layoutInfo = Bridge.resolveResourceId(resource); |
| 278 | if (layoutInfo != null) { |
| 279 | value = bridgeContext.getRenderResources().getFrameworkResource( |
| 280 | ResourceType.LAYOUT, layoutInfo.getSecond()); |
| 281 | } else { |
Deepanshu Gupta | 37dbb8b | 2015-04-14 16:39:41 -0700 | [diff] [blame] | 282 | layoutInfo = mLayoutlibCallback.resolveResourceId(resource); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 283 | |
| 284 | if (layoutInfo != null) { |
| 285 | value = bridgeContext.getRenderResources().getProjectResource( |
| 286 | ResourceType.LAYOUT, layoutInfo.getSecond()); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (value != null) { |
| 291 | File f = new File(value.getValue()); |
| 292 | if (f.isFile()) { |
| 293 | try { |
Deepanshu Gupta | e91096c | 2015-09-28 18:03:18 -0700 | [diff] [blame] | 294 | XmlPullParser parser = ParserFactory.create(f, true); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 295 | |
| 296 | BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser( |
Deepanshu Gupta | 33c1c07 | 2015-02-23 09:18:08 -0800 | [diff] [blame] | 297 | parser, bridgeContext, value.isFramework()); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 298 | |
| 299 | return inflate(bridgeParser, root); |
| 300 | } catch (Exception e) { |
| 301 | Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, |
Deepanshu Gupta | d345f44 | 2015-03-05 18:38:37 -0800 | [diff] [blame] | 302 | "Failed to parse file " + f.getAbsolutePath(), e, null); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 303 | |
| 304 | return null; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | return null; |
| 310 | } |
| 311 | |
Diego Perez | 3a6cdb1 | 2016-07-28 17:04:05 +0100 | [diff] [blame] | 312 | /** |
| 313 | * Instantiates the given view name and returns the instance. If the view doesn't exist, a |
| 314 | * MockView or null might be returned. |
| 315 | * @param name the custom view name |
| 316 | * @param attrs the {@link AttributeSet} to be passed to the view constructor |
| 317 | * @param silent if true, errors while loading the view won't be reported and, if the view |
| 318 | * doesn't exist, null will be returned. |
| 319 | */ |
| 320 | private View loadCustomView(String name, AttributeSet attrs, boolean silent) throws Exception { |
Deepanshu Gupta | 37dbb8b | 2015-04-14 16:39:41 -0700 | [diff] [blame] | 321 | if (mLayoutlibCallback != null) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 322 | // first get the classname in case it's not the node name |
| 323 | if (name.equals("view")) { |
| 324 | name = attrs.getAttributeValue(null, "class"); |
Diego Perez | e852d19 | 2016-05-27 14:38:49 +0100 | [diff] [blame] | 325 | if (name == null) { |
| 326 | return null; |
| 327 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | mConstructorArgs[1] = attrs; |
| 331 | |
Diego Perez | 3a6cdb1 | 2016-07-28 17:04:05 +0100 | [diff] [blame] | 332 | Object customView = silent ? |
| 333 | mLayoutlibCallback.loadClass(name, mConstructorSignature, mConstructorArgs) |
| 334 | : mLayoutlibCallback.loadView(name, mConstructorSignature, mConstructorArgs); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 335 | |
| 336 | if (customView instanceof View) { |
| 337 | return (View)customView; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return null; |
| 342 | } |
| 343 | |
Diego Perez | 3a6cdb1 | 2016-07-28 17:04:05 +0100 | [diff] [blame] | 344 | private View loadCustomView(String name, AttributeSet attrs) throws Exception { |
| 345 | return loadCustomView(name, attrs, false); |
| 346 | } |
| 347 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 348 | private void setupViewInContext(View view, AttributeSet attrs) { |
Deepanshu Gupta | 1001961 | 2014-04-18 12:32:38 -0700 | [diff] [blame] | 349 | Context context = getContext(); |
Deepanshu Gupta | ada8590 | 2015-03-13 11:06:47 -0700 | [diff] [blame] | 350 | context = getBaseContext(context); |
Deepanshu Gupta | 1001961 | 2014-04-18 12:32:38 -0700 | [diff] [blame] | 351 | if (context instanceof BridgeContext) { |
| 352 | BridgeContext bc = (BridgeContext) context; |
| 353 | // get the view key |
| 354 | Object viewKey = getViewKeyFromParser(attrs, bc, mResourceReference, mIsInMerge); |
| 355 | if (viewKey != null) { |
| 356 | bc.addViewKey(view, viewKey); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 357 | } |
Diego Perez | d154591 | 2016-03-04 18:33:26 +0000 | [diff] [blame] | 358 | String scrollPosX = attrs.getAttributeValue(BridgeConstants.NS_RESOURCES, "scrollX"); |
| 359 | if (scrollPosX != null && scrollPosX.endsWith("px")) { |
| 360 | int value = Integer.parseInt(scrollPosX.substring(0, scrollPosX.length() - 2)); |
| 361 | bc.setScrollXPos(view, value); |
| 362 | } |
| 363 | String scrollPosY = attrs.getAttributeValue(BridgeConstants.NS_RESOURCES, "scrollY"); |
| 364 | if (scrollPosY != null && scrollPosY.endsWith("px")) { |
| 365 | int value = Integer.parseInt(scrollPosY.substring(0, scrollPosY.length() - 2)); |
| 366 | bc.setScrollYPos(view, value); |
Jens Ole Lauridsen | 4dfe4d4 | 2015-06-05 08:04:27 -0700 | [diff] [blame] | 367 | } |
Deepanshu Gupta | 61f23e9 | 2015-07-06 18:31:20 -0700 | [diff] [blame] | 368 | if (ReflectionUtils.isInstanceOf(view, RecyclerViewUtil.CN_RECYCLER_VIEW)) { |
| 369 | Integer resourceId = null; |
| 370 | String attrVal = attrs.getAttributeValue(BridgeConstants.NS_TOOLS_URI, |
| 371 | BridgeConstants.ATTR_LIST_ITEM); |
| 372 | if (attrVal != null && !attrVal.isEmpty()) { |
| 373 | ResourceValue resValue = bc.getRenderResources().findResValue(attrVal, false); |
| 374 | if (resValue.isFramework()) { |
| 375 | resourceId = Bridge.getResourceId(resValue.getResourceType(), |
| 376 | resValue.getName()); |
| 377 | } else { |
| 378 | resourceId = mLayoutlibCallback.getResourceId(resValue.getResourceType(), |
| 379 | resValue.getName()); |
| 380 | } |
| 381 | } |
| 382 | if (resourceId == null) { |
| 383 | resourceId = 0; |
| 384 | } |
| 385 | RecyclerViewUtil.setAdapter(view, bc, mLayoutlibCallback, resourceId); |
Deepanshu Gupta | ccbc117 | 2015-07-10 17:38:29 -0700 | [diff] [blame] | 386 | } else if (ReflectionUtils.isInstanceOf(view, DrawerLayoutUtil.CN_DRAWER_LAYOUT)) { |
| 387 | String attrVal = attrs.getAttributeValue(BridgeConstants.NS_TOOLS_URI, |
| 388 | BridgeConstants.ATTR_OPEN_DRAWER); |
| 389 | if (attrVal != null) { |
| 390 | getDrawerLayoutMap().put(view, attrVal); |
| 391 | } |
Deepanshu Gupta | 61f23e9 | 2015-07-06 18:31:20 -0700 | [diff] [blame] | 392 | } |
Jerome Gaillard | 1d8b0bb | 2016-06-02 15:35:22 +0100 | [diff] [blame] | 393 | else if (view instanceof NumberPicker) { |
| 394 | NumberPicker numberPicker = (NumberPicker) view; |
| 395 | String minValue = attrs.getAttributeValue(BridgeConstants.NS_TOOLS_URI, "minValue"); |
| 396 | if (minValue != null) { |
| 397 | numberPicker.setMinValue(Integer.parseInt(minValue)); |
| 398 | } |
| 399 | String maxValue = attrs.getAttributeValue(BridgeConstants.NS_TOOLS_URI, "maxValue"); |
| 400 | if (maxValue != null) { |
| 401 | numberPicker.setMaxValue(Integer.parseInt(maxValue)); |
| 402 | } |
| 403 | } |
Deepanshu Gupta | ccbc117 | 2015-07-10 17:38:29 -0700 | [diff] [blame] | 404 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | public void setIsInMerge(boolean isInMerge) { |
| 409 | mIsInMerge = isInMerge; |
| 410 | } |
| 411 | |
| 412 | public void setResourceReference(ResourceReference reference) { |
| 413 | mResourceReference = reference; |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public LayoutInflater cloneInContext(Context newContext) { |
| 418 | return new BridgeInflater(this, newContext); |
| 419 | } |
Deepanshu Gupta | 1001961 | 2014-04-18 12:32:38 -0700 | [diff] [blame] | 420 | |
| 421 | /*package*/ static Object getViewKeyFromParser(AttributeSet attrs, BridgeContext bc, |
| 422 | ResourceReference resourceReference, boolean isInMerge) { |
| 423 | |
| 424 | if (!(attrs instanceof BridgeXmlBlockParser)) { |
| 425 | return null; |
| 426 | } |
| 427 | BridgeXmlBlockParser parser = ((BridgeXmlBlockParser) attrs); |
| 428 | |
| 429 | // get the view key |
| 430 | Object viewKey = parser.getViewCookie(); |
| 431 | |
| 432 | if (viewKey == null) { |
| 433 | int currentDepth = parser.getDepth(); |
| 434 | |
| 435 | // test whether we are in an included file or in a adapter binding view. |
| 436 | BridgeXmlBlockParser previousParser = bc.getPreviousParser(); |
| 437 | if (previousParser != null) { |
| 438 | // looks like we are inside an embedded layout. |
| 439 | // only apply the cookie of the calling node (<include>) if we are at the |
| 440 | // top level of the embedded layout. If there is a merge tag, then |
| 441 | // skip it and look for the 2nd level |
| 442 | int testDepth = isInMerge ? 2 : 1; |
| 443 | if (currentDepth == testDepth) { |
| 444 | viewKey = previousParser.getViewCookie(); |
| 445 | // if we are in a merge, wrap the cookie in a MergeCookie. |
| 446 | if (viewKey != null && isInMerge) { |
| 447 | viewKey = new MergeCookie(viewKey); |
| 448 | } |
| 449 | } |
| 450 | } else if (resourceReference != null && currentDepth == 1) { |
| 451 | // else if there's a resource reference, this means we are in an adapter |
| 452 | // binding case. Set the resource ref as the view cookie only for the top |
| 453 | // level view. |
| 454 | viewKey = resourceReference; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return viewKey; |
| 459 | } |
Deepanshu Gupta | ccbc117 | 2015-07-10 17:38:29 -0700 | [diff] [blame] | 460 | |
| 461 | public void postInflateProcess(View view) { |
| 462 | if (mOpenDrawerLayouts != null) { |
| 463 | String gravity = mOpenDrawerLayouts.get(view); |
| 464 | if (gravity != null) { |
| 465 | DrawerLayoutUtil.openDrawer(view, gravity); |
| 466 | } |
| 467 | mOpenDrawerLayouts.remove(view); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | @NonNull |
| 472 | private Map<View, String> getDrawerLayoutMap() { |
| 473 | if (mOpenDrawerLayouts == null) { |
| 474 | mOpenDrawerLayouts = new HashMap<View, String>(4); |
| 475 | } |
| 476 | return mOpenDrawerLayouts; |
| 477 | } |
| 478 | |
| 479 | public void onDoneInflation() { |
| 480 | if (mOpenDrawerLayouts != null) { |
| 481 | mOpenDrawerLayouts.clear(); |
| 482 | } |
| 483 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 484 | } |