Home | History | Annotate | Download | only in scene
      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 
     18 package com.android.hierarchyviewer.scene;
     19 
     20 import com.android.ddmlib.IDevice;
     21 import com.android.hierarchyviewer.device.DeviceBridge;
     22 
     23 import java.io.BufferedReader;
     24 import java.io.BufferedWriter;
     25 import java.io.IOException;
     26 import java.io.InputStreamReader;
     27 import java.io.OutputStreamWriter;
     28 import java.net.InetSocketAddress;
     29 import java.net.Socket;
     30 
     31 public class VersionLoader {
     32     public static int loadServerVersion(IDevice device) {
     33         return loadVersion(device, "SERVER");
     34     }
     35 
     36     public static int loadProtocolVersion(IDevice device) {
     37         return loadVersion(device, "PROTOCOL");
     38     }
     39 
     40     private static int loadVersion(IDevice device, String command) {
     41         Socket socket = null;
     42         BufferedReader in = null;
     43         BufferedWriter out = null;
     44 
     45         try {
     46             socket = new Socket();
     47             socket.connect(new InetSocketAddress("127.0.0.1",
     48                     DeviceBridge.getDeviceLocalPort(device)));
     49 
     50             out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
     51             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     52 
     53             out.write(command);
     54             out.newLine();
     55             out.flush();
     56 
     57             return Integer.parseInt(in.readLine());
     58         } catch (Exception e) {
     59             // Empty
     60         } finally {
     61             try {
     62                 if (out != null) {
     63                     out.close();
     64                 }
     65                 if (in != null) {
     66                     in.close();
     67                 }
     68                 if (socket != null) {
     69                     socket.close();
     70                 }
     71             } catch (IOException ex) {
     72                 ex.printStackTrace();
     73             }
     74         }
     75 
     76         // Versioning of the protocol and server was added with version 2
     77         return 2;
     78     }
     79 }
     80