summaryrefslogtreecommitdiff
path: root/vdrmanager/src/org/fueri/reeldroid
diff options
context:
space:
mode:
authorlado <herrlado@gmail.com>2013-03-20 10:55:58 +0100
committerlado <herrlado@gmail.com>2013-03-20 10:55:58 +0100
commitf467c0600198c2bfba4be0eecd4709b58b4211b3 (patch)
tree958bd845415a19aee06f204fd32d1bd8efeeb4a2 /vdrmanager/src/org/fueri/reeldroid
parentcae390b45e2977def1d593009e76854ed76e281a (diff)
downloadvdr-manager-f467c0600198c2bfba4be0eecd4709b58b4211b3.tar.gz
vdr-manager-f467c0600198c2bfba4be0eecd4709b58b4211b3.tar.bz2
Feature #1304
Diffstat (limited to 'vdrmanager/src/org/fueri/reeldroid')
-rw-r--r--vdrmanager/src/org/fueri/reeldroid/network/DeviceManager.java148
1 files changed, 148 insertions, 0 deletions
diff --git a/vdrmanager/src/org/fueri/reeldroid/network/DeviceManager.java b/vdrmanager/src/org/fueri/reeldroid/network/DeviceManager.java
new file mode 100644
index 0000000..59e9e3a
--- /dev/null
+++ b/vdrmanager/src/org/fueri/reeldroid/network/DeviceManager.java
@@ -0,0 +1,148 @@
+package org.fueri.reeldroid.network;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.List;
+
+import android.content.Context;
+import android.net.ConnectivityManager;
+import android.net.wifi.WifiInfo;
+import android.net.wifi.WifiManager;
+
+/***
+ *
+ * DeviceManager singleton
+ *
+ * @author Patrick Fürlinger fueri@fueri.ch
+ *
+ * This program 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/>.
+ *
+ *
+ *
+ */
+public class DeviceManager {
+
+ private static final int MAX_PING_TIMEOUT_MS = 40;
+
+ public static interface ProgressListener {
+ void publish(String currentIP);
+ }
+
+ /**
+ *
+ * @param defaultPort
+ * @return
+ */
+ public static List<String> findVDRHosts(Context context,
+ Integer defaultPort, ProgressListener listener) {
+
+ if (defaultPort == null) {
+ defaultPort = 6420;
+ }
+
+ List<String> list = new ArrayList<String>();
+ ConnectivityManager conMgr = (ConnectivityManager) context
+ .getSystemService(Context.CONNECTIVITY_SERVICE);
+
+ WifiManager wifiManager = (WifiManager) context
+ .getSystemService(Context.WIFI_SERVICE);
+
+ boolean wifiStatus = (conMgr
+ .getNetworkInfo(ConnectivityManager.TYPE_WIFI)).isConnected();
+
+ if (wifiStatus == false) {
+ return list;
+ }
+
+ WifiInfo wifiInfo = wifiManager.getConnectionInfo();
+ String baseIp = intToBaseIp(wifiInfo.getIpAddress());
+
+ for (Integer i = 1; i <= 254; i++) {
+ InetAddress ia;
+ try {
+ String ipHost = baseIp + i.toString();
+ if (listener != null) {
+ listener.publish(ipHost);
+ }
+ ia = InetAddress.getByName(ipHost);
+ if (findHost(ia, defaultPort, MAX_PING_TIMEOUT_MS) == false) {
+ continue;
+ }
+ list.add(ipHost);
+ } catch (UnknownHostException e) {
+ }
+
+ }
+
+ return list;
+ }
+
+ /**
+ *
+ * @param ip
+ * @param port
+ * @return
+ */
+ public static boolean findHost(String ip, int port, int pingTimeout) {
+ InetAddress ia = null;
+ try {
+ ia = InetAddress.getByName(ip);
+ } catch (UnknownHostException e) {
+ return false;
+ }
+ return findHost(ia, port, pingTimeout);
+ }
+
+ /**
+ *
+ * @param ip
+ * @param port
+ * @return
+ */
+ public static boolean findHost(InetAddress ip, int port, int pingTimeout) {
+
+ try {
+ InetAddress address = ip;
+
+ boolean reachable = address.isReachable(pingTimeout);
+
+ if (reachable == false) {
+ return false;
+ }
+
+ Socket socket = new Socket();
+ socket.connect(new InetSocketAddress(ip, port), 1000);
+ socket.close();
+ return true;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * return base ip as a string.
+ *
+ * @param ip_address
+ * @return String
+ */
+ public static String intToBaseIp(int ip_address) {
+ return (ip_address & 0xFF) + "." + ((ip_address >> 8) & 0xFF) + "."
+ + ((ip_address >> 16) & 0xFF) + ".";
+
+ }
+
+}