| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.annotation.LayoutRes; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.SystemService; |
| 23 |
import android.annotation.TestApi; |
| 24 |
import android.annotation.UiContext; |
| 25 |
import android.compat.annotation.UnsupportedAppUsage; |
| 26 |
import android.content.Context; |
| 27 |
import android.content.pm.ApplicationInfo; |
| 28 |
import android.content.res.Resources; |
| 29 |
import android.content.res.TypedArray; |
| 30 |
import android.content.res.XmlResourceParser; |
| 31 |
import android.graphics.Canvas; |
| 32 |
import android.os.Build; |
| 33 |
import android.os.Handler; |
| 34 |
import android.os.Message; |
| 35 |
import android.os.StrictMode; |
| 36 |
import android.os.Trace; |
| 37 |
import android.util.AttributeSet; |
| 38 |
import android.util.Log; |
| 39 |
import android.util.TypedValue; |
| 40 |
import android.util.Xml; |
| 41 |
import android.widget.FrameLayout; |
| 42 |
|
| 43 |
import com.android.internal.R; |
| 44 |
|
| 45 |
import dalvik.system.PathClassLoader; |
| 46 |
|
| 47 |
import org.xmlpull.v1.XmlPullParser; |
| 48 |
import org.xmlpull.v1.XmlPullParserException; |
| 49 |
|
| 50 |
import java.io.File; |
| 51 |
import java.io.IOException; |
| 52 |
import java.lang.reflect.Constructor; |
| 53 |
import java.lang.reflect.Method; |
| 54 |
import java.util.HashMap; |
| 55 |
import java.util.Objects; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
@SystemService(Context.LAYOUT_INFLATER_SERVICE) |
| 80 |
public abstract class LayoutInflater { |
| 81 |
|
| 82 |
private static final String TAG = LayoutInflater.class.getSimpleName(); |
| 83 |
private static final boolean DEBUG = false; |
| 84 |
|
| 85 |
private static final String COMPILED_VIEW_DEX_FILE_NAME = "/compiled_view.dex"; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private static final String USE_PRECOMPILED_LAYOUT = "view.precompiled_layout_enabled"; |
| 90 |
|
| 91 |
|
| 92 |
private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0]; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 100 |
@UiContext |
| 101 |
protected final Context mContext; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 110 |
private boolean mFactorySet; |
| 111 |
@UnsupportedAppUsage |
| 112 |
private Factory mFactory; |
| 113 |
@UnsupportedAppUsage |
| 114 |
private Factory2 mFactory2; |
| 115 |
@UnsupportedAppUsage |
| 116 |
private Factory2 mPrivateFactory; |
| 117 |
private Filter mFilter; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
private boolean mUseCompiledView; |
| 122 |
|
| 123 |
|
| 124 |
private ClassLoader mPrecompiledClassLoader; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 132 |
final Object[] mConstructorArgs = new Object[2]; |
| 133 |
|
| 134 |
@UnsupportedAppUsage |
| 135 |
static final Class<?>[] mConstructorSignature = new Class[] { |
| 136 |
Context.class, AttributeSet.class}; |
| 137 |
|
| 138 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 123769490) |
| 139 |
private static final HashMap<String, Constructor<? extends View>> sConstructorMap = |
| 140 |
new HashMap<String, Constructor<? extends View>>(); |
| 141 |
|
| 142 |
private HashMap<String, Boolean> mFilterMap; |
| 143 |
|
| 144 |
private TypedValue mTempValue; |
| 145 |
|
| 146 |
private static final String TAG_MERGE = "merge"; |
| 147 |
private static final String TAG_INCLUDE = "include"; |
| 148 |
private static final String TAG_1995 = "blink"; |
| 149 |
private static final String TAG_REQUEST_FOCUS = "requestFocus"; |
| 150 |
private static final String TAG_TAG = "tag"; |
| 151 |
|
| 152 |
private static final String ATTR_LAYOUT = "layout"; |
| 153 |
|
| 154 |
@UnsupportedAppUsage |
| 155 |
private static final int[] ATTRS_THEME = new int[] { |
| 156 |
com.android.internal.R.attr.theme }; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public interface Filter { |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
@SuppressWarnings("unchecked") |
| 173 |
boolean onLoadClass(Class clazz); |
| 174 |
} |
| 175 |
|
| 176 |
public interface Factory { |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
@Nullable |
| 195 |
View onCreateView(@NonNull String name, @NonNull Context context, |
| 196 |
@NonNull AttributeSet attrs); |
| 197 |
} |
| 198 |
|
| 199 |
public interface Factory2 extends Factory { |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
@Nullable |
| 215 |
View onCreateView(@Nullable View parent, @NonNull String name, |
| 216 |
@NonNull Context context, @NonNull AttributeSet attrs); |
| 217 |
} |
| 218 |
|
| 219 |
private static class FactoryMerger implements Factory2 { |
| 220 |
private final Factory mF1, mF2; |
| 221 |
private final Factory2 mF12, mF22; |
| 222 |
|
| 223 |
FactoryMerger(Factory f1, Factory2 f12, Factory f2, Factory2 f22) { |
| 224 |
mF1 = f1; |
| 225 |
mF2 = f2; |
| 226 |
mF12 = f12; |
| 227 |
mF22 = f22; |
| 228 |
} |
| 229 |
|
| 230 |
@Nullable |
| 231 |
public View onCreateView(@NonNull String name, @NonNull Context context, |
| 232 |
@NonNull AttributeSet attrs) { |
| 233 |
View v = mF1.onCreateView(name, context, attrs); |
| 234 |
if (v != null) return v; |
| 235 |
return mF2.onCreateView(name, context, attrs); |
| 236 |
} |
| 237 |
|
| 238 |
@Nullable |
| 239 |
public View onCreateView(@Nullable View parent, @NonNull String name, |
| 240 |
@NonNull Context context, @NonNull AttributeSet attrs) { |
| 241 |
View v = mF12 != null ? mF12.onCreateView(parent, name, context, attrs) |
| 242 |
: mF1.onCreateView(name, context, attrs); |
| 243 |
if (v != null) return v; |
| 244 |
return mF22 != null ? mF22.onCreateView(parent, name, context, attrs) |
| 245 |
: mF2.onCreateView(name, context, attrs); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
protected LayoutInflater(Context context) { |
| 260 |
StrictMode.assertConfigurationContext(context, "LayoutInflater"); |
| 261 |
mContext = context; |
| 262 |
initPrecompiledViews(); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
protected LayoutInflater(LayoutInflater original, Context newContext) { |
| 274 |
StrictMode.assertConfigurationContext(newContext, "LayoutInflater"); |
| 275 |
mContext = newContext; |
| 276 |
mFactory = original.mFactory; |
| 277 |
mFactory2 = original.mFactory2; |
| 278 |
mPrivateFactory = original.mPrivateFactory; |
| 279 |
setFilter(original.mFilter); |
| 280 |
initPrecompiledViews(); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public static LayoutInflater from(@UiContext Context context) { |
| 287 |
LayoutInflater LayoutInflater = |
| 288 |
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 289 |
if (LayoutInflater == null) { |
| 290 |
throw new AssertionError("LayoutInflater not found."); |
| 291 |
} |
| 292 |
return LayoutInflater; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public abstract LayoutInflater cloneInContext(Context newContext); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public Context getContext() { |
| 314 |
return mContext; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public final Factory getFactory() { |
| 323 |
return mFactory; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public final Factory2 getFactory2() { |
| 334 |
return mFactory2; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public void setFactory(Factory factory) { |
| 353 |
if (mFactorySet) { |
| 354 |
throw new IllegalStateException("A factory has already been set on this LayoutInflater"); |
| 355 |
} |
| 356 |
if (factory == null) { |
| 357 |
throw new NullPointerException("Given factory can not be null"); |
| 358 |
} |
| 359 |
mFactorySet = true; |
| 360 |
if (mFactory == null) { |
| 361 |
mFactory = factory; |
| 362 |
} else { |
| 363 |
mFactory = new FactoryMerger(factory, null, mFactory, mFactory2); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public void setFactory2(Factory2 factory) { |
| 372 |
if (mFactorySet) { |
| 373 |
throw new IllegalStateException("A factory has already been set on this LayoutInflater"); |
| 374 |
} |
| 375 |
if (factory == null) { |
| 376 |
throw new NullPointerException("Given factory can not be null"); |
| 377 |
} |
| 378 |
mFactorySet = true; |
| 379 |
if (mFactory == null) { |
| 380 |
mFactory = mFactory2 = factory; |
| 381 |
} else { |
| 382 |
mFactory = mFactory2 = new FactoryMerger(factory, factory, mFactory, mFactory2); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
@UnsupportedAppUsage |
| 390 |
public void setPrivateFactory(Factory2 factory) { |
| 391 |
if (mPrivateFactory == null) { |
| 392 |
mPrivateFactory = factory; |
| 393 |
} else { |
| 394 |
mPrivateFactory = new FactoryMerger(factory, factory, mPrivateFactory, mPrivateFactory); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public Filter getFilter() { |
| 403 |
return mFilter; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public void setFilter(Filter filter) { |
| 416 |
mFilter = filter; |
| 417 |
if (filter != null) { |
| 418 |
mFilterMap = new HashMap<String, Boolean>(); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
private void initPrecompiledViews() { |
| 423 |
|
| 424 |
boolean enabled = false; |
| 425 |
initPrecompiledViews(enabled); |
| 426 |
} |
| 427 |
|
| 428 |
private void initPrecompiledViews(boolean enablePrecompiledViews) { |
| 429 |
mUseCompiledView = enablePrecompiledViews; |
| 430 |
|
| 431 |
if (!mUseCompiledView) { |
| 432 |
mPrecompiledClassLoader = null; |
| 433 |
return; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
ApplicationInfo appInfo = mContext.getApplicationInfo(); |
| 438 |
if (appInfo.isEmbeddedDexUsed() || appInfo.isPrivilegedApp()) { |
| 439 |
mUseCompiledView = false; |
| 440 |
return; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
try { |
| 445 |
mPrecompiledClassLoader = mContext.getClassLoader(); |
| 446 |
String dexFile = mContext.getCodeCacheDir() + COMPILED_VIEW_DEX_FILE_NAME; |
| 447 |
if (new File(dexFile).exists()) { |
| 448 |
mPrecompiledClassLoader = new PathClassLoader(dexFile, mPrecompiledClassLoader); |
| 449 |
} else { |
| 450 |
|
| 451 |
|
| 452 |
mUseCompiledView = false; |
| 453 |
} |
| 454 |
} catch (Throwable e) { |
| 455 |
if (DEBUG) { |
| 456 |
Log.e(TAG, "Failed to initialized precompiled views layouts", e); |
| 457 |
} |
| 458 |
mUseCompiledView = false; |
| 459 |
} |
| 460 |
if (!mUseCompiledView) { |
| 461 |
mPrecompiledClassLoader = null; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
@TestApi |
| 469 |
public void setPrecompiledLayoutsEnabledForTesting(boolean enablePrecompiledLayouts) { |
| 470 |
initPrecompiledViews(enablePrecompiledLayouts); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) { |
| 485 |
return inflate(resource, root, root != null); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
public View inflate(XmlPullParser parser, @Nullable ViewGroup root) { |
| 505 |
return inflate(parser, root, root != null); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) { |
| 526 |
final Resources res = getContext().getResources(); |
| 527 |
if (DEBUG) { |
| 528 |
Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" (" |
| 529 |
+ Integer.toHexString(resource) + ")"); |
| 530 |
} |
| 531 |
|
| 532 |
View view = tryInflatePrecompiled(resource, res, root, attachToRoot); |
| 533 |
if (view != null) { |
| 534 |
return view; |
| 535 |
} |
| 536 |
XmlResourceParser parser = res.getLayout(resource); |
| 537 |
try { |
| 538 |
return inflate(parser, root, attachToRoot); |
| 539 |
} finally { |
| 540 |
parser.close(); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
private @Nullable |
| 545 |
View tryInflatePrecompiled(@LayoutRes int resource, Resources res, @Nullable ViewGroup root, |
| 546 |
boolean attachToRoot) { |
| 547 |
if (!mUseCompiledView) { |
| 548 |
return null; |
| 549 |
} |
| 550 |
|
| 551 |
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate (precompiled)"); |
| 552 |
|
| 553 |
|
| 554 |
String pkg = res.getResourcePackageName(resource); |
| 555 |
String layout = res.getResourceEntryName(resource); |
| 556 |
|
| 557 |
try { |
| 558 |
Class clazz = Class.forName("" + pkg + ".CompiledView", false, mPrecompiledClassLoader); |
| 559 |
Method inflater = clazz.getMethod(layout, Context.class, int.class); |
| 560 |
View view = (View) inflater.invoke(null, mContext, resource); |
| 561 |
|
| 562 |
if (view != null && root != null) { |
| 563 |
|
| 564 |
|
| 565 |
XmlResourceParser parser = res.getLayout(resource); |
| 566 |
try { |
| 567 |
AttributeSet attrs = Xml.asAttributeSet(parser); |
| 568 |
advanceToRootNode(parser); |
| 569 |
ViewGroup.LayoutParams params = root.generateLayoutParams(attrs); |
| 570 |
|
| 571 |
if (attachToRoot) { |
| 572 |
root.addView(view, params); |
| 573 |
} else { |
| 574 |
view.setLayoutParams(params); |
| 575 |
} |
| 576 |
} finally { |
| 577 |
parser.close(); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
return view; |
| 582 |
} catch (Throwable e) { |
| 583 |
if (DEBUG) { |
| 584 |
Log.e(TAG, "Failed to use precompiled view", e); |
| 585 |
} |
| 586 |
} finally { |
| 587 |
Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 588 |
} |
| 589 |
return null; |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
private void advanceToRootNode(XmlPullParser parser) |
| 597 |
throws InflateException, IOException, XmlPullParserException { |
| 598 |
|
| 599 |
int type; |
| 600 |
while ((type = parser.next()) != XmlPullParser.START_TAG && |
| 601 |
type != XmlPullParser.END_DOCUMENT) { |
| 602 |
|
| 603 |
} |
| 604 |
|
| 605 |
if (type != XmlPullParser.START_TAG) { |
| 606 |
throw new InflateException(parser.getPositionDescription() |
| 607 |
+ ": No start tag found!"); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { |
| 634 |
synchronized (mConstructorArgs) { |
| 635 |
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate"); |
| 636 |
|
| 637 |
final Context inflaterContext = mContext; |
| 638 |
final AttributeSet attrs = Xml.asAttributeSet(parser); |
| 639 |
Context lastContext = (Context) mConstructorArgs[0]; |
| 640 |
mConstructorArgs[0] = inflaterContext; |
| 641 |
View result = root; |
| 642 |
|
| 643 |
try { |
| 644 |
advanceToRootNode(parser); |
| 645 |
final String name = parser.getName(); |
| 646 |
|
| 647 |
if (DEBUG) { |
| 648 |
System.out.println("**************************"); |
| 649 |
System.out.println("Creating root view: " |
| 650 |
+ name); |
| 651 |
System.out.println("**************************"); |
| 652 |
} |
| 653 |
|
| 654 |
if (TAG_MERGE.equals(name)) { |
| 655 |
if (root == null || !attachToRoot) { |
| 656 |
throw new InflateException("<merge /> can be used only with a valid " |
| 657 |
+ "ViewGroup root and attachToRoot=true"); |
| 658 |
} |
| 659 |
|
| 660 |
rInflate(parser, root, inflaterContext, attrs, false); |
| 661 |
} else { |
| 662 |
|
| 663 |
final View temp = createViewFromTag(root, name, inflaterContext, attrs); |
| 664 |
|
| 665 |
ViewGroup.LayoutParams params = null; |
| 666 |
|
| 667 |
if (root != null) { |
| 668 |
if (DEBUG) { |
| 669 |
System.out.println("Creating params from root: " + |
| 670 |
root); |
| 671 |
} |
| 672 |
|
| 673 |
params = root.generateLayoutParams(attrs); |
| 674 |
if (!attachToRoot) { |
| 675 |
|
| 676 |
|
| 677 |
temp.setLayoutParams(params); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
if (DEBUG) { |
| 682 |
System.out.println("-----> start inflating children"); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
rInflateChildren(parser, temp, attrs, true); |
| 687 |
|
| 688 |
if (DEBUG) { |
| 689 |
System.out.println("-----> done inflating children"); |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
if (root != null && attachToRoot) { |
| 695 |
root.addView(temp, params); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
if (root == null || !attachToRoot) { |
| 701 |
result = temp; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
} catch (XmlPullParserException e) { |
| 706 |
final InflateException ie = new InflateException(e.getMessage(), e); |
| 707 |
ie.setStackTrace(EMPTY_STACK_TRACE); |
| 708 |
throw ie; |
| 709 |
} catch (Exception e) { |
| 710 |
final InflateException ie = new InflateException( |
| 711 |
getParserStateDescription(inflaterContext, attrs) |
| 712 |
+ ": " + e.getMessage(), e); |
| 713 |
ie.setStackTrace(EMPTY_STACK_TRACE); |
| 714 |
throw ie; |
| 715 |
} finally { |
| 716 |
|
| 717 |
mConstructorArgs[0] = lastContext; |
| 718 |
mConstructorArgs[1] = null; |
| 719 |
|
| 720 |
Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 721 |
} |
| 722 |
|
| 723 |
return result; |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
private static String getParserStateDescription(Context context, AttributeSet attrs) { |
| 728 |
int sourceResId = Resources.getAttributeSetSourceResId(attrs); |
| 729 |
if (sourceResId == Resources.ID_NULL) { |
| 730 |
return attrs.getPositionDescription(); |
| 731 |
} else { |
| 732 |
return attrs.getPositionDescription() + " in " |
| 733 |
+ context.getResources().getResourceName(sourceResId); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
private static final ClassLoader BOOT_CLASS_LOADER = LayoutInflater.class.getClassLoader(); |
| 738 |
|
| 739 |
private final boolean verifyClassLoader(Constructor<? extends View> constructor) { |
| 740 |
final ClassLoader constructorLoader = constructor.getDeclaringClass().getClassLoader(); |
| 741 |
if (constructorLoader == BOOT_CLASS_LOADER) { |
| 742 |
|
| 743 |
return true; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
ClassLoader cl = mContext.getClassLoader(); |
| 748 |
do { |
| 749 |
if (constructorLoader == cl) { |
| 750 |
return true; |
| 751 |
} |
| 752 |
cl = cl.getParent(); |
| 753 |
} while (cl != null); |
| 754 |
return false; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
public final View createView(String name, String prefix, AttributeSet attrs) |
| 775 |
throws ClassNotFoundException, InflateException { |
| 776 |
Context context = (Context) mConstructorArgs[0]; |
| 777 |
if (context == null) { |
| 778 |
context = mContext; |
| 779 |
} |
| 780 |
return createView(context, name, prefix, attrs); |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
@Nullable |
| 802 |
public final View createView(@NonNull Context viewContext, @NonNull String name, |
| 803 |
@Nullable String prefix, @Nullable AttributeSet attrs) |
| 804 |
throws ClassNotFoundException, InflateException { |
| 805 |
Objects.requireNonNull(viewContext); |
| 806 |
Objects.requireNonNull(name); |
| 807 |
Constructor<? extends View> constructor = sConstructorMap.get(name); |
| 808 |
if (constructor != null && !verifyClassLoader(constructor)) { |
| 809 |
constructor = null; |
| 810 |
sConstructorMap.remove(name); |
| 811 |
} |
| 812 |
Class<? extends View> clazz = null; |
| 813 |
|
| 814 |
try { |
| 815 |
Trace.traceBegin(Trace.TRACE_TAG_VIEW, name); |
| 816 |
|
| 817 |
if (constructor == null) { |
| 818 |
|
| 819 |
clazz = Class.forName(prefix != null ? (prefix + name) : name, false, |
| 820 |
mContext.getClassLoader()).asSubclass(View.class); |
| 821 |
|
| 822 |
if (mFilter != null && clazz != null) { |
| 823 |
boolean allowed = mFilter.onLoadClass(clazz); |
| 824 |
if (!allowed) { |
| 825 |
failNotAllowed(name, prefix, viewContext, attrs); |
| 826 |
} |
| 827 |
} |
| 828 |
constructor = clazz.getConstructor(mConstructorSignature); |
| 829 |
constructor.setAccessible(true); |
| 830 |
sConstructorMap.put(name, constructor); |
| 831 |
} else { |
| 832 |
|
| 833 |
if (mFilter != null) { |
| 834 |
|
| 835 |
Boolean allowedState = mFilterMap.get(name); |
| 836 |
if (allowedState == null) { |
| 837 |
|
| 838 |
clazz = Class.forName(prefix != null ? (prefix + name) : name, false, |
| 839 |
mContext.getClassLoader()).asSubclass(View.class); |
| 840 |
|
| 841 |
boolean allowed = clazz != null && mFilter.onLoadClass(clazz); |
| 842 |
mFilterMap.put(name, allowed); |
| 843 |
if (!allowed) { |
| 844 |
failNotAllowed(name, prefix, viewContext, attrs); |
| 845 |
} |
| 846 |
} else if (allowedState.equals(Boolean.FALSE)) { |
| 847 |
failNotAllowed(name, prefix, viewContext, attrs); |
| 848 |
} |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
Object lastContext = mConstructorArgs[0]; |
| 853 |
mConstructorArgs[0] = viewContext; |
| 854 |
Object[] args = mConstructorArgs; |
| 855 |
args[1] = attrs; |
| 856 |
|
| 857 |
try { |
| 858 |
final View view = constructor.newInstance(args); |
| 859 |
if (view instanceof ViewStub) { |
| 860 |
|
| 861 |
final ViewStub viewStub = (ViewStub) view; |
| 862 |
viewStub.setLayoutInflater(cloneInContext((Context) args[0])); |
| 863 |
} |
| 864 |
return view; |
| 865 |
} finally { |
| 866 |
mConstructorArgs[0] = lastContext; |
| 867 |
} |
| 868 |
} catch (NoSuchMethodException e) { |
| 869 |
final InflateException ie = new InflateException( |
| 870 |
getParserStateDescription(viewContext, attrs) |
| 871 |
+ ": Error inflating class " + (prefix != null ? (prefix + name) : name), e); |
| 872 |
ie.setStackTrace(EMPTY_STACK_TRACE); |
| 873 |
throw ie; |
| 874 |
|
| 875 |
} catch (ClassCastException e) { |
| 876 |
|
| 877 |
final InflateException ie = new InflateException( |
| 878 |
getParserStateDescription(viewContext, attrs) |
| 879 |
+ ": Class is not a View " + (prefix != null ? (prefix + name) : name), e); |
| 880 |
ie.setStackTrace(EMPTY_STACK_TRACE); |
| 881 |
throw ie; |
| 882 |
} catch (ClassNotFoundException e) { |
| 883 |
|
| 884 |
throw e; |
| 885 |
} catch (Exception e) { |
| 886 |
final InflateException ie = new InflateException( |
| 887 |
getParserStateDescription(viewContext, attrs) + ": Error inflating class " |
| 888 |
+ (clazz == null ? "<unknown>" : clazz.getName()), e); |
| 889 |
ie.setStackTrace(EMPTY_STACK_TRACE); |
| 890 |
throw ie; |
| 891 |
} finally { |
| 892 |
Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
private void failNotAllowed(String name, String prefix, Context context, AttributeSet attrs) { |
| 900 |
throw new InflateException(getParserStateDescription(context, attrs) |
| 901 |
+ ": Class not allowed to be inflated "+ (prefix != null ? (prefix + name) : name)); |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
protected View onCreateView(String name, AttributeSet attrs) |
| 916 |
throws ClassNotFoundException { |
| 917 |
return createView(name, "android.view.", attrs); |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
protected View onCreateView(View parent, String name, AttributeSet attrs) |
| 933 |
throws ClassNotFoundException { |
| 934 |
return onCreateView(name, attrs); |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
@Nullable |
| 951 |
public View onCreateView(@NonNull Context viewContext, @Nullable View parent, |
| 952 |
@NonNull String name, @Nullable AttributeSet attrs) |
| 953 |
throws ClassNotFoundException { |
| 954 |
return onCreateView(parent, name, attrs); |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 964 |
private View createViewFromTag(View parent, String name, Context context, AttributeSet attrs) { |
| 965 |
return createViewFromTag(parent, name, context, attrs, false); |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 984 |
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs, |
| 985 |
boolean ignoreThemeAttr) { |
| 986 |
if (name.equals("view")) { |
| 987 |
name = attrs.getAttributeValue(null, "class"); |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
if (!ignoreThemeAttr) { |
| 992 |
final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME); |
| 993 |
final int themeResId = ta.getResourceId(0, 0); |
| 994 |
if (themeResId != 0) { |
| 995 |
context = new ContextThemeWrapper(context, themeResId); |
| 996 |
} |
| 997 |
ta.recycle(); |
| 998 |
} |
| 999 |
|
| 1000 |
try { |
| 1001 |
View view = tryCreateView(parent, name, context, attrs); |
| 1002 |
|
| 1003 |
if (view == null) { |
| 1004 |
final Object lastContext = mConstructorArgs[0]; |
| 1005 |
mConstructorArgs[0] = context; |
| 1006 |
try { |
| 1007 |
if (-1 == name.indexOf('.')) { |
| 1008 |
view = onCreateView(context, parent, name, attrs); |
| 1009 |
} else { |
| 1010 |
view = createView(context, name, null, attrs); |
| 1011 |
} |
| 1012 |
} finally { |
| 1013 |
mConstructorArgs[0] = lastContext; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
return view; |
| 1018 |
} catch (InflateException e) { |
| 1019 |
throw e; |
| 1020 |
|
| 1021 |
} catch (ClassNotFoundException e) { |
| 1022 |
final InflateException ie = new InflateException( |
| 1023 |
getParserStateDescription(context, attrs) |
| 1024 |
+ ": Error inflating class " + name, e); |
| 1025 |
ie.setStackTrace(EMPTY_STACK_TRACE); |
| 1026 |
throw ie; |
| 1027 |
|
| 1028 |
} catch (Exception e) { |
| 1029 |
final InflateException ie = new InflateException( |
| 1030 |
getParserStateDescription(context, attrs) |
| 1031 |
+ ": Error inflating class " + name, e); |
| 1032 |
ie.setStackTrace(EMPTY_STACK_TRACE); |
| 1033 |
throw ie; |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
@UnsupportedAppUsage(trackingBug = 122360734) |
| 1054 |
@Nullable |
| 1055 |
public final View tryCreateView(@Nullable View parent, @NonNull String name, |
| 1056 |
@NonNull Context context, |
| 1057 |
@NonNull AttributeSet attrs) { |
| 1058 |
if (name.equals(TAG_1995)) { |
| 1059 |
|
| 1060 |
return new BlinkLayout(context, attrs); |
| 1061 |
} |
| 1062 |
|
| 1063 |
View view; |
| 1064 |
if (mFactory2 != null) { |
| 1065 |
view = mFactory2.onCreateView(parent, name, context, attrs); |
| 1066 |
} else if (mFactory != null) { |
| 1067 |
view = mFactory.onCreateView(name, context, attrs); |
| 1068 |
} else { |
| 1069 |
view = null; |
| 1070 |
} |
| 1071 |
|
| 1072 |
if (view == null && mPrivateFactory != null) { |
| 1073 |
view = mPrivateFactory.onCreateView(parent, name, context, attrs); |
| 1074 |
} |
| 1075 |
|
| 1076 |
return view; |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
final void rInflateChildren(XmlPullParser parser, View parent, AttributeSet attrs, |
| 1087 |
boolean finishInflate) throws XmlPullParserException, IOException { |
| 1088 |
rInflate(parser, parent, parent.getContext(), attrs, finishInflate); |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
void rInflate(XmlPullParser parser, View parent, Context context, |
| 1099 |
AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException { |
| 1100 |
|
| 1101 |
final int depth = parser.getDepth(); |
| 1102 |
int type; |
| 1103 |
boolean pendingRequestFocus = false; |
| 1104 |
|
| 1105 |
while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 1106 |
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 1107 |
|
| 1108 |
if (type != XmlPullParser.START_TAG) { |
| 1109 |
continue; |
| 1110 |
} |
| 1111 |
|
| 1112 |
final String name = parser.getName(); |
| 1113 |
|
| 1114 |
if (TAG_REQUEST_FOCUS.equals(name)) { |
| 1115 |
pendingRequestFocus = true; |
| 1116 |
consumeChildElements(parser); |
| 1117 |
} else if (TAG_TAG.equals(name)) { |
| 1118 |
parseViewTag(parser, parent, attrs); |
| 1119 |
} else if (TAG_INCLUDE.equals(name)) { |
| 1120 |
if (parser.getDepth() == 0) { |
| 1121 |
throw new InflateException("<include /> cannot be the root element"); |
| 1122 |
} |
| 1123 |
parseInclude(parser, context, parent, attrs); |
| 1124 |
} else if (TAG_MERGE.equals(name)) { |
| 1125 |
throw new InflateException("<merge /> must be the root element"); |
| 1126 |
} else { |
| 1127 |
final View view = createViewFromTag(parent, name, context, attrs); |
| 1128 |
final ViewGroup viewGroup = (ViewGroup) parent; |
| 1129 |
final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs); |
| 1130 |
rInflateChildren(parser, view, attrs, true); |
| 1131 |
viewGroup.addView(view, params); |
| 1132 |
} |
| 1133 |
} |
| 1134 |
|
| 1135 |
if (pendingRequestFocus) { |
| 1136 |
parent.restoreDefaultFocus(); |
| 1137 |
} |
| 1138 |
|
| 1139 |
if (finishInflate) { |
| 1140 |
parent.onFinishInflate(); |
| 1141 |
} |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
private void parseViewTag(XmlPullParser parser, View view, AttributeSet attrs) |
| 1149 |
throws XmlPullParserException, IOException { |
| 1150 |
final Context context = view.getContext(); |
| 1151 |
final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ViewTag); |
| 1152 |
final int key = ta.getResourceId(R.styleable.ViewTag_id, 0); |
| 1153 |
final CharSequence value = ta.getText(R.styleable.ViewTag_value); |
| 1154 |
view.setTag(key, value); |
| 1155 |
ta.recycle(); |
| 1156 |
|
| 1157 |
consumeChildElements(parser); |
| 1158 |
} |
| 1159 |
|
| 1160 |
@UnsupportedAppUsage |
| 1161 |
private void parseInclude(XmlPullParser parser, Context context, View parent, |
| 1162 |
AttributeSet attrs) throws XmlPullParserException, IOException { |
| 1163 |
int type; |
| 1164 |
|
| 1165 |
if (!(parent instanceof ViewGroup)) { |
| 1166 |
throw new InflateException("<include /> can only be used inside of a ViewGroup"); |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME); |
| 1174 |
final int themeResId = ta.getResourceId(0, 0); |
| 1175 |
final boolean hasThemeOverride = themeResId != 0; |
| 1176 |
if (hasThemeOverride) { |
| 1177 |
context = new ContextThemeWrapper(context, themeResId); |
| 1178 |
} |
| 1179 |
ta.recycle(); |
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
int layout = attrs.getAttributeResourceValue(null, ATTR_LAYOUT, 0); |
| 1184 |
if (layout == 0) { |
| 1185 |
final String value = attrs.getAttributeValue(null, ATTR_LAYOUT); |
| 1186 |
if (value == null || value.length() <= 0) { |
| 1187 |
throw new InflateException("You must specify a layout in the" |
| 1188 |
+ " include tag: <include layout=\"@layout/layoutID\" />"); |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
layout = context.getResources().getIdentifier( |
| 1194 |
value.substring(1), "attr", context.getPackageName()); |
| 1195 |
|
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
if (mTempValue == null) { |
| 1200 |
mTempValue = new TypedValue(); |
| 1201 |
} |
| 1202 |
if (layout != 0 && context.getTheme().resolveAttribute(layout, mTempValue, true)) { |
| 1203 |
layout = mTempValue.resourceId; |
| 1204 |
} |
| 1205 |
|
| 1206 |
if (layout == 0) { |
| 1207 |
final String value = attrs.getAttributeValue(null, ATTR_LAYOUT); |
| 1208 |
throw new InflateException("You must specify a valid layout " |
| 1209 |
+ "reference. The layout ID " + value + " is not valid."); |
| 1210 |
} |
| 1211 |
|
| 1212 |
final View precompiled = tryInflatePrecompiled(layout, context.getResources(), |
| 1213 |
(ViewGroup) parent, true); |
| 1214 |
if (precompiled == null) { |
| 1215 |
final XmlResourceParser childParser = context.getResources().getLayout(layout); |
| 1216 |
|
| 1217 |
try { |
| 1218 |
final AttributeSet childAttrs = Xml.asAttributeSet(childParser); |
| 1219 |
|
| 1220 |
while ((type = childParser.next()) != XmlPullParser.START_TAG && |
| 1221 |
type != XmlPullParser.END_DOCUMENT) { |
| 1222 |
|
| 1223 |
} |
| 1224 |
|
| 1225 |
if (type != XmlPullParser.START_TAG) { |
| 1226 |
throw new InflateException(getParserStateDescription(context, childAttrs) |
| 1227 |
+ ": No start tag found!"); |
| 1228 |
} |
| 1229 |
|
| 1230 |
final String childName = childParser.getName(); |
| 1231 |
|
| 1232 |
if (TAG_MERGE.equals(childName)) { |
| 1233 |
|
| 1234 |
|
| 1235 |
rInflate(childParser, parent, context, childAttrs, false); |
| 1236 |
} else { |
| 1237 |
final View view = createViewFromTag(parent, childName, |
| 1238 |
context, childAttrs, hasThemeOverride); |
| 1239 |
final ViewGroup group = (ViewGroup) parent; |
| 1240 |
|
| 1241 |
final TypedArray a = context.obtainStyledAttributes( |
| 1242 |
attrs, R.styleable.Include); |
| 1243 |
final int id = a.getResourceId(R.styleable.Include_id, View.NO_ID); |
| 1244 |
final int visibility = a.getInt(R.styleable.Include_visibility, -1); |
| 1245 |
a.recycle(); |
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
ViewGroup.LayoutParams params = null; |
| 1256 |
try { |
| 1257 |
params = group.generateLayoutParams(attrs); |
| 1258 |
} catch (RuntimeException e) { |
| 1259 |
|
| 1260 |
} |
| 1261 |
if (params == null) { |
| 1262 |
params = group.generateLayoutParams(childAttrs); |
| 1263 |
} |
| 1264 |
view.setLayoutParams(params); |
| 1265 |
|
| 1266 |
|
| 1267 |
rInflateChildren(childParser, view, childAttrs, true); |
| 1268 |
|
| 1269 |
if (id != View.NO_ID) { |
| 1270 |
view.setId(id); |
| 1271 |
} |
| 1272 |
|
| 1273 |
switch (visibility) { |
| 1274 |
case 0: |
| 1275 |
view.setVisibility(View.VISIBLE); |
| 1276 |
break; |
| 1277 |
case 1: |
| 1278 |
view.setVisibility(View.INVISIBLE); |
| 1279 |
break; |
| 1280 |
case 2: |
| 1281 |
view.setVisibility(View.GONE); |
| 1282 |
break; |
| 1283 |
} |
| 1284 |
|
| 1285 |
group.addView(view); |
| 1286 |
} |
| 1287 |
} finally { |
| 1288 |
childParser.close(); |
| 1289 |
} |
| 1290 |
} |
| 1291 |
LayoutInflater.consumeChildElements(parser); |
| 1292 |
} |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
final static void consumeChildElements(XmlPullParser parser) |
| 1299 |
throws XmlPullParserException, IOException { |
| 1300 |
int type; |
| 1301 |
final int currentDepth = parser.getDepth(); |
| 1302 |
while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 1303 |
parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) { |
| 1304 |
|
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
private static class BlinkLayout extends FrameLayout { |
| 1309 |
private static final int MESSAGE_BLINK = 0x42; |
| 1310 |
private static final int BLINK_DELAY = 500; |
| 1311 |
|
| 1312 |
private boolean mBlink; |
| 1313 |
private boolean mBlinkState; |
| 1314 |
private final Handler mHandler; |
| 1315 |
|
| 1316 |
public BlinkLayout(Context context, AttributeSet attrs) { |
| 1317 |
super(context, attrs); |
| 1318 |
mHandler = new Handler(new Handler.Callback() { |
| 1319 |
@Override |
| 1320 |
public boolean handleMessage(Message msg) { |
| 1321 |
if (msg.what == MESSAGE_BLINK) { |
| 1322 |
if (mBlink) { |
| 1323 |
mBlinkState = !mBlinkState; |
| 1324 |
makeBlink(); |
| 1325 |
} |
| 1326 |
invalidate(); |
| 1327 |
return true; |
| 1328 |
} |
| 1329 |
return false; |
| 1330 |
} |
| 1331 |
}); |
| 1332 |
} |
| 1333 |
|
| 1334 |
private void makeBlink() { |
| 1335 |
Message message = mHandler.obtainMessage(MESSAGE_BLINK); |
| 1336 |
mHandler.sendMessageDelayed(message, BLINK_DELAY); |
| 1337 |
} |
| 1338 |
|
| 1339 |
@Override |
| 1340 |
protected void onAttachedToWindow() { |
| 1341 |
super.onAttachedToWindow(); |
| 1342 |
|
| 1343 |
mBlink = true; |
| 1344 |
mBlinkState = true; |
| 1345 |
|
| 1346 |
makeBlink(); |
| 1347 |
} |
| 1348 |
|
| 1349 |
@Override |
| 1350 |
protected void onDetachedFromWindow() { |
| 1351 |
super.onDetachedFromWindow(); |
| 1352 |
|
| 1353 |
mBlink = false; |
| 1354 |
mBlinkState = true; |
| 1355 |
|
| 1356 |
mHandler.removeMessages(MESSAGE_BLINK); |
| 1357 |
} |
| 1358 |
|
| 1359 |
@Override |
| 1360 |
protected void dispatchDraw(Canvas canvas) { |
| 1361 |
if (mBlinkState) { |
| 1362 |
super.dispatchDraw(canvas); |
| 1363 |
} |
| 1364 |
} |
| 1365 |
} |
| 1366 |
} |