| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.preference; |
| 18 |
|
| 19 |
import android.annotation.XmlRes; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.res.XmlResourceParser; |
| 22 |
import android.util.AttributeSet; |
| 23 |
import android.util.Xml; |
| 24 |
import android.view.ContextThemeWrapper; |
| 25 |
import android.view.InflateException; |
| 26 |
import android.view.LayoutInflater; |
| 27 |
|
| 28 |
import org.xmlpull.v1.XmlPullParser; |
| 29 |
import org.xmlpull.v1.XmlPullParserException; |
| 30 |
|
| 31 |
import java.io.IOException; |
| 32 |
import java.lang.reflect.Constructor; |
| 33 |
import java.util.HashMap; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
@Deprecated |
| 52 |
abstract class GenericInflater<T, P extends GenericInflater.Parent> { |
| 53 |
private final boolean DEBUG = false; |
| 54 |
|
| 55 |
protected final Context mContext; |
| 56 |
|
| 57 |
|
| 58 |
private boolean mFactorySet; |
| 59 |
private Factory<T> mFactory; |
| 60 |
|
| 61 |
private final Object[] mConstructorArgs = new Object[2]; |
| 62 |
|
| 63 |
private static final Class[] mConstructorSignature = new Class[] { |
| 64 |
Context.class, AttributeSet.class}; |
| 65 |
|
| 66 |
private static final HashMap sConstructorMap = new HashMap(); |
| 67 |
|
| 68 |
private String mDefaultPackage; |
| 69 |
|
| 70 |
public interface Parent<T> { |
| 71 |
public void addItemFromInflater(T child); |
| 72 |
} |
| 73 |
|
| 74 |
public interface Factory<T> { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public T onCreateItem(String name, Context context, AttributeSet attrs); |
| 90 |
} |
| 91 |
|
| 92 |
private static class FactoryMerger<T> implements Factory<T> { |
| 93 |
private final Factory<T> mF1, mF2; |
| 94 |
|
| 95 |
FactoryMerger(Factory<T> f1, Factory<T> f2) { |
| 96 |
mF1 = f1; |
| 97 |
mF2 = f2; |
| 98 |
} |
| 99 |
|
| 100 |
public T onCreateItem(String name, Context context, AttributeSet attrs) { |
| 101 |
T v = mF1.onCreateItem(name, context, attrs); |
| 102 |
if (v != null) return v; |
| 103 |
return mF2.onCreateItem(name, context, attrs); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
protected GenericInflater(Context context) { |
| 117 |
mContext = context; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
protected GenericInflater(GenericInflater<T,P> original, Context newContext) { |
| 129 |
mContext = newContext; |
| 130 |
mFactory = original.mFactory; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public abstract GenericInflater cloneInContext(Context newContext); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public void setDefaultPackage(String defaultPackage) { |
| 155 |
mDefaultPackage = defaultPackage; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public String getDefaultPackage() { |
| 165 |
return mDefaultPackage; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public Context getContext() { |
| 173 |
return mContext; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public final Factory<T> getFactory() { |
| 182 |
return mFactory; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public void setFactory(Factory<T> factory) { |
| 200 |
if (mFactorySet) { |
| 201 |
throw new IllegalStateException("" + |
| 202 |
"A factory has already been set on this inflater"); |
| 203 |
} |
| 204 |
if (factory == null) { |
| 205 |
throw new NullPointerException("Given factory can not be null"); |
| 206 |
} |
| 207 |
mFactorySet = true; |
| 208 |
if (mFactory == null) { |
| 209 |
mFactory = factory; |
| 210 |
} else { |
| 211 |
mFactory = new FactoryMerger<T>(factory, mFactory); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public T inflate(@XmlRes int resource, P root) { |
| 228 |
return inflate(resource, root, root != null); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public T inflate(XmlPullParser parser, P root) { |
| 248 |
return inflate(parser, root, root != null); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public T inflate(@XmlRes int resource, P root, boolean attachToRoot) { |
| 268 |
if (DEBUG) System.out.println("INFLATING from resource: " + resource); |
| 269 |
XmlResourceParser parser = getContext().getResources().getXml(resource); |
| 270 |
try { |
| 271 |
return inflate(parser, root, attachToRoot); |
| 272 |
} finally { |
| 273 |
parser.close(); |
| 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 |
public T inflate(XmlPullParser parser, P root, |
| 299 |
boolean attachToRoot) { |
| 300 |
synchronized (mConstructorArgs) { |
| 301 |
final AttributeSet attrs = Xml.asAttributeSet(parser); |
| 302 |
mConstructorArgs[0] = mContext; |
| 303 |
T result = (T) root; |
| 304 |
|
| 305 |
try { |
| 306 |
|
| 307 |
int type; |
| 308 |
while ((type = parser.next()) != parser.START_TAG |
| 309 |
&& type != parser.END_DOCUMENT) { |
| 310 |
; |
| 311 |
} |
| 312 |
|
| 313 |
if (type != parser.START_TAG) { |
| 314 |
throw new InflateException(parser.getPositionDescription() |
| 315 |
+ ": No start tag found!"); |
| 316 |
} |
| 317 |
|
| 318 |
if (DEBUG) { |
| 319 |
System.out.println("**************************"); |
| 320 |
System.out.println("Creating root: " |
| 321 |
+ parser.getName()); |
| 322 |
System.out.println("**************************"); |
| 323 |
} |
| 324 |
|
| 325 |
T xmlRoot = createItemFromTag(parser, parser.getName(), |
| 326 |
attrs); |
| 327 |
|
| 328 |
result = (T) onMergeRoots(root, attachToRoot, (P) xmlRoot); |
| 329 |
|
| 330 |
if (DEBUG) { |
| 331 |
System.out.println("-----> start inflating children"); |
| 332 |
} |
| 333 |
|
| 334 |
rInflate(parser, result, attrs); |
| 335 |
if (DEBUG) { |
| 336 |
System.out.println("-----> done inflating children"); |
| 337 |
} |
| 338 |
|
| 339 |
} catch (InflateException e) { |
| 340 |
throw e; |
| 341 |
|
| 342 |
} catch (XmlPullParserException e) { |
| 343 |
InflateException ex = new InflateException(e.getMessage()); |
| 344 |
ex.initCause(e); |
| 345 |
throw ex; |
| 346 |
} catch (IOException e) { |
| 347 |
InflateException ex = new InflateException( |
| 348 |
parser.getPositionDescription() |
| 349 |
+ ": " + e.getMessage()); |
| 350 |
ex.initCause(e); |
| 351 |
throw ex; |
| 352 |
} |
| 353 |
|
| 354 |
return result; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public final T createItem(String name, String prefix, AttributeSet attrs) |
| 376 |
throws ClassNotFoundException, InflateException { |
| 377 |
Constructor constructor = (Constructor) sConstructorMap.get(name); |
| 378 |
|
| 379 |
try { |
| 380 |
if (null == constructor) { |
| 381 |
|
| 382 |
|
| 383 |
Class clazz = mContext.getClassLoader().loadClass( |
| 384 |
prefix != null ? (prefix + name) : name); |
| 385 |
constructor = clazz.getConstructor(mConstructorSignature); |
| 386 |
constructor.setAccessible(true); |
| 387 |
sConstructorMap.put(name, constructor); |
| 388 |
} |
| 389 |
|
| 390 |
Object[] args = mConstructorArgs; |
| 391 |
args[1] = attrs; |
| 392 |
return (T) constructor.newInstance(args); |
| 393 |
|
| 394 |
} catch (NoSuchMethodException e) { |
| 395 |
InflateException ie = new InflateException(attrs |
| 396 |
.getPositionDescription() |
| 397 |
+ ": Error inflating class " |
| 398 |
+ (prefix != null ? (prefix + name) : name)); |
| 399 |
ie.initCause(e); |
| 400 |
throw ie; |
| 401 |
|
| 402 |
} catch (ClassNotFoundException e) { |
| 403 |
|
| 404 |
throw e; |
| 405 |
} catch (Exception e) { |
| 406 |
InflateException ie = new InflateException(attrs |
| 407 |
.getPositionDescription() |
| 408 |
+ ": Error inflating class " |
| 409 |
+ constructor.getDeclaringClass().getName()); |
| 410 |
ie.initCause(e); |
| 411 |
throw ie; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
protected T onCreateItem(String name, AttributeSet attrs) throws ClassNotFoundException { |
| 426 |
return createItem(name, mDefaultPackage, attrs); |
| 427 |
} |
| 428 |
|
| 429 |
private final T createItemFromTag(XmlPullParser parser, String name, AttributeSet attrs) { |
| 430 |
if (DEBUG) System.out.println("******** Creating item: " + name); |
| 431 |
|
| 432 |
try { |
| 433 |
T item = (mFactory == null) ? null : mFactory.onCreateItem(name, mContext, attrs); |
| 434 |
|
| 435 |
if (item == null) { |
| 436 |
if (-1 == name.indexOf('.')) { |
| 437 |
item = onCreateItem(name, attrs); |
| 438 |
} else { |
| 439 |
item = createItem(name, null, attrs); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
if (DEBUG) System.out.println("Created item is: " + item); |
| 444 |
return item; |
| 445 |
|
| 446 |
} catch (InflateException e) { |
| 447 |
throw e; |
| 448 |
|
| 449 |
} catch (ClassNotFoundException e) { |
| 450 |
InflateException ie = new InflateException(attrs |
| 451 |
.getPositionDescription() |
| 452 |
+ ": Error inflating class " + name); |
| 453 |
ie.initCause(e); |
| 454 |
throw ie; |
| 455 |
|
| 456 |
} catch (Exception e) { |
| 457 |
InflateException ie = new InflateException(attrs |
| 458 |
.getPositionDescription() |
| 459 |
+ ": Error inflating class " + name); |
| 460 |
ie.initCause(e); |
| 461 |
throw ie; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
private void rInflate(XmlPullParser parser, T parent, final AttributeSet attrs) |
| 470 |
throws XmlPullParserException, IOException { |
| 471 |
final int depth = parser.getDepth(); |
| 472 |
|
| 473 |
int type; |
| 474 |
while (((type = parser.next()) != parser.END_TAG || |
| 475 |
parser.getDepth() > depth) && type != parser.END_DOCUMENT) { |
| 476 |
|
| 477 |
if (type != parser.START_TAG) { |
| 478 |
continue; |
| 479 |
} |
| 480 |
|
| 481 |
if (onCreateCustomFromTag(parser, parent, attrs)) { |
| 482 |
continue; |
| 483 |
} |
| 484 |
|
| 485 |
if (DEBUG) { |
| 486 |
System.out.println("Now inflating tag: " + parser.getName()); |
| 487 |
} |
| 488 |
String name = parser.getName(); |
| 489 |
|
| 490 |
T item = createItemFromTag(parser, name, attrs); |
| 491 |
|
| 492 |
if (DEBUG) { |
| 493 |
System.out |
| 494 |
.println("Creating params from parent: " + parent); |
| 495 |
} |
| 496 |
|
| 497 |
((P) parent).addItemFromInflater(item); |
| 498 |
|
| 499 |
if (DEBUG) { |
| 500 |
System.out.println("-----> start inflating children"); |
| 501 |
} |
| 502 |
rInflate(parser, item, attrs); |
| 503 |
if (DEBUG) { |
| 504 |
System.out.println("-----> done inflating children"); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
protected boolean onCreateCustomFromTag(XmlPullParser parser, T parent, |
| 522 |
final AttributeSet attrs) throws XmlPullParserException { |
| 523 |
return false; |
| 524 |
} |
| 525 |
|
| 526 |
protected P onMergeRoots(P givenRoot, boolean attachToGivenRoot, P xmlRoot) { |
| 527 |
return xmlRoot; |
| 528 |
} |
| 529 |
} |