diff options
author | lado <herrlado@gmail.com> | 2013-03-20 10:55:58 +0100 |
---|---|---|
committer | lado <herrlado@gmail.com> | 2013-03-20 10:55:58 +0100 |
commit | f467c0600198c2bfba4be0eecd4709b58b4211b3 (patch) | |
tree | 958bd845415a19aee06f204fd32d1bd8efeeb4a2 /vdrmanager/src | |
parent | cae390b45e2977def1d593009e76854ed76e281a (diff) | |
download | vdr-manager-f467c0600198c2bfba4be0eecd4709b58b4211b3.tar.gz vdr-manager-f467c0600198c2bfba4be0eecd4709b58b4211b3.tar.bz2 |
Feature #1304
Diffstat (limited to 'vdrmanager/src')
-rw-r--r-- | vdrmanager/src/de/bjusystems/vdrmanager/data/EpgCache.java | 2 | ||||
-rw-r--r-- | vdrmanager/src/de/bjusystems/vdrmanager/data/FetchEditTextPreference.java (renamed from vdrmanager/src/de/bjusystems/vdrmanager/data/MacFetchEditTextPreference.java) | 10 | ||||
-rw-r--r-- | vdrmanager/src/de/bjusystems/vdrmanager/data/VdrSharedPreferences.java | 23 | ||||
-rw-r--r-- | vdrmanager/src/de/bjusystems/vdrmanager/gui/BasePreferencesActivity.java | 10 | ||||
-rw-r--r-- | vdrmanager/src/de/bjusystems/vdrmanager/gui/Utils.java | 6 | ||||
-rw-r--r-- | vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrPreferencesActivity.java | 134 | ||||
-rw-r--r-- | vdrmanager/src/org/fueri/reeldroid/network/DeviceManager.java | 148 |
7 files changed, 287 insertions, 46 deletions
diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/data/EpgCache.java b/vdrmanager/src/de/bjusystems/vdrmanager/data/EpgCache.java index a66b499..fd1a895 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/data/EpgCache.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/data/EpgCache.java @@ -9,6 +9,4 @@ public class EpgCache { public static WeakHashMap<String, ArrayList<Epg>> CACHE = new WeakHashMap<String, ArrayList<Epg>>(); public static WeakHashMap<String, Date> NEXT_REFRESH = new WeakHashMap<String, Date>(); - - } diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/data/MacFetchEditTextPreference.java b/vdrmanager/src/de/bjusystems/vdrmanager/data/FetchEditTextPreference.java index 3b5ab4f..5e4022e 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/data/MacFetchEditTextPreference.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/data/FetchEditTextPreference.java @@ -10,7 +10,7 @@ import android.widget.ImageButton; import de.bjusystems.vdrmanager.R; import de.bjusystems.vdrmanager.data.VdrSharedPreferences.Editor; -public class MacFetchEditTextPreference extends DialogPreference { +public class FetchEditTextPreference extends DialogPreference{ private EditText mEditText; @@ -38,15 +38,15 @@ public class MacFetchEditTextPreference extends DialogPreference { //private CharSequence mCompoundButtonText; private View.OnClickListener mCompoundButtonCallback; - public MacFetchEditTextPreference(Context context, AttributeSet attrs, + public FetchEditTextPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); - setDialogLayoutResource(R.layout.mac_fetch_preference); + setDialogLayoutResource(R.layout.fetch_preference); } - public MacFetchEditTextPreference(Context context, AttributeSet attrs) { + public FetchEditTextPreference(Context context, AttributeSet attrs) { super(context, attrs); - setDialogLayoutResource(R.layout.mac_fetch_preference); + setDialogLayoutResource(R.layout.fetch_preference); } @Override diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/data/VdrSharedPreferences.java b/vdrmanager/src/de/bjusystems/vdrmanager/data/VdrSharedPreferences.java index a55e821..8839fa0 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/data/VdrSharedPreferences.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/data/VdrSharedPreferences.java @@ -39,9 +39,9 @@ public class VdrSharedPreferences implements SharedPreferences { } -// public VdrSharedPreferences(Vdr vdr) { + // public VdrSharedPreferences(Vdr vdr) { -// } + // } public boolean contains(String key) { return map.containsKey(key); @@ -64,11 +64,21 @@ public class VdrSharedPreferences implements SharedPreferences { } public int getInt(String key, int defValue) { - return get(key, defValue); + String val = get(key, String.valueOf(defValue)); + try { + return Integer.valueOf(val); + } catch (Exception ex) { + return defValue; + } } public long getLong(String key, long defValue) { - return get(key, defValue); + String val = get(key, String.valueOf(defValue)); + try { + return Long.valueOf(val); + } catch (Exception ex) { + return defValue; + } } public <T> T get(String key, T defValue) { @@ -118,9 +128,7 @@ public class VdrSharedPreferences implements SharedPreferences { public boolean commit() { - - - if(instance == null){ + if (instance == null) { map.putAll(modified); return true; } @@ -136,7 +144,6 @@ public class VdrSharedPreferences implements SharedPreferences { map.putAll(modified); - ++commits; // and update any listeners diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/gui/BasePreferencesActivity.java b/vdrmanager/src/de/bjusystems/vdrmanager/gui/BasePreferencesActivity.java index 168eef3..50e9bb7 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/gui/BasePreferencesActivity.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/gui/BasePreferencesActivity.java @@ -1,6 +1,5 @@ package de.bjusystems.vdrmanager.gui; -import android.preference.DialogPreference; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; @@ -9,7 +8,7 @@ import android.text.TextUtils; import android.view.inputmethod.EditorInfo; import android.widget.EditText; import de.bjusystems.vdrmanager.R; -import de.bjusystems.vdrmanager.data.MacFetchEditTextPreference; +import de.bjusystems.vdrmanager.data.FetchEditTextPreference; /** * @@ -24,8 +23,8 @@ public abstract class BasePreferencesActivity extends PreferenceActivity { updateSummary((EditTextPreference) ep); } else if (ep instanceof ListPreference) { updateSummary((ListPreference) ep); - } else if(ep instanceof MacFetchEditTextPreference){ - updateSummary((MacFetchEditTextPreference)ep); + } else if(ep instanceof FetchEditTextPreference){ + updateSummary((FetchEditTextPreference)ep); } } @@ -34,12 +33,11 @@ public abstract class BasePreferencesActivity extends PreferenceActivity { * * @param ep */ - protected void updateSummary(MacFetchEditTextPreference ep) { + protected void updateSummary(FetchEditTextPreference ep) { String text = ep.getText(); if (text == null) { text = ""; } - setSummary(text, ep); } diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/gui/Utils.java b/vdrmanager/src/de/bjusystems/vdrmanager/gui/Utils.java index 2b30fc6..06e2522 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/gui/Utils.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/gui/Utils.java @@ -410,6 +410,12 @@ public class Utils { t.show(); } + public static void say(Context ctx, int msg) { + Toast t = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT); + t.setGravity(Gravity.CENTER, 0, 0); + t.show(); + } + /** * Formats the date and time based on user's phone date/time preferences. * diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrPreferencesActivity.java b/vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrPreferencesActivity.java index c166333..35798d2 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrPreferencesActivity.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrPreferencesActivity.java @@ -6,13 +6,20 @@ import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; +import java.util.ArrayList; +import java.util.List; + +import org.fueri.reeldroid.network.DeviceManager; import android.app.Activity; +import android.app.AlertDialog; import android.app.ProgressDialog; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.os.AsyncTask; import android.os.Bundle; import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; @@ -20,7 +27,7 @@ import android.view.View; import de.bjusystems.vdrmanager.R; import de.bjusystems.vdrmanager.ZonePicker; import de.bjusystems.vdrmanager.app.Intents; -import de.bjusystems.vdrmanager.data.MacFetchEditTextPreference; +import de.bjusystems.vdrmanager.data.FetchEditTextPreference; import de.bjusystems.vdrmanager.data.Preferences; import de.bjusystems.vdrmanager.data.Vdr; import de.bjusystems.vdrmanager.data.VdrSharedPreferences; @@ -30,7 +37,7 @@ import de.bjusystems.vdrmanager.tasks.VoidAsyncTask; public class VdrPreferencesActivity extends BasePreferencesActivity implements OnSharedPreferenceChangeListener, OnPreferenceClickListener { - public static final int REQUEST_CODE_PICK_A_TIME_ZONE = 1; + public static final int REQUEST_CODE_PICK_A_TIME_ZONE = 1; Vdr vdr; @@ -48,10 +55,9 @@ public class VdrPreferencesActivity extends BasePreferencesActivity implements return super.findPreference(key); } - @Override protected void updateSummary(Preference ep) { - if(ep.getKey().equals("key_timezone")) { + if (ep.getKey().equals("key_timezone")) { String text = vdr.getServerTimeZone(); if (text == null) { return; @@ -61,6 +67,7 @@ public class VdrPreferencesActivity extends BasePreferencesActivity implements } super.updateSummary(ep); } + private void initVDRInstance() { id = getIntent().getIntExtra(Intents.VDR_ID, -1); if (id == -1) {// new vdr @@ -124,16 +131,15 @@ public class VdrPreferencesActivity extends BasePreferencesActivity implements return null; } - private String getIp() throws Exception { - final Preferences prefs = Preferences.get(); - String host = prefs.getSvdrpHost(); - return InetAddress.getByName(host).getHostAddress(); - } +// private String getIp() throws Exception { +// final Preferences prefs = Preferences.get(); +// String host = prefs.getSvdrpHost(); +// return InetAddress.getByName(host).getHostAddress(); +// } - private void ping(String ip) throws Exception { - final Preferences prefs = Preferences.get(); + private void ping(String ip, int port) throws Exception { Socket socket = new Socket(); - socket.connect(new InetSocketAddress(ip, prefs.getSvdrpPort()), + socket.connect(new InetSocketAddress(ip, port), 5 * 1000); socket.setSoTimeout(5 * 1000); } @@ -155,8 +161,6 @@ public class VdrPreferencesActivity extends BasePreferencesActivity implements pref.registerOnSharedPreferenceChangeListener(this); - - String recstream = pref.getString("key_recstream_method", "vdr-live"); if (recstream.equals("vdr-live") == false) { @@ -167,20 +171,22 @@ public class VdrPreferencesActivity extends BasePreferencesActivity implements // cat.removePreference(p); } - final String host = pref.getString(getString(R.string.vdr_host_key), - null); + // create background task // start task - final MacFetchEditTextPreference macedit = (MacFetchEditTextPreference) findPreference(getString(R.string.wakeup_wol_mac_key)); + final FetchEditTextPreference macedit = (FetchEditTextPreference) findPreference(getString(R.string.wakeup_wol_mac_key)); String mac = vdr.getMac(); + if(mac == null){ + mac = ""; + } macedit.setText(mac); macedit.setCompoundButtonListener(new View.OnClickListener() { @Override public void onClick(View v) { - + final String host = vdr.getHost(); if (host == null) { Utils.say(VdrPreferencesActivity.this, getString(R.string.vdr_host_not_defined)); @@ -213,8 +219,8 @@ public class VdrPreferencesActivity extends BasePreferencesActivity implements @Override protected Void doInBackground(Void... params) { try { - String ip = getIp(); - ping(ip); + String ip = InetAddress.getByName(host).getHostAddress(); + ping(ip, vdr.getPort()); mac = getMacFromArpCache(ip); } catch (Exception ex) { message = ex.getLocalizedMessage(); @@ -226,9 +232,87 @@ public class VdrPreferencesActivity extends BasePreferencesActivity implements } }); + final FetchEditTextPreference ipEdit = (FetchEditTextPreference) findPreference(getString(R.string.vdr_host_key)); + String ip = vdr.getHost(); + ipEdit.setText(ip); + ipEdit.setCompoundButtonListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + new AsyncTask<Void, String, List<String>>() { + + ProgressDialog pd; + + String message; + + protected void onPreExecute() { + pd = new ProgressDialog(VdrPreferencesActivity.this); + pd.setMessage(getString(R.string.processing)); + pd.show(); + }; + + protected void onPostExecute(final List<String> ips) { + pd.dismiss(); + if (message != null) { + Utils.say(VdrPreferencesActivity.this, message); + return; + } + + if(ips.isEmpty()){ + Utils.say(VdrPreferencesActivity.this, R.string.no_results); + return; + } + if (ips.size() == 1) { + ipEdit.setEditText(ips.get(0).toString()); + } else { + new AlertDialog.Builder(VdrPreferencesActivity.this).setItems( + ips.toArray(new CharSequence[] {}), + new DialogInterface.OnClickListener() { + @Override + public void onClick( + DialogInterface dialog, + int which) { + String ip = ips.get(which); + ipEdit.setEditText(ip); + } + }).show(); + } + + } + + protected void onProgressUpdate(String... values) { + pd.setMessage(getString(R.string.probing, values[0])); + }; + + @Override + protected List<String> doInBackground(Void... params) { + try { + + final int port = vdr.getPort(); + return DeviceManager.findVDRHosts( + VdrPreferencesActivity.this, port, + new DeviceManager.ProgressListener() { + + @Override + public void publish(String currentIP) { + publishProgress(currentIP); + } + }); + + } catch (Exception ex) { + message = ex.getLocalizedMessage(); + } + return new ArrayList<String>(0); + } + }.execute(); + + } + }); + updateChildPreferences(); - findPreference(getString(R.string.timezone_key)).setOnPreferenceClickListener(this); + findPreference(getString(R.string.timezone_key)) + .setOnPreferenceClickListener(this); } @@ -374,19 +458,19 @@ public class VdrPreferencesActivity extends BasePreferencesActivity implements @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if(resultCode != Activity.RESULT_OK){ + if (resultCode != Activity.RESULT_OK) { super.onActivityResult(requestCode, resultCode, data); return; } - if(requestCode == REQUEST_CODE_PICK_A_TIME_ZONE){ + if (requestCode == REQUEST_CODE_PICK_A_TIME_ZONE) { String ntz = data.getStringExtra("new_tz"); - if(ntz != null){ + if (ntz != null) { vdr.setServerTimeZone(ntz); Editor editor = findPreference("key_timezone").getEditor(); editor.putString("key_timezone", ntz); editor.commit(); - //setSummary(ntz, ); + // setSummary(ntz, ); } } } 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) + "."; + + } + +} |