Skip to content

Commit

Permalink
Add support for Tox DNS Discovery version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
markwinter committed Apr 2, 2014
1 parent 05d85af commit 93bd012
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ dependencies {
compile files('libs/jToxcore.jar')
compile files('libs/core.jar')
compile files('libs/jsoup-1.7.3.jar')
compile files('libs/dnsjava-2.1.6.jar')
}
Binary file added app/libs/dnsjava-2.1.6.jar
Binary file not shown.
95 changes: 93 additions & 2 deletions app/src/main/java/im/tox/antox/activities/AddFriendActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.TXTRecord;
import org.xbill.DNS.Type;

import im.tox.QR.IntentIntegrator;
import im.tox.QR.IntentResult;
import im.tox.antox.data.AntoxDB;
Expand All @@ -31,6 +38,8 @@

public class AddFriendActivity extends ActionBarActivity {

String _friendID = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -74,12 +83,23 @@ public void addFriend(View view) {
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);

/* Send intent to ToxService */
EditText friendID = (EditText) findViewById(R.id.addfriend_key);
EditText friendMessage = (EditText) findViewById(R.id.addfriend_message);
EditText friendAlias = (EditText) findViewById(R.id.addfriend_friendAlias);

/*validates key*/
if(friendID.getText().toString().contains("@")) {
// Get the first TXT record
try {
//.get() is a possible ui lag on very slow internet connections where dns lookup takes a long time
new DNSLookup().execute(friendID.getText().toString()).get();
} catch (Exception e) {
e.printStackTrace();
}
}

if(!_friendID.equals(""))
friendID.setText(_friendID);

if (validateFriendKey(friendID.getText().toString())) {
String ID = friendID.getText().toString();
String message = friendMessage.getText().toString();
Expand Down Expand Up @@ -121,6 +141,41 @@ public void addFriend(View view) {
finish();
}

private void checkAndAdd(String key, String message, String alias) {

Context context = getApplicationContext();
CharSequence text = getString(R.string.addfriend_friend_added);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);

if (validateFriendKey(key)) {
String[] friendData = {key, message, alias};

AntoxDB db = new AntoxDB(getApplicationContext());
if (!db.doesFriendExist(key)) {
Intent addFriend = new Intent(this, ToxService.class);
addFriend.setAction(Constants.ADD_FRIEND);
addFriend.putExtra("friendData", friendData);
this.startService(addFriend);

if (!alias.equals(""))
key = alias;

db.addFriend(key, "Friend Request Sent", alias);
} else {
toast = Toast.makeText(context, getString(R.string.addfriend_friend_exists), Toast.LENGTH_SHORT);
}
db.close();

toast.show();

} else {
toast = Toast.makeText(context, getResources().getString(R.string.invalid_friend_ID), Toast.LENGTH_SHORT);
toast.show();
return;
}
}

/*
* handle intent to read a friend QR code
* */
Expand Down Expand Up @@ -185,4 +240,40 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

private class DNSLookup extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... params) {

String user = params[0].substring(0, params[0].indexOf("@"));
String domain = params[0].substring(params[0].indexOf("@")+1);
String lookup = user + "._tox." + domain;
Log.d("DNSLOOKUP", lookup);

TXTRecord txt = null;
try {
Record[] records = new Lookup(lookup, Type.TXT).run();
txt = (TXTRecord) records[0];
} catch (Exception e) {
e.printStackTrace();
}

if(txt != null) {
String txtString = txt.toString().substring(txt.toString().indexOf('"'));
Log.d("DNSLOOKUP", txtString);

if(txtString.contains("tox1")) {
String key = txtString.substring(11, txtString.length()-1);
Log.d("DNSLOOKUP", key);
_friendID = key;

} else if (txtString.contains("tox2")) {
String key = txtString.substring(12, txtString.lastIndexOf(";"));
Log.d("DNSLOOKUP", key);
_friendID = key;
}
}

return null;
}
}

}
3 changes: 0 additions & 3 deletions app/src/main/java/im/tox/antox/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -672,16 +672,13 @@ protected Void doInBackground(Void... params) {
Socket socket = null;
Log.d(TAG, "DhtNode size: " + DhtNode.ipv4.size());
for(int i = 0;i < DhtNode.ipv4.size(); i++) {
Log.d(TAG, "i = " + i);
try {
long currentTime = System.currentTimeMillis();
boolean reachable = InetAddress.getByName(DhtNode.ipv4.get(i)).isReachable(400);
long elapsedTime = System.currentTimeMillis() - currentTime;
Log.d(TAG, "Elapsed time: " + elapsedTime);
if (reachable && (elapsedTime < shortestTime)) {
shortestTime = elapsedTime;
pos = i;
Log.d(TAG, "Shortest time found: " + shortestTime + " at pos: " + pos);
}

} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
<string name="friend_action_deletechat">Delete Chat Logs</string>
<string name="friend_action_block_confirmation">
Blocking this user will remove them from your friends list and you will no longer see friend
requests from them. To unblock them, go to Settings. Are you sure you wish to continue?
requests from them. Are you sure you wish to continue?
</string>

<string name="contacts_actions_on">Actions on</string>
Expand Down

0 comments on commit 93bd012

Please sign in to comment.