Adding timeout support for httpPing

optional param timeout which user can either pass or it defaults
to 50 secs, which is less than sl4a rpc timeout of 60 secs

Test: Yes, locally
Bug: 115916707
Change-Id: I99c8a3944821a7e47f79f435cd4b6828a2533692
diff --git a/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java b/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java
index 6f173b5..0d3d887 100644
--- a/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java
@@ -96,13 +96,19 @@
      * Send an http request and get the response.
      *
      * @param url The url to send request to.
+     * @param timeout Time to load the page
      * @return The HttpURLConnection object.
      */
-    private HttpURLConnection httpRequest(String url) throws IOException {
+    private HttpURLConnection httpRequest(String url, Integer timeout) throws IOException {
+        if (timeout == null) {
+            timeout = 50000;
+        }
         URL targetURL = new URL(url);
         HttpURLConnection urlConnection;
         try {
             urlConnection = (HttpURLConnection) targetURL.openConnection();
+            urlConnection.setConnectTimeout(9000);
+            urlConnection.setReadTimeout(timeout);
             urlConnection.connect();
             int respCode = urlConnection.getResponseCode();
             String respMsg = urlConnection.getResponseMessage();
@@ -132,7 +138,7 @@
     public void httpDownloadFile(@RpcParameter(name = "url") String url,
             @RpcParameter(name="outPath") @RpcOptional String outPath) throws IOException {
         // Create the input stream
-        HttpURLConnection urlConnection = httpRequest(url);
+        HttpURLConnection urlConnection = httpRequest(url, null);
         // Parse destination path and create the output stream. The function assumes that the path
         // is specified relative to the system default Download dir.
         File outFile = FileUtils.getExternalDownload();
@@ -179,10 +185,12 @@
     }
 
     @Rpc(description = "Make an http request and return the response message.")
-    public HttpURLConnection httpPing(@RpcParameter(name = "url") String url) throws IOException {
+    public HttpURLConnection httpPing(
+            @RpcParameter(name = "url") String url,
+            @RpcParameter(name = "timeout") @RpcOptional Integer timeout) throws IOException {
         try {
             HttpURLConnection urlConnection = null;
-            urlConnection = httpRequest(url);
+            urlConnection = httpRequest(url, timeout);
             urlConnection.disconnect();
             return urlConnection;
         } catch (UnknownHostException e) {
@@ -192,7 +200,7 @@
 
     @Rpc(description = "Make an http request and return the response content as a string.")
     public String httpRequestString(@RpcParameter(name = "url") String url) throws IOException {
-        HttpURLConnection urlConnection = httpRequest(url);
+        HttpURLConnection urlConnection = httpRequest(url, null);
         InputStream in = new BufferedInputStream(urlConnection.getInputStream());
         String result = inputStreamToString(in);
         Log.d("Fetched: " + result);