/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.view; import com.android.ide.common.rendering.api.LayoutLog; import com.android.ide.common.rendering.api.LayoutlibCallback; import com.android.ide.common.rendering.api.MergeCookie; import com.android.ide.common.rendering.api.ResourceReference; import com.android.ide.common.rendering.api.ResourceValue; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.android.BridgeContext; import com.android.layoutlib.bridge.android.BridgeXmlBlockParser; import com.android.layoutlib.bridge.android.support.RecyclerViewUtil; import com.android.layoutlib.bridge.impl.ParserFactory; import com.android.layoutlib.bridge.util.ReflectionUtils; import com.android.resources.ResourceType; import com.android.util.Pair; import org.xmlpull.v1.XmlPullParser; import android.content.Context; import android.util.AttributeSet; import java.io.File; import static com.android.layoutlib.bridge.android.BridgeContext.getBaseContext; /** * Custom implementation of {@link LayoutInflater} to handle custom views. */ public final class BridgeInflater extends LayoutInflater { private final LayoutlibCallback mLayoutlibCallback; private boolean mIsInMerge = false; private ResourceReference mResourceReference; /** * List of class prefixes which are tried first by default. *
* This should match the list in com.android.internal.policy.impl.PhoneLayoutInflater. */ private static final String[] sClassPrefixList = { "android.widget.", "android.webkit.", "android.app." }; public static String[] getClassPrefixList() { return sClassPrefixList; } protected BridgeInflater(LayoutInflater original, Context newContext) { super(original, newContext); newContext = getBaseContext(newContext); if (newContext instanceof BridgeContext) { mLayoutlibCallback = ((BridgeContext) newContext).getLayoutlibCallback(); } else { mLayoutlibCallback = null; } } /** * Instantiate a new BridgeInflater with an {@link LayoutlibCallback} object. * * @param context The Android application context. * @param layoutlibCallback the {@link LayoutlibCallback} object. */ public BridgeInflater(Context context, LayoutlibCallback layoutlibCallback) { super(context); mLayoutlibCallback = layoutlibCallback; mConstructorArgs[0] = context; } @Override public View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException { View view = null; try { // First try to find a class using the default Android prefixes for (String prefix : sClassPrefixList) { try { view = createView(name, prefix, attrs); if (view != null) { break; } } catch (ClassNotFoundException e) { // Ignore. We'll try again using the base class below. } } // Next try using the parent loader. This will most likely only work for // fully-qualified class names. try { if (view == null) { view = super.onCreateView(name, attrs); } } catch (ClassNotFoundException e) { // Ignore. We'll try again using the custom view loader below. } // Finally try again using the custom view loader if (view == null) { view = loadCustomView(name, attrs); } } catch (Exception e) { // Wrap the real exception in a ClassNotFoundException, so that the calling method // can deal with it. throw new ClassNotFoundException("onCreateView", e); } setupViewInContext(view, attrs); return view; } @Override public View createViewFromTag(View parent, String name, Context context, AttributeSet attrs, boolean ignoreThemeAttrs) { View view; try { view = super.createViewFromTag(parent, name, context, attrs, ignoreThemeAttrs); } catch (InflateException e) { // try to load the class from using the custom view loader try { view = loadCustomView(name, attrs); } catch (Exception e2) { // Wrap the real exception in an InflateException so that the calling // method can deal with it. InflateException exception = new InflateException(); if (!e2.getClass().equals(ClassNotFoundException.class)) { exception.initCause(e2); } else { exception.initCause(e); } throw exception; } } setupViewInContext(view, attrs); return view; } @Override public View inflate(int resource, ViewGroup root) { Context context = getContext(); context = getBaseContext(context); if (context instanceof BridgeContext) { BridgeContext bridgeContext = (BridgeContext)context; ResourceValue value = null; @SuppressWarnings("deprecation") Pair