| 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.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.TestApi; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.res.Resources; |
| 25 |
import android.graphics.Bitmap; |
| 26 |
import android.graphics.Canvas; |
| 27 |
import android.graphics.HardwareRenderer; |
| 28 |
import android.graphics.Picture; |
| 29 |
import android.graphics.RecordingCanvas; |
| 30 |
import android.graphics.Rect; |
| 31 |
import android.graphics.RenderNode; |
| 32 |
import android.os.Build; |
| 33 |
import android.os.Debug; |
| 34 |
import android.os.Handler; |
| 35 |
import android.os.Looper; |
| 36 |
import android.os.Message; |
| 37 |
import android.util.DisplayMetrics; |
| 38 |
import android.util.Log; |
| 39 |
import android.util.TypedValue; |
| 40 |
|
| 41 |
import libcore.util.HexEncoding; |
| 42 |
|
| 43 |
import java.io.BufferedOutputStream; |
| 44 |
import java.io.BufferedWriter; |
| 45 |
import java.io.ByteArrayOutputStream; |
| 46 |
import java.io.DataOutputStream; |
| 47 |
import java.io.IOException; |
| 48 |
import java.io.OutputStream; |
| 49 |
import java.io.OutputStreamWriter; |
| 50 |
import java.lang.annotation.Annotation; |
| 51 |
import java.lang.annotation.ElementType; |
| 52 |
import java.lang.annotation.Retention; |
| 53 |
import java.lang.annotation.RetentionPolicy; |
| 54 |
import java.lang.annotation.Target; |
| 55 |
import java.lang.reflect.AccessibleObject; |
| 56 |
import java.lang.reflect.Field; |
| 57 |
import java.lang.reflect.InvocationTargetException; |
| 58 |
import java.lang.reflect.Member; |
| 59 |
import java.lang.reflect.Method; |
| 60 |
import java.util.ArrayDeque; |
| 61 |
import java.util.Arrays; |
| 62 |
import java.util.HashMap; |
| 63 |
import java.util.concurrent.Callable; |
| 64 |
import java.util.concurrent.CountDownLatch; |
| 65 |
import java.util.concurrent.ExecutionException; |
| 66 |
import java.util.concurrent.Executor; |
| 67 |
import java.util.concurrent.FutureTask; |
| 68 |
import java.util.concurrent.TimeUnit; |
| 69 |
import java.util.concurrent.TimeoutException; |
| 70 |
import java.util.concurrent.atomic.AtomicReference; |
| 71 |
import java.util.concurrent.locks.ReentrantLock; |
| 72 |
import java.util.function.Function; |
| 73 |
import java.util.stream.Stream; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public class ViewDebug { |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
@Deprecated |
| 83 |
public static final boolean TRACE_HIERARCHY = false; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@Deprecated |
| 89 |
public static final boolean TRACE_RECYCLER = false; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static final boolean DEBUG_DRAG = false; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static final boolean DEBUG_POSITIONING = false; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
@Target({ ElementType.FIELD, ElementType.METHOD }) |
| 109 |
@Retention(RetentionPolicy.RUNTIME) |
| 110 |
public @interface ExportedProperty { |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
boolean resolveId() default false; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
IntToString[] mapping() default { }; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
IntToString[] indexMapping() default { }; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
FlagToString[] flagMapping() default { }; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
boolean deepExport() default false; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
String prefix() default ""; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
String category() default ""; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
boolean formatToHexString() default false; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
boolean hasAdjacentMapping() default false; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
@Target({ ElementType.TYPE }) |
| 235 |
@Retention(RetentionPolicy.RUNTIME) |
| 236 |
public @interface IntToString { |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
int from(); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
String to(); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
@Target({ ElementType.TYPE }) |
| 259 |
@Retention(RetentionPolicy.RUNTIME) |
| 260 |
public @interface FlagToString { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
int mask(); |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
int equals(); |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
String name(); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
boolean outputIf() default true; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
@Target({ ElementType.FIELD, ElementType.METHOD }) |
| 296 |
@Retention(RetentionPolicy.RUNTIME) |
| 297 |
public @interface CapturedViewProperty { |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
boolean retrieveReturn() default false; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public interface HierarchyHandler { |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public void dumpViewHierarchyWithProperties(BufferedWriter out, int level); |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public View findHierarchyView(String className, int hashCode); |
| 335 |
} |
| 336 |
|
| 337 |
private abstract static class PropertyInfo<T extends Annotation, |
| 338 |
R extends AccessibleObject & Member> { |
| 339 |
|
| 340 |
public final R member; |
| 341 |
public final T property; |
| 342 |
public final String name; |
| 343 |
public final Class<?> returnType; |
| 344 |
|
| 345 |
public String entrySuffix = ""; |
| 346 |
public String valueSuffix = ""; |
| 347 |
|
| 348 |
PropertyInfo(Class<T> property, R member, Class<?> returnType) { |
| 349 |
this.member = member; |
| 350 |
this.name = member.getName(); |
| 351 |
this.property = member.getAnnotation(property); |
| 352 |
this.returnType = returnType; |
| 353 |
} |
| 354 |
|
| 355 |
public abstract Object invoke(Object target) throws Exception; |
| 356 |
|
| 357 |
static <T extends Annotation> PropertyInfo<T, ?> forMethod(Method method, |
| 358 |
Class<T> property) { |
| 359 |
|
| 360 |
try { |
| 361 |
if ((method.getReturnType() == Void.class) |
| 362 |
|| (method.getParameterTypes().length != 0)) { |
| 363 |
return null; |
| 364 |
} |
| 365 |
} catch (NoClassDefFoundError e) { |
| 366 |
return null; |
| 367 |
} |
| 368 |
if (!method.isAnnotationPresent(property)) { |
| 369 |
return null; |
| 370 |
} |
| 371 |
method.setAccessible(true); |
| 372 |
|
| 373 |
PropertyInfo info = new MethodPI(method, property); |
| 374 |
info.entrySuffix = "()"; |
| 375 |
info.valueSuffix = ";"; |
| 376 |
return info; |
| 377 |
} |
| 378 |
|
| 379 |
static <T extends Annotation> PropertyInfo<T, ?> forField(Field field, Class<T> property) { |
| 380 |
if (!field.isAnnotationPresent(property)) { |
| 381 |
return null; |
| 382 |
} |
| 383 |
field.setAccessible(true); |
| 384 |
return new FieldPI<>(field, property); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
private static class MethodPI<T extends Annotation> extends PropertyInfo<T, Method> { |
| 389 |
|
| 390 |
MethodPI(Method method, Class<T> property) { |
| 391 |
super(property, method, method.getReturnType()); |
| 392 |
} |
| 393 |
|
| 394 |
@Override |
| 395 |
public Object invoke(Object target) throws Exception { |
| 396 |
return member.invoke(target); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
private static class FieldPI<T extends Annotation> extends PropertyInfo<T, Field> { |
| 401 |
|
| 402 |
FieldPI(Field field, Class<T> property) { |
| 403 |
super(property, field, field.getType()); |
| 404 |
} |
| 405 |
|
| 406 |
@Override |
| 407 |
public Object invoke(Object target) throws Exception { |
| 408 |
return member.get(target); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
private static final int CAPTURE_TIMEOUT = 6000; |
| 414 |
|
| 415 |
private static final String REMOTE_COMMAND_CAPTURE = "CAPTURE"; |
| 416 |
private static final String REMOTE_COMMAND_DUMP = "DUMP"; |
| 417 |
private static final String REMOTE_COMMAND_DUMP_THEME = "DUMP_THEME"; |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
public static final String REMOTE_COMMAND_DUMP_ENCODED = "DUMP_ENCODED"; |
| 423 |
private static final String REMOTE_COMMAND_INVALIDATE = "INVALIDATE"; |
| 424 |
private static final String REMOTE_COMMAND_REQUEST_LAYOUT = "REQUEST_LAYOUT"; |
| 425 |
private static final String REMOTE_PROFILE = "PROFILE"; |
| 426 |
private static final String REMOTE_COMMAND_CAPTURE_LAYERS = "CAPTURE_LAYERS"; |
| 427 |
private static final String REMOTE_COMMAND_OUTPUT_DISPLAYLIST = "OUTPUT_DISPLAYLIST"; |
| 428 |
|
| 429 |
private static HashMap<Class<?>, PropertyInfo<ExportedProperty, ?>[]> sExportProperties; |
| 430 |
private static HashMap<Class<?>, PropertyInfo<CapturedViewProperty, ?>[]> |
| 431 |
sCapturedViewProperties; |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
@Deprecated |
| 437 |
public enum HierarchyTraceType { |
| 438 |
INVALIDATE, |
| 439 |
INVALIDATE_CHILD, |
| 440 |
INVALIDATE_CHILD_IN_PARENT, |
| 441 |
REQUEST_LAYOUT, |
| 442 |
ON_LAYOUT, |
| 443 |
ON_MEASURE, |
| 444 |
DRAW, |
| 445 |
BUILD_CACHE |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
@Deprecated |
| 452 |
public enum RecyclerTraceType { |
| 453 |
NEW_VIEW, |
| 454 |
BIND_VIEW, |
| 455 |
RECYCLE_FROM_ACTIVE_HEAP, |
| 456 |
RECYCLE_FROM_SCRAP_HEAP, |
| 457 |
MOVE_TO_SCRAP_HEAP, |
| 458 |
MOVE_FROM_ACTIVE_TO_SCRAP_HEAP |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
@UnsupportedAppUsage |
| 469 |
public static long getViewInstanceCount() { |
| 470 |
return Debug.countInstancesOfClass(View.class); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 481 |
public static long getViewRootImplCount() { |
| 482 |
return Debug.countInstancesOfClass(ViewRootImpl.class); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
@Deprecated |
| 489 |
@SuppressWarnings({ "UnusedParameters", "deprecation" }) |
| 490 |
public static void trace(View view, RecyclerTraceType type, int... parameters) { |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
@Deprecated |
| 497 |
@SuppressWarnings("UnusedParameters") |
| 498 |
public static void startRecyclerTracing(String prefix, View view) { |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
@Deprecated |
| 505 |
@SuppressWarnings("UnusedParameters") |
| 506 |
public static void stopRecyclerTracing() { |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
@Deprecated |
| 513 |
@SuppressWarnings({ "UnusedParameters", "deprecation" }) |
| 514 |
public static void trace(View view, HierarchyTraceType type) { |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
@Deprecated |
| 521 |
@SuppressWarnings("UnusedParameters") |
| 522 |
public static void startHierarchyTracing(String prefix, View view) { |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
@Deprecated |
| 529 |
public static void stopHierarchyTracing() { |
| 530 |
} |
| 531 |
|
| 532 |
@UnsupportedAppUsage |
| 533 |
static void dispatchCommand(View view, String command, String parameters, |
| 534 |
OutputStream clientStream) throws IOException { |
| 535 |
|
| 536 |
view = view.getRootView(); |
| 537 |
|
| 538 |
if (REMOTE_COMMAND_DUMP.equalsIgnoreCase(command)) { |
| 539 |
dump(view, false, true, clientStream); |
| 540 |
} else if (REMOTE_COMMAND_DUMP_THEME.equalsIgnoreCase(command)) { |
| 541 |
dumpTheme(view, clientStream); |
| 542 |
} else if (REMOTE_COMMAND_DUMP_ENCODED.equalsIgnoreCase(command)) { |
| 543 |
dumpEncoded(view, clientStream); |
| 544 |
} else if (REMOTE_COMMAND_CAPTURE_LAYERS.equalsIgnoreCase(command)) { |
| 545 |
captureLayers(view, new DataOutputStream(clientStream)); |
| 546 |
} else { |
| 547 |
final String[] params = parameters.split(" "); |
| 548 |
if (REMOTE_COMMAND_CAPTURE.equalsIgnoreCase(command)) { |
| 549 |
capture(view, clientStream, params[0]); |
| 550 |
} else if (REMOTE_COMMAND_OUTPUT_DISPLAYLIST.equalsIgnoreCase(command)) { |
| 551 |
outputDisplayList(view, params[0]); |
| 552 |
} else if (REMOTE_COMMAND_INVALIDATE.equalsIgnoreCase(command)) { |
| 553 |
invalidate(view, params[0]); |
| 554 |
} else if (REMOTE_COMMAND_REQUEST_LAYOUT.equalsIgnoreCase(command)) { |
| 555 |
requestLayout(view, params[0]); |
| 556 |
} else if (REMOTE_PROFILE.equalsIgnoreCase(command)) { |
| 557 |
profile(view, clientStream, params[0]); |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
public static View findView(View root, String parameter) { |
| 564 |
|
| 565 |
if (parameter.indexOf('@') != -1) { |
| 566 |
final String[] ids = parameter.split("@"); |
| 567 |
final String className = ids[0]; |
| 568 |
final int hashCode = (int) Long.parseLong(ids[1], 16); |
| 569 |
|
| 570 |
View view = root.getRootView(); |
| 571 |
if (view instanceof ViewGroup) { |
| 572 |
return findView((ViewGroup) view, className, hashCode); |
| 573 |
} |
| 574 |
} else { |
| 575 |
|
| 576 |
final int id = root.getResources().getIdentifier(parameter, null, null); |
| 577 |
return root.getRootView().findViewById(id); |
| 578 |
} |
| 579 |
|
| 580 |
return null; |
| 581 |
} |
| 582 |
|
| 583 |
private static void invalidate(View root, String parameter) { |
| 584 |
final View view = findView(root, parameter); |
| 585 |
if (view != null) { |
| 586 |
view.postInvalidate(); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
private static void requestLayout(View root, String parameter) { |
| 591 |
final View view = findView(root, parameter); |
| 592 |
if (view != null) { |
| 593 |
root.post(new Runnable() { |
| 594 |
public void run() { |
| 595 |
view.requestLayout(); |
| 596 |
} |
| 597 |
}); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
private static void profile(View root, OutputStream clientStream, String parameter) |
| 602 |
throws IOException { |
| 603 |
|
| 604 |
final View view = findView(root, parameter); |
| 605 |
BufferedWriter out = null; |
| 606 |
try { |
| 607 |
out = new BufferedWriter(new OutputStreamWriter(clientStream), 32 * 1024); |
| 608 |
|
| 609 |
if (view != null) { |
| 610 |
profileViewAndChildren(view, out); |
| 611 |
} else { |
| 612 |
out.write("-1 -1 -1"); |
| 613 |
out.newLine(); |
| 614 |
} |
| 615 |
out.write("DONE."); |
| 616 |
out.newLine(); |
| 617 |
} catch (Exception e) { |
| 618 |
android.util.Log.w("View", "Problem profiling the view:", e); |
| 619 |
} finally { |
| 620 |
if (out != null) { |
| 621 |
out.close(); |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
public static void profileViewAndChildren(final View view, BufferedWriter out) |
| 628 |
throws IOException { |
| 629 |
RenderNode node = RenderNode.create("ViewDebug", null); |
| 630 |
profileViewAndChildren(view, node, out, true); |
| 631 |
} |
| 632 |
|
| 633 |
private static void profileViewAndChildren(View view, RenderNode node, BufferedWriter out, |
| 634 |
boolean root) throws IOException { |
| 635 |
long durationMeasure = |
| 636 |
(root || (view.mPrivateFlags & View.PFLAG_MEASURED_DIMENSION_SET) != 0) |
| 637 |
? profileViewMeasure(view) : 0; |
| 638 |
long durationLayout = |
| 639 |
(root || (view.mPrivateFlags & View.PFLAG_LAYOUT_REQUIRED) != 0) |
| 640 |
? profileViewLayout(view) : 0; |
| 641 |
long durationDraw = |
| 642 |
(root || !view.willNotDraw() || (view.mPrivateFlags & View.PFLAG_DRAWN) != 0) |
| 643 |
? profileViewDraw(view, node) : 0; |
| 644 |
|
| 645 |
out.write(String.valueOf(durationMeasure)); |
| 646 |
out.write(' '); |
| 647 |
out.write(String.valueOf(durationLayout)); |
| 648 |
out.write(' '); |
| 649 |
out.write(String.valueOf(durationDraw)); |
| 650 |
out.newLine(); |
| 651 |
if (view instanceof ViewGroup) { |
| 652 |
ViewGroup group = (ViewGroup) view; |
| 653 |
final int count = group.getChildCount(); |
| 654 |
for (int i = 0; i < count; i++) { |
| 655 |
profileViewAndChildren(group.getChildAt(i), node, out, false); |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
private static long profileViewMeasure(final View view) { |
| 661 |
return profileViewOperation(view, new ViewOperation() { |
| 662 |
@Override |
| 663 |
public void pre() { |
| 664 |
forceLayout(view); |
| 665 |
} |
| 666 |
|
| 667 |
private void forceLayout(View view) { |
| 668 |
view.forceLayout(); |
| 669 |
if (view instanceof ViewGroup) { |
| 670 |
ViewGroup group = (ViewGroup) view; |
| 671 |
final int count = group.getChildCount(); |
| 672 |
for (int i = 0; i < count; i++) { |
| 673 |
forceLayout(group.getChildAt(i)); |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
@Override |
| 679 |
public void run() { |
| 680 |
view.measure(view.mOldWidthMeasureSpec, view.mOldHeightMeasureSpec); |
| 681 |
} |
| 682 |
}); |
| 683 |
} |
| 684 |
|
| 685 |
private static long profileViewLayout(View view) { |
| 686 |
return profileViewOperation(view, |
| 687 |
() -> view.layout(view.mLeft, view.mTop, view.mRight, view.mBottom)); |
| 688 |
} |
| 689 |
|
| 690 |
private static long profileViewDraw(View view, RenderNode node) { |
| 691 |
DisplayMetrics dm = view.getResources().getDisplayMetrics(); |
| 692 |
if (dm == null) { |
| 693 |
return 0; |
| 694 |
} |
| 695 |
|
| 696 |
if (view.isHardwareAccelerated()) { |
| 697 |
RecordingCanvas canvas = node.beginRecording(dm.widthPixels, dm.heightPixels); |
| 698 |
try { |
| 699 |
return profileViewOperation(view, () -> view.draw(canvas)); |
| 700 |
} finally { |
| 701 |
node.endRecording(); |
| 702 |
} |
| 703 |
} else { |
| 704 |
Bitmap bitmap = Bitmap.createBitmap( |
| 705 |
dm, dm.widthPixels, dm.heightPixels, Bitmap.Config.RGB_565); |
| 706 |
Canvas canvas = new Canvas(bitmap); |
| 707 |
try { |
| 708 |
return profileViewOperation(view, () -> view.draw(canvas)); |
| 709 |
} finally { |
| 710 |
canvas.setBitmap(null); |
| 711 |
bitmap.recycle(); |
| 712 |
} |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
interface ViewOperation { |
| 717 |
default void pre() {} |
| 718 |
|
| 719 |
void run(); |
| 720 |
} |
| 721 |
|
| 722 |
private static long profileViewOperation(View view, final ViewOperation operation) { |
| 723 |
final CountDownLatch latch = new CountDownLatch(1); |
| 724 |
final long[] duration = new long[1]; |
| 725 |
|
| 726 |
view.post(() -> { |
| 727 |
try { |
| 728 |
operation.pre(); |
| 729 |
long start = Debug.threadCpuTimeNanos(); |
| 730 |
|
| 731 |
operation.run(); |
| 732 |
duration[0] = Debug.threadCpuTimeNanos() - start; |
| 733 |
} finally { |
| 734 |
latch.countDown(); |
| 735 |
} |
| 736 |
}); |
| 737 |
|
| 738 |
try { |
| 739 |
if (!latch.await(CAPTURE_TIMEOUT, TimeUnit.MILLISECONDS)) { |
| 740 |
Log.w("View", "Could not complete the profiling of the view " + view); |
| 741 |
return -1; |
| 742 |
} |
| 743 |
} catch (InterruptedException e) { |
| 744 |
Log.w("View", "Could not complete the profiling of the view " + view); |
| 745 |
Thread.currentThread().interrupt(); |
| 746 |
return -1; |
| 747 |
} |
| 748 |
|
| 749 |
return duration[0]; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
public static void captureLayers(View root, final DataOutputStream clientStream) |
| 754 |
throws IOException { |
| 755 |
|
| 756 |
try { |
| 757 |
Rect outRect = new Rect(); |
| 758 |
root.mAttachInfo.mViewRootImpl.getDisplayFrame(outRect); |
| 759 |
|
| 760 |
clientStream.writeInt(outRect.width()); |
| 761 |
clientStream.writeInt(outRect.height()); |
| 762 |
|
| 763 |
captureViewLayer(root, clientStream, true); |
| 764 |
|
| 765 |
clientStream.write(2); |
| 766 |
} finally { |
| 767 |
clientStream.close(); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
private static void captureViewLayer(View view, DataOutputStream clientStream, boolean visible) |
| 772 |
throws IOException { |
| 773 |
|
| 774 |
final boolean localVisible = view.getVisibility() == View.VISIBLE && visible; |
| 775 |
|
| 776 |
if ((view.mPrivateFlags & View.PFLAG_SKIP_DRAW) != View.PFLAG_SKIP_DRAW) { |
| 777 |
final int id = view.getId(); |
| 778 |
String name = view.getClass().getSimpleName(); |
| 779 |
if (id != View.NO_ID) { |
| 780 |
name = resolveId(view.getContext(), id).toString(); |
| 781 |
} |
| 782 |
|
| 783 |
clientStream.write(1); |
| 784 |
clientStream.writeUTF(name); |
| 785 |
clientStream.writeByte(localVisible ? 1 : 0); |
| 786 |
|
| 787 |
int[] position = new int[2]; |
| 788 |
|
| 789 |
view.getLocationInWindow(position); |
| 790 |
|
| 791 |
clientStream.writeInt(position[0]); |
| 792 |
clientStream.writeInt(position[1]); |
| 793 |
clientStream.flush(); |
| 794 |
|
| 795 |
Bitmap b = performViewCapture(view, true); |
| 796 |
if (b != null) { |
| 797 |
ByteArrayOutputStream arrayOut = new ByteArrayOutputStream(b.getWidth() * |
| 798 |
b.getHeight() * 2); |
| 799 |
b.compress(Bitmap.CompressFormat.PNG, 100, arrayOut); |
| 800 |
clientStream.writeInt(arrayOut.size()); |
| 801 |
arrayOut.writeTo(clientStream); |
| 802 |
} |
| 803 |
clientStream.flush(); |
| 804 |
} |
| 805 |
|
| 806 |
if (view instanceof ViewGroup) { |
| 807 |
ViewGroup group = (ViewGroup) view; |
| 808 |
int count = group.getChildCount(); |
| 809 |
|
| 810 |
for (int i = 0; i < count; i++) { |
| 811 |
captureViewLayer(group.getChildAt(i), clientStream, localVisible); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
if (view.mOverlay != null) { |
| 816 |
ViewGroup overlayContainer = view.getOverlay().mOverlayViewGroup; |
| 817 |
captureViewLayer(overlayContainer, clientStream, localVisible); |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
private static void outputDisplayList(View root, String parameter) throws IOException { |
| 822 |
final View view = findView(root, parameter); |
| 823 |
view.getViewRootImpl().outputDisplayList(view); |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
public static void outputDisplayList(View root, View target) { |
| 828 |
root.getViewRootImpl().outputDisplayList(target); |
| 829 |
} |
| 830 |
|
| 831 |
private static class PictureCallbackHandler implements AutoCloseable, |
| 832 |
HardwareRenderer.PictureCapturedCallback, Runnable { |
| 833 |
private final HardwareRenderer mRenderer; |
| 834 |
private final Function<Picture, Boolean> mCallback; |
| 835 |
private final Executor mExecutor; |
| 836 |
private final ReentrantLock mLock = new ReentrantLock(false); |
| 837 |
private final ArrayDeque<Picture> mQueue = new ArrayDeque<>(3); |
| 838 |
private boolean mStopListening; |
| 839 |
private Thread mRenderThread; |
| 840 |
|
| 841 |
private PictureCallbackHandler(HardwareRenderer renderer, |
| 842 |
Function<Picture, Boolean> callback, Executor executor) { |
| 843 |
mRenderer = renderer; |
| 844 |
mCallback = callback; |
| 845 |
mExecutor = executor; |
| 846 |
mRenderer.setPictureCaptureCallback(this); |
| 847 |
} |
| 848 |
|
| 849 |
@Override |
| 850 |
public void close() { |
| 851 |
mLock.lock(); |
| 852 |
mStopListening = true; |
| 853 |
mLock.unlock(); |
| 854 |
mRenderer.setPictureCaptureCallback(null); |
| 855 |
} |
| 856 |
|
| 857 |
@Override |
| 858 |
public void onPictureCaptured(Picture picture) { |
| 859 |
mLock.lock(); |
| 860 |
if (mStopListening) { |
| 861 |
mLock.unlock(); |
| 862 |
mRenderer.setPictureCaptureCallback(null); |
| 863 |
return; |
| 864 |
} |
| 865 |
if (mRenderThread == null) { |
| 866 |
mRenderThread = Thread.currentThread(); |
| 867 |
} |
| 868 |
Picture toDestroy = null; |
| 869 |
if (mQueue.size() == 3) { |
| 870 |
toDestroy = mQueue.removeLast(); |
| 871 |
} |
| 872 |
mQueue.add(picture); |
| 873 |
mLock.unlock(); |
| 874 |
if (toDestroy == null) { |
| 875 |
mExecutor.execute(this); |
| 876 |
} else { |
| 877 |
toDestroy.close(); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
@Override |
| 882 |
public void run() { |
| 883 |
mLock.lock(); |
| 884 |
final Picture picture = mQueue.poll(); |
| 885 |
final boolean isStopped = mStopListening; |
| 886 |
mLock.unlock(); |
| 887 |
if (Thread.currentThread() == mRenderThread) { |
| 888 |
close(); |
| 889 |
throw new IllegalStateException( |
| 890 |
"ViewDebug#startRenderingCommandsCapture must be given an executor that " |
| 891 |
+ "invokes asynchronously"); |
| 892 |
} |
| 893 |
if (isStopped) { |
| 894 |
picture.close(); |
| 895 |
return; |
| 896 |
} |
| 897 |
final boolean keepReceiving = mCallback.apply(picture); |
| 898 |
if (!keepReceiving) { |
| 899 |
close(); |
| 900 |
} |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
@TestApi |
| 931 |
@Nullable |
| 932 |
@Deprecated |
| 933 |
public static AutoCloseable startRenderingCommandsCapture(View tree, Executor executor, |
| 934 |
Function<Picture, Boolean> callback) { |
| 935 |
final View.AttachInfo attachInfo = tree.mAttachInfo; |
| 936 |
if (attachInfo == null) { |
| 937 |
throw new IllegalArgumentException("Given view isn't attached"); |
| 938 |
} |
| 939 |
if (attachInfo.mHandler.getLooper() != Looper.myLooper()) { |
| 940 |
throw new IllegalStateException("Called on the wrong thread." |
| 941 |
+ " Must be called on the thread that owns the given View"); |
| 942 |
} |
| 943 |
final HardwareRenderer renderer = attachInfo.mThreadedRenderer; |
| 944 |
if (renderer != null) { |
| 945 |
return new PictureCallbackHandler(renderer, callback, executor); |
| 946 |
} |
| 947 |
return null; |
| 948 |
} |
| 949 |
|
| 950 |
private static class StreamingPictureCallbackHandler implements AutoCloseable, |
| 951 |
HardwareRenderer.PictureCapturedCallback, Runnable { |
| 952 |
private final HardwareRenderer mRenderer; |
| 953 |
private final Callable<OutputStream> mCallback; |
| 954 |
private final Executor mExecutor; |
| 955 |
private final ReentrantLock mLock = new ReentrantLock(false); |
| 956 |
private final ArrayDeque<Picture> mQueue = new ArrayDeque<>(3); |
| 957 |
private boolean mStopListening; |
| 958 |
private Thread mRenderThread; |
| 959 |
|
| 960 |
private StreamingPictureCallbackHandler(HardwareRenderer renderer, |
| 961 |
Callable<OutputStream> callback, Executor executor) { |
| 962 |
mRenderer = renderer; |
| 963 |
mCallback = callback; |
| 964 |
mExecutor = executor; |
| 965 |
mRenderer.setPictureCaptureCallback(this); |
| 966 |
} |
| 967 |
|
| 968 |
@Override |
| 969 |
public void close() { |
| 970 |
mLock.lock(); |
| 971 |
mStopListening = true; |
| 972 |
mLock.unlock(); |
| 973 |
mRenderer.setPictureCaptureCallback(null); |
| 974 |
} |
| 975 |
|
| 976 |
@Override |
| 977 |
public void onPictureCaptured(Picture picture) { |
| 978 |
mLock.lock(); |
| 979 |
if (mStopListening) { |
| 980 |
mLock.unlock(); |
| 981 |
mRenderer.setPictureCaptureCallback(null); |
| 982 |
return; |
| 983 |
} |
| 984 |
if (mRenderThread == null) { |
| 985 |
mRenderThread = Thread.currentThread(); |
| 986 |
} |
| 987 |
boolean needsInvoke = true; |
| 988 |
if (mQueue.size() == 3) { |
| 989 |
mQueue.removeLast(); |
| 990 |
needsInvoke = false; |
| 991 |
} |
| 992 |
mQueue.add(picture); |
| 993 |
mLock.unlock(); |
| 994 |
|
| 995 |
if (needsInvoke) { |
| 996 |
mExecutor.execute(this); |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
@Override |
| 1001 |
public void run() { |
| 1002 |
mLock.lock(); |
| 1003 |
final Picture picture = mQueue.poll(); |
| 1004 |
final boolean isStopped = mStopListening; |
| 1005 |
mLock.unlock(); |
| 1006 |
if (Thread.currentThread() == mRenderThread) { |
| 1007 |
close(); |
| 1008 |
throw new IllegalStateException( |
| 1009 |
"ViewDebug#startRenderingCommandsCapture must be given an executor that " |
| 1010 |
+ "invokes asynchronously"); |
| 1011 |
} |
| 1012 |
if (isStopped) { |
| 1013 |
return; |
| 1014 |
} |
| 1015 |
OutputStream stream = null; |
| 1016 |
try { |
| 1017 |
stream = mCallback.call(); |
| 1018 |
} catch (Exception ex) { |
| 1019 |
Log.w("ViewDebug", "Aborting rendering commands capture " |
| 1020 |
+ "because callback threw exception", ex); |
| 1021 |
} |
| 1022 |
if (stream != null) { |
| 1023 |
try { |
| 1024 |
picture.writeToStream(stream); |
| 1025 |
stream.flush(); |
| 1026 |
} catch (IOException ex) { |
| 1027 |
Log.w("ViewDebug", "Aborting rendering commands capture " |
| 1028 |
+ "due to IOException writing to output stream", ex); |
| 1029 |
} |
| 1030 |
} else { |
| 1031 |
close(); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
@TestApi |
| 1068 |
@Nullable |
| 1069 |
@UnsupportedAppUsage |
| 1070 |
public static AutoCloseable startRenderingCommandsCapture(View tree, Executor executor, |
| 1071 |
Callable<OutputStream> callback) { |
| 1072 |
final View.AttachInfo attachInfo = tree.mAttachInfo; |
| 1073 |
if (attachInfo == null) { |
| 1074 |
throw new IllegalArgumentException("Given view isn't attached"); |
| 1075 |
} |
| 1076 |
if (attachInfo.mHandler.getLooper() != Looper.myLooper()) { |
| 1077 |
throw new IllegalStateException("Called on the wrong thread." |
| 1078 |
+ " Must be called on the thread that owns the given View"); |
| 1079 |
} |
| 1080 |
final HardwareRenderer renderer = attachInfo.mThreadedRenderer; |
| 1081 |
if (renderer != null) { |
| 1082 |
return new StreamingPictureCallbackHandler(renderer, callback, executor); |
| 1083 |
} |
| 1084 |
return null; |
| 1085 |
} |
| 1086 |
|
| 1087 |
private static void capture(View root, final OutputStream clientStream, String parameter) |
| 1088 |
throws IOException { |
| 1089 |
|
| 1090 |
final View captureView = findView(root, parameter); |
| 1091 |
capture(root, clientStream, captureView); |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
public static void capture(View root, final OutputStream clientStream, View captureView) |
| 1096 |
throws IOException { |
| 1097 |
Bitmap b = performViewCapture(captureView, false); |
| 1098 |
|
| 1099 |
if (b == null) { |
| 1100 |
Log.w("View", "Failed to create capture bitmap!"); |
| 1101 |
|
| 1102 |
|
| 1103 |
b = Bitmap.createBitmap(root.getResources().getDisplayMetrics(), |
| 1104 |
1, 1, Bitmap.Config.ARGB_8888); |
| 1105 |
} |
| 1106 |
|
| 1107 |
BufferedOutputStream out = null; |
| 1108 |
try { |
| 1109 |
out = new BufferedOutputStream(clientStream, 32 * 1024); |
| 1110 |
b.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 1111 |
out.flush(); |
| 1112 |
} finally { |
| 1113 |
if (out != null) { |
| 1114 |
out.close(); |
| 1115 |
} |
| 1116 |
b.recycle(); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
private static Bitmap performViewCapture(final View captureView, final boolean skipChildren) { |
| 1121 |
if (captureView != null) { |
| 1122 |
final CountDownLatch latch = new CountDownLatch(1); |
| 1123 |
final Bitmap[] cache = new Bitmap[1]; |
| 1124 |
|
| 1125 |
captureView.post(() -> { |
| 1126 |
try { |
| 1127 |
CanvasProvider provider = captureView.isHardwareAccelerated() |
| 1128 |
? new HardwareCanvasProvider() : new SoftwareCanvasProvider(); |
| 1129 |
cache[0] = captureView.createSnapshot(provider, skipChildren); |
| 1130 |
} catch (OutOfMemoryError e) { |
| 1131 |
Log.w("View", "Out of memory for bitmap"); |
| 1132 |
} finally { |
| 1133 |
latch.countDown(); |
| 1134 |
} |
| 1135 |
}); |
| 1136 |
|
| 1137 |
try { |
| 1138 |
latch.await(CAPTURE_TIMEOUT, TimeUnit.MILLISECONDS); |
| 1139 |
return cache[0]; |
| 1140 |
} catch (InterruptedException e) { |
| 1141 |
Log.w("View", "Could not complete the capture of the view " + captureView); |
| 1142 |
Thread.currentThread().interrupt(); |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
return null; |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
@Deprecated |
| 1155 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1156 |
public static void dump(View root, boolean skipChildren, boolean includeProperties, |
| 1157 |
OutputStream clientStream) throws IOException { |
| 1158 |
BufferedWriter out = null; |
| 1159 |
try { |
| 1160 |
out = new BufferedWriter(new OutputStreamWriter(clientStream, "utf-8"), 32 * 1024); |
| 1161 |
View view = root.getRootView(); |
| 1162 |
if (view instanceof ViewGroup) { |
| 1163 |
ViewGroup group = (ViewGroup) view; |
| 1164 |
dumpViewHierarchy(group.getContext(), group, out, 0, |
| 1165 |
skipChildren, includeProperties); |
| 1166 |
} |
| 1167 |
out.write("DONE."); |
| 1168 |
out.newLine(); |
| 1169 |
} catch (Exception e) { |
| 1170 |
android.util.Log.w("View", "Problem dumping the view:", e); |
| 1171 |
} finally { |
| 1172 |
if (out != null) { |
| 1173 |
out.close(); |
| 1174 |
} |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
public static void dumpv2(@NonNull final View view, @NonNull ByteArrayOutputStream out) |
| 1184 |
throws InterruptedException { |
| 1185 |
final ViewHierarchyEncoder encoder = new ViewHierarchyEncoder(out); |
| 1186 |
final CountDownLatch latch = new CountDownLatch(1); |
| 1187 |
|
| 1188 |
view.post(new Runnable() { |
| 1189 |
@Override |
| 1190 |
public void run() { |
| 1191 |
encoder.addProperty("window:left", view.mAttachInfo.mWindowLeft); |
| 1192 |
encoder.addProperty("window:top", view.mAttachInfo.mWindowTop); |
| 1193 |
view.encode(encoder); |
| 1194 |
latch.countDown(); |
| 1195 |
} |
| 1196 |
}); |
| 1197 |
|
| 1198 |
latch.await(2, TimeUnit.SECONDS); |
| 1199 |
encoder.endStream(); |
| 1200 |
} |
| 1201 |
|
| 1202 |
private static void dumpEncoded(@NonNull final View view, @NonNull OutputStream out) |
| 1203 |
throws IOException { |
| 1204 |
ByteArrayOutputStream baOut = new ByteArrayOutputStream(); |
| 1205 |
|
| 1206 |
final ViewHierarchyEncoder encoder = new ViewHierarchyEncoder(baOut); |
| 1207 |
encoder.setUserPropertiesEnabled(false); |
| 1208 |
encoder.addProperty("window:left", view.mAttachInfo.mWindowLeft); |
| 1209 |
encoder.addProperty("window:top", view.mAttachInfo.mWindowTop); |
| 1210 |
view.encode(encoder); |
| 1211 |
encoder.endStream(); |
| 1212 |
out.write(baOut.toByteArray()); |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
public static void dumpTheme(View view, OutputStream clientStream) throws IOException { |
| 1220 |
BufferedWriter out = null; |
| 1221 |
try { |
| 1222 |
out = new BufferedWriter(new OutputStreamWriter(clientStream, "utf-8"), 32 * 1024); |
| 1223 |
String[] attributes = getStyleAttributesDump(view.getContext().getResources(), |
| 1224 |
view.getContext().getTheme()); |
| 1225 |
if (attributes != null) { |
| 1226 |
for (int i = 0; i < attributes.length; i += 2) { |
| 1227 |
if (attributes[i] != null) { |
| 1228 |
out.write(attributes[i] + "\n"); |
| 1229 |
out.write(attributes[i + 1] + "\n"); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
} |
| 1233 |
out.write("DONE."); |
| 1234 |
out.newLine(); |
| 1235 |
} catch (Exception e) { |
| 1236 |
android.util.Log.w("View", "Problem dumping View Theme:", e); |
| 1237 |
} finally { |
| 1238 |
if (out != null) { |
| 1239 |
out.close(); |
| 1240 |
} |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
private static String[] getStyleAttributesDump(Resources resources, Resources.Theme theme) { |
| 1255 |
TypedValue outValue = new TypedValue(); |
| 1256 |
String nullString = "null"; |
| 1257 |
int i = 0; |
| 1258 |
int[] attributes = theme.getAllAttributes(); |
| 1259 |
String[] data = new String[attributes.length * 2]; |
| 1260 |
for (int attributeId : attributes) { |
| 1261 |
try { |
| 1262 |
data[i] = resources.getResourceName(attributeId); |
| 1263 |
data[i + 1] = theme.resolveAttribute(attributeId, outValue, true) ? |
| 1264 |
outValue.coerceToString().toString() : nullString; |
| 1265 |
i += 2; |
| 1266 |
|
| 1267 |
|
| 1268 |
if (outValue.type == TypedValue.TYPE_REFERENCE) { |
| 1269 |
data[i - 1] = resources.getResourceName(outValue.resourceId); |
| 1270 |
} |
| 1271 |
} catch (Resources.NotFoundException e) { |
| 1272 |
|
| 1273 |
} |
| 1274 |
} |
| 1275 |
return data; |
| 1276 |
} |
| 1277 |
|
| 1278 |
private static View findView(ViewGroup group, String className, int hashCode) { |
| 1279 |
if (isRequestedView(group, className, hashCode)) { |
| 1280 |
return group; |
| 1281 |
} |
| 1282 |
|
| 1283 |
final int count = group.getChildCount(); |
| 1284 |
for (int i = 0; i < count; i++) { |
| 1285 |
final View view = group.getChildAt(i); |
| 1286 |
if (view instanceof ViewGroup) { |
| 1287 |
final View found = findView((ViewGroup) view, className, hashCode); |
| 1288 |
if (found != null) { |
| 1289 |
return found; |
| 1290 |
} |
| 1291 |
} else if (isRequestedView(view, className, hashCode)) { |
| 1292 |
return view; |
| 1293 |
} |
| 1294 |
if (view.mOverlay != null) { |
| 1295 |
final View found = findView((ViewGroup) view.mOverlay.mOverlayViewGroup, |
| 1296 |
className, hashCode); |
| 1297 |
if (found != null) { |
| 1298 |
return found; |
| 1299 |
} |
| 1300 |
} |
| 1301 |
if (view instanceof HierarchyHandler) { |
| 1302 |
final View found = ((HierarchyHandler)view) |
| 1303 |
.findHierarchyView(className, hashCode); |
| 1304 |
if (found != null) { |
| 1305 |
return found; |
| 1306 |
} |
| 1307 |
} |
| 1308 |
} |
| 1309 |
return null; |
| 1310 |
} |
| 1311 |
|
| 1312 |
private static boolean isRequestedView(View view, String className, int hashCode) { |
| 1313 |
if (view.hashCode() == hashCode) { |
| 1314 |
String viewClassName = view.getClass().getName(); |
| 1315 |
if (className.equals("ViewOverlay")) { |
| 1316 |
return viewClassName.equals("android.view.ViewOverlay$OverlayViewGroup"); |
| 1317 |
} else { |
| 1318 |
return className.equals(viewClassName); |
| 1319 |
} |
| 1320 |
} |
| 1321 |
return false; |
| 1322 |
} |
| 1323 |
|
| 1324 |
private static void dumpViewHierarchy(Context context, ViewGroup group, |
| 1325 |
BufferedWriter out, int level, boolean skipChildren, boolean includeProperties) { |
| 1326 |
cacheExportedProperties(group.getClass()); |
| 1327 |
if (!skipChildren) { |
| 1328 |
cacheExportedPropertiesForChildren(group); |
| 1329 |
} |
| 1330 |
|
| 1331 |
Handler handler = group.getHandler(); |
| 1332 |
|
| 1333 |
if (handler == null) { |
| 1334 |
handler = new Handler(Looper.getMainLooper()); |
| 1335 |
} |
| 1336 |
|
| 1337 |
if (handler.getLooper() == Looper.myLooper()) { |
| 1338 |
dumpViewHierarchyOnUIThread(context, group, out, level, skipChildren, |
| 1339 |
includeProperties); |
| 1340 |
} else { |
| 1341 |
FutureTask task = new FutureTask(() -> |
| 1342 |
dumpViewHierarchyOnUIThread(context, group, out, level, skipChildren, |
| 1343 |
includeProperties), null); |
| 1344 |
Message msg = Message.obtain(handler, task); |
| 1345 |
msg.setAsynchronous(true); |
| 1346 |
handler.sendMessage(msg); |
| 1347 |
while (true) { |
| 1348 |
try { |
| 1349 |
task.get(CAPTURE_TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); |
| 1350 |
return; |
| 1351 |
} catch (InterruptedException e) { |
| 1352 |
|
| 1353 |
} catch (ExecutionException | TimeoutException e) { |
| 1354 |
|
| 1355 |
throw new RuntimeException(e); |
| 1356 |
} |
| 1357 |
} |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
private static void cacheExportedPropertiesForChildren(ViewGroup group) { |
| 1362 |
final int count = group.getChildCount(); |
| 1363 |
for (int i = 0; i < count; i++) { |
| 1364 |
final View view = group.getChildAt(i); |
| 1365 |
cacheExportedProperties(view.getClass()); |
| 1366 |
if (view instanceof ViewGroup) { |
| 1367 |
cacheExportedPropertiesForChildren((ViewGroup) view); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
} |
| 1371 |
|
| 1372 |
private static void cacheExportedProperties(Class<?> klass) { |
| 1373 |
if (sExportProperties != null && sExportProperties.containsKey(klass)) { |
| 1374 |
return; |
| 1375 |
} |
| 1376 |
do { |
| 1377 |
for (PropertyInfo<ExportedProperty, ?> info : getExportedProperties(klass)) { |
| 1378 |
if (!info.returnType.isPrimitive() && info.property.deepExport()) { |
| 1379 |
cacheExportedProperties(info.returnType); |
| 1380 |
} |
| 1381 |
} |
| 1382 |
klass = klass.getSuperclass(); |
| 1383 |
} while (klass != Object.class); |
| 1384 |
} |
| 1385 |
|
| 1386 |
|
| 1387 |
private static void dumpViewHierarchyOnUIThread(Context context, ViewGroup group, |
| 1388 |
BufferedWriter out, int level, boolean skipChildren, boolean includeProperties) { |
| 1389 |
if (!dumpView(context, group, out, level, includeProperties)) { |
| 1390 |
return; |
| 1391 |
} |
| 1392 |
|
| 1393 |
if (skipChildren) { |
| 1394 |
return; |
| 1395 |
} |
| 1396 |
|
| 1397 |
final int count = group.getChildCount(); |
| 1398 |
for (int i = 0; i < count; i++) { |
| 1399 |
final View view = group.getChildAt(i); |
| 1400 |
if (view instanceof ViewGroup) { |
| 1401 |
dumpViewHierarchyOnUIThread(context, (ViewGroup) view, out, level + 1, |
| 1402 |
skipChildren, includeProperties); |
| 1403 |
} else { |
| 1404 |
dumpView(context, view, out, level + 1, includeProperties); |
| 1405 |
} |
| 1406 |
if (view.mOverlay != null) { |
| 1407 |
ViewOverlay overlay = view.getOverlay(); |
| 1408 |
ViewGroup overlayContainer = overlay.mOverlayViewGroup; |
| 1409 |
dumpViewHierarchyOnUIThread(context, overlayContainer, out, level + 2, |
| 1410 |
skipChildren, includeProperties); |
| 1411 |
} |
| 1412 |
} |
| 1413 |
if (group instanceof HierarchyHandler) { |
| 1414 |
((HierarchyHandler)group).dumpViewHierarchyWithProperties(out, level + 1); |
| 1415 |
} |
| 1416 |
} |
| 1417 |
|
| 1418 |
private static boolean dumpView(Context context, View view, |
| 1419 |
BufferedWriter out, int level, boolean includeProperties) { |
| 1420 |
|
| 1421 |
try { |
| 1422 |
for (int i = 0; i < level; i++) { |
| 1423 |
out.write(' '); |
| 1424 |
} |
| 1425 |
String className = view.getClass().getName(); |
| 1426 |
if (className.equals("android.view.ViewOverlay$OverlayViewGroup")) { |
| 1427 |
className = "ViewOverlay"; |
| 1428 |
} |
| 1429 |
out.write(className); |
| 1430 |
out.write('@'); |
| 1431 |
out.write(Integer.toHexString(view.hashCode())); |
| 1432 |
out.write(' '); |
| 1433 |
if (includeProperties) { |
| 1434 |
dumpViewProperties(context, view, out); |
| 1435 |
} |
| 1436 |
out.newLine(); |
| 1437 |
} catch (IOException e) { |
| 1438 |
Log.w("View", "Error while dumping hierarchy tree"); |
| 1439 |
return false; |
| 1440 |
} |
| 1441 |
return true; |
| 1442 |
} |
| 1443 |
|
| 1444 |
private static <T extends Annotation> PropertyInfo<T, ?>[] convertToPropertyInfos( |
| 1445 |
Method[] methods, Field[] fields, Class<T> property) { |
| 1446 |
return Stream.of(Arrays.stream(methods).map(m -> PropertyInfo.forMethod(m, property)), |
| 1447 |
Arrays.stream(fields).map(f -> PropertyInfo.forField(f, property))) |
| 1448 |
.flatMap(Function.identity()) |
| 1449 |
.filter(i -> i != null) |
| 1450 |
.toArray(PropertyInfo[]::new); |
| 1451 |
} |
| 1452 |
|
| 1453 |
private static PropertyInfo<ExportedProperty, ?>[] getExportedProperties(Class<?> klass) { |
| 1454 |
if (sExportProperties == null) { |
| 1455 |
sExportProperties = new HashMap<>(); |
| 1456 |
} |
| 1457 |
final HashMap<Class<?>, PropertyInfo<ExportedProperty, ?>[]> map = sExportProperties; |
| 1458 |
PropertyInfo<ExportedProperty, ?>[] properties = sExportProperties.get(klass); |
| 1459 |
|
| 1460 |
if (properties == null) { |
| 1461 |
properties = convertToPropertyInfos(klass.getDeclaredMethods(), |
| 1462 |
klass.getDeclaredFields(), ExportedProperty.class); |
| 1463 |
map.put(klass, properties); |
| 1464 |
} |
| 1465 |
return properties; |
| 1466 |
} |
| 1467 |
|
| 1468 |
private static void dumpViewProperties(Context context, Object view, |
| 1469 |
BufferedWriter out) throws IOException { |
| 1470 |
|
| 1471 |
dumpViewProperties(context, view, out, ""); |
| 1472 |
} |
| 1473 |
|
| 1474 |
private static void dumpViewProperties(Context context, Object view, |
| 1475 |
BufferedWriter out, String prefix) throws IOException { |
| 1476 |
|
| 1477 |
if (view == null) { |
| 1478 |
out.write(prefix + "=4,null "); |
| 1479 |
return; |
| 1480 |
} |
| 1481 |
|
| 1482 |
Class<?> klass = view.getClass(); |
| 1483 |
do { |
| 1484 |
writeExportedProperties(context, view, out, klass, prefix); |
| 1485 |
klass = klass.getSuperclass(); |
| 1486 |
} while (klass != Object.class); |
| 1487 |
} |
| 1488 |
|
| 1489 |
private static String formatIntToHexString(int value) { |
| 1490 |
return "0x" + Integer.toHexString(value).toUpperCase(); |
| 1491 |
} |
| 1492 |
|
| 1493 |
private static void writeExportedProperties(Context context, Object view, BufferedWriter out, |
| 1494 |
Class<?> klass, String prefix) throws IOException { |
| 1495 |
for (PropertyInfo<ExportedProperty, ?> info : getExportedProperties(klass)) { |
| 1496 |
|
| 1497 |
Object value; |
| 1498 |
try { |
| 1499 |
value = info.invoke(view); |
| 1500 |
} catch (Exception e) { |
| 1501 |
|
| 1502 |
continue; |
| 1503 |
} |
| 1504 |
|
| 1505 |
String categoryPrefix = |
| 1506 |
info.property.category().length() != 0 ? info.property.category() + ":" : ""; |
| 1507 |
|
| 1508 |
if (info.returnType == int.class || info.returnType == byte.class) { |
| 1509 |
if (info.property.resolveId() && context != null) { |
| 1510 |
final int id = (Integer) value; |
| 1511 |
value = resolveId(context, id); |
| 1512 |
|
| 1513 |
} else if (info.property.formatToHexString()) { |
| 1514 |
if (info.returnType == int.class) { |
| 1515 |
value = formatIntToHexString((Integer) value); |
| 1516 |
} else if (info.returnType == byte.class) { |
| 1517 |
value = "0x" |
| 1518 |
+ HexEncoding.encodeToString((Byte) value, true); |
| 1519 |
} |
| 1520 |
} else { |
| 1521 |
final ViewDebug.FlagToString[] flagsMapping = info.property.flagMapping(); |
| 1522 |
if (flagsMapping.length > 0) { |
| 1523 |
final int intValue = (Integer) value; |
| 1524 |
final String valuePrefix = |
| 1525 |
categoryPrefix + prefix + info.name + '_'; |
| 1526 |
exportUnrolledFlags(out, flagsMapping, intValue, valuePrefix); |
| 1527 |
} |
| 1528 |
|
| 1529 |
final ViewDebug.IntToString[] mapping = info.property.mapping(); |
| 1530 |
if (mapping.length > 0) { |
| 1531 |
final int intValue = (Integer) value; |
| 1532 |
boolean mapped = false; |
| 1533 |
int mappingCount = mapping.length; |
| 1534 |
for (int j = 0; j < mappingCount; j++) { |
| 1535 |
final ViewDebug.IntToString mapper = mapping[j]; |
| 1536 |
if (mapper.from() == intValue) { |
| 1537 |
value = mapper.to(); |
| 1538 |
mapped = true; |
| 1539 |
break; |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
if (!mapped) { |
| 1544 |
value = intValue; |
| 1545 |
} |
| 1546 |
} |
| 1547 |
} |
| 1548 |
} else if (info.returnType == int[].class) { |
| 1549 |
final int[] array = (int[]) value; |
| 1550 |
final String valuePrefix = categoryPrefix + prefix + info.name + '_'; |
| 1551 |
exportUnrolledArray(context, out, info.property, array, valuePrefix, |
| 1552 |
info.entrySuffix); |
| 1553 |
|
| 1554 |
continue; |
| 1555 |
} else if (info.returnType == String[].class) { |
| 1556 |
final String[] array = (String[]) value; |
| 1557 |
if (info.property.hasAdjacentMapping() && array != null) { |
| 1558 |
for (int j = 0; j < array.length; j += 2) { |
| 1559 |
if (array[j] != null) { |
| 1560 |
writeEntry(out, categoryPrefix + prefix, array[j], |
| 1561 |
info.entrySuffix, array[j + 1] == null ? "null" : array[j + 1]); |
| 1562 |
} |
| 1563 |
} |
| 1564 |
} |
| 1565 |
|
| 1566 |
continue; |
| 1567 |
} else if (!info.returnType.isPrimitive()) { |
| 1568 |
if (info.property.deepExport()) { |
| 1569 |
dumpViewProperties(context, value, out, prefix + info.property.prefix()); |
| 1570 |
continue; |
| 1571 |
} |
| 1572 |
} |
| 1573 |
|
| 1574 |
writeEntry(out, categoryPrefix + prefix, info.name, info.entrySuffix, value); |
| 1575 |
} |
| 1576 |
} |
| 1577 |
|
| 1578 |
private static void writeEntry(BufferedWriter out, String prefix, String name, |
| 1579 |
String suffix, Object value) throws IOException { |
| 1580 |
|
| 1581 |
out.write(prefix); |
| 1582 |
out.write(name); |
| 1583 |
out.write(suffix); |
| 1584 |
out.write("="); |
| 1585 |
writeValue(out, value); |
| 1586 |
out.write(' '); |
| 1587 |
} |
| 1588 |
|
| 1589 |
private static void exportUnrolledFlags(BufferedWriter out, FlagToString[] mapping, |
| 1590 |
int intValue, String prefix) throws IOException { |
| 1591 |
|
| 1592 |
final int count = mapping.length; |
| 1593 |
for (int j = 0; j < count; j++) { |
| 1594 |
final FlagToString flagMapping = mapping[j]; |
| 1595 |
final boolean ifTrue = flagMapping.outputIf(); |
| 1596 |
final int maskResult = intValue & flagMapping.mask(); |
| 1597 |
final boolean test = maskResult == flagMapping.equals(); |
| 1598 |
if ((test && ifTrue) || (!test && !ifTrue)) { |
| 1599 |
final String name = flagMapping.name(); |
| 1600 |
final String value = formatIntToHexString(maskResult); |
| 1601 |
writeEntry(out, prefix, name, "", value); |
| 1602 |
} |
| 1603 |
} |
| 1604 |
} |
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
public static String intToString(Class<?> clazz, String field, int integer) { |
| 1617 |
final IntToString[] mapping = getMapping(clazz, field); |
| 1618 |
if (mapping == null) { |
| 1619 |
return Integer.toString(integer); |
| 1620 |
} |
| 1621 |
final int count = mapping.length; |
| 1622 |
for (int j = 0; j < count; j++) { |
| 1623 |
final IntToString map = mapping[j]; |
| 1624 |
if (map.from() == integer) { |
| 1625 |
return map.to(); |
| 1626 |
} |
| 1627 |
} |
| 1628 |
return Integer.toString(integer); |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
public static String flagsToString(Class<?> clazz, String field, int flags) { |
| 1642 |
final FlagToString[] mapping = getFlagMapping(clazz, field); |
| 1643 |
if (mapping == null) { |
| 1644 |
return Integer.toHexString(flags); |
| 1645 |
} |
| 1646 |
final StringBuilder result = new StringBuilder(); |
| 1647 |
final int count = mapping.length; |
| 1648 |
for (int j = 0; j < count; j++) { |
| 1649 |
final FlagToString flagMapping = mapping[j]; |
| 1650 |
final boolean ifTrue = flagMapping.outputIf(); |
| 1651 |
final int maskResult = flags & flagMapping.mask(); |
| 1652 |
final boolean test = maskResult == flagMapping.equals(); |
| 1653 |
if (test && ifTrue) { |
| 1654 |
final String name = flagMapping.name(); |
| 1655 |
result.append(name).append(' '); |
| 1656 |
} |
| 1657 |
} |
| 1658 |
if (result.length() > 0) { |
| 1659 |
result.deleteCharAt(result.length() - 1); |
| 1660 |
} |
| 1661 |
return result.toString(); |
| 1662 |
} |
| 1663 |
|
| 1664 |
private static FlagToString[] getFlagMapping(Class<?> clazz, String field) { |
| 1665 |
try { |
| 1666 |
return clazz.getDeclaredField(field).getAnnotation(ExportedProperty.class) |
| 1667 |
.flagMapping(); |
| 1668 |
} catch (NoSuchFieldException e) { |
| 1669 |
return null; |
| 1670 |
} |
| 1671 |
} |
| 1672 |
|
| 1673 |
private static IntToString[] getMapping(Class<?> clazz, String field) { |
| 1674 |
try { |
| 1675 |
return clazz.getDeclaredField(field).getAnnotation(ExportedProperty.class).mapping(); |
| 1676 |
} catch (NoSuchFieldException e) { |
| 1677 |
return null; |
| 1678 |
} |
| 1679 |
} |
| 1680 |
|
| 1681 |
private static void exportUnrolledArray(Context context, BufferedWriter out, |
| 1682 |
ExportedProperty property, int[] array, String prefix, String suffix) |
| 1683 |
throws IOException { |
| 1684 |
|
| 1685 |
final IntToString[] indexMapping = property.indexMapping(); |
| 1686 |
final boolean hasIndexMapping = indexMapping.length > 0; |
| 1687 |
|
| 1688 |
final IntToString[] mapping = property.mapping(); |
| 1689 |
final boolean hasMapping = mapping.length > 0; |
| 1690 |
|
| 1691 |
final boolean resolveId = property.resolveId() && context != null; |
| 1692 |
final int valuesCount = array.length; |
| 1693 |
|
| 1694 |
for (int j = 0; j < valuesCount; j++) { |
| 1695 |
String name; |
| 1696 |
String value = null; |
| 1697 |
|
| 1698 |
final int intValue = array[j]; |
| 1699 |
|
| 1700 |
name = String.valueOf(j); |
| 1701 |
if (hasIndexMapping) { |
| 1702 |
int mappingCount = indexMapping.length; |
| 1703 |
for (int k = 0; k < mappingCount; k++) { |
| 1704 |
final IntToString mapped = indexMapping[k]; |
| 1705 |
if (mapped.from() == j) { |
| 1706 |
name = mapped.to(); |
| 1707 |
break; |
| 1708 |
} |
| 1709 |
} |
| 1710 |
} |
| 1711 |
|
| 1712 |
if (hasMapping) { |
| 1713 |
int mappingCount = mapping.length; |
| 1714 |
for (int k = 0; k < mappingCount; k++) { |
| 1715 |
final IntToString mapped = mapping[k]; |
| 1716 |
if (mapped.from() == intValue) { |
| 1717 |
value = mapped.to(); |
| 1718 |
break; |
| 1719 |
} |
| 1720 |
} |
| 1721 |
} |
| 1722 |
|
| 1723 |
if (resolveId) { |
| 1724 |
if (value == null) value = (String) resolveId(context, intValue); |
| 1725 |
} else { |
| 1726 |
value = String.valueOf(intValue); |
| 1727 |
} |
| 1728 |
|
| 1729 |
writeEntry(out, prefix, name, suffix, value); |
| 1730 |
} |
| 1731 |
} |
| 1732 |
|
| 1733 |
static Object resolveId(Context context, int id) { |
| 1734 |
Object fieldValue; |
| 1735 |
final Resources resources = context.getResources(); |
| 1736 |
if (id >= 0) { |
| 1737 |
try { |
| 1738 |
fieldValue = resources.getResourceTypeName(id) + '/' + |
| 1739 |
resources.getResourceEntryName(id); |
| 1740 |
} catch (Resources.NotFoundException e) { |
| 1741 |
fieldValue = "id/" + formatIntToHexString(id); |
| 1742 |
} |
| 1743 |
} else { |
| 1744 |
fieldValue = "NO_ID"; |
| 1745 |
} |
| 1746 |
return fieldValue; |
| 1747 |
} |
| 1748 |
|
| 1749 |
private static void writeValue(BufferedWriter out, Object value) throws IOException { |
| 1750 |
if (value != null) { |
| 1751 |
String output = "[EXCEPTION]"; |
| 1752 |
try { |
| 1753 |
output = value.toString().replace("\n", "\\n"); |
| 1754 |
} finally { |
| 1755 |
out.write(String.valueOf(output.length())); |
| 1756 |
out.write(","); |
| 1757 |
out.write(output); |
| 1758 |
} |
| 1759 |
} else { |
| 1760 |
out.write("4,null"); |
| 1761 |
} |
| 1762 |
} |
| 1763 |
|
| 1764 |
private static PropertyInfo<CapturedViewProperty, ?>[] getCapturedViewProperties( |
| 1765 |
Class<?> klass) { |
| 1766 |
if (sCapturedViewProperties == null) { |
| 1767 |
sCapturedViewProperties = new HashMap<>(); |
| 1768 |
} |
| 1769 |
final HashMap<Class<?>, PropertyInfo<CapturedViewProperty, ?>[]> map = |
| 1770 |
sCapturedViewProperties; |
| 1771 |
|
| 1772 |
PropertyInfo<CapturedViewProperty, ?>[] infos = map.get(klass); |
| 1773 |
if (infos == null) { |
| 1774 |
infos = convertToPropertyInfos(klass.getMethods(), klass.getFields(), |
| 1775 |
CapturedViewProperty.class); |
| 1776 |
map.put(klass, infos); |
| 1777 |
} |
| 1778 |
return infos; |
| 1779 |
} |
| 1780 |
|
| 1781 |
private static String exportCapturedViewProperties(Object obj, Class<?> klass, String prefix) { |
| 1782 |
if (obj == null) { |
| 1783 |
return "null"; |
| 1784 |
} |
| 1785 |
|
| 1786 |
StringBuilder sb = new StringBuilder(); |
| 1787 |
|
| 1788 |
for (PropertyInfo<CapturedViewProperty, ?> pi : getCapturedViewProperties(klass)) { |
| 1789 |
try { |
| 1790 |
Object methodValue = pi.invoke(obj); |
| 1791 |
|
| 1792 |
if (pi.property.retrieveReturn()) { |
| 1793 |
|
| 1794 |
sb.append(exportCapturedViewProperties(methodValue, pi.returnType, |
| 1795 |
pi.name + "#")); |
| 1796 |
} else { |
| 1797 |
sb.append(prefix).append(pi.name).append(pi.entrySuffix).append("="); |
| 1798 |
|
| 1799 |
if (methodValue != null) { |
| 1800 |
final String value = methodValue.toString().replace("\n", "\\n"); |
| 1801 |
sb.append(value); |
| 1802 |
} else { |
| 1803 |
sb.append("null"); |
| 1804 |
} |
| 1805 |
sb.append(pi.valueSuffix).append(" "); |
| 1806 |
} |
| 1807 |
} catch (Exception e) { |
| 1808 |
|
| 1809 |
} |
| 1810 |
} |
| 1811 |
return sb.toString(); |
| 1812 |
} |
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
public static void dumpCapturedView(String tag, Object view) { |
| 1822 |
Class<?> klass = view.getClass(); |
| 1823 |
StringBuilder sb = new StringBuilder(klass.getName() + ": "); |
| 1824 |
sb.append(exportCapturedViewProperties(view, klass, "")); |
| 1825 |
Log.d(tag, sb.toString()); |
| 1826 |
} |
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
public static Object invokeViewMethod(final View view, final Method method, |
| 1837 |
final Object[] args) { |
| 1838 |
final CountDownLatch latch = new CountDownLatch(1); |
| 1839 |
final AtomicReference<Object> result = new AtomicReference<Object>(); |
| 1840 |
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>(); |
| 1841 |
|
| 1842 |
view.post(new Runnable() { |
| 1843 |
@Override |
| 1844 |
public void run() { |
| 1845 |
try { |
| 1846 |
result.set(method.invoke(view, args)); |
| 1847 |
} catch (InvocationTargetException e) { |
| 1848 |
exception.set(e.getCause()); |
| 1849 |
} catch (Exception e) { |
| 1850 |
exception.set(e); |
| 1851 |
} |
| 1852 |
|
| 1853 |
latch.countDown(); |
| 1854 |
} |
| 1855 |
}); |
| 1856 |
|
| 1857 |
try { |
| 1858 |
latch.await(); |
| 1859 |
} catch (InterruptedException e) { |
| 1860 |
throw new RuntimeException(e); |
| 1861 |
} |
| 1862 |
|
| 1863 |
if (exception.get() != null) { |
| 1864 |
throw new RuntimeException(exception.get()); |
| 1865 |
} |
| 1866 |
|
| 1867 |
return result.get(); |
| 1868 |
} |
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
public static void setLayoutParameter(final View view, final String param, final int value) |
| 1874 |
throws NoSuchFieldException, IllegalAccessException { |
| 1875 |
final ViewGroup.LayoutParams p = view.getLayoutParams(); |
| 1876 |
final Field f = p.getClass().getField(param); |
| 1877 |
if (f.getType() != int.class) { |
| 1878 |
throw new RuntimeException("Only integer layout parameters can be set. Field " |
| 1879 |
+ param + " is of type " + f.getType().getSimpleName()); |
| 1880 |
} |
| 1881 |
|
| 1882 |
f.set(p, Integer.valueOf(value)); |
| 1883 |
|
| 1884 |
view.post(new Runnable() { |
| 1885 |
@Override |
| 1886 |
public void run() { |
| 1887 |
view.setLayoutParams(p); |
| 1888 |
} |
| 1889 |
}); |
| 1890 |
} |
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
public static class SoftwareCanvasProvider implements CanvasProvider { |
| 1896 |
|
| 1897 |
private Canvas mCanvas; |
| 1898 |
private Bitmap mBitmap; |
| 1899 |
private boolean mEnabledHwFeaturesInSwMode; |
| 1900 |
|
| 1901 |
@Override |
| 1902 |
public Canvas getCanvas(View view, int width, int height) { |
| 1903 |
mBitmap = Bitmap.createBitmap(view.getResources().getDisplayMetrics(), |
| 1904 |
width, height, Bitmap.Config.ARGB_8888); |
| 1905 |
if (mBitmap == null) { |
| 1906 |
throw new OutOfMemoryError(); |
| 1907 |
} |
| 1908 |
mBitmap.setDensity(view.getResources().getDisplayMetrics().densityDpi); |
| 1909 |
|
| 1910 |
if (view.mAttachInfo != null) { |
| 1911 |
mCanvas = view.mAttachInfo.mCanvas; |
| 1912 |
} |
| 1913 |
if (mCanvas == null) { |
| 1914 |
mCanvas = new Canvas(); |
| 1915 |
} |
| 1916 |
mEnabledHwFeaturesInSwMode = mCanvas.isHwFeaturesInSwModeEnabled(); |
| 1917 |
mCanvas.setBitmap(mBitmap); |
| 1918 |
return mCanvas; |
| 1919 |
} |
| 1920 |
|
| 1921 |
@Override |
| 1922 |
public Bitmap createBitmap() { |
| 1923 |
mCanvas.setBitmap(null); |
| 1924 |
mCanvas.setHwFeaturesInSwModeEnabled(mEnabledHwFeaturesInSwMode); |
| 1925 |
return mBitmap; |
| 1926 |
} |
| 1927 |
} |
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
public static class HardwareCanvasProvider implements CanvasProvider { |
| 1933 |
private Picture mPicture; |
| 1934 |
|
| 1935 |
@Override |
| 1936 |
public Canvas getCanvas(View view, int width, int height) { |
| 1937 |
mPicture = new Picture(); |
| 1938 |
return mPicture.beginRecording(width, height); |
| 1939 |
} |
| 1940 |
|
| 1941 |
@Override |
| 1942 |
public Bitmap createBitmap() { |
| 1943 |
mPicture.endRecording(); |
| 1944 |
return Bitmap.createBitmap(mPicture); |
| 1945 |
} |
| 1946 |
} |
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
public interface CanvasProvider { |
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
Canvas getCanvas(View view, int width, int height); |
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
Bitmap createBitmap(); |
| 1963 |
} |
| 1964 |
} |