1/*
2 * Copyright (C) 2008 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
17package com.android.hierarchyviewer.scene;
18
19import com.android.ddmlib.IDevice;
20import com.android.hierarchyviewer.device.DeviceBridge;
21import com.android.hierarchyviewer.device.Window;
22
23import java.io.BufferedReader;
24import java.io.BufferedWriter;
25import java.io.IOException;
26import java.io.InputStreamReader;
27import java.io.OutputStreamWriter;
28import java.net.InetSocketAddress;
29import java.net.Socket;
30import java.util.ArrayList;
31
32public class WindowsLoader {
33    public static Window[] loadWindows(IDevice device, int protocol, int server) {
34        Socket socket = null;
35        BufferedReader in = null;
36        BufferedWriter out = null;
37        System.out.println("protocol = " + protocol);
38        System.out.println("version = " + server);
39        try {
40            ArrayList<Window> windows = new ArrayList<Window>();
41
42            socket = new Socket();
43            socket.connect(new InetSocketAddress("127.0.0.1",
44                    DeviceBridge.getDeviceLocalPort(device)));
45
46            out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
47            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
48
49            out.write("LIST");
50            out.newLine();
51            out.flush();
52
53            String line;
54            while ((line = in.readLine()) != null) {
55                if ("DONE.".equalsIgnoreCase(line)) {
56                    break;
57                }
58
59                int index = line.indexOf(' ');
60                if (index != -1) {
61                    String windowId = line.substring(0, index);
62
63                    int id;
64                    if (server > 2) {
65                        id = (int) Long.parseLong(windowId, 16);
66                    } else {
67                        id = Integer.parseInt(windowId, 16);
68                    }
69
70                    Window w = new Window(line.substring(index + 1), id);
71                    windows.add(w);
72                }
73            }
74
75            return windows.toArray(new Window[windows.size()]);
76        } catch (IOException e) {
77            // Empty
78        } finally {
79            try {
80                if (out != null) {
81                    out.close();
82                }
83                if (in != null) {
84                    in.close();
85                }
86                if (socket != null) {
87                    socket.close();
88                }
89            } catch (IOException ex) {
90                ex.printStackTrace();
91            }
92        }
93
94        return new Window[0];
95    }
96}
97