diff options
| author | Siva Velusamy <vsiva@google.com> | 2013-01-17 18:01:52 -0800 |
|---|---|---|
| committer | Siva Velusamy <vsiva@google.com> | 2013-01-22 17:25:46 -0800 |
| commit | f9455fafb690f23cee9cc9a59bfb68f31e695990 (patch) | |
| tree | 3b33a5764c20b71ed2b48476f20fd6b177a80d58 /core/java/android/view/ViewDebug.java | |
| parent | 0f8d155363c361199a9d9aa5dcdbc4088990f893 (diff) | |
Support invoking view methods from hierarchy viewer
This CL adds support for invoking any view method with
primtive arguments, and setting layout parameters from
the debugger (hierarchy viewer).
requestLayout() and invalidate() are now just implemented
using the more generic invokeMethod() command.
Change-Id: Icffda251728a4963b35266786b0b6143bae7fe8e
Diffstat (limited to 'core/java/android/view/ViewDebug.java')
| -rw-r--r-- | core/java/android/view/ViewDebug.java | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/core/java/android/view/ViewDebug.java b/core/java/android/view/ViewDebug.java index 6e2826881b1d..987ff7857c32 100644 --- a/core/java/android/view/ViewDebug.java +++ b/core/java/android/view/ViewDebug.java @@ -45,6 +45,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; /** * Various debugging/tracing tools related to {@link View} and the view hierarchy. @@ -1374,4 +1375,68 @@ public class ViewDebug { sb.append(capturedViewExportMethods(view, klass, "")); Log.d(tag, sb.toString()); } + + /** + * Invoke a particular method on given view. + * The given method is always invoked on the UI thread. The caller thread will stall until the + * method invocation is complete. Returns an object equal to the result of the method + * invocation, null if the method is declared to return void + * @throws Exception if the method invocation caused any exception + * @hide + */ + public static Object invokeViewMethod(final View view, final Method method, + final Object[] args) { + final CountDownLatch latch = new CountDownLatch(1); + final AtomicReference<Object> result = new AtomicReference<Object>(); + final AtomicReference<Throwable> exception = new AtomicReference<Throwable>(); + + view.post(new Runnable() { + @Override + public void run() { + try { + result.set(method.invoke(view, args)); + } catch (InvocationTargetException e) { + exception.set(e.getCause()); + } catch (Exception e) { + exception.set(e); + } + + latch.countDown(); + } + }); + + try { + latch.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + if (exception.get() != null) { + throw new RuntimeException(exception.get()); + } + + return result.get(); + } + + /** + * @hide + */ + public static void setLayoutParameter(final View view, final String param, final int value) + throws NoSuchFieldException, IllegalAccessException { + final ViewGroup.LayoutParams p = view.getLayoutParams(); + final Field f = p.getClass().getField(param); + if (f.getType() != int.class) { + throw new RuntimeException("Only integer layout parameters can be set. Field " + + param + " is of type " + f.getType().getSimpleName()); + } + + f.set(p, Integer.valueOf(value)); + + view.post(new Runnable() { + @Override + public void run() { + view.setLayoutParams(p); + } + }); + } } |
