The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
Romain Guy | e551dc7 | 2009-07-28 15:30:11 -0700 | [diff] [blame] | 19 | import android.util.Config; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.util.Log; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 21 | import android.util.DisplayMetrics; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.res.Resources; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 23 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 25 | import android.graphics.Canvas; |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 26 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.os.Environment; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 28 | import android.os.Debug; |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 29 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 31 | import java.io.ByteArrayOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import java.io.File; |
| 33 | import java.io.BufferedWriter; |
| 34 | import java.io.FileWriter; |
| 35 | import java.io.IOException; |
| 36 | import java.io.FileOutputStream; |
| 37 | import java.io.DataOutputStream; |
| 38 | import java.io.OutputStreamWriter; |
| 39 | import java.io.BufferedOutputStream; |
| 40 | import java.io.OutputStream; |
| 41 | import java.util.List; |
| 42 | import java.util.LinkedList; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.HashMap; |
| 45 | import java.util.concurrent.CountDownLatch; |
| 46 | import java.util.concurrent.TimeUnit; |
| 47 | import java.lang.annotation.Target; |
| 48 | import java.lang.annotation.ElementType; |
| 49 | import java.lang.annotation.Retention; |
| 50 | import java.lang.annotation.RetentionPolicy; |
| 51 | import java.lang.reflect.Field; |
| 52 | import java.lang.reflect.Method; |
| 53 | import java.lang.reflect.InvocationTargetException; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 54 | import java.lang.reflect.AccessibleObject; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Various debugging/tracing tools related to {@link View} and the view hierarchy. |
| 58 | */ |
| 59 | public class ViewDebug { |
| 60 | /** |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 61 | * Log tag used to log errors related to the consistency of the view hierarchy. |
| 62 | * |
| 63 | * @hide |
| 64 | */ |
| 65 | public static final String CONSISTENCY_LOG_TAG = "ViewConsistency"; |
| 66 | |
| 67 | /** |
| 68 | * Flag indicating the consistency check should check layout-related properties. |
| 69 | * |
| 70 | * @hide |
| 71 | */ |
| 72 | public static final int CONSISTENCY_LAYOUT = 0x1; |
| 73 | |
| 74 | /** |
| 75 | * Flag indicating the consistency check should check drawing-related properties. |
| 76 | * |
| 77 | * @hide |
| 78 | */ |
| 79 | public static final int CONSISTENCY_DRAWING = 0x2; |
| 80 | |
| 81 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | * Enables or disables view hierarchy tracing. Any invoker of |
| 83 | * {@link #trace(View, android.view.ViewDebug.HierarchyTraceType)} should first |
| 84 | * check that this value is set to true as not to affect performance. |
| 85 | */ |
| 86 | public static final boolean TRACE_HIERARCHY = false; |
| 87 | |
| 88 | /** |
| 89 | * Enables or disables view recycler tracing. Any invoker of |
| 90 | * {@link #trace(View, android.view.ViewDebug.RecyclerTraceType, int[])} should first |
| 91 | * check that this value is set to true as not to affect performance. |
| 92 | */ |
| 93 | public static final boolean TRACE_RECYCLER = false; |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | /** |
Romain Guy | cf635ae | 2010-01-22 11:00:29 -0800 | [diff] [blame] | 96 | * Enables or disables motion events tracing. Any invoker of |
| 97 | * {@link #trace(View, MotionEvent, MotionEventTraceType)} should first check |
| 98 | * that this value is set to true as not to affect performance. |
| 99 | * |
| 100 | * @hide |
| 101 | */ |
| 102 | public static final boolean TRACE_MOTION_EVENTS = false; |
| 103 | |
| 104 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | * The system property of dynamic switch for capturing view information |
| 106 | * when it is set, we dump interested fields and methods for the view on focus |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 107 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | static final String SYSTEM_PROPERTY_CAPTURE_VIEW = "debug.captureview"; |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | /** |
| 111 | * The system property of dynamic switch for capturing event information |
| 112 | * when it is set, we log key events, touch/motion and trackball events |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 113 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | static final String SYSTEM_PROPERTY_CAPTURE_EVENT = "debug.captureevent"; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | /** |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 117 | * Profiles drawing times in the events log. |
| 118 | * |
| 119 | * @hide |
| 120 | */ |
Romain Guy | 5429e1d | 2010-09-07 12:38:00 -0700 | [diff] [blame] | 121 | public static final boolean DEBUG_PROFILE_DRAWING = false; |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Profiles layout times in the events log. |
| 125 | * |
| 126 | * @hide |
| 127 | */ |
Romain Guy | 5429e1d | 2010-09-07 12:38:00 -0700 | [diff] [blame] | 128 | public static final boolean DEBUG_PROFILE_LAYOUT = false; |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Profiles real fps (times between draws) and displays the result. |
| 132 | * |
| 133 | * @hide |
| 134 | */ |
Romain Guy | 5429e1d | 2010-09-07 12:38:00 -0700 | [diff] [blame] | 135 | public static final boolean DEBUG_SHOW_FPS = false; |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 136 | |
| 137 | /** |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 138 | * Enables detailed logging of drag/drop operations. |
| 139 | * @hide |
| 140 | */ |
| 141 | public static final boolean DEBUG_DRAG = true; |
| 142 | |
| 143 | /** |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 144 | * <p>Enables or disables views consistency check. Even when this property is enabled, |
| 145 | * view consistency checks happen only if {@link android.util.Config#DEBUG} is set |
| 146 | * to true. The value of this property can be configured externally in one of the |
| 147 | * following files:</p> |
| 148 | * <ul> |
| 149 | * <li>/system/debug.prop</li> |
| 150 | * <li>/debug.prop</li> |
| 151 | * <li>/data/debug.prop</li> |
| 152 | * </ul> |
| 153 | * @hide |
| 154 | */ |
| 155 | @Debug.DebugProperty |
| 156 | public static boolean consistencyCheckEnabled = false; |
| 157 | |
| 158 | static { |
Romain Guy | e551dc7 | 2009-07-28 15:30:11 -0700 | [diff] [blame] | 159 | if (Config.DEBUG) { |
| 160 | Debug.setFieldsOn(ViewDebug.class, true); |
| 161 | } |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | * This annotation can be used to mark fields and methods to be dumped by |
| 166 | * the view server. Only non-void methods with no arguments can be annotated |
| 167 | * by this annotation. |
| 168 | */ |
| 169 | @Target({ ElementType.FIELD, ElementType.METHOD }) |
| 170 | @Retention(RetentionPolicy.RUNTIME) |
| 171 | public @interface ExportedProperty { |
| 172 | /** |
| 173 | * When resolveId is true, and if the annotated field/method return value |
| 174 | * is an int, the value is converted to an Android's resource name. |
| 175 | * |
| 176 | * @return true if the property's value must be transformed into an Android |
| 177 | * resource name, false otherwise |
| 178 | */ |
| 179 | boolean resolveId() default false; |
| 180 | |
| 181 | /** |
| 182 | * A mapping can be defined to map int values to specific strings. For |
| 183 | * instance, View.getVisibility() returns 0, 4 or 8. However, these values |
| 184 | * actually mean VISIBLE, INVISIBLE and GONE. A mapping can be used to see |
| 185 | * these human readable values: |
| 186 | * |
| 187 | * <pre> |
| 188 | * @ViewDebug.ExportedProperty(mapping = { |
| 189 | * @ViewDebug.IntToString(from = 0, to = "VISIBLE"), |
| 190 | * @ViewDebug.IntToString(from = 4, to = "INVISIBLE"), |
| 191 | * @ViewDebug.IntToString(from = 8, to = "GONE") |
| 192 | * }) |
| 193 | * public int getVisibility() { ... |
| 194 | * <pre> |
| 195 | * |
| 196 | * @return An array of int to String mappings |
| 197 | * |
| 198 | * @see android.view.ViewDebug.IntToString |
| 199 | */ |
| 200 | IntToString[] mapping() default { }; |
| 201 | |
| 202 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 203 | * A mapping can be defined to map array indices to specific strings. |
| 204 | * A mapping can be used to see human readable values for the indices |
| 205 | * of an array: |
| 206 | * |
| 207 | * <pre> |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 208 | * @ViewDebug.ExportedProperty(indexMapping = { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 209 | * @ViewDebug.IntToString(from = 0, to = "INVALID"), |
| 210 | * @ViewDebug.IntToString(from = 1, to = "FIRST"), |
| 211 | * @ViewDebug.IntToString(from = 2, to = "SECOND") |
| 212 | * }) |
| 213 | * private int[] mElements; |
| 214 | * <pre> |
| 215 | * |
| 216 | * @return An array of int to String mappings |
| 217 | * |
| 218 | * @see android.view.ViewDebug.IntToString |
| 219 | * @see #mapping() |
| 220 | */ |
| 221 | IntToString[] indexMapping() default { }; |
| 222 | |
| 223 | /** |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 224 | * A flags mapping can be defined to map flags encoded in an integer to |
| 225 | * specific strings. A mapping can be used to see human readable values |
| 226 | * for the flags of an integer: |
| 227 | * |
| 228 | * <pre> |
| 229 | * @ViewDebug.ExportedProperty(flagMapping = { |
| 230 | * @ViewDebug.FlagToString(mask = ENABLED_MASK, equals = ENABLED, name = "ENABLED"), |
| 231 | * @ViewDebug.FlagToString(mask = ENABLED_MASK, equals = DISABLED, name = "DISABLED"), |
| 232 | * }) |
| 233 | * private int mFlags; |
| 234 | * <pre> |
| 235 | * |
| 236 | * A specified String is output when the following is true: |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 237 | * |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 238 | * @return An array of int to String mappings |
| 239 | */ |
| 240 | FlagToString[] flagMapping() default { }; |
| 241 | |
| 242 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | * When deep export is turned on, this property is not dumped. Instead, the |
| 244 | * properties contained in this property are dumped. Each child property |
| 245 | * is prefixed with the name of this property. |
| 246 | * |
| 247 | * @return true if the properties of this property should be dumped |
| 248 | * |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 249 | * @see #prefix() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | */ |
| 251 | boolean deepExport() default false; |
| 252 | |
| 253 | /** |
| 254 | * The prefix to use on child properties when deep export is enabled |
| 255 | * |
| 256 | * @return a prefix as a String |
| 257 | * |
| 258 | * @see #deepExport() |
| 259 | */ |
| 260 | String prefix() default ""; |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 261 | |
| 262 | /** |
| 263 | * Specifies the category the property falls into, such as measurement, |
| 264 | * layout, drawing, etc. |
| 265 | * |
| 266 | * @return the category as String |
| 267 | */ |
| 268 | String category() default ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Defines a mapping from an int value to a String. Such a mapping can be used |
| 273 | * in a @ExportedProperty to provide more meaningful values to the end user. |
| 274 | * |
| 275 | * @see android.view.ViewDebug.ExportedProperty |
| 276 | */ |
| 277 | @Target({ ElementType.TYPE }) |
| 278 | @Retention(RetentionPolicy.RUNTIME) |
| 279 | public @interface IntToString { |
| 280 | /** |
| 281 | * The original int value to map to a String. |
| 282 | * |
| 283 | * @return An arbitrary int value. |
| 284 | */ |
| 285 | int from(); |
| 286 | |
| 287 | /** |
| 288 | * The String to use in place of the original int value. |
| 289 | * |
| 290 | * @return An arbitrary non-null String. |
| 291 | */ |
| 292 | String to(); |
| 293 | } |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Defines a mapping from an flag to a String. Such a mapping can be used |
| 297 | * in a @ExportedProperty to provide more meaningful values to the end user. |
| 298 | * |
| 299 | * @see android.view.ViewDebug.ExportedProperty |
| 300 | */ |
| 301 | @Target({ ElementType.TYPE }) |
| 302 | @Retention(RetentionPolicy.RUNTIME) |
| 303 | public @interface FlagToString { |
| 304 | /** |
| 305 | * The mask to apply to the original value. |
| 306 | * |
| 307 | * @return An arbitrary int value. |
| 308 | */ |
| 309 | int mask(); |
| 310 | |
| 311 | /** |
| 312 | * The value to compare to the result of: |
| 313 | * <code>original value & {@link #mask()}</code>. |
| 314 | * |
| 315 | * @return An arbitrary value. |
| 316 | */ |
| 317 | int equals(); |
| 318 | |
| 319 | /** |
| 320 | * The String to use in place of the original int value. |
| 321 | * |
| 322 | * @return An arbitrary non-null String. |
| 323 | */ |
| 324 | String name(); |
| 325 | |
| 326 | /** |
| 327 | * Indicates whether to output the flag when the test is true, |
| 328 | * or false. Defaults to true. |
| 329 | */ |
| 330 | boolean outputIf() default true; |
| 331 | } |
| 332 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | /** |
| 334 | * This annotation can be used to mark fields and methods to be dumped when |
| 335 | * the view is captured. Methods with this annotation must have no arguments |
Andy Stadler | f8a7cea | 2009-04-10 16:24:47 -0700 | [diff] [blame] | 336 | * and must return a valid type of data. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | */ |
| 338 | @Target({ ElementType.FIELD, ElementType.METHOD }) |
| 339 | @Retention(RetentionPolicy.RUNTIME) |
| 340 | public @interface CapturedViewProperty { |
| 341 | /** |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 342 | * When retrieveReturn is true, we need to retrieve second level methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | * e.g., we need myView.getFirstLevelMethod().getSecondLevelMethod() |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 344 | * we will set retrieveReturn = true on the annotation of |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | * myView.getFirstLevelMethod() |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 346 | * @return true if we need the second level methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | */ |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 348 | boolean retrieveReturn() default false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | } |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | private static HashMap<Class<?>, Method[]> mCapturedViewMethodsForClasses = null; |
| 352 | private static HashMap<Class<?>, Field[]> mCapturedViewFieldsForClasses = null; |
| 353 | |
| 354 | // Maximum delay in ms after which we stop trying to capture a View's drawing |
| 355 | private static final int CAPTURE_TIMEOUT = 4000; |
| 356 | |
| 357 | private static final String REMOTE_COMMAND_CAPTURE = "CAPTURE"; |
| 358 | private static final String REMOTE_COMMAND_DUMP = "DUMP"; |
| 359 | private static final String REMOTE_COMMAND_INVALIDATE = "INVALIDATE"; |
| 360 | private static final String REMOTE_COMMAND_REQUEST_LAYOUT = "REQUEST_LAYOUT"; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 361 | private static final String REMOTE_PROFILE = "PROFILE"; |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 362 | private static final String REMOTE_COMMAND_CAPTURE_LAYERS = "CAPTURE_LAYERS"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | |
| 364 | private static HashMap<Class<?>, Field[]> sFieldsForClasses; |
| 365 | private static HashMap<Class<?>, Method[]> sMethodsForClasses; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 366 | private static HashMap<AccessibleObject, ExportedProperty> sAnnotations; |
| 367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | /** |
| 369 | * Defines the type of hierarhcy trace to output to the hierarchy traces file. |
| 370 | */ |
| 371 | public enum HierarchyTraceType { |
| 372 | INVALIDATE, |
| 373 | INVALIDATE_CHILD, |
| 374 | INVALIDATE_CHILD_IN_PARENT, |
| 375 | REQUEST_LAYOUT, |
| 376 | ON_LAYOUT, |
| 377 | ON_MEASURE, |
| 378 | DRAW, |
| 379 | BUILD_CACHE |
| 380 | } |
| 381 | |
| 382 | private static BufferedWriter sHierarchyTraces; |
| 383 | private static ViewRoot sHierarhcyRoot; |
| 384 | private static String sHierarchyTracePrefix; |
| 385 | |
| 386 | /** |
| 387 | * Defines the type of recycler trace to output to the recycler traces file. |
| 388 | */ |
| 389 | public enum RecyclerTraceType { |
| 390 | NEW_VIEW, |
| 391 | BIND_VIEW, |
| 392 | RECYCLE_FROM_ACTIVE_HEAP, |
| 393 | RECYCLE_FROM_SCRAP_HEAP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | MOVE_TO_SCRAP_HEAP, |
| 395 | MOVE_FROM_ACTIVE_TO_SCRAP_HEAP |
| 396 | } |
| 397 | |
| 398 | private static class RecyclerTrace { |
| 399 | public int view; |
| 400 | public RecyclerTraceType type; |
| 401 | public int position; |
| 402 | public int indexOnScreen; |
| 403 | } |
| 404 | |
| 405 | private static View sRecyclerOwnerView; |
| 406 | private static List<View> sRecyclerViews; |
| 407 | private static List<RecyclerTrace> sRecyclerTraces; |
| 408 | private static String sRecyclerTracePrefix; |
| 409 | |
| 410 | /** |
Romain Guy | cf635ae | 2010-01-22 11:00:29 -0800 | [diff] [blame] | 411 | * Defines the type of motion events trace to output to the motion events traces file. |
| 412 | * |
| 413 | * @hide |
| 414 | */ |
| 415 | public enum MotionEventTraceType { |
| 416 | DISPATCH, |
| 417 | ON_INTERCEPT, |
| 418 | ON_TOUCH |
| 419 | } |
| 420 | |
| 421 | private static BufferedWriter sMotionEventTraces; |
| 422 | private static ViewRoot sMotionEventRoot; |
| 423 | private static String sMotionEventTracePrefix; |
| 424 | |
| 425 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | * Returns the number of instanciated Views. |
| 427 | * |
| 428 | * @return The number of Views instanciated in the current process. |
| 429 | * |
| 430 | * @hide |
| 431 | */ |
| 432 | public static long getViewInstanceCount() { |
Brian Carlstrom | c21550a | 2010-10-05 21:34:06 -0700 | [diff] [blame] | 433 | return Debug.countInstancesOfClass(View.class); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Returns the number of instanciated ViewRoots. |
| 438 | * |
| 439 | * @return The number of ViewRoots instanciated in the current process. |
| 440 | * |
| 441 | * @hide |
| 442 | */ |
| 443 | public static long getViewRootInstanceCount() { |
Brian Carlstrom | c21550a | 2010-10-05 21:34:06 -0700 | [diff] [blame] | 444 | return Debug.countInstancesOfClass(ViewRoot.class); |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 445 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | |
| 447 | /** |
| 448 | * Outputs a trace to the currently opened recycler traces. The trace records the type of |
| 449 | * recycler action performed on the supplied view as well as a number of parameters. |
| 450 | * |
| 451 | * @param view the view to trace |
| 452 | * @param type the type of the trace |
| 453 | * @param parameters parameters depending on the type of the trace |
| 454 | */ |
| 455 | public static void trace(View view, RecyclerTraceType type, int... parameters) { |
| 456 | if (sRecyclerOwnerView == null || sRecyclerViews == null) { |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | if (!sRecyclerViews.contains(view)) { |
| 461 | sRecyclerViews.add(view); |
| 462 | } |
| 463 | |
| 464 | final int index = sRecyclerViews.indexOf(view); |
| 465 | |
| 466 | RecyclerTrace trace = new RecyclerTrace(); |
| 467 | trace.view = index; |
| 468 | trace.type = type; |
| 469 | trace.position = parameters[0]; |
| 470 | trace.indexOnScreen = parameters[1]; |
| 471 | |
| 472 | sRecyclerTraces.add(trace); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Starts tracing the view recycler of the specified view. The trace is identified by a prefix, |
| 477 | * used to build the traces files names: <code>/EXTERNAL/view-recycler/PREFIX.traces</code> and |
| 478 | * <code>/EXTERNAL/view-recycler/PREFIX.recycler</code>. |
| 479 | * |
| 480 | * Only one view recycler can be traced at the same time. After calling this method, any |
| 481 | * other invocation will result in a <code>IllegalStateException</code> unless |
| 482 | * {@link #stopRecyclerTracing()} is invoked before. |
| 483 | * |
| 484 | * Traces files are created only after {@link #stopRecyclerTracing()} is invoked. |
| 485 | * |
| 486 | * This method will return immediately if TRACE_RECYCLER is false. |
| 487 | * |
| 488 | * @param prefix the traces files name prefix |
| 489 | * @param view the view whose recycler must be traced |
| 490 | * |
| 491 | * @see #stopRecyclerTracing() |
| 492 | * @see #trace(View, android.view.ViewDebug.RecyclerTraceType, int[]) |
| 493 | */ |
| 494 | public static void startRecyclerTracing(String prefix, View view) { |
| 495 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 496 | if (!TRACE_RECYCLER) { |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | if (sRecyclerOwnerView != null) { |
| 501 | throw new IllegalStateException("You must call stopRecyclerTracing() before running" + |
| 502 | " a new trace!"); |
| 503 | } |
| 504 | |
| 505 | sRecyclerTracePrefix = prefix; |
| 506 | sRecyclerOwnerView = view; |
| 507 | sRecyclerViews = new ArrayList<View>(); |
| 508 | sRecyclerTraces = new LinkedList<RecyclerTrace>(); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Stops the current view recycer tracing. |
| 513 | * |
| 514 | * Calling this method creates the file <code>/EXTERNAL/view-recycler/PREFIX.traces</code> |
| 515 | * containing all the traces (or method calls) relative to the specified view's recycler. |
| 516 | * |
| 517 | * Calling this method creates the file <code>/EXTERNAL/view-recycler/PREFIX.recycler</code> |
| 518 | * containing all of the views used by the recycler of the view supplied to |
| 519 | * {@link #startRecyclerTracing(String, View)}. |
| 520 | * |
| 521 | * This method will return immediately if TRACE_RECYCLER is false. |
| 522 | * |
| 523 | * @see #startRecyclerTracing(String, View) |
| 524 | * @see #trace(View, android.view.ViewDebug.RecyclerTraceType, int[]) |
| 525 | */ |
| 526 | public static void stopRecyclerTracing() { |
| 527 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 528 | if (!TRACE_RECYCLER) { |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | if (sRecyclerOwnerView == null || sRecyclerViews == null) { |
| 533 | throw new IllegalStateException("You must call startRecyclerTracing() before" + |
| 534 | " stopRecyclerTracing()!"); |
| 535 | } |
| 536 | |
| 537 | File recyclerDump = new File(Environment.getExternalStorageDirectory(), "view-recycler/"); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 538 | //noinspection ResultOfMethodCallIgnored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | recyclerDump.mkdirs(); |
| 540 | |
| 541 | recyclerDump = new File(recyclerDump, sRecyclerTracePrefix + ".recycler"); |
| 542 | try { |
| 543 | final BufferedWriter out = new BufferedWriter(new FileWriter(recyclerDump), 8 * 1024); |
| 544 | |
| 545 | for (View view : sRecyclerViews) { |
| 546 | final String name = view.getClass().getName(); |
| 547 | out.write(name); |
| 548 | out.newLine(); |
| 549 | } |
| 550 | |
| 551 | out.close(); |
| 552 | } catch (IOException e) { |
| 553 | Log.e("View", "Could not dump recycler content"); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | recyclerDump = new File(Environment.getExternalStorageDirectory(), "view-recycler/"); |
| 558 | recyclerDump = new File(recyclerDump, sRecyclerTracePrefix + ".traces"); |
| 559 | try { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 560 | if (recyclerDump.exists()) { |
| 561 | recyclerDump.delete(); |
| 562 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | final FileOutputStream file = new FileOutputStream(recyclerDump); |
| 564 | final DataOutputStream out = new DataOutputStream(file); |
| 565 | |
| 566 | for (RecyclerTrace trace : sRecyclerTraces) { |
| 567 | out.writeInt(trace.view); |
| 568 | out.writeInt(trace.type.ordinal()); |
| 569 | out.writeInt(trace.position); |
| 570 | out.writeInt(trace.indexOnScreen); |
| 571 | out.flush(); |
| 572 | } |
| 573 | |
| 574 | out.close(); |
| 575 | } catch (IOException e) { |
| 576 | Log.e("View", "Could not dump recycler traces"); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | sRecyclerViews.clear(); |
| 581 | sRecyclerViews = null; |
| 582 | |
| 583 | sRecyclerTraces.clear(); |
| 584 | sRecyclerTraces = null; |
| 585 | |
| 586 | sRecyclerOwnerView = null; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Outputs a trace to the currently opened traces file. The trace contains the class name |
| 591 | * and instance's hashcode of the specified view as well as the supplied trace type. |
| 592 | * |
| 593 | * @param view the view to trace |
| 594 | * @param type the type of the trace |
| 595 | */ |
| 596 | public static void trace(View view, HierarchyTraceType type) { |
| 597 | if (sHierarchyTraces == null) { |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | try { |
| 602 | sHierarchyTraces.write(type.name()); |
| 603 | sHierarchyTraces.write(' '); |
| 604 | sHierarchyTraces.write(view.getClass().getName()); |
| 605 | sHierarchyTraces.write('@'); |
| 606 | sHierarchyTraces.write(Integer.toHexString(view.hashCode())); |
| 607 | sHierarchyTraces.newLine(); |
| 608 | } catch (IOException e) { |
| 609 | Log.w("View", "Error while dumping trace of type " + type + " for view " + view); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Starts tracing the view hierarchy of the specified view. The trace is identified by a prefix, |
| 615 | * used to build the traces files names: <code>/EXTERNAL/view-hierarchy/PREFIX.traces</code> and |
| 616 | * <code>/EXTERNAL/view-hierarchy/PREFIX.tree</code>. |
| 617 | * |
| 618 | * Only one view hierarchy can be traced at the same time. After calling this method, any |
| 619 | * other invocation will result in a <code>IllegalStateException</code> unless |
| 620 | * {@link #stopHierarchyTracing()} is invoked before. |
| 621 | * |
| 622 | * Calling this method creates the file <code>/EXTERNAL/view-hierarchy/PREFIX.traces</code> |
| 623 | * containing all the traces (or method calls) relative to the specified view's hierarchy. |
| 624 | * |
| 625 | * This method will return immediately if TRACE_HIERARCHY is false. |
| 626 | * |
| 627 | * @param prefix the traces files name prefix |
| 628 | * @param view the view whose hierarchy must be traced |
| 629 | * |
| 630 | * @see #stopHierarchyTracing() |
| 631 | * @see #trace(View, android.view.ViewDebug.HierarchyTraceType) |
| 632 | */ |
| 633 | public static void startHierarchyTracing(String prefix, View view) { |
| 634 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 635 | if (!TRACE_HIERARCHY) { |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | if (sHierarhcyRoot != null) { |
| 640 | throw new IllegalStateException("You must call stopHierarchyTracing() before running" + |
| 641 | " a new trace!"); |
| 642 | } |
| 643 | |
| 644 | File hierarchyDump = new File(Environment.getExternalStorageDirectory(), "view-hierarchy/"); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 645 | //noinspection ResultOfMethodCallIgnored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | hierarchyDump.mkdirs(); |
| 647 | |
| 648 | hierarchyDump = new File(hierarchyDump, prefix + ".traces"); |
| 649 | sHierarchyTracePrefix = prefix; |
| 650 | |
| 651 | try { |
| 652 | sHierarchyTraces = new BufferedWriter(new FileWriter(hierarchyDump), 8 * 1024); |
| 653 | } catch (IOException e) { |
| 654 | Log.e("View", "Could not dump view hierarchy"); |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | sHierarhcyRoot = (ViewRoot) view.getRootView().getParent(); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Stops the current view hierarchy tracing. This method closes the file |
| 663 | * <code>/EXTERNAL/view-hierarchy/PREFIX.traces</code>. |
| 664 | * |
| 665 | * Calling this method creates the file <code>/EXTERNAL/view-hierarchy/PREFIX.tree</code> |
| 666 | * containing the view hierarchy of the view supplied to |
| 667 | * {@link #startHierarchyTracing(String, View)}. |
| 668 | * |
| 669 | * This method will return immediately if TRACE_HIERARCHY is false. |
| 670 | * |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 671 | * @see #startHierarchyTracing(String, View) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | * @see #trace(View, android.view.ViewDebug.HierarchyTraceType) |
| 673 | */ |
| 674 | public static void stopHierarchyTracing() { |
| 675 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 676 | if (!TRACE_HIERARCHY) { |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | if (sHierarhcyRoot == null || sHierarchyTraces == null) { |
| 681 | throw new IllegalStateException("You must call startHierarchyTracing() before" + |
| 682 | " stopHierarchyTracing()!"); |
| 683 | } |
| 684 | |
| 685 | try { |
| 686 | sHierarchyTraces.close(); |
| 687 | } catch (IOException e) { |
| 688 | Log.e("View", "Could not write view traces"); |
| 689 | } |
| 690 | sHierarchyTraces = null; |
| 691 | |
| 692 | File hierarchyDump = new File(Environment.getExternalStorageDirectory(), "view-hierarchy/"); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 693 | //noinspection ResultOfMethodCallIgnored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | hierarchyDump.mkdirs(); |
| 695 | hierarchyDump = new File(hierarchyDump, sHierarchyTracePrefix + ".tree"); |
| 696 | |
| 697 | BufferedWriter out; |
| 698 | try { |
| 699 | out = new BufferedWriter(new FileWriter(hierarchyDump), 8 * 1024); |
| 700 | } catch (IOException e) { |
| 701 | Log.e("View", "Could not dump view hierarchy"); |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | View view = sHierarhcyRoot.getView(); |
| 706 | if (view instanceof ViewGroup) { |
| 707 | ViewGroup group = (ViewGroup) view; |
| 708 | dumpViewHierarchy(group, out, 0); |
| 709 | try { |
| 710 | out.close(); |
| 711 | } catch (IOException e) { |
| 712 | Log.e("View", "Could not dump view hierarchy"); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | sHierarhcyRoot = null; |
| 717 | } |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 718 | |
Romain Guy | cf635ae | 2010-01-22 11:00:29 -0800 | [diff] [blame] | 719 | /** |
| 720 | * Outputs a trace to the currently opened traces file. The trace contains the class name |
| 721 | * and instance's hashcode of the specified view as well as the supplied trace type. |
| 722 | * |
| 723 | * @param view the view to trace |
| 724 | * @param event the event of the trace |
| 725 | * @param type the type of the trace |
| 726 | * |
| 727 | * @hide |
| 728 | */ |
| 729 | public static void trace(View view, MotionEvent event, MotionEventTraceType type) { |
| 730 | if (sMotionEventTraces == null) { |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | try { |
| 735 | sMotionEventTraces.write(type.name()); |
| 736 | sMotionEventTraces.write(' '); |
| 737 | sMotionEventTraces.write(event.getAction()); |
| 738 | sMotionEventTraces.write(' '); |
| 739 | sMotionEventTraces.write(view.getClass().getName()); |
| 740 | sMotionEventTraces.write('@'); |
| 741 | sMotionEventTraces.write(Integer.toHexString(view.hashCode())); |
| 742 | sHierarchyTraces.newLine(); |
| 743 | } catch (IOException e) { |
| 744 | Log.w("View", "Error while dumping trace of event " + event + " for view " + view); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * Starts tracing the motion events for the hierarchy of the specificy view. |
| 750 | * The trace is identified by a prefix, used to build the traces files names: |
| 751 | * <code>/EXTERNAL/motion-events/PREFIX.traces</code> and |
| 752 | * <code>/EXTERNAL/motion-events/PREFIX.tree</code>. |
| 753 | * |
| 754 | * Only one view hierarchy can be traced at the same time. After calling this method, any |
| 755 | * other invocation will result in a <code>IllegalStateException</code> unless |
| 756 | * {@link #stopMotionEventTracing()} is invoked before. |
| 757 | * |
| 758 | * Calling this method creates the file <code>/EXTERNAL/motion-events/PREFIX.traces</code> |
| 759 | * containing all the traces (or method calls) relative to the specified view's hierarchy. |
| 760 | * |
| 761 | * This method will return immediately if TRACE_HIERARCHY is false. |
| 762 | * |
| 763 | * @param prefix the traces files name prefix |
| 764 | * @param view the view whose hierarchy must be traced |
| 765 | * |
| 766 | * @see #stopMotionEventTracing() |
| 767 | * @see #trace(View, MotionEvent, android.view.ViewDebug.MotionEventTraceType) |
| 768 | * |
| 769 | * @hide |
| 770 | */ |
| 771 | public static void startMotionEventTracing(String prefix, View view) { |
| 772 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 773 | if (!TRACE_MOTION_EVENTS) { |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | if (sMotionEventRoot != null) { |
| 778 | throw new IllegalStateException("You must call stopMotionEventTracing() before running" + |
| 779 | " a new trace!"); |
| 780 | } |
| 781 | |
| 782 | File hierarchyDump = new File(Environment.getExternalStorageDirectory(), "motion-events/"); |
| 783 | //noinspection ResultOfMethodCallIgnored |
| 784 | hierarchyDump.mkdirs(); |
| 785 | |
| 786 | hierarchyDump = new File(hierarchyDump, prefix + ".traces"); |
| 787 | sMotionEventTracePrefix = prefix; |
| 788 | |
| 789 | try { |
| 790 | sMotionEventTraces = new BufferedWriter(new FileWriter(hierarchyDump), 32 * 1024); |
| 791 | } catch (IOException e) { |
| 792 | Log.e("View", "Could not dump view hierarchy"); |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | sMotionEventRoot = (ViewRoot) view.getRootView().getParent(); |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Stops the current motion events tracing. This method closes the file |
| 801 | * <code>/EXTERNAL/motion-events/PREFIX.traces</code>. |
| 802 | * |
| 803 | * Calling this method creates the file <code>/EXTERNAL/motion-events/PREFIX.tree</code> |
| 804 | * containing the view hierarchy of the view supplied to |
| 805 | * {@link #startMotionEventTracing(String, View)}. |
| 806 | * |
| 807 | * This method will return immediately if TRACE_HIERARCHY is false. |
| 808 | * |
| 809 | * @see #startMotionEventTracing(String, View) |
| 810 | * @see #trace(View, MotionEvent, android.view.ViewDebug.MotionEventTraceType) |
| 811 | * |
| 812 | * @hide |
| 813 | */ |
| 814 | public static void stopMotionEventTracing() { |
| 815 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 816 | if (!TRACE_MOTION_EVENTS) { |
| 817 | return; |
| 818 | } |
| 819 | |
| 820 | if (sMotionEventRoot == null || sMotionEventTraces == null) { |
| 821 | throw new IllegalStateException("You must call startMotionEventTracing() before" + |
| 822 | " stopMotionEventTracing()!"); |
| 823 | } |
| 824 | |
| 825 | try { |
| 826 | sMotionEventTraces.close(); |
| 827 | } catch (IOException e) { |
| 828 | Log.e("View", "Could not write view traces"); |
| 829 | } |
| 830 | sMotionEventTraces = null; |
| 831 | |
| 832 | File hierarchyDump = new File(Environment.getExternalStorageDirectory(), "motion-events/"); |
| 833 | //noinspection ResultOfMethodCallIgnored |
| 834 | hierarchyDump.mkdirs(); |
| 835 | hierarchyDump = new File(hierarchyDump, sMotionEventTracePrefix + ".tree"); |
| 836 | |
| 837 | BufferedWriter out; |
| 838 | try { |
| 839 | out = new BufferedWriter(new FileWriter(hierarchyDump), 8 * 1024); |
| 840 | } catch (IOException e) { |
| 841 | Log.e("View", "Could not dump view hierarchy"); |
| 842 | return; |
| 843 | } |
| 844 | |
| 845 | View view = sMotionEventRoot.getView(); |
| 846 | if (view instanceof ViewGroup) { |
| 847 | ViewGroup group = (ViewGroup) view; |
| 848 | dumpViewHierarchy(group, out, 0); |
| 849 | try { |
| 850 | out.close(); |
| 851 | } catch (IOException e) { |
| 852 | Log.e("View", "Could not dump view hierarchy"); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | sHierarhcyRoot = null; |
| 857 | } |
| 858 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | static void dispatchCommand(View view, String command, String parameters, |
| 860 | OutputStream clientStream) throws IOException { |
| 861 | |
| 862 | // Paranoid but safe... |
| 863 | view = view.getRootView(); |
| 864 | |
| 865 | if (REMOTE_COMMAND_DUMP.equalsIgnoreCase(command)) { |
| 866 | dump(view, clientStream); |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 867 | } else if (REMOTE_COMMAND_CAPTURE_LAYERS.equalsIgnoreCase(command)) { |
| 868 | captureLayers(view, new DataOutputStream(clientStream)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | } else { |
| 870 | final String[] params = parameters.split(" "); |
| 871 | if (REMOTE_COMMAND_CAPTURE.equalsIgnoreCase(command)) { |
| 872 | capture(view, clientStream, params[0]); |
| 873 | } else if (REMOTE_COMMAND_INVALIDATE.equalsIgnoreCase(command)) { |
| 874 | invalidate(view, params[0]); |
| 875 | } else if (REMOTE_COMMAND_REQUEST_LAYOUT.equalsIgnoreCase(command)) { |
| 876 | requestLayout(view, params[0]); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 877 | } else if (REMOTE_PROFILE.equalsIgnoreCase(command)) { |
| 878 | profile(view, clientStream, params[0]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 883 | private static View findView(View root, String parameter) { |
| 884 | // Look by type/hashcode |
| 885 | if (parameter.indexOf('@') != -1) { |
| 886 | final String[] ids = parameter.split("@"); |
| 887 | final String className = ids[0]; |
Romain Guy | 236092a | 2009-12-14 15:31:48 -0800 | [diff] [blame] | 888 | final int hashCode = (int) Long.parseLong(ids[1], 16); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 890 | View view = root.getRootView(); |
| 891 | if (view instanceof ViewGroup) { |
| 892 | return findView((ViewGroup) view, className, hashCode); |
| 893 | } |
| 894 | } else { |
| 895 | // Look by id |
| 896 | final int id = root.getResources().getIdentifier(parameter, null, null); |
| 897 | return root.getRootView().findViewById(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | return null; |
| 901 | } |
| 902 | |
| 903 | private static void invalidate(View root, String parameter) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 904 | final View view = findView(root, parameter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | if (view != null) { |
| 906 | view.postInvalidate(); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | private static void requestLayout(View root, String parameter) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 911 | final View view = findView(root, parameter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 912 | if (view != null) { |
| 913 | root.post(new Runnable() { |
| 914 | public void run() { |
| 915 | view.requestLayout(); |
| 916 | } |
| 917 | }); |
| 918 | } |
| 919 | } |
| 920 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 921 | private static void profile(View root, OutputStream clientStream, String parameter) |
| 922 | throws IOException { |
| 923 | |
| 924 | final View view = findView(root, parameter); |
| 925 | BufferedWriter out = null; |
| 926 | try { |
| 927 | out = new BufferedWriter(new OutputStreamWriter(clientStream), 32 * 1024); |
| 928 | |
| 929 | if (view != null) { |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 930 | profileViewAndChildren(view, out); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 931 | } else { |
| 932 | out.write("-1 -1 -1"); |
| 933 | out.newLine(); |
| 934 | } |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 935 | out.write("DONE."); |
| 936 | out.newLine(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 937 | } catch (Exception e) { |
| 938 | android.util.Log.w("View", "Problem profiling the view:", e); |
| 939 | } finally { |
| 940 | if (out != null) { |
| 941 | out.close(); |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 946 | private static void profileViewAndChildren(final View view, BufferedWriter out) |
| 947 | throws IOException { |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 948 | profileViewAndChildren(view, out, true); |
| 949 | } |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 950 | |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 951 | private static void profileViewAndChildren(final View view, BufferedWriter out, boolean root) |
| 952 | throws IOException { |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 953 | |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 954 | long durationMeasure = |
| 955 | (root || (view.mPrivateFlags & View.MEASURED_DIMENSION_SET) != 0) ? profileViewOperation( |
| 956 | view, new ViewOperation<Void>() { |
| 957 | public Void[] pre() { |
| 958 | forceLayout(view); |
| 959 | return null; |
| 960 | } |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 961 | |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 962 | private void forceLayout(View view) { |
| 963 | view.forceLayout(); |
| 964 | if (view instanceof ViewGroup) { |
| 965 | ViewGroup group = (ViewGroup) view; |
| 966 | final int count = group.getChildCount(); |
| 967 | for (int i = 0; i < count; i++) { |
| 968 | forceLayout(group.getChildAt(i)); |
| 969 | } |
| 970 | } |
| 971 | } |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 972 | |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 973 | public void run(Void... data) { |
| 974 | view.measure(view.mOldWidthMeasureSpec, view.mOldHeightMeasureSpec); |
| 975 | } |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 976 | |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 977 | public void post(Void... data) { |
| 978 | } |
| 979 | }) |
| 980 | : 0; |
| 981 | long durationLayout = |
| 982 | (root || (view.mPrivateFlags & View.LAYOUT_REQUIRED) != 0) ? profileViewOperation( |
| 983 | view, new ViewOperation<Void>() { |
| 984 | public Void[] pre() { |
| 985 | return null; |
| 986 | } |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 987 | |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 988 | public void run(Void... data) { |
| 989 | view.layout(view.mLeft, view.mTop, view.mRight, view.mBottom); |
| 990 | } |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 991 | |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 992 | public void post(Void... data) { |
| 993 | } |
| 994 | }) : 0; |
| 995 | long durationDraw = |
Konstantin Lopyrev | bef337f | 2010-08-18 11:31:27 -0700 | [diff] [blame] | 996 | (root || !view.willNotDraw() || (view.mPrivateFlags & View.DRAWN) != 0) ? profileViewOperation( |
| 997 | view, |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 998 | new ViewOperation<Object>() { |
| 999 | public Object[] pre() { |
| 1000 | final DisplayMetrics metrics = |
| 1001 | view.getResources().getDisplayMetrics(); |
| 1002 | final Bitmap bitmap = |
| 1003 | Bitmap.createBitmap(metrics.widthPixels, |
| 1004 | metrics.heightPixels, Bitmap.Config.RGB_565); |
| 1005 | final Canvas canvas = new Canvas(bitmap); |
| 1006 | return new Object[] { |
| 1007 | bitmap, canvas |
| 1008 | }; |
| 1009 | } |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 1010 | |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 1011 | public void run(Object... data) { |
| 1012 | view.draw((Canvas) data[1]); |
| 1013 | } |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 1014 | |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 1015 | public void post(Object... data) { |
| 1016 | ((Bitmap) data[0]).recycle(); |
| 1017 | } |
| 1018 | }) : 0; |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 1019 | out.write(String.valueOf(durationMeasure)); |
| 1020 | out.write(' '); |
| 1021 | out.write(String.valueOf(durationLayout)); |
| 1022 | out.write(' '); |
| 1023 | out.write(String.valueOf(durationDraw)); |
| 1024 | out.newLine(); |
| 1025 | if (view instanceof ViewGroup) { |
| 1026 | ViewGroup group = (ViewGroup) view; |
| 1027 | final int count = group.getChildCount(); |
| 1028 | for (int i = 0; i < count; i++) { |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 1029 | profileViewAndChildren(group.getChildAt(i), out, false); |
Konstantin Lopyrev | f8e1219 | 2010-08-02 19:08:56 -0700 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1034 | interface ViewOperation<T> { |
| 1035 | T[] pre(); |
| 1036 | void run(T... data); |
| 1037 | void post(T... data); |
| 1038 | } |
| 1039 | |
| 1040 | private static <T> long profileViewOperation(View view, final ViewOperation<T> operation) { |
| 1041 | final CountDownLatch latch = new CountDownLatch(1); |
| 1042 | final long[] duration = new long[1]; |
| 1043 | |
| 1044 | view.post(new Runnable() { |
| 1045 | public void run() { |
| 1046 | try { |
| 1047 | T[] data = operation.pre(); |
| 1048 | long start = Debug.threadCpuTimeNanos(); |
| 1049 | operation.run(data); |
| 1050 | duration[0] = Debug.threadCpuTimeNanos() - start; |
| 1051 | operation.post(data); |
| 1052 | } finally { |
| 1053 | latch.countDown(); |
| 1054 | } |
| 1055 | } |
| 1056 | }); |
| 1057 | |
| 1058 | try { |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 1059 | if (!latch.await(CAPTURE_TIMEOUT, TimeUnit.MILLISECONDS)) { |
| 1060 | Log.w("View", "Could not complete the profiling of the view " + view); |
| 1061 | return -1; |
| 1062 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1063 | } catch (InterruptedException e) { |
| 1064 | Log.w("View", "Could not complete the profiling of the view " + view); |
| 1065 | Thread.currentThread().interrupt(); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | |
| 1069 | return duration[0]; |
| 1070 | } |
| 1071 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1072 | private static void captureLayers(View root, final DataOutputStream clientStream) |
| 1073 | throws IOException { |
| 1074 | |
| 1075 | try { |
| 1076 | Rect outRect = new Rect(); |
| 1077 | try { |
| 1078 | root.mAttachInfo.mSession.getDisplayFrame(root.mAttachInfo.mWindow, outRect); |
| 1079 | } catch (RemoteException e) { |
| 1080 | // Ignore |
| 1081 | } |
| 1082 | |
| 1083 | clientStream.writeInt(outRect.width()); |
| 1084 | clientStream.writeInt(outRect.height()); |
| 1085 | |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 1086 | captureViewLayer(root, clientStream, true); |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1087 | |
| 1088 | clientStream.write(2); |
| 1089 | } finally { |
| 1090 | clientStream.close(); |
| 1091 | } |
| 1092 | } |
| 1093 | |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 1094 | private static void captureViewLayer(View view, DataOutputStream clientStream, boolean visible) |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1095 | throws IOException { |
| 1096 | |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 1097 | final boolean localVisible = view.getVisibility() == View.VISIBLE && visible; |
| 1098 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1099 | if ((view.mPrivateFlags & View.SKIP_DRAW) != View.SKIP_DRAW) { |
| 1100 | final int id = view.getId(); |
| 1101 | String name = view.getClass().getSimpleName(); |
| 1102 | if (id != View.NO_ID) { |
| 1103 | name = resolveId(view.getContext(), id).toString(); |
| 1104 | } |
| 1105 | |
| 1106 | clientStream.write(1); |
| 1107 | clientStream.writeUTF(name); |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 1108 | clientStream.writeByte(localVisible ? 1 : 0); |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1109 | |
| 1110 | int[] position = new int[2]; |
| 1111 | // XXX: Should happen on the UI thread |
| 1112 | view.getLocationInWindow(position); |
| 1113 | |
| 1114 | clientStream.writeInt(position[0]); |
| 1115 | clientStream.writeInt(position[1]); |
| 1116 | clientStream.flush(); |
| 1117 | |
| 1118 | Bitmap b = performViewCapture(view, true); |
| 1119 | if (b != null) { |
| 1120 | ByteArrayOutputStream arrayOut = new ByteArrayOutputStream(b.getWidth() * |
| 1121 | b.getHeight() * 2); |
| 1122 | b.compress(Bitmap.CompressFormat.PNG, 100, arrayOut); |
| 1123 | clientStream.writeInt(arrayOut.size()); |
| 1124 | arrayOut.writeTo(clientStream); |
| 1125 | } |
| 1126 | clientStream.flush(); |
| 1127 | } |
| 1128 | |
| 1129 | if (view instanceof ViewGroup) { |
| 1130 | ViewGroup group = (ViewGroup) view; |
| 1131 | int count = group.getChildCount(); |
| 1132 | |
| 1133 | for (int i = 0; i < count; i++) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 1134 | captureViewLayer(group.getChildAt(i), clientStream, localVisible); |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1139 | private static void capture(View root, final OutputStream clientStream, String parameter) |
| 1140 | throws IOException { |
| 1141 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1142 | final View captureView = findView(root, parameter); |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1143 | Bitmap b = performViewCapture(captureView, false); |
Konstantin Lopyrev | 43b9b48 | 2010-08-24 22:00:12 -0700 | [diff] [blame] | 1144 | |
| 1145 | if (b == null) { |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1146 | Log.w("View", "Failed to create capture bitmap!"); |
Konstantin Lopyrev | 43b9b48 | 2010-08-24 22:00:12 -0700 | [diff] [blame] | 1147 | // Send an empty one so that it doesn't get stuck waiting for |
| 1148 | // something. |
| 1149 | b = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
| 1150 | } |
| 1151 | |
| 1152 | BufferedOutputStream out = null; |
| 1153 | try { |
| 1154 | out = new BufferedOutputStream(clientStream, 32 * 1024); |
| 1155 | b.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 1156 | out.flush(); |
| 1157 | } finally { |
| 1158 | if (out != null) { |
| 1159 | out.close(); |
| 1160 | } |
| 1161 | b.recycle(); |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1162 | } |
| 1163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1165 | private static Bitmap performViewCapture(final View captureView, final boolean skpiChildren) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | if (captureView != null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1167 | final CountDownLatch latch = new CountDownLatch(1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1168 | final Bitmap[] cache = new Bitmap[1]; |
| 1169 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1170 | captureView.post(new Runnable() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | public void run() { |
| 1172 | try { |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 1173 | cache[0] = captureView.createSnapshot( |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1174 | Bitmap.Config.ARGB_8888, 0, skpiChildren); |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 1175 | } catch (OutOfMemoryError e) { |
| 1176 | try { |
| 1177 | cache[0] = captureView.createSnapshot( |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1178 | Bitmap.Config.ARGB_4444, 0, skpiChildren); |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 1179 | } catch (OutOfMemoryError e2) { |
| 1180 | Log.w("View", "Out of memory for bitmap"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | } finally { |
| 1183 | latch.countDown(); |
| 1184 | } |
| 1185 | } |
| 1186 | }); |
| 1187 | |
| 1188 | try { |
| 1189 | latch.await(CAPTURE_TIMEOUT, TimeUnit.MILLISECONDS); |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1190 | return cache[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1191 | } catch (InterruptedException e) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1192 | Log.w("View", "Could not complete the capture of the view " + captureView); |
| 1193 | Thread.currentThread().interrupt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | } |
| 1195 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1196 | |
| 1197 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | private static void dump(View root, OutputStream clientStream) throws IOException { |
| 1201 | BufferedWriter out = null; |
| 1202 | try { |
Romain Guy | 38e951b | 2009-12-17 13:28:30 -0800 | [diff] [blame] | 1203 | out = new BufferedWriter(new OutputStreamWriter(clientStream, "utf-8"), 32 * 1024); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1204 | View view = root.getRootView(); |
| 1205 | if (view instanceof ViewGroup) { |
| 1206 | ViewGroup group = (ViewGroup) view; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1207 | dumpViewHierarchyWithProperties(group.getContext(), group, out, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | } |
| 1209 | out.write("DONE."); |
| 1210 | out.newLine(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1211 | } catch (Exception e) { |
| 1212 | android.util.Log.w("View", "Problem dumping the view:", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | } finally { |
| 1214 | if (out != null) { |
| 1215 | out.close(); |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | private static View findView(ViewGroup group, String className, int hashCode) { |
| 1221 | if (isRequestedView(group, className, hashCode)) { |
| 1222 | return group; |
| 1223 | } |
| 1224 | |
| 1225 | final int count = group.getChildCount(); |
| 1226 | for (int i = 0; i < count; i++) { |
| 1227 | final View view = group.getChildAt(i); |
| 1228 | if (view instanceof ViewGroup) { |
| 1229 | final View found = findView((ViewGroup) view, className, hashCode); |
| 1230 | if (found != null) { |
| 1231 | return found; |
| 1232 | } |
| 1233 | } else if (isRequestedView(view, className, hashCode)) { |
| 1234 | return view; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | return null; |
| 1239 | } |
| 1240 | |
| 1241 | private static boolean isRequestedView(View view, String className, int hashCode) { |
| 1242 | return view.getClass().getName().equals(className) && view.hashCode() == hashCode; |
| 1243 | } |
| 1244 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1245 | private static void dumpViewHierarchyWithProperties(Context context, ViewGroup group, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1246 | BufferedWriter out, int level) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1247 | if (!dumpViewWithProperties(context, group, out, level)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | return; |
| 1249 | } |
| 1250 | |
| 1251 | final int count = group.getChildCount(); |
| 1252 | for (int i = 0; i < count; i++) { |
| 1253 | final View view = group.getChildAt(i); |
| 1254 | if (view instanceof ViewGroup) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1255 | dumpViewHierarchyWithProperties(context, (ViewGroup) view, out, level + 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | } else { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1257 | dumpViewWithProperties(context, view, out, level + 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1262 | private static boolean dumpViewWithProperties(Context context, View view, |
| 1263 | BufferedWriter out, int level) { |
| 1264 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | try { |
| 1266 | for (int i = 0; i < level; i++) { |
| 1267 | out.write(' '); |
| 1268 | } |
| 1269 | out.write(view.getClass().getName()); |
| 1270 | out.write('@'); |
| 1271 | out.write(Integer.toHexString(view.hashCode())); |
| 1272 | out.write(' '); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1273 | dumpViewProperties(context, view, out); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1274 | out.newLine(); |
| 1275 | } catch (IOException e) { |
| 1276 | Log.w("View", "Error while dumping hierarchy tree"); |
| 1277 | return false; |
| 1278 | } |
| 1279 | return true; |
| 1280 | } |
| 1281 | |
| 1282 | private static Field[] getExportedPropertyFields(Class<?> klass) { |
| 1283 | if (sFieldsForClasses == null) { |
| 1284 | sFieldsForClasses = new HashMap<Class<?>, Field[]>(); |
| 1285 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1286 | if (sAnnotations == null) { |
| 1287 | sAnnotations = new HashMap<AccessibleObject, ExportedProperty>(512); |
| 1288 | } |
| 1289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | final HashMap<Class<?>, Field[]> map = sFieldsForClasses; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1291 | final HashMap<AccessibleObject, ExportedProperty> annotations = sAnnotations; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | |
| 1293 | Field[] fields = map.get(klass); |
| 1294 | if (fields != null) { |
| 1295 | return fields; |
| 1296 | } |
| 1297 | |
| 1298 | final ArrayList<Field> foundFields = new ArrayList<Field>(); |
| 1299 | fields = klass.getDeclaredFields(); |
| 1300 | |
| 1301 | int count = fields.length; |
| 1302 | for (int i = 0; i < count; i++) { |
| 1303 | final Field field = fields[i]; |
| 1304 | if (field.isAnnotationPresent(ExportedProperty.class)) { |
| 1305 | field.setAccessible(true); |
| 1306 | foundFields.add(field); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1307 | annotations.put(field, field.getAnnotation(ExportedProperty.class)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | fields = foundFields.toArray(new Field[foundFields.size()]); |
| 1312 | map.put(klass, fields); |
| 1313 | |
| 1314 | return fields; |
| 1315 | } |
| 1316 | |
| 1317 | private static Method[] getExportedPropertyMethods(Class<?> klass) { |
| 1318 | if (sMethodsForClasses == null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1319 | sMethodsForClasses = new HashMap<Class<?>, Method[]>(100); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1320 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1321 | if (sAnnotations == null) { |
| 1322 | sAnnotations = new HashMap<AccessibleObject, ExportedProperty>(512); |
| 1323 | } |
| 1324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | final HashMap<Class<?>, Method[]> map = sMethodsForClasses; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1326 | final HashMap<AccessibleObject, ExportedProperty> annotations = sAnnotations; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1327 | |
| 1328 | Method[] methods = map.get(klass); |
| 1329 | if (methods != null) { |
| 1330 | return methods; |
| 1331 | } |
| 1332 | |
| 1333 | final ArrayList<Method> foundMethods = new ArrayList<Method>(); |
| 1334 | methods = klass.getDeclaredMethods(); |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1335 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1336 | int count = methods.length; |
| 1337 | for (int i = 0; i < count; i++) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1338 | final Method method = methods[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | if (method.getParameterTypes().length == 0 && |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1340 | method.isAnnotationPresent(ExportedProperty.class) && |
| 1341 | method.getReturnType() != Void.class) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | method.setAccessible(true); |
| 1343 | foundMethods.add(method); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1344 | annotations.put(method, method.getAnnotation(ExportedProperty.class)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | methods = foundMethods.toArray(new Method[foundMethods.size()]); |
| 1349 | map.put(klass, methods); |
| 1350 | |
| 1351 | return methods; |
| 1352 | } |
| 1353 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1354 | private static void dumpViewProperties(Context context, Object view, |
| 1355 | BufferedWriter out) throws IOException { |
| 1356 | |
| 1357 | dumpViewProperties(context, view, out, ""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | } |
| 1359 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1360 | private static void dumpViewProperties(Context context, Object view, |
| 1361 | BufferedWriter out, String prefix) throws IOException { |
| 1362 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | Class<?> klass = view.getClass(); |
| 1364 | |
| 1365 | do { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1366 | exportFields(context, view, out, klass, prefix); |
| 1367 | exportMethods(context, view, out, klass, prefix); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1368 | klass = klass.getSuperclass(); |
| 1369 | } while (klass != Object.class); |
| 1370 | } |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1371 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1372 | private static void exportMethods(Context context, Object view, BufferedWriter out, |
| 1373 | Class<?> klass, String prefix) throws IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1374 | |
| 1375 | final Method[] methods = getExportedPropertyMethods(klass); |
| 1376 | |
| 1377 | int count = methods.length; |
| 1378 | for (int i = 0; i < count; i++) { |
| 1379 | final Method method = methods[i]; |
| 1380 | //noinspection EmptyCatchBlock |
| 1381 | try { |
| 1382 | // TODO: This should happen on the UI thread |
| 1383 | Object methodValue = method.invoke(view, (Object[]) null); |
| 1384 | final Class<?> returnType = method.getReturnType(); |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1385 | final ExportedProperty property = sAnnotations.get(method); |
Konstantin Lopyrev | 91a7f5f | 2010-08-10 18:54:54 -0700 | [diff] [blame] | 1386 | String categoryPrefix = |
| 1387 | property.category().length() != 0 ? property.category() + ":" : ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1388 | |
| 1389 | if (returnType == int.class) { |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1390 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1391 | if (property.resolveId() && context != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | final int id = (Integer) methodValue; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1393 | methodValue = resolveId(context, id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1394 | } else { |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 1395 | final FlagToString[] flagsMapping = property.flagMapping(); |
| 1396 | if (flagsMapping.length > 0) { |
| 1397 | final int intValue = (Integer) methodValue; |
Konstantin Lopyrev | 91a7f5f | 2010-08-10 18:54:54 -0700 | [diff] [blame] | 1398 | final String valuePrefix = |
| 1399 | categoryPrefix + prefix + method.getName() + '_'; |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 1400 | exportUnrolledFlags(out, flagsMapping, intValue, valuePrefix); |
| 1401 | } |
| 1402 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | final IntToString[] mapping = property.mapping(); |
| 1404 | if (mapping.length > 0) { |
| 1405 | final int intValue = (Integer) methodValue; |
| 1406 | boolean mapped = false; |
| 1407 | int mappingCount = mapping.length; |
| 1408 | for (int j = 0; j < mappingCount; j++) { |
| 1409 | final IntToString mapper = mapping[j]; |
| 1410 | if (mapper.from() == intValue) { |
| 1411 | methodValue = mapper.to(); |
| 1412 | mapped = true; |
| 1413 | break; |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | if (!mapped) { |
| 1418 | methodValue = intValue; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1422 | } else if (returnType == int[].class) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1423 | final int[] array = (int[]) methodValue; |
Konstantin Lopyrev | 91a7f5f | 2010-08-10 18:54:54 -0700 | [diff] [blame] | 1424 | final String valuePrefix = categoryPrefix + prefix + method.getName() + '_'; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1425 | final String suffix = "()"; |
| 1426 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1427 | exportUnrolledArray(context, out, property, array, valuePrefix, suffix); |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1428 | |
| 1429 | // Probably want to return here, same as for fields. |
| 1430 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1431 | } else if (!returnType.isPrimitive()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | if (property.deepExport()) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1433 | dumpViewProperties(context, methodValue, out, prefix + property.prefix()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | continue; |
| 1435 | } |
| 1436 | } |
| 1437 | |
Konstantin Lopyrev | 91a7f5f | 2010-08-10 18:54:54 -0700 | [diff] [blame] | 1438 | writeEntry(out, categoryPrefix + prefix, method.getName(), "()", methodValue); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | } catch (IllegalAccessException e) { |
| 1440 | } catch (InvocationTargetException e) { |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1445 | private static void exportFields(Context context, Object view, BufferedWriter out, |
| 1446 | Class<?> klass, String prefix) throws IOException { |
| 1447 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1448 | final Field[] fields = getExportedPropertyFields(klass); |
| 1449 | |
| 1450 | int count = fields.length; |
| 1451 | for (int i = 0; i < count; i++) { |
| 1452 | final Field field = fields[i]; |
| 1453 | |
| 1454 | //noinspection EmptyCatchBlock |
| 1455 | try { |
| 1456 | Object fieldValue = null; |
| 1457 | final Class<?> type = field.getType(); |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1458 | final ExportedProperty property = sAnnotations.get(field); |
Konstantin Lopyrev | 91a7f5f | 2010-08-10 18:54:54 -0700 | [diff] [blame] | 1459 | String categoryPrefix = |
| 1460 | property.category().length() != 0 ? property.category() + ":" : ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1461 | |
| 1462 | if (type == int.class) { |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1463 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1464 | if (property.resolveId() && context != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1465 | final int id = field.getInt(view); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1466 | fieldValue = resolveId(context, id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1467 | } else { |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 1468 | final FlagToString[] flagsMapping = property.flagMapping(); |
| 1469 | if (flagsMapping.length > 0) { |
| 1470 | final int intValue = field.getInt(view); |
Konstantin Lopyrev | 91a7f5f | 2010-08-10 18:54:54 -0700 | [diff] [blame] | 1471 | final String valuePrefix = |
| 1472 | categoryPrefix + prefix + field.getName() + '_'; |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 1473 | exportUnrolledFlags(out, flagsMapping, intValue, valuePrefix); |
| 1474 | } |
| 1475 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1476 | final IntToString[] mapping = property.mapping(); |
| 1477 | if (mapping.length > 0) { |
| 1478 | final int intValue = field.getInt(view); |
| 1479 | int mappingCount = mapping.length; |
| 1480 | for (int j = 0; j < mappingCount; j++) { |
| 1481 | final IntToString mapped = mapping[j]; |
| 1482 | if (mapped.from() == intValue) { |
| 1483 | fieldValue = mapped.to(); |
| 1484 | break; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | if (fieldValue == null) { |
| 1489 | fieldValue = intValue; |
| 1490 | } |
| 1491 | } |
| 1492 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1493 | } else if (type == int[].class) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1494 | final int[] array = (int[]) field.get(view); |
Konstantin Lopyrev | 91a7f5f | 2010-08-10 18:54:54 -0700 | [diff] [blame] | 1495 | final String valuePrefix = categoryPrefix + prefix + field.getName() + '_'; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1496 | final String suffix = ""; |
| 1497 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1498 | exportUnrolledArray(context, out, property, array, valuePrefix, suffix); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1499 | |
| 1500 | // We exit here! |
| 1501 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | } else if (!type.isPrimitive()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | if (property.deepExport()) { |
Konstantin Lopyrev | 91a7f5f | 2010-08-10 18:54:54 -0700 | [diff] [blame] | 1504 | dumpViewProperties(context, field.get(view), out, prefix |
| 1505 | + property.prefix()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1506 | continue; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | if (fieldValue == null) { |
| 1511 | fieldValue = field.get(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1512 | } |
| 1513 | |
Konstantin Lopyrev | 91a7f5f | 2010-08-10 18:54:54 -0700 | [diff] [blame] | 1514 | writeEntry(out, categoryPrefix + prefix, field.getName(), "", fieldValue); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1515 | } catch (IllegalAccessException e) { |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1520 | private static void writeEntry(BufferedWriter out, String prefix, String name, |
| 1521 | String suffix, Object value) throws IOException { |
| 1522 | |
| 1523 | out.write(prefix); |
| 1524 | out.write(name); |
| 1525 | out.write(suffix); |
| 1526 | out.write("="); |
| 1527 | writeValue(out, value); |
| 1528 | out.write(' '); |
| 1529 | } |
| 1530 | |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 1531 | private static void exportUnrolledFlags(BufferedWriter out, FlagToString[] mapping, |
| 1532 | int intValue, String prefix) throws IOException { |
| 1533 | |
| 1534 | final int count = mapping.length; |
| 1535 | for (int j = 0; j < count; j++) { |
| 1536 | final FlagToString flagMapping = mapping[j]; |
| 1537 | final boolean ifTrue = flagMapping.outputIf(); |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 1538 | final int maskResult = intValue & flagMapping.mask(); |
| 1539 | final boolean test = maskResult == flagMapping.equals(); |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 1540 | if ((test && ifTrue) || (!test && !ifTrue)) { |
| 1541 | final String name = flagMapping.name(); |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 1542 | final String value = "0x" + Integer.toHexString(maskResult); |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 1543 | writeEntry(out, prefix, name, "", value); |
| 1544 | } |
| 1545 | } |
| 1546 | } |
| 1547 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1548 | private static void exportUnrolledArray(Context context, BufferedWriter out, |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1549 | ExportedProperty property, int[] array, String prefix, String suffix) |
| 1550 | throws IOException { |
| 1551 | |
| 1552 | final IntToString[] indexMapping = property.indexMapping(); |
| 1553 | final boolean hasIndexMapping = indexMapping.length > 0; |
| 1554 | |
| 1555 | final IntToString[] mapping = property.mapping(); |
| 1556 | final boolean hasMapping = mapping.length > 0; |
| 1557 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1558 | final boolean resolveId = property.resolveId() && context != null; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1559 | final int valuesCount = array.length; |
| 1560 | |
| 1561 | for (int j = 0; j < valuesCount; j++) { |
| 1562 | String name; |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1563 | String value = null; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1564 | |
| 1565 | final int intValue = array[j]; |
| 1566 | |
| 1567 | name = String.valueOf(j); |
| 1568 | if (hasIndexMapping) { |
| 1569 | int mappingCount = indexMapping.length; |
| 1570 | for (int k = 0; k < mappingCount; k++) { |
| 1571 | final IntToString mapped = indexMapping[k]; |
| 1572 | if (mapped.from() == j) { |
| 1573 | name = mapped.to(); |
| 1574 | break; |
| 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1579 | if (hasMapping) { |
| 1580 | int mappingCount = mapping.length; |
| 1581 | for (int k = 0; k < mappingCount; k++) { |
| 1582 | final IntToString mapped = mapping[k]; |
| 1583 | if (mapped.from() == intValue) { |
| 1584 | value = mapped.to(); |
| 1585 | break; |
| 1586 | } |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | if (resolveId) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1591 | if (value == null) value = (String) resolveId(context, intValue); |
| 1592 | } else { |
| 1593 | value = String.valueOf(intValue); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | writeEntry(out, prefix, name, suffix, value); |
| 1597 | } |
| 1598 | } |
| 1599 | |
Romain Guy | 237c1ce | 2009-12-08 11:30:25 -0800 | [diff] [blame] | 1600 | static Object resolveId(Context context, int id) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1601 | Object fieldValue; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1602 | final Resources resources = context.getResources(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1603 | if (id >= 0) { |
| 1604 | try { |
| 1605 | fieldValue = resources.getResourceTypeName(id) + '/' + |
| 1606 | resources.getResourceEntryName(id); |
| 1607 | } catch (Resources.NotFoundException e) { |
| 1608 | fieldValue = "id/0x" + Integer.toHexString(id); |
| 1609 | } |
| 1610 | } else { |
| 1611 | fieldValue = "NO_ID"; |
| 1612 | } |
| 1613 | return fieldValue; |
| 1614 | } |
| 1615 | |
| 1616 | private static void writeValue(BufferedWriter out, Object value) throws IOException { |
| 1617 | if (value != null) { |
| 1618 | String output = value.toString().replace("\n", "\\n"); |
| 1619 | out.write(String.valueOf(output.length())); |
| 1620 | out.write(","); |
| 1621 | out.write(output); |
| 1622 | } else { |
| 1623 | out.write("4,null"); |
| 1624 | } |
| 1625 | } |
| 1626 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1627 | private static void dumpViewHierarchy(ViewGroup group, BufferedWriter out, int level) { |
| 1628 | if (!dumpView(group, out, level)) { |
| 1629 | return; |
| 1630 | } |
| 1631 | |
| 1632 | final int count = group.getChildCount(); |
| 1633 | for (int i = 0; i < count; i++) { |
| 1634 | final View view = group.getChildAt(i); |
| 1635 | if (view instanceof ViewGroup) { |
| 1636 | dumpViewHierarchy((ViewGroup) view, out, level + 1); |
| 1637 | } else { |
| 1638 | dumpView(view, out, level + 1); |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | private static boolean dumpView(Object view, BufferedWriter out, int level) { |
| 1644 | try { |
| 1645 | for (int i = 0; i < level; i++) { |
| 1646 | out.write(' '); |
| 1647 | } |
| 1648 | out.write(view.getClass().getName()); |
| 1649 | out.write('@'); |
| 1650 | out.write(Integer.toHexString(view.hashCode())); |
| 1651 | out.newLine(); |
| 1652 | } catch (IOException e) { |
| 1653 | Log.w("View", "Error while dumping hierarchy tree"); |
| 1654 | return false; |
| 1655 | } |
| 1656 | return true; |
| 1657 | } |
| 1658 | |
| 1659 | private static Field[] capturedViewGetPropertyFields(Class<?> klass) { |
| 1660 | if (mCapturedViewFieldsForClasses == null) { |
| 1661 | mCapturedViewFieldsForClasses = new HashMap<Class<?>, Field[]>(); |
| 1662 | } |
| 1663 | final HashMap<Class<?>, Field[]> map = mCapturedViewFieldsForClasses; |
| 1664 | |
| 1665 | Field[] fields = map.get(klass); |
| 1666 | if (fields != null) { |
| 1667 | return fields; |
| 1668 | } |
| 1669 | |
| 1670 | final ArrayList<Field> foundFields = new ArrayList<Field>(); |
| 1671 | fields = klass.getFields(); |
| 1672 | |
| 1673 | int count = fields.length; |
| 1674 | for (int i = 0; i < count; i++) { |
| 1675 | final Field field = fields[i]; |
| 1676 | if (field.isAnnotationPresent(CapturedViewProperty.class)) { |
| 1677 | field.setAccessible(true); |
| 1678 | foundFields.add(field); |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | fields = foundFields.toArray(new Field[foundFields.size()]); |
| 1683 | map.put(klass, fields); |
| 1684 | |
| 1685 | return fields; |
| 1686 | } |
| 1687 | |
| 1688 | private static Method[] capturedViewGetPropertyMethods(Class<?> klass) { |
| 1689 | if (mCapturedViewMethodsForClasses == null) { |
| 1690 | mCapturedViewMethodsForClasses = new HashMap<Class<?>, Method[]>(); |
| 1691 | } |
| 1692 | final HashMap<Class<?>, Method[]> map = mCapturedViewMethodsForClasses; |
| 1693 | |
| 1694 | Method[] methods = map.get(klass); |
| 1695 | if (methods != null) { |
| 1696 | return methods; |
| 1697 | } |
| 1698 | |
| 1699 | final ArrayList<Method> foundMethods = new ArrayList<Method>(); |
| 1700 | methods = klass.getMethods(); |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1701 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1702 | int count = methods.length; |
| 1703 | for (int i = 0; i < count; i++) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1704 | final Method method = methods[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1705 | if (method.getParameterTypes().length == 0 && |
| 1706 | method.isAnnotationPresent(CapturedViewProperty.class) && |
| 1707 | method.getReturnType() != Void.class) { |
| 1708 | method.setAccessible(true); |
| 1709 | foundMethods.add(method); |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | methods = foundMethods.toArray(new Method[foundMethods.size()]); |
| 1714 | map.put(klass, methods); |
| 1715 | |
| 1716 | return methods; |
| 1717 | } |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1718 | |
| 1719 | private static String capturedViewExportMethods(Object obj, Class<?> klass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | String prefix) { |
| 1721 | |
| 1722 | if (obj == null) { |
| 1723 | return "null"; |
| 1724 | } |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | StringBuilder sb = new StringBuilder(); |
| 1727 | final Method[] methods = capturedViewGetPropertyMethods(klass); |
| 1728 | |
| 1729 | int count = methods.length; |
| 1730 | for (int i = 0; i < count; i++) { |
| 1731 | final Method method = methods[i]; |
| 1732 | try { |
| 1733 | Object methodValue = method.invoke(obj, (Object[]) null); |
| 1734 | final Class<?> returnType = method.getReturnType(); |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1735 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1736 | CapturedViewProperty property = method.getAnnotation(CapturedViewProperty.class); |
| 1737 | if (property.retrieveReturn()) { |
| 1738 | //we are interested in the second level data only |
| 1739 | sb.append(capturedViewExportMethods(methodValue, returnType, method.getName() + "#")); |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1740 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1741 | sb.append(prefix); |
| 1742 | sb.append(method.getName()); |
| 1743 | sb.append("()="); |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1745 | if (methodValue != null) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1746 | final String value = methodValue.toString().replace("\n", "\\n"); |
| 1747 | sb.append(value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1748 | } else { |
| 1749 | sb.append("null"); |
| 1750 | } |
| 1751 | sb.append("; "); |
| 1752 | } |
| 1753 | } catch (IllegalAccessException e) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1754 | //Exception IllegalAccess, it is OK here |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1755 | //we simply ignore this method |
| 1756 | } catch (InvocationTargetException e) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1757 | //Exception InvocationTarget, it is OK here |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1758 | //we simply ignore this method |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1759 | } |
| 1760 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1761 | return sb.toString(); |
| 1762 | } |
| 1763 | |
| 1764 | private static String capturedViewExportFields(Object obj, Class<?> klass, String prefix) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1765 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1766 | if (obj == null) { |
| 1767 | return "null"; |
| 1768 | } |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1769 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1770 | StringBuilder sb = new StringBuilder(); |
| 1771 | final Field[] fields = capturedViewGetPropertyFields(klass); |
| 1772 | |
| 1773 | int count = fields.length; |
| 1774 | for (int i = 0; i < count; i++) { |
| 1775 | final Field field = fields[i]; |
| 1776 | try { |
| 1777 | Object fieldValue = field.get(obj); |
| 1778 | |
| 1779 | sb.append(prefix); |
| 1780 | sb.append(field.getName()); |
| 1781 | sb.append("="); |
| 1782 | |
| 1783 | if (fieldValue != null) { |
| 1784 | final String value = fieldValue.toString().replace("\n", "\\n"); |
| 1785 | sb.append(value); |
| 1786 | } else { |
| 1787 | sb.append("null"); |
| 1788 | } |
| 1789 | sb.append(' '); |
| 1790 | } catch (IllegalAccessException e) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1791 | //Exception IllegalAccess, it is OK here |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | //we simply ignore this field |
| 1793 | } |
| 1794 | } |
| 1795 | return sb.toString(); |
| 1796 | } |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1797 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1798 | /** |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1799 | * Dump view info for id based instrument test generation |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | * (and possibly further data analysis). The results are dumped |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1801 | * to the log. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1802 | * @param tag for log |
| 1803 | * @param view for dump |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1804 | */ |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1805 | public static void dumpCapturedView(String tag, Object view) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1806 | Class<?> klass = view.getClass(); |
| 1807 | StringBuilder sb = new StringBuilder(klass.getName() + ": "); |
| 1808 | sb.append(capturedViewExportFields(view, klass, "")); |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1809 | sb.append(capturedViewExportMethods(view, klass, "")); |
| 1810 | Log.d(tag, sb.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1811 | } |
| 1812 | } |