Skip to content

Commit

Permalink
Find non-network-control TiVos and warn the user.
Browse files Browse the repository at this point in the history
  • Loading branch information
arantius committed Aug 20, 2011
1 parent 811f6cb commit d31666f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 21 deletions.
13 changes: 13 additions & 0 deletions res/layout/help.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ with this program; if not, write to the Free Software Foundation, Inc.,
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center" android:layout_margin="8dip" />

<LinearLayout android:id="@+id/note_layout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:visibility="gone"
>
<ImageView android:id="@+id/imageView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_alert" />
<TextView android:id="@+id/note" android:textColor="#ECF76A"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="6dip" android:gravity="center_vertical"
android:minHeight="30dip" android:layout_marginLeft="6dip" />
</LinearLayout>

<TextView android:id="@+id/textView2"
android:text="Hints: WiFi should be on. If the TiVo is not found, you're probably not connected to the right network."
android:layout_width="wrap_content" android:layout_height="wrap_content"
Expand Down
113 changes: 92 additions & 21 deletions src/com/arantius/tivocommander/Discover.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
// TODO: Need 3.4.1 for upstream bug fix? See http://goo.gl/TdffF

public class Discover extends ListActivity {
protected final class AddHost implements Runnable {
private final class AddHost implements Runnable {
private final String mAddr;
private final String mName;
private final int mPort;
Expand All @@ -62,22 +62,80 @@ public AddHost(String name, String addr, int port) {
}

public void run() {
HashMap<String, String> host;
int i;
boolean found = false;
for (i = 0; i < mHosts.size(); i++) {
host = mHosts.get(i);
if (mName.equals(host.get("name"))) {
if (host.get("addr") == "" && mAddr != "") {
// If we found a name-matching host with no address, and we have
// one, prepare to use it.
found = true;
break;
} else {
// Otherwise, this host is of no benefit, stop.
return;
}
}
}

HashMap<String, String> listItem = new HashMap<String, String>();
listItem.put("addr", mAddr);
listItem.put("name", mName);
listItem.put("port", new Integer(mPort).toString());
mHosts.add(listItem);

if (found) {
// Replace the found host.
mHosts.set(i, listItem);
} else {
// Add a new one.
mHosts.add(listItem);
}
mHostAdapter.notifyDataSetChanged();
}
}

private final class MdnsQuery implements Runnable {
private final String mServiceName;

public MdnsQuery(String service) {
mServiceName = service;
}

public void run() {
try {
mJmdns.addServiceListener(mServiceName, mServiceListener);
mJmdns.requestServiceInfo(mServiceName, "", mTimeout);
} catch (IllegalStateException e) {
// No-op, just clean up below.
}
mJmdns.removeServiceListener(mServiceName, mServiceListener);
}
}

private TextView mEmpty;
private SimpleAdapter mHostAdapter;
private final ArrayList<HashMap<String, String>> mHosts =
new ArrayList<HashMap<String, String>>();
private JmDNS mJmdns;
private MulticastLock mMulticastLock = null;
private final OnItemClickListener mOnClickListener =
new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
final HashMap<String, String> item = mHosts.get(position);

if (item.get("addr") == "") {
// A saved null host means we found a device but not a rpc endpoint.
stopQuery();
Intent intent = new Intent(Discover.this, Help.class);
intent.putExtra("note",
"You need to enable network control to connect to this TiVo.");
startActivity(intent);
return;
}

final SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(Discover.this
.getBaseContext());
Expand Down Expand Up @@ -112,25 +170,27 @@ public void serviceRemoved(ServiceEvent event) {

public void serviceResolved(ServiceEvent event) {
ServiceInfo info = event.getInfo();
runOnUiThread(new AddHost(event.getName(), info.getHostAddresses()[0],
info.getPort()));
if (mServiceNameRpc.equals(event.getType())) {
runOnUiThread(new AddHost(event.getName(), info.getHostAddresses()[0],
info.getPort()));
} else {
runOnUiThread(new AddHost(event.getName(), "", 0));
}
}
};

protected SimpleAdapter mHostAdapter;
protected ArrayList<HashMap<String, String>> mHosts =
new ArrayList<HashMap<String, String>>();
protected JmDNS mJmdns;
protected final String mServiceName = "_tivo-mindrpc._tcp.local.";
protected final long mTimeout = 2500;
private final String mServiceNameDevice = "_tivo-device._tcp.local.";
private final String mServiceNameRpc = "_tivo-mindrpc._tcp.local.";
private final long mTimeout = 3500;

public final void customSettings(View v) {
stopQuery();
Intent intent = new Intent(Discover.this, Settings.class);
startActivity(intent);
finish();
}

public final void showHelp(View V) {
stopQuery();
Intent intent = new Intent(Discover.this, Help.class);
startActivity(intent);
}
Expand All @@ -150,22 +210,34 @@ public final void startQuery(View v) {
mMulticastLock.acquire();

setProgressBarIndeterminateVisibility(true);

try {
mJmdns = JmDNS.create();
} catch (IOException e) {
Utils.logError("Couldn't do mDNS", e);
return;
}

// Issue queries.
final ThreadGroup tg = new ThreadGroup("queryThreadGroup");
new Thread(tg, new MdnsQuery(mServiceNameDevice)).start();
new Thread(tg, new MdnsQuery(mServiceNameRpc)).start();
// Wait for them and update the UI.
new Thread(new Runnable() {
public void run() {
try {
mJmdns = JmDNS.create();
} catch (IOException e) {
Utils.logError("Couldn't do mDNS", e);
return;
while (tg.activeCount() > 0) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// No-op.
}
}
mJmdns.addServiceListener(mServiceName, mServiceListener);
mJmdns.requestServiceInfo(mServiceName, "", mTimeout);
stopQuery();
runOnUiThread(new Runnable() {
public void run() {
setProgressBarIndeterminateVisibility(false);
}
});
stopQuery();
}
}).start();
}
Expand Down Expand Up @@ -207,11 +279,10 @@ public void run() {
});

if (mJmdns != null) {
mJmdns.removeServiceListener(mServiceName, mServiceListener);
try {
mJmdns.close();
mJmdns = null;
} catch (IOException e) {
Utils.logError("Closing jmdns", e);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/com/arantius/tivocommander/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Help extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help);

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
((TextView) findViewById(R.id.note)).setText(bundle.getString("note"));
findViewById(R.id.note_layout).setVisibility(View.VISIBLE);
}
}
}

0 comments on commit d31666f

Please sign in to comment.