summaryrefslogtreecommitdiff
path: root/android/view/BridgeInflater.java
blob: 58d8c527464269538e967d23a230ef90394da5a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * 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.MockView;
import com.android.layoutlib.bridge.android.BridgeContext;
import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
import com.android.layoutlib.bridge.android.support.DrawerLayoutUtil;
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.annotation.NonNull;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.NumberPicker;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static com.android.SdkConstants.AUTO_COMPLETE_TEXT_VIEW;
import static com.android.SdkConstants.BUTTON;
import static com.android.SdkConstants.CHECKED_TEXT_VIEW;
import static com.android.SdkConstants.CHECK_BOX;
import static com.android.SdkConstants.EDIT_TEXT;
import static com.android.SdkConstants.IMAGE_BUTTON;
import static com.android.SdkConstants.IMAGE_VIEW;
import static com.android.SdkConstants.MULTI_AUTO_COMPLETE_TEXT_VIEW;
import static com.android.SdkConstants.RADIO_BUTTON;
import static com.android.SdkConstants.SEEK_BAR;
import static com.android.SdkConstants.SPINNER;
import static com.android.SdkConstants.TEXT_VIEW;
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;
    /**
     * If true, the inflater will try to replace the framework widgets with the AppCompat versions.
     * Ideally, this should be based on the activity being an AppCompat activity but since that is
     * not trivial to check from layoutlib, we currently base the decision on the current theme
     * being an AppCompat theme.
     */
    private boolean mLoadAppCompatViews;
    /**
     * This set contains the framework views that have an AppCompat version but failed to load.
     * This might happen because not all widgets are contained in all versions of the support
     * library.
     * This will help us to avoid trying to load the AppCompat version multiple times if it
     * doesn't exist.
     */
    private Set<String> mFailedAppCompatViews = new HashSet<>();
    private boolean mIsInMerge = false;
    private ResourceReference mResourceReference;
    private Map<View, String> mOpenDrawerLayouts;

    // Keep in sync with the same value in LayoutInflater.
    private static final int[] ATTRS_THEME = new int[] {com.android.internal.R.attr.theme };

    private static final String APPCOMPAT_WIDGET_PREFIX = "android.support.v7.widget.AppCompat";
    /** List of platform widgets that have an AppCompat version */
    private static final Set<String> APPCOMPAT_VIEWS = Collections.unmodifiableSet(
            new HashSet<>(
                    Arrays.asList(TEXT_VIEW, IMAGE_VIEW, BUTTON, EDIT_TEXT, SPINNER,
                            IMAGE_BUTTON, CHECK_BOX, RADIO_BUTTON, CHECKED_TEXT_VIEW,
                            AUTO_COMPLETE_TEXT_VIEW, MULTI_AUTO_COMPLETE_TEXT_VIEW, "RatingBar",
                            SEEK_BAR)));

    /**
     * List of class prefixes which are tried first by default.
     * <p/>
     * 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;
    }

    private BridgeInflater(LayoutInflater original, Context newContext) {
        super(original, newContext);
        newContext = getBaseContext(newContext);
        if (newContext instanceof BridgeContext) {
            mLayoutlibCallback = ((BridgeContext) newContext).getLayoutlibCallback();
            mLoadAppCompatViews = ((BridgeContext) newContext).isAppCompatTheme();
        } else {
            mLayoutlibCallback = null;
            mLoadAppCompatViews = false;
        }
    }

    /**
     * Instantiate a new BridgeInflater with an {@link LayoutlibCallback} object.
     *
     * @param context The Android application context.
     * @param layoutlibCallback the {@link LayoutlibCallback} object.
     */
    public BridgeInflater(BridgeContext context, LayoutlibCallback layoutlibCallback) {
        super(context);
        mLayoutlibCallback = layoutlibCallback;
        mConstructorArgs[0] = context;
        mLoadAppCompatViews = context.isAppCompatTheme();
    }

    @Override
    public View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
        View view = null;

        try {
            if (mLoadAppCompatViews
                    && APPCOMPAT_VIEWS.contains(name)
                    && !mFailedAppCompatViews.contains(name)) {
                // We are using an AppCompat theme so try to load the appcompat views
                view = loadCustomView(APPCOMPAT_WIDGET_PREFIX + name, attrs, true);

                if (view == null) {
                    mFailedAppCompatViews.add(name); // Do not try this one anymore
                }
            }

            if (view == null) {
                // 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 (InflateException e) {
            // Don't catch the InflateException below as that results in hiding the real cause.
            throw e;
        } 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 ignoreThemeAttr) {
        View view = null;
        if (name.equals("view")) {
            // This is usually done by the superclass but this allows us catching the error and
            // reporting something useful.
            name = attrs.getAttributeValue(null, "class");

            if (name == null) {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to inflate view tag without " +
                  "class attribute", null);
                // We weren't able to resolve the view so we just pass a mock View to be able to
                // continue rendering.
                view = new MockView(context, attrs);
                ((MockView) view).setText("view");
            }
        }

        try {
            if (view == null) {
                view = super.createViewFromTag(parent, name, context, attrs, ignoreThemeAttr);
            }
        } catch (InflateException e) {
            // Creation of ContextThemeWrapper code is same as in the super method.
            // Apply a theme wrapper, if allowed and one is specified.
            if (!ignoreThemeAttr) {
                final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
                final int themeResId = ta.getResourceId(0, 0);
                if (themeResId != 0) {
                    context = new ContextThemeWrapper(context, themeResId);
                }
                ta.recycle();
            }
            if (!(e.getCause() instanceof ClassNotFoundException)) {
                // There is some unknown inflation exception in inflating a View that was found.
                view = new MockView(context, attrs);
                ((MockView) view).setText(name);
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, e.getMessage(), e, null);
            } else {
                final Object lastContext = mConstructorArgs[0];
                mConstructorArgs[0] = context;
                // 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;
                } finally {
                    mConstructorArgs[0] = lastContext;
                }
            }
        }

        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<ResourceType, String> layoutInfo = Bridge.resolveResourceId(resource);
            if (layoutInfo != null) {
                value = bridgeContext.getRenderResources().getFrameworkResource(
                        ResourceType.LAYOUT, layoutInfo.getSecond());
            } else {
                layoutInfo = mLayoutlibCallback.resolveResourceId(resource);

                if (layoutInfo != null) {
                    value = bridgeContext.getRenderResources().getProjectResource(
                            ResourceType.LAYOUT, layoutInfo.getSecond());
                }
            }

            if (value != null) {
                File f = new File(value.getValue());
                if (f.isFile()) {
                    try {
                        XmlPullParser parser = ParserFactory.create(f, true);

                        BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(
                                parser, bridgeContext, value.isFramework());

                        return inflate(bridgeParser, root);
                    } catch (Exception e) {
                        Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ,
                                "Failed to parse file " + f.getAbsolutePath(), e, null);

                        return null;
                    }
                }
            }
        }
        return null;
    }

    /**
     * Instantiates the given view name and returns the instance. If the view doesn't exist, a
     * MockView or null might be returned.
     * @param name the custom view name
     * @param attrs the {@link AttributeSet} to be passed to the view constructor
     * @param silent if true, errors while loading the view won't be reported and, if the view
     * doesn't exist, null will be returned.
     */
    private View loadCustomView(String name, AttributeSet attrs, boolean silent) throws Exception {
        if (mLayoutlibCallback != null) {
            // first get the classname in case it's not the node name
            if (name.equals("view")) {
                name = attrs.getAttributeValue(null, "class");
                if (name == null) {
                    return null;
                }
            }

            mConstructorArgs[1] = attrs;

            Object customView = silent ?
                    mLayoutlibCallback.loadClass(name, mConstructorSignature, mConstructorArgs)
                    : mLayoutlibCallback.loadView(name, mConstructorSignature, mConstructorArgs);

            if (customView instanceof View) {
                return (View)customView;
            }
        }

        return null;
    }

    private View loadCustomView(String name, AttributeSet attrs) throws Exception {
        return loadCustomView(name, attrs, false);
    }

    private void setupViewInContext(View view, AttributeSet attrs) {
        Context context = getContext();
        context = getBaseContext(context);
        if (context instanceof BridgeContext) {
            BridgeContext bc = (BridgeContext) context;
            // get the view key
            Object viewKey = getViewKeyFromParser(attrs, bc, mResourceReference, mIsInMerge);
            if (viewKey != null) {
                bc.addViewKey(view, viewKey);
            }
            String scrollPosX = attrs.getAttributeValue(BridgeConstants.NS_RESOURCES, "scrollX");
            if (scrollPosX != null && scrollPosX.endsWith("px")) {
                int value = Integer.parseInt(scrollPosX.substring(0, scrollPosX.length() - 2));
                bc.setScrollXPos(view, value);
            }
            String scrollPosY = attrs.getAttributeValue(BridgeConstants.NS_RESOURCES, "scrollY");
            if (scrollPosY != null && scrollPosY.endsWith("px")) {
                int value = Integer.parseInt(scrollPosY.substring(0, scrollPosY.length() - 2));
                bc.setScrollYPos(view, value);
            }
            if (ReflectionUtils.isInstanceOf(view, RecyclerViewUtil.CN_RECYCLER_VIEW)) {
                Integer resourceId = null;
                String attrListItemValue = attrs.getAttributeValue(BridgeConstants.NS_TOOLS_URI,
                        BridgeConstants.ATTR_LIST_ITEM);
                int attrItemCountValue = attrs.getAttributeIntValue(BridgeConstants.NS_TOOLS_URI,
                        BridgeConstants.ATTR_ITEM_COUNT, -1);
                if (attrListItemValue != null && !attrListItemValue.isEmpty()) {
                    ResourceValue resValue = bc.getRenderResources().findResValue(attrListItemValue, false);
                    if (resValue.isFramework()) {
                        resourceId = Bridge.getResourceId(resValue.getResourceType(),
                                resValue.getName());
                    } else {
                        resourceId = mLayoutlibCallback.getResourceId(resValue.getResourceType(),
                                resValue.getName());
                    }
                }
                if (resourceId == null) {
                    resourceId = 0;
                }
                RecyclerViewUtil.setAdapter(view, bc, mLayoutlibCallback, resourceId, attrItemCountValue);
            } else if (ReflectionUtils.isInstanceOf(view, DrawerLayoutUtil.CN_DRAWER_LAYOUT)) {
                String attrVal = attrs.getAttributeValue(BridgeConstants.NS_TOOLS_URI,
                        BridgeConstants.ATTR_OPEN_DRAWER);
                if (attrVal != null) {
                    getDrawerLayoutMap().put(view, attrVal);
                }
            }
            else if (view instanceof NumberPicker) {
                NumberPicker numberPicker = (NumberPicker) view;
                String minValue = attrs.getAttributeValue(BridgeConstants.NS_TOOLS_URI, "minValue");
                if (minValue != null) {
                    numberPicker.setMinValue(Integer.parseInt(minValue));
                }
                String maxValue = attrs.getAttributeValue(BridgeConstants.NS_TOOLS_URI, "maxValue");
                if (maxValue != null) {
                    numberPicker.setMaxValue(Integer.parseInt(maxValue));
                }
            }
            else if (view instanceof ImageView) {
                ImageView img = (ImageView) view;
                Drawable drawable = img.getDrawable();
                if (drawable instanceof Animatable) {
                    if (!((Animatable) drawable).isRunning()) {
                        ((Animatable) drawable).start();
                    }
                }
            }

        }
    }

    public void setIsInMerge(boolean isInMerge) {
        mIsInMerge = isInMerge;
    }

    public void setResourceReference(ResourceReference reference) {
        mResourceReference = reference;
    }

    @Override
    public LayoutInflater cloneInContext(Context newContext) {
        return new BridgeInflater(this, newContext);
    }

    /*package*/ static Object getViewKeyFromParser(AttributeSet attrs, BridgeContext bc,
            ResourceReference resourceReference, boolean isInMerge) {

        if (!(attrs instanceof BridgeXmlBlockParser)) {
            return null;
        }
        BridgeXmlBlockParser parser = ((BridgeXmlBlockParser) attrs);

        // get the view key
        Object viewKey = parser.getViewCookie();

        if (viewKey == null) {
            int currentDepth = parser.getDepth();

            // test whether we are in an included file or in a adapter binding view.
            BridgeXmlBlockParser previousParser = bc.getPreviousParser();
            if (previousParser != null) {
                // looks like we are inside an embedded layout.
                // only apply the cookie of the calling node (<include>) if we are at the
                // top level of the embedded layout. If there is a merge tag, then
                // skip it and look for the 2nd level
                int testDepth = isInMerge ? 2 : 1;
                if (currentDepth == testDepth) {
                    viewKey = previousParser.getViewCookie();
                    // if we are in a merge, wrap the cookie in a MergeCookie.
                    if (viewKey != null && isInMerge) {
                        viewKey = new MergeCookie(viewKey);
                    }
                }
            } else if (resourceReference != null && currentDepth == 1) {
                // else if there's a resource reference, this means we are in an adapter
                // binding case. Set the resource ref as the view cookie only for the top
                // level view.
                viewKey = resourceReference;
            }
        }

        return viewKey;
    }

    public void postInflateProcess(View view) {
        if (mOpenDrawerLayouts != null) {
            String gravity = mOpenDrawerLayouts.get(view);
            if (gravity != null) {
                DrawerLayoutUtil.openDrawer(view, gravity);
            }
            mOpenDrawerLayouts.remove(view);
        }
    }

    @NonNull
    private Map<View, String> getDrawerLayoutMap() {
        if (mOpenDrawerLayouts == null) {
            mOpenDrawerLayouts = new HashMap<View, String>(4);
        }
        return mOpenDrawerLayouts;
    }

    public void onDoneInflation() {
        if (mOpenDrawerLayouts != null) {
            mOpenDrawerLayouts.clear();
        }
    }
}