Skip to content

Commit

Permalink
Add Tox DNS Discovery Verison 2
Browse files Browse the repository at this point in the history
  • Loading branch information
markwinter committed Apr 3, 2014
1 parent b1de4a2 commit 75b24a3
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 56 deletions.
213 changes: 169 additions & 44 deletions app/src/main/java/im/tox/antox/activities/AddFriendActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.NavUtils;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -25,6 +27,8 @@
import im.tox.QR.IntentIntegrator;
import im.tox.QR.IntentResult;
import im.tox.antox.data.AntoxDB;

import im.tox.antox.fragments.PinDialogFragment;
import im.tox.antox.utils.Constants;
import im.tox.antox.R;
import im.tox.antox.tox.ToxService;
Expand All @@ -36,12 +40,20 @@
* @author Mark Winter (Astonex)
*/

public class AddFriendActivity extends ActionBarActivity {
public class AddFriendActivity extends ActionBarActivity implements PinDialogFragment.PinDialogListener {

String _friendID = "";
String _friendCHECK = "";
String _originalUsername = "";

EditText friendID;
EditText friendMessage;
EditText friendAlias;

boolean isV2 = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/* Fix for an android 4.1.x bug */
Expand All @@ -57,15 +69,71 @@ protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
getSupportActionBar().setIcon(R.drawable.ic_actionbar);
}
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
EditText friendID = (EditText) findViewById(R.id.addfriend_key);
Intent intentURI = getIntent();
Uri uri;
if (Intent.ACTION_VIEW.equals(intentURI.getAction())
&& intentURI != null) {
uri = intentURI.getData();


Intent intent = getIntent();
//If coming from tox uri link
if (Intent.ACTION_VIEW.equals(intent.getAction())
&& intent != null) {
EditText friendID = (EditText) findViewById(R.id.addfriend_key);
Uri uri;
uri = intent.getData();
if (uri != null)
friendID.setText(uri.getHost());
} else if (intent.getAction() == "toxv2") {
//else if it came from toxv2 restart
friendID = (EditText) findViewById(R.id.addfriend_key);
friendMessage = (EditText) findViewById(R.id.addfriend_message);
friendAlias = (EditText) findViewById(R.id.addfriend_friendAlias);

friendID.setText(intent.getStringExtra("originalUsername"));
friendAlias.setText(intent.getStringExtra("alias"));
friendMessage.setText(intent.getStringExtra("message"));

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(intent.getStringExtra("key"))) {
String ID = intent.getStringExtra("key");
String message = friendMessage.getText().toString();
String alias = friendAlias.getText().toString();

String[] friendData = {ID, message, alias};

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

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

db.addFriend(ID, "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;
}

Intent update = new Intent(Constants.BROADCAST_ACTION);
update.putExtra("action", Constants.UPDATE);
LocalBroadcastManager.getInstance(this).sendBroadcast(update);
Intent i = new Intent();
setResult(RESULT_OK, i);

// Close activity
finish();
}
}

Expand All @@ -83,11 +151,12 @@ public void addFriend(View view) {
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);

EditText friendID = (EditText) findViewById(R.id.addfriend_key);
EditText friendMessage = (EditText) findViewById(R.id.addfriend_message);
EditText friendAlias = (EditText) findViewById(R.id.addfriend_friendAlias);
friendID = (EditText) findViewById(R.id.addfriend_key);
friendMessage = (EditText) findViewById(R.id.addfriend_message);
friendAlias = (EditText) findViewById(R.id.addfriend_friendAlias);

if(friendID.getText().toString().contains("@")) {
_originalUsername = friendID.getText().toString();
// Get the first TXT record
try {
//.get() is a possible ui lag on very slow internet connections where dns lookup takes a long time
Expand All @@ -97,50 +166,102 @@ public void addFriend(View view) {
}
}

if(isV2) {
DialogFragment dialog = new PinDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("Enter Friend's Pin", "Enter Friend's Pin");
dialog.setArguments(bundle);
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
}

String finalFriendKey = friendID.getText().toString();

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

if(!isV2) {
if (validateFriendKey(finalFriendKey)) {
String ID = finalFriendKey;
String message = friendMessage.getText().toString();
String alias = friendAlias.getText().toString();

if (validateFriendKey(friendID.getText().toString())) {
String ID = friendID.getText().toString();
String message = friendMessage.getText().toString();
String alias = friendAlias.getText().toString();
String[] friendData = {ID, message, alias};

String[] friendData = {ID, message, alias};
AntoxDB db = new AntoxDB(getApplicationContext());
if (!db.doesFriendExist(friendID.getText().toString())) {
Intent addFriend = new Intent(this, ToxService.class);
addFriend.setAction(Constants.ADD_FRIEND);
addFriend.putExtra("friendData", friendData);
this.startService(addFriend);

AntoxDB db = new AntoxDB(getApplicationContext());
if (!db.doesFriendExist(friendID.getText().toString())) {
Intent addFriend = new Intent(this, ToxService.class);
addFriend.setAction(Constants.ADD_FRIEND);
addFriend.putExtra("friendData", friendData);
this.startService(addFriend);
if (!alias.equals(""))
ID = alias;

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

toast.show();

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

toast.show();
Intent update = new Intent(Constants.BROADCAST_ACTION);
update.putExtra("action", Constants.UPDATE);
LocalBroadcastManager.getInstance(this).sendBroadcast(update);
Intent i = new Intent();
setResult(RESULT_OK, i);

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

Intent update = new Intent(Constants.BROADCAST_ACTION);
update.putExtra("action", Constants.UPDATE);
LocalBroadcastManager.getInstance(this).sendBroadcast(update);
Intent i = new Intent();
setResult(RESULT_OK, i);
@Override
public void onDialogPositiveClick(DialogFragment dialog, String pin) {
pin = pin + "==";
//Base64 to Bytes
try {
byte[] decoded = Base64.decode(pin, Base64.DEFAULT);
//Bytes to Hex
StringBuilder sb = new StringBuilder();
for(byte b: decoded)
sb.append(String.format("%02x", b&0xff));
String encodedString = sb.toString();
//XOR key and pin to verify

//Finally set the correct ID to add
_friendID = _friendID + encodedString + _friendCHECK;

//Restart activity with info needed
Intent restart = new Intent(this, AddFriendActivity.class);
restart.putExtra("key", _friendID);
restart.putExtra("alias", friendAlias.getText().toString());
restart.putExtra("message", friendMessage.getText().toString());
restart.putExtra("originalUsername", _originalUsername);
restart.setAction("toxv2");
startActivity(restart);

finish();

} catch (IllegalArgumentException e) {
Context context = getApplicationContext();
CharSequence text = getString(R.string.addfriend_invalid_pin);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
e.printStackTrace();
}

// Close activity
finish();
}

@Override
public void onDialogNegativeClick(DialogFragment dialog) {}

/*
* handle intent to read a friend QR code
* */
Expand Down Expand Up @@ -227,13 +348,17 @@ protected Void doInBackground(String... params) {

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

} else if (txtString.contains("tox2")) {
String key = txtString.substring(12, txtString.lastIndexOf(";"));
Log.d("DNSLOOKUP", key);
isV2 = true;
String key = txtString.substring(12, 12+64);
String check = txtString.substring(12+64+7,12+64+7+4);
Log.d("DNSLOOKUP", "V2KEY: " + key);
Log.d("DNSLOOKUP", "V2CHECK: " + check);
_friendID = key;
_friendCHECK = check;
}
}

Expand Down
13 changes: 1 addition & 12 deletions app/src/main/java/im/tox/antox/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ private void clearUselessNotifications () {
AntoxFriend friend = toxSingleton.friendsList.getById(toxSingleton.activeFriendKey);
toxSingleton.mNotificationManager.cancel(friend.getFriendnumber());
}
db.close();
}

@Override
Expand Down Expand Up @@ -711,18 +712,6 @@ protected Void doInBackground(Void... params) {
@Override
protected void onPostExecute(Void result)
{
try {
//Checking the details
System.out.println("node details:");
System.out.println(DhtNode.ipv4);
System.out.println(DhtNode.ipv6);
System.out.println(DhtNode.port);
System.out.println(DhtNode.key);
System.out.println(DhtNode.owner);
System.out.println(DhtNode.location);
}catch (NullPointerException e){
Toast.makeText(MainActivity.this,getString(R.string.main_node_list_download_error),Toast.LENGTH_SHORT).show();
}
/**
* There is a chance that downloading finishes later than the bootstrapping call in the
* ToxService, because both are in separate threads. In that case to make sure the nodes
Expand Down
67 changes: 67 additions & 0 deletions app/src/main/java/im/tox/antox/fragments/PinDialogFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package im.tox.antox.fragments;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import im.tox.antox.R;

/**
* Created by soft on 03/04/14.
*/
public class PinDialogFragment extends DialogFragment {
public interface PinDialogListener {
public void onDialogPositiveClick(DialogFragment dialog, String pin);
public void onDialogNegativeClick(DialogFragment dialog);
}

PinDialogListener mListener;
EditText pin;

public PinDialogFragment() {}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (PinDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement DHTDialogFragment");
}
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = (View) inflater.inflate(R.layout.dialog_pin, null);
builder.setView(view);

pin = (EditText) view.findViewById(R.id.pin);

builder.setMessage(getResources().getString(R.string.dialog_pin))
.setPositiveButton(getResources().getString(R.string.button_confirm), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick(PinDialogFragment.this,
pin.getText().toString());
}
})
.setNegativeButton(getResources().getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogNegativeClick(PinDialogFragment.this);
}
});
return builder.create();
}

public void onCancel(DialogInterface dialog) {
mListener.onDialogNegativeClick(PinDialogFragment.this);
}
}

0 comments on commit 75b24a3

Please sign in to comment.