-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLayoutLoader.java
More file actions
291 lines (275 loc) · 9.55 KB
/
LayoutLoader.java
File metadata and controls
291 lines (275 loc) · 9.55 KB
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
/*
* Copyright 2014 Yahoo Inc.
*
* Licensed under the terms of the Apache License, Version 2.
* Please see LICENSE.txt in the project root for terms.
*/
package com.yahoo.android.dlayout;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* <code>LayoutLoader</code> helps you load compiled layouts from any source.
*
* <p>
* The layout can be loaded from either an {@link InputStream} or raw bytes.
* </p>
*
* <p>
* A typical application using {@link LayoutLoader} will have code similar to that given below:
* </p>
*
* <pre>
* class MyApplication extends Application {
* private LayoutLoader layoutLoader;
* private LayoutCache layoutCache;
*
* protected void onCreate() {
* layoutLoader = new LayoutLoader().initialize();
* layoutCache = new LayoutCache();
* }
*
* public LayoutLoader getLayoutLoader() {
* return layoutLoader;
* }
*
* public LayoutCache getLayoutCache() {
* return layoutCache;
* }
* }
*
* class LayoutCache {
* private LruCache<String, byte[]> cache = new LruCache<>();
* private Map<String, Boolean> fetchedURLs = new HashMap<>();
*
* public void load(String url) {
* //load from network
* //validate signature
* //add to cache
* if(data != null) {
* cache.put(ur, data);
* }
* fetchedURLs.put(url, data != null);
* }
*
* public boolean shouldFetch(String url) {
* return !fetchedURLs.containsKey(url);
* }
*
* public boolean hasLayout(String url) {
* Boolean isDataValid = fetchedURLs.get(url);
* if(isDataValid == null) {
* return false;
* }
* return isDataValid;
* }
*
* public byte[] get(String url) {
* return cache.get(url);
* }
* }
*
* class SomeActivity extends Activity {
* protected void onCreate(Bundle savedInstanceState) {
* MyApplication app = (MyApplication) getApplicationContext();
* LayoutCache cache = app.getLayoutCache();
* String url = getFromSomeConfig(this); //load app config, a/b test config etc
*
* View root = null;
* if(cache.shouldFetch(url)) {
* cache.load(url);
* } else if(cache.hasLayout(url) {
* root = app.getLayoutLoader().load(cache.get(url), this, null, false);
* }
*
* //If the layout was loaded dynamically, great!
* // else, use the bundled layout.
* if(root != null) {
* setContentView(root);
* } else {
* setContentView(R.layout.activity_some);
* }
* }
* }
*
* </pre>
*
* @author Gaurav Vaish
* @since v1.0
* @see #load(InputStream, Context, ViewGroup, boolean)
* @see #load(byte[], Context, ViewGroup, boolean)
*/
public class LayoutLoader {
public static final String TAG = "LayoutLoader";
private Constructor<?> xmlBlockCtor;
private Method newParserMethod;
private boolean ready;
/**
* Initializes the loader.
*
* <p>
* This method must be called before {@link #load(byte[], Context, ViewGroup, boolean)}
* or {@link #load(InputStream, Context, ViewGroup, boolean)} can be used.
* </p>
*
* <p>
* The method is reentrant (can be called multiple times) and thread-safe.
* </p>
*
* <p>
* This method has been provided for lazy initialization.
* </p>
*
* @return {@link LayoutLoader} instance for chaining.
*/
public LayoutLoader initialize() {
if(!ready) {
synchronized(this) {
if(!ready) {
initializeImpl();
ready = true;
}
}
}
return this;
}
/**
* Clears up the internal state of the loader.
*
* <p>
* After the cleanup, the loader must be {@link #initialize()}d again before reuse.
* </p>
*
* <p>
* The method is reentrant (can be called multiple times) and thread-safe.
* </p>
*
* @return {@link LayoutLoader} instance for chaining.
*/
public LayoutLoader cleanup() {
if(ready) {
synchronized(this) {
if(ready) {
xmlBlockCtor = null;
newParserMethod = null;
ready = false;
}
}
}
return this;
}
protected void initializeImpl() {
try {
Class<?> cls = Class.forName("android.content.res.XmlBlock");
xmlBlockCtor = cls.getDeclaredConstructor(byte[].class);
xmlBlockCtor.setAccessible(true);
newParserMethod = cls.getDeclaredMethod("newParser");
newParserMethod.setAccessible(true);
} catch(RuntimeException e) {
Log.e(TAG, "Failed initializing loader", e);
} catch(Exception e) {
Log.e(TAG, "Failed initializing loader", e);
}
}
/**
* Loads the template from the given {@link InputStream}.
*
* <p>
* It returns the {@link View} if it can be inflated, <code>null</code> otherwise.
* </p>
*
* <p>
* It internally uses {@link LayoutInflater#inflate(int, ViewGroup, boolean)}.
* All semantics of the corresponding method apply.
* </p>
*
* @param input {@link InputStream} from where the contents of the compiled layout can be read
* @param context {@link Context} to load the resources from
* @param root Optional view to be the parent of the generated hierarchy (if
* <em>attachToRoot</em> is true), or else simply an object that
* provides a set of LayoutParams values for root of the returned
* hierarchy (if <em>attachToRoot</em> is false.)
* @param attachToRoot Whether the inflated hierarchy should be attached to
* the root parameter? If <code>false</code>, root is only used to create the
* correct subclass of LayoutParams for the root view in the XML.
* @return The root View of the inflated hierarchy. If root was supplied and
* attachToRoot is true, this is root; otherwise it is the root of
* the inflated XML file.
* @see #load(byte[], Context, ViewGroup, boolean)
*/
public View load(InputStream input, Context context, ViewGroup root, boolean attachToRoot) {
View rv = null;
if(ready && xmlBlockCtor != null && newParserMethod != null) {
byte[] data = readAll(input);
rv = load(data, context, root, attachToRoot);
}
return rv;
}
/**
* Loads the template from the given {@link InputStream}.
*
* <p>
* It returns the {@link View} if it can be inflated, <code>null</code> otherwise.
* </p>
*
* <p>
* It internally uses {@link LayoutInflater#inflate(int, ViewGroup, boolean)}.
* All semantics of the corresponding method apply.
* </p>
*
* @param data Raw data of the compiled layout.
* @param context {@link Context} to load the resources from
* @param root Optional view to be the parent of the generated hierarchy (if
* <em>attachToRoot</em> is true), or else simply an object that
* provides a set of LayoutParams values for root of the returned
* hierarchy (if <em>attachToRoot</em> is false.)
* @param attachToRoot Whether the inflated hierarchy should be attached to
* the root parameter? If <code>false</code>, root is only used to create the
* correct subclass of LayoutParams for the root view in the XML.
* @return The root View of the inflated hierarchy. If root was supplied and
* attachToRoot is true, this is root; otherwise it is the root of
* the inflated XML file.
* @see #load(InputStream, Context, ViewGroup, boolean)
*/
public View load(byte[] data, Context context, ViewGroup root, boolean attachToRoot) {
View rv = null;
if(ready && xmlBlockCtor != null && newParserMethod != null && data != null) {
try {
Object xmlBlock = xmlBlockCtor.newInstance(data);
XmlResourceParser parser = (XmlResourceParser) newParserMethod.invoke(xmlBlock);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rv = inflater.inflate(parser, root, attachToRoot);
} catch(RuntimeException e) {
Log.e(TAG, "Failed loading layout", e);
} catch(Exception e) {
Log.e(TAG, "Failed loading layout", e);
}
}
return rv;
}
private byte[] readAll(InputStream input) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read;
byte[] rv = null;
try {
read = input.read(buffer, 0, buffer.length);
while(read >= 0) {
baos.write(buffer, 0, read);
read = input.read(buffer, 0, buffer.length);
}
rv = baos.toByteArray();
} catch(IOException e) {
Log.e(TAG, "Failed reading layout content", e);
}
return rv;
}
}