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 
17 package com.android.hierarchyviewer.scene;
18 
19 import com.android.ddmlib.IDevice;
20 import com.android.hierarchyviewer.device.Window;
21 import com.android.hierarchyviewer.device.DeviceBridge;
22 
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 import java.io.OutputStreamWriter;
26 import java.net.InetSocketAddress;
27 import java.net.Socket;
28 
29 public class ViewManager {
invalidate(IDevice device, Window window, String params)30     public static void invalidate(IDevice device, Window window, String params) {
31         sendCommand("INVALIDATE", device, window, params);
32     }
33 
requestLayout(IDevice device, Window window, String params)34     public static void requestLayout(IDevice device, Window window, String params) {
35         sendCommand("REQUEST_LAYOUT", device, window, params);
36     }
37 
outputDisplayList(IDevice device, Window window, String params)38     public static void outputDisplayList(IDevice device, Window window, String params) {
39         sendCommand("OUTPUT_DISPLAYLIST", device, window, params);
40     }
41 
sendCommand(String command, IDevice device, Window window, String params)42     private static void sendCommand(String command, IDevice device, Window window, String params) {
43         Socket socket = null;
44         BufferedWriter out = null;
45 
46         try {
47             socket = new Socket();
48             socket.connect(new InetSocketAddress("127.0.0.1",
49                     DeviceBridge.getDeviceLocalPort(device)));
50 
51             out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
52 
53             out.write(command + " " + window.encode() + " " + params);
54             out.newLine();
55             out.flush();
56         } catch (IOException e) {
57             // Empty
58         } finally {
59             try {
60                 if (out != null) {
61                     out.close();
62                 }
63                 if (socket != null) {
64                     socket.close();
65                 }
66             } catch (IOException ex) {
67                 ex.printStackTrace();
68             }
69         }
70     }
71 }
72