From 8390291ef57f66762af079bec951145dfcfd1b27 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Mon, 14 Dec 2009 18:54:11 -0800 Subject: [PATCH] Handle version 3 of the View server. --- .../hierarchyviewer/scene/VersionLoader.java | 79 ++++++++++++++++++++++ .../hierarchyviewer/scene/WindowsLoader.java | 17 +++-- .../com/android/hierarchyviewer/ui/Workspace.java | 27 ++++++-- 3 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 hierarchyviewer/src/com/android/hierarchyviewer/scene/VersionLoader.java diff --git a/hierarchyviewer/src/com/android/hierarchyviewer/scene/VersionLoader.java b/hierarchyviewer/src/com/android/hierarchyviewer/scene/VersionLoader.java new file mode 100644 index 000000000..ccc187d9b --- /dev/null +++ b/hierarchyviewer/src/com/android/hierarchyviewer/scene/VersionLoader.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package com.android.hierarchyviewer.scene; + +import com.android.ddmlib.IDevice; +import com.android.hierarchyviewer.device.DeviceBridge; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.InetSocketAddress; +import java.net.Socket; + +public class VersionLoader { + public static int loadServerVersion(IDevice device) { + return loadVersion(device, "SERVER"); + } + + public static int loadProtocolVersion(IDevice device) { + return loadVersion(device, "PROTOCOL"); + } + + private static int loadVersion(IDevice device, String command) { + Socket socket = null; + BufferedReader in = null; + BufferedWriter out = null; + + try { + socket = new Socket(); + socket.connect(new InetSocketAddress("127.0.0.1", + DeviceBridge.getDeviceLocalPort(device))); + + out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); + in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + + out.write(command); + out.newLine(); + out.flush(); + + return Integer.parseInt(in.readLine()); + } catch (Exception e) { + // Empty + } finally { + try { + if (out != null) { + out.close(); + } + if (in != null) { + in.close(); + } + if (socket != null) { + socket.close(); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + + // Versioning of the protocol and server was added with version 2 + return 2; + } +} diff --git a/hierarchyviewer/src/com/android/hierarchyviewer/scene/WindowsLoader.java b/hierarchyviewer/src/com/android/hierarchyviewer/scene/WindowsLoader.java index d06aca692..75f179f88 100644 --- a/hierarchyviewer/src/com/android/hierarchyviewer/scene/WindowsLoader.java +++ b/hierarchyviewer/src/com/android/hierarchyviewer/scene/WindowsLoader.java @@ -30,11 +30,12 @@ import java.net.Socket; import java.util.ArrayList; public class WindowsLoader { - public static Window[] loadWindows(IDevice device) { + public static Window[] loadWindows(IDevice device, int protocol, int server) { Socket socket = null; BufferedReader in = null; BufferedWriter out = null; - + System.out.println("protocol = " + protocol); + System.out.println("version = " + server); try { ArrayList windows = new ArrayList(); @@ -57,8 +58,16 @@ public class WindowsLoader { int index = line.indexOf(' '); if (index != -1) { - Window w = new Window(line.substring(index + 1), - (int) Long.parseLong(line.substring(0, index), 16)); + String windowId = line.substring(0, index); + + int id; + if (server > 2) { + id = (int) Long.parseLong(windowId, 16); + } else { + id = Integer.parseInt(windowId, 16); + } + + Window w = new Window(line.substring(index + 1), id); windows.add(w); } } diff --git a/hierarchyviewer/src/com/android/hierarchyviewer/ui/Workspace.java b/hierarchyviewer/src/com/android/hierarchyviewer/ui/Workspace.java index 1361243a5..56864961e 100644 --- a/hierarchyviewer/src/com/android/hierarchyviewer/ui/Workspace.java +++ b/hierarchyviewer/src/com/android/hierarchyviewer/ui/Workspace.java @@ -22,6 +22,7 @@ import com.android.hierarchyviewer.device.DeviceBridge; import com.android.hierarchyviewer.device.Window; import com.android.hierarchyviewer.laf.UnifiedContentBorder; import com.android.hierarchyviewer.scene.CaptureLoader; +import com.android.hierarchyviewer.scene.VersionLoader; import com.android.hierarchyviewer.scene.ViewHierarchyLoader; import com.android.hierarchyviewer.scene.ViewHierarchyScene; import com.android.hierarchyviewer.scene.ViewManager; @@ -172,6 +173,9 @@ public class Workspace extends JFrame { private JTextField filterText; private JLabel filterLabel; + private int protocolVersion; + private int serverVersion; + public Workspace() { super("Hierarchy Viewer"); @@ -1105,23 +1109,38 @@ public class Workspace extends JFrame { } } } + + static class WindowsResult { + Window[] windows; + int serverVersion; + int protocolVersion; + } - private class LoadWindowsTask extends SwingWorker { + private class LoadWindowsTask extends SwingWorker { private LoadWindowsTask() { beginTask(); } @Override @WorkerThread - protected Window[] doInBackground() throws Exception { - return WindowsLoader.loadWindows(currentDevice); + protected WindowsResult doInBackground() throws Exception { + WindowsResult r = new WindowsResult(); + r.protocolVersion = VersionLoader.loadProtocolVersion(currentDevice); + r.serverVersion = VersionLoader.loadServerVersion(currentDevice); + r.windows = WindowsLoader.loadWindows(currentDevice, + r.protocolVersion, r.serverVersion); + return r; } @Override protected void done() { try { + WindowsResult result = get(); + protocolVersion = result.protocolVersion; + serverVersion = result.serverVersion; + windowsTableModel.clear(); - windowsTableModel.addWindows(get()); + windowsTableModel.addWindows(result.windows); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { -- 2.11.0