• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
sendCommand(String command, IDevice device, Window window, String params)38     private static void sendCommand(String command, IDevice device, Window window, String params) {
39         Socket socket = null;
40         BufferedWriter out = null;
41 
42         try {
43             socket = new Socket();
44             socket.connect(new InetSocketAddress("127.0.0.1",
45                     DeviceBridge.getDeviceLocalPort(device)));
46 
47             out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
48 
49             out.write(command + " " + window.encode() + " " + params);
50             out.newLine();
51             out.flush();
52         } catch (IOException e) {
53             // Empty
54         } finally {
55             try {
56                 if (out != null) {
57                     out.close();
58                 }
59                 if (socket != null) {
60                     socket.close();
61                 }
62             } catch (IOException ex) {
63                 ex.printStackTrace();
64             }
65         }
66     }
67 }
68