Skip to content

Commit

Permalink
Refactor duplication of code and make it a bit more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
markwinter committed Apr 11, 2014
1 parent eeb76f8 commit 593f880
Showing 1 changed file with 65 additions and 73 deletions.
138 changes: 65 additions & 73 deletions app/src/main/java/im/tox/antox/activities/AddFriendActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ public class AddFriendActivity extends ActionBarActivity implements PinDialogFra
String _friendCHECK = "";
String _originalUsername = "";

boolean isV2 = false;

Context context;
CharSequence text;
int duration = Toast.LENGTH_SHORT;
Toast toast;

EditText friendID;
EditText friendMessage;
EditText friendAlias;

boolean isV2 = false;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -87,6 +92,13 @@ public void onClick(DialogInterface dialog, int which) {
alertDialog.show();
}

context = getApplicationContext();
text = getString(R.string.addfriend_friend_added);

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

Intent intent = getIntent();
//If coming from tox uri link
if (Intent.ACTION_VIEW.equals(intent.getAction())
Expand All @@ -96,60 +108,64 @@ public void onClick(DialogInterface dialog, int which) {
uri = intent.getData();
if (uri != null)
friendID.setText(uri.getHost());
//TODO: ACCEPT DNS LOOKUPS FROM URI

} 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};
if(checkAndSend(intent.getStringExtra("key"), intent.getStringExtra("originalUsername")) == 0) {
toast = Toast.makeText(context, text, duration);
toast.show();
} else if (checkAndSend(intent.getStringExtra("key"), intent.getStringExtra("originalUsername")) == -1) {
toast = Toast.makeText(context, getResources().getString(R.string.invalid_friend_ID), Toast.LENGTH_SHORT);
toast.show();
return;
} else if (checkAndSend(intent.getStringExtra("key"), intent.getStringExtra("originalUsername")) == -2) {
toast = Toast.makeText(context, getString(R.string.addfriend_friend_exists), Toast.LENGTH_SHORT);
toast.show();
}

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);
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);

if (!alias.equals(""))
ID = alias;
// Close activity
finish();
}
}

db.addFriend(ID, "Friend Request Sent", alias, intent.getStringExtra("originalUsername"));
} else {
toast = Toast.makeText(context, getString(R.string.addfriend_friend_exists), Toast.LENGTH_SHORT);
}
db.close();
private int checkAndSend(String key, String originalUsername) {
if(validateFriendKey(key)) {
String ID = key;
String message = friendMessage.getText().toString();
String alias = friendAlias.getText().toString();

toast.show();
String[] friendData = {ID, message, alias};

} else {
toast = Toast.makeText(context, getResources().getString(R.string.invalid_friend_ID), Toast.LENGTH_SHORT);
toast.show();
return;
}
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);

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);
if (!alias.equals(""))
ID = alias;

// Close activity
finish();
db.addFriend(ID, "Friend Request Sent", alias, originalUsername);
} else {
return -2;
}
db.close();
return 0;
} else {
return -1;
}
}

Expand All @@ -162,14 +178,6 @@ private void scanIntent() {
}

public void addFriend(View view) {
Context context = getApplicationContext();
CharSequence text = getString(R.string.addfriend_friend_added);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);

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();
Expand All @@ -196,32 +204,16 @@ public void addFriend(View view) {
finalFriendKey = _friendID;

if(!isV2) {
if (validateFriendKey(finalFriendKey)) {
String ID = finalFriendKey;
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);

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

if(checkAndSend(finalFriendKey, "") == 0) {
toast = Toast.makeText(context, text, duration);
toast.show();

} else {
} else if(checkAndSend(finalFriendKey, "") == -1) {
toast = Toast.makeText(context, getResources().getString(R.string.invalid_friend_ID), Toast.LENGTH_SHORT);
toast.show();
return;
} else if(checkAndSend(finalFriendKey, "") == -2) {
toast = Toast.makeText(context, getString(R.string.addfriend_friend_exists), Toast.LENGTH_SHORT);
toast.show();
}

Intent update = new Intent(Constants.BROADCAST_ACTION);
Expand All @@ -240,12 +232,12 @@ public void onDialogPositiveClick(DialogFragment dialog, String 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;
Expand Down

0 comments on commit 593f880

Please sign in to comment.