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.HierarchyViewer; 21 import com.android.hierarchyviewer.device.Window; 22 import com.android.hierarchyviewer.device.DeviceBridge; 23 24 import java.net.Socket; 25 import java.net.InetSocketAddress; 26 import java.io.BufferedWriter; 27 import java.io.OutputStreamWriter; 28 import java.io.IOException; 29 import java.io.BufferedReader; 30 import java.io.InputStreamReader; 31 32 public class ProfilesLoader { 33 public static double[] loadProfiles(IDevice device, Window window, String params) { 34 if (!HierarchyViewer.isProfilingEnabled()) { 35 return new double[] { 0.0, 0.0, 0.0 }; 36 } 37 38 Socket socket = null; 39 BufferedReader in = 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 in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 49 50 out.write("PROFILE " + window.encode() + " " + params); 51 out.newLine(); 52 out.flush(); 53 54 String response = in.readLine(); 55 String[] data = response.split(" "); 56 57 double[] profiles = new double[data.length]; 58 for (int i = 0; i < data.length; i++) { 59 profiles[i] = (Long.parseLong(data[i]) / 1000.0) / 1000.0; // convert to ms 60 } 61 return profiles; 62 } catch (IOException e) { 63 // Empty 64 } finally { 65 try { 66 if (out != null) { 67 out.close(); 68 } 69 if (in != null) { 70 in.close(); 71 } 72 if (socket != null) { 73 socket.close(); 74 } 75 } catch (IOException ex) { 76 ex.printStackTrace(); 77 } 78 } 79 80 return null; 81 } 82 } 83