| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package android.testing; |
| 16 |
|
| 17 |
import android.annotation.NonNull; |
| 18 |
import android.content.Context; |
| 19 |
import android.util.ArrayMap; |
| 20 |
import android.util.ArraySet; |
| 21 |
import android.util.AttributeSet; |
| 22 |
import android.util.Log; |
| 23 |
import android.view.LayoutInflater; |
| 24 |
import android.view.View; |
| 25 |
import java.util.Map; |
| 26 |
import java.util.Set; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class LayoutInflaterBuilder { |
| 37 |
private static final String TAG = "LayoutInflaterBuilder"; |
| 38 |
|
| 39 |
private Context mFromContext; |
| 40 |
private Context mTargetContext; |
| 41 |
private Map<String, String> mReplaceMap; |
| 42 |
private Set<Class> mDisallowedClasses; |
| 43 |
private LayoutInflater mBuiltInflater; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public LayoutInflaterBuilder(@NonNull Context fromContext) { |
| 53 |
mFromContext = fromContext; |
| 54 |
mTargetContext = fromContext; |
| 55 |
mReplaceMap = null; |
| 56 |
mDisallowedClasses = null; |
| 57 |
mBuiltInflater = null; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public LayoutInflaterBuilder target(@NonNull Context targetContext) { |
| 68 |
assertIfAlreadyBuilt(); |
| 69 |
mTargetContext = targetContext; |
| 70 |
return this; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public LayoutInflaterBuilder replace(@NonNull Class from, @NonNull Class to) { |
| 82 |
return replace(from.getName(), to); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public LayoutInflaterBuilder replace(@NonNull String tag, @NonNull Class to) { |
| 94 |
assertIfAlreadyBuilt(); |
| 95 |
if (mReplaceMap == null) { |
| 96 |
mReplaceMap = new ArrayMap<String, String>(); |
| 97 |
} |
| 98 |
mReplaceMap.put(tag, to.getName()); |
| 99 |
return this; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public LayoutInflaterBuilder disallow(@NonNull Class disallowedClass) { |
| 110 |
assertIfAlreadyBuilt(); |
| 111 |
if (mDisallowedClasses == null) { |
| 112 |
mDisallowedClasses = new ArraySet<Class>(); |
| 113 |
} |
| 114 |
mDisallowedClasses.add(disallowedClass); |
| 115 |
return this; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public LayoutInflater build() { |
| 123 |
assertIfAlreadyBuilt(); |
| 124 |
mBuiltInflater = |
| 125 |
LayoutInflater.from(mFromContext).cloneInContext(mTargetContext); |
| 126 |
setFactoryIfNeeded(mBuiltInflater); |
| 127 |
setFilterIfNeeded(mBuiltInflater); |
| 128 |
return mBuiltInflater; |
| 129 |
} |
| 130 |
|
| 131 |
private void assertIfAlreadyBuilt() { |
| 132 |
if (mBuiltInflater != null) { |
| 133 |
throw new AssertionError("Cannot use this Builder after build() has been called."); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private void setFactoryIfNeeded(LayoutInflater inflater) { |
| 138 |
if (mReplaceMap == null) { |
| 139 |
return; |
| 140 |
} |
| 141 |
inflater.setFactory( |
| 142 |
new LayoutInflater.Factory() { |
| 143 |
@Override |
| 144 |
public View onCreateView(String name, Context context, AttributeSet attrs) { |
| 145 |
String replacingClassName = mReplaceMap.get(name); |
| 146 |
if (replacingClassName != null) { |
| 147 |
try { |
| 148 |
return inflater.createView(replacingClassName, null, attrs); |
| 149 |
} catch (ClassNotFoundException e) { |
| 150 |
Log.e(TAG, "Could not replace " + name |
| 151 |
+ " with " + replacingClassName |
| 152 |
+ ", Exception: ", e); |
| 153 |
} |
| 154 |
} |
| 155 |
return null; |
| 156 |
} |
| 157 |
}); |
| 158 |
} |
| 159 |
|
| 160 |
private void setFilterIfNeeded(LayoutInflater inflater) { |
| 161 |
if (mDisallowedClasses == null) { |
| 162 |
return; |
| 163 |
} |
| 164 |
inflater.setFilter( |
| 165 |
new LayoutInflater.Filter() { |
| 166 |
@Override |
| 167 |
public boolean onLoadClass(Class clazz) { |
| 168 |
return !mDisallowedClasses.contains(clazz); |
| 169 |
} |
| 170 |
}); |
| 171 |
} |
| 172 |
} |