From f467c0600198c2bfba4be0eecd4709b58b4211b3 Mon Sep 17 00:00:00 2001 From: lado Date: Wed, 20 Mar 2013 10:55:58 +0100 Subject: Feature #1304 --- vdrmanager/AndroidManifest.xml | 2 + vdrmanager/res/layout/fetch_preference.xml | 23 ++++ vdrmanager/res/layout/mac_fetch_preference.xml | 23 ---- vdrmanager/res/values/strings.xml | 4 +- vdrmanager/res/xml/vdr_prefs.xml | 4 +- .../de/bjusystems/vdrmanager/data/EpgCache.java | 2 - .../vdrmanager/data/FetchEditTextPreference.java | 94 +++++++++++++ .../data/MacFetchEditTextPreference.java | 94 ------------- .../vdrmanager/data/VdrSharedPreferences.java | 23 ++-- .../vdrmanager/gui/BasePreferencesActivity.java | 10 +- .../src/de/bjusystems/vdrmanager/gui/Utils.java | 6 + .../vdrmanager/gui/VdrPreferencesActivity.java | 134 +++++++++++++++---- .../org/fueri/reeldroid/network/DeviceManager.java | 148 +++++++++++++++++++++ 13 files changed, 406 insertions(+), 161 deletions(-) create mode 100644 vdrmanager/res/layout/fetch_preference.xml delete mode 100644 vdrmanager/res/layout/mac_fetch_preference.xml create mode 100644 vdrmanager/src/de/bjusystems/vdrmanager/data/FetchEditTextPreference.java delete mode 100644 vdrmanager/src/de/bjusystems/vdrmanager/data/MacFetchEditTextPreference.java create mode 100644 vdrmanager/src/org/fueri/reeldroid/network/DeviceManager.java (limited to 'vdrmanager') diff --git a/vdrmanager/AndroidManifest.xml b/vdrmanager/AndroidManifest.xml index 5597ec1..10d5357 100644 --- a/vdrmanager/AndroidManifest.xml +++ b/vdrmanager/AndroidManifest.xml @@ -6,9 +6,11 @@ + + diff --git a/vdrmanager/res/layout/fetch_preference.xml b/vdrmanager/res/layout/fetch_preference.xml new file mode 100644 index 0000000..d276625 --- /dev/null +++ b/vdrmanager/res/layout/fetch_preference.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/vdrmanager/res/layout/mac_fetch_preference.xml b/vdrmanager/res/layout/mac_fetch_preference.xml deleted file mode 100644 index d276625..0000000 --- a/vdrmanager/res/layout/mac_fetch_preference.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/vdrmanager/res/values/strings.xml b/vdrmanager/res/values/strings.xml index 7ca5e39..e11d792 100644 --- a/vdrmanager/res/values/strings.xml +++ b/vdrmanager/res/values/strings.xml @@ -391,5 +391,7 @@ Select server time zone Sort alphabetically Sort by time zone - + Probing %1$s … + No results… + \ No newline at end of file diff --git a/vdrmanager/res/xml/vdr_prefs.xml b/vdrmanager/res/xml/vdr_prefs.xml index feee1e9..3720db4 100644 --- a/vdrmanager/res/xml/vdr_prefs.xml +++ b/vdrmanager/res/xml/vdr_prefs.xml @@ -11,7 +11,7 @@ - - > CACHE = new WeakHashMap>(); public static WeakHashMap NEXT_REFRESH = new WeakHashMap(); - - } diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/data/FetchEditTextPreference.java b/vdrmanager/src/de/bjusystems/vdrmanager/data/FetchEditTextPreference.java new file mode 100644 index 0000000..5e4022e --- /dev/null +++ b/vdrmanager/src/de/bjusystems/vdrmanager/data/FetchEditTextPreference.java @@ -0,0 +1,94 @@ +package de.bjusystems.vdrmanager.data; + +import android.content.Context; +import android.content.DialogInterface; +import android.preference.DialogPreference; +import android.util.AttributeSet; +import android.view.View; +import android.widget.EditText; +import android.widget.ImageButton; +import de.bjusystems.vdrmanager.R; +import de.bjusystems.vdrmanager.data.VdrSharedPreferences.Editor; + +public class FetchEditTextPreference extends DialogPreference{ + + private EditText mEditText; + + private String initialValue; + + public String getInitialValue() { + return initialValue; + } + + public void setInitialValue(String initialValue) { + this.initialValue = initialValue; + } + + public EditText getmEditText() { + return mEditText; + } + + public void setEditText(String text) { + this.mEditText.setText(text); + } + + private ImageButton mButton; + + private String mText; + //private CharSequence mCompoundButtonText; + private View.OnClickListener mCompoundButtonCallback; + + public FetchEditTextPreference(Context context, AttributeSet attrs, + int defStyle) { + super(context, attrs, defStyle); + setDialogLayoutResource(R.layout.fetch_preference); + } + + public FetchEditTextPreference(Context context, AttributeSet attrs) { + super(context, attrs); + setDialogLayoutResource(R.layout.fetch_preference); + } + + @Override + protected View onCreateDialogView() { + View root = super.onCreateDialogView(); + mEditText = (EditText) root.findViewById(R.id.edit); + mButton = (ImageButton) root.findViewById(R.id.button); + return root; + } + + public void setText(String text) { + mText = text; + } + + public String getText() { + return mText; + } + + @Override + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case DialogInterface.BUTTON_POSITIVE: // User clicked OK! + mText = mEditText.getText().toString(); + callChangeListener(mText); + Editor editor = (Editor) getEditor(); + editor.putString(getKey(), mText); + editor.commit(); + break; + } + super.onClick(dialog, which); + } + + @Override + protected void onBindDialogView(View view) { + mEditText.setText(mText); + //mButton.setText(mCompoundButtonText); + + // Set a callback to our button. + mButton.setOnClickListener(mCompoundButtonCallback); + } + + public void setCompoundButtonListener(View.OnClickListener callback) { + mCompoundButtonCallback = callback; + } +} \ No newline at end of file diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/data/MacFetchEditTextPreference.java b/vdrmanager/src/de/bjusystems/vdrmanager/data/MacFetchEditTextPreference.java deleted file mode 100644 index 3b5ab4f..0000000 --- a/vdrmanager/src/de/bjusystems/vdrmanager/data/MacFetchEditTextPreference.java +++ /dev/null @@ -1,94 +0,0 @@ -package de.bjusystems.vdrmanager.data; - -import android.content.Context; -import android.content.DialogInterface; -import android.preference.DialogPreference; -import android.util.AttributeSet; -import android.view.View; -import android.widget.EditText; -import android.widget.ImageButton; -import de.bjusystems.vdrmanager.R; -import de.bjusystems.vdrmanager.data.VdrSharedPreferences.Editor; - -public class MacFetchEditTextPreference extends DialogPreference { - - private EditText mEditText; - - private String initialValue; - - public String getInitialValue() { - return initialValue; - } - - public void setInitialValue(String initialValue) { - this.initialValue = initialValue; - } - - public EditText getmEditText() { - return mEditText; - } - - public void setEditText(String text) { - this.mEditText.setText(text); - } - - private ImageButton mButton; - - private String mText; - //private CharSequence mCompoundButtonText; - private View.OnClickListener mCompoundButtonCallback; - - public MacFetchEditTextPreference(Context context, AttributeSet attrs, - int defStyle) { - super(context, attrs, defStyle); - setDialogLayoutResource(R.layout.mac_fetch_preference); - } - - public MacFetchEditTextPreference(Context context, AttributeSet attrs) { - super(context, attrs); - setDialogLayoutResource(R.layout.mac_fetch_preference); - } - - @Override - protected View onCreateDialogView() { - View root = super.onCreateDialogView(); - mEditText = (EditText) root.findViewById(R.id.edit); - mButton = (ImageButton) root.findViewById(R.id.button); - return root; - } - - public void setText(String text) { - mText = text; - } - - public String getText() { - return mText; - } - - @Override - public void onClick(DialogInterface dialog, int which) { - switch (which) { - case DialogInterface.BUTTON_POSITIVE: // User clicked OK! - mText = mEditText.getText().toString(); - callChangeListener(mText); - Editor editor = (Editor) getEditor(); - editor.putString(getKey(), mText); - editor.commit(); - break; - } - super.onClick(dialog, which); - } - - @Override - protected void onBindDialogView(View view) { - mEditText.setText(mText); - //mButton.setText(mCompoundButtonText); - - // Set a callback to our button. - mButton.setOnClickListener(mCompoundButtonCallback); - } - - public void setCompoundButtonListener(View.OnClickListener callback) { - mCompoundButtonCallback = callback; - } -} \ No newline at end of file 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 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>() { + + 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 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 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(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 . + * + * + * + */ +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 findVDRHosts(Context context, + Integer defaultPort, ProgressListener listener) { + + if (defaultPort == null) { + defaultPort = 6420; + } + + List list = new ArrayList(); + 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) + "."; + + } + +} -- cgit v1.2.3