1 /* 2 * Copyright (C) 2009 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.net.Socket; 24 import java.net.InetSocketAddress; 25 import java.io.BufferedWriter; 26 import java.io.OutputStreamWriter; 27 import java.io.IOException; 28 import java.io.BufferedReader; 29 import java.io.InputStreamReader; 30 31 public class ProfilesLoader { loadProfiles(IDevice device, Window window, String params)32 public static double[] loadProfiles(IDevice device, Window window, String params) { 33 Socket socket = null; 34 BufferedReader in = null; 35 BufferedWriter out = null; 36 37 try { 38 socket = new Socket(); 39 socket.connect(new InetSocketAddress("127.0.0.1", 40 DeviceBridge.getDeviceLocalPort(device))); 41 42 out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 43 in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 44 45 out.write("PROFILE " + window.encode() + " " + params); 46 out.newLine(); 47 out.flush(); 48 49 String response = in.readLine(); 50 String[] data = response.split(" "); 51 52 double[] profiles = new double[data.length]; 53 for (int i = 0; i < data.length; i++) { 54 profiles[i] = (Long.parseLong(data[i]) / 1000.0) / 1000.0; // convert to ms 55 } 56 return profiles; 57 } catch (IOException e) { 58 // Empty 59 } finally { 60 try { 61 if (out != null) { 62 out.close(); 63 } 64 if (in != null) { 65 in.close(); 66 } 67 if (socket != null) { 68 socket.close(); 69 } 70 } catch (IOException ex) { 71 ex.printStackTrace(); 72 } 73 } 74 75 return null; 76 } 77 } 78