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 final class LayoutInflaterCompat { 27 28 interface LayoutInflaterCompatImpl { 29 public void setFactory(LayoutInflater layoutInflater, LayoutInflaterFactory factory); 30 public LayoutInflaterFactory getFactory(LayoutInflater layoutInflater); 31 } 32 33 static class LayoutInflaterCompatImplBase implements LayoutInflaterCompatImpl { 34 @Override 35 public void setFactory(LayoutInflater layoutInflater, LayoutInflaterFactory factory) { 36 LayoutInflaterCompatBase.setFactory(layoutInflater, factory); 37 } 38 39 @Override 40 public LayoutInflaterFactory getFactory(LayoutInflater layoutInflater) { 41 return LayoutInflaterCompatBase.getFactory(layoutInflater); 42 } 43 } 44 45 static class LayoutInflaterCompatImplV11 extends LayoutInflaterCompatImplBase { 46 @Override 47 public void setFactory(LayoutInflater layoutInflater, LayoutInflaterFactory factory) { 48 LayoutInflaterCompatHC.setFactory(layoutInflater, factory); 49 } 50 } 51 52 static class LayoutInflaterCompatImplV21 extends LayoutInflaterCompatImplV11 { 53 @Override 54 public void setFactory(LayoutInflater layoutInflater, LayoutInflaterFactory factory) { 55 LayoutInflaterCompatLollipop.setFactory(layoutInflater, factory); 56 } 57 } 58 59 static final LayoutInflaterCompatImpl IMPL; 60 static { 61 final int version = Build.VERSION.SDK_INT; 62 if (version >= 21) { 63 IMPL = new LayoutInflaterCompatImplV21(); 64 } else if (version >= 11) { 65 IMPL = new LayoutInflaterCompatImplV11(); 66 } else { 67 IMPL = new LayoutInflaterCompatImplBase(); 68 } 69 } 70 71 /* 72 * Hide the constructor. 73 */ 74 private LayoutInflaterCompat() { 75 } 76 77 /** 78 * Attach a custom Factory interface for creating views while using 79 * this LayoutInflater. This must not be null, and can only be set once; 80 * after setting, you can not change the factory. 81 * 82 * @see LayoutInflater#setFactory(android.view.LayoutInflater.Factory) 83 */ 84 public static void setFactory(LayoutInflater inflater, LayoutInflaterFactory factory) { 85 IMPL.setFactory(inflater, factory); 86 } 87 88 /** 89 * Return the current {@link LayoutInflaterFactory} (or null). This is 90 * called on each element name. If the factory returns a View, add that 91 * to the hierarchy. If it returns null, proceed to call onCreateView(name). 92 * 93 * @return The {@link LayoutInflaterFactory} associated with the 94 * {@link LayoutInflater}. Will be {@code null} if the inflater does not 95 * have a {@link LayoutInflaterFactory} but a raw {@link LayoutInflater.Factory}. 96 * @see LayoutInflater#getFactory() 97 */ 98 public static LayoutInflaterFactory getFactory(LayoutInflater inflater) { 99 return IMPL.getFactory(inflater); 100 } 101 102} 103