1/*
2 * Copyright (C) 2014 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
17package android.support.v4.view;
18
19import android.os.Build;
20import android.view.LayoutInflater;
21
22/**
23 * Helper for accessing features in {@link android.view.LayoutInflater}
24 * introduced after API level 4 in a backwards compatible fashion.
25 */
26public class LayoutInflaterCompat {
27
28    interface LayoutInflaterCompatImpl {
29        public void setFactory(LayoutInflater layoutInflater, LayoutInflaterFactory factory);
30    }
31
32    static class LayoutInflaterCompatImplBase implements LayoutInflaterCompatImpl {
33        @Override
34        public void setFactory(LayoutInflater layoutInflater, LayoutInflaterFactory factory) {
35            LayoutInflaterCompatBase.setFactory(layoutInflater, factory);
36        }
37    }
38
39    static class LayoutInflaterCompatImplV11 extends LayoutInflaterCompatImplBase {
40        @Override
41        public void setFactory(LayoutInflater layoutInflater, LayoutInflaterFactory factory) {
42            LayoutInflaterCompatHC.setFactory(layoutInflater, factory);
43        }
44    }
45
46    static class LayoutInflaterCompatImplV21 extends LayoutInflaterCompatImplV11 {
47        @Override
48        public void setFactory(LayoutInflater layoutInflater, LayoutInflaterFactory factory) {
49            LayoutInflaterCompatLollipop.setFactory(layoutInflater, factory);
50        }
51    }
52
53    static final LayoutInflaterCompatImpl IMPL;
54    static {
55        final int version = Build.VERSION.SDK_INT;
56        if (version >= 21) {
57            IMPL = new LayoutInflaterCompatImplV21();
58        } else if (version >= 11) {
59            IMPL = new LayoutInflaterCompatImplV11();
60        } else {
61            IMPL = new LayoutInflaterCompatImplBase();
62        }
63    }
64
65    /*
66     * Hide the constructor.
67     */
68    private LayoutInflaterCompat() {
69    }
70
71    /**
72     * Attach a custom Factory interface for creating views while using
73     * this LayoutInflater. This must not be null, and can only be set once;
74     * after setting, you can not change the factory.
75     *
76     * @see LayoutInflater#setFactory(android.view.LayoutInflater.Factory)
77     */
78    public static void setFactory(LayoutInflater inflater, LayoutInflaterFactory factory) {
79        IMPL.setFactory(inflater, factory);
80    }
81
82}
83