Java tutorial
/* * This file is part of Android-Showoff created by Pierre Laporte. * * Android-Showoff is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package fr.pingtimeout; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; public class ConnectionUtils { private final static String loggerName = ConnectionUtils.class.getName(); public static boolean isOnWifi(Context context) { Log.d(loggerName, "Checking if connection is Wifi"); ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiNetworkInfo.isConnected()) { Log.d(loggerName, "Wifi is connected"); return true; } else { Log.d(loggerName, "Wifi is not connected"); return false; } } public static boolean pingServer(String serverHostname) { String pingUrlAsString = String.format("http://%s:8080/ping", serverHostname); Log.d(loggerName, "Pinging server " + pingUrlAsString); BufferedReader bufferedReader = null; try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(new URI(pingUrlAsString)); HttpResponse response = client.execute(request); bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); while (bufferedReader.readLine() != null) { } } catch (Exception e) { e.printStackTrace(); return false; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException ignored) { } } } return true; } public static boolean pressLeftOnServer(String serverHostname) { String pingUrlAsString = String.format("http://%s:8080/left", serverHostname); Log.d(loggerName, "Triggering a LEFT keystroke on server using " + pingUrlAsString); BufferedReader bufferedReader = null; try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(new URI(pingUrlAsString)); HttpResponse response = client.execute(request); bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); while (bufferedReader.readLine() != null) { } } catch (Exception e) { e.printStackTrace(); return false; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException ignored) { } } } return true; } public static boolean pressRightOnServer(String serverHostname) { String pingUrlAsString = String.format("http://%s:8080/right", serverHostname); Log.d(loggerName, "Triggering a RIGHT keystroke on server using " + pingUrlAsString); BufferedReader bufferedReader = null; try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(new URI(pingUrlAsString)); HttpResponse response = client.execute(request); bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); while (bufferedReader.readLine() != null) { } } catch (Exception e) { e.printStackTrace(); return false; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException ignored) { } } } return true; } }