Skip to content

Commit

Permalink
Move callback related functions to the callback classes, removing the…
Browse files Browse the repository at this point in the history
…m from ToxService
  • Loading branch information
markwinter committed Jun 4, 2014
1 parent f26dcf2 commit 859ec0e
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 146 deletions.
25 changes: 18 additions & 7 deletions app/src/main/java/im/tox/antox/activities/AddFriendActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,26 @@
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Spinner;
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.R;
import im.tox.antox.data.AntoxDB;

import im.tox.antox.fragments.PinDialogFragment;
import im.tox.antox.tox.ToxService;
import im.tox.antox.tox.ToxSingleton;
import im.tox.antox.utils.Constants;
import im.tox.antox.R;
import im.tox.antox.utils.DhtNode;
import im.tox.jtoxcore.FriendExistsException;
import im.tox.jtoxcore.ToxException;

public class AddFriendActivity extends ActionBarActivity implements PinDialogFragment.PinDialogListener {

Expand All @@ -51,6 +56,7 @@ public class AddFriendActivity extends ActionBarActivity implements PinDialogFra
EditText friendID;
EditText friendMessage;
EditText friendAlias;
Spinner friendGroup;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -150,15 +156,20 @@ private int checkAndSend(String key, String originalUsername) {
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 (!db.doesFriendExist(ID)) {
try {
ToxSingleton toxSingleton = ToxSingleton.getInstance();
toxSingleton.jTox.addFriend(friendData[0], friendData[1]);
} catch (ToxException e) {
e.printStackTrace();
} catch (FriendExistsException e) {
e.printStackTrace();
}

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

String group = friendGroup.getSelectedItem().toString();
Log.d("AddFriendActivity","Adding friend to database");
db.addFriend(ID, "Friend Request Sent", alias, originalUsername);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package im.tox.antox.callbacks;

import android.content.Context;
import android.util.Log;

import im.tox.antox.utils.AntoxFriend;
import im.tox.jtoxcore.callbacks.OnActionCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import java.util.ArrayList;

import im.tox.antox.data.AntoxDB;
import im.tox.antox.tox.ToxService;
import im.tox.antox.tox.ToxSingleton;
import im.tox.antox.utils.AntoxFriend;
import im.tox.antox.utils.Constants;
import im.tox.antox.tox.ToxService;
import im.tox.antox.utils.Message;
import im.tox.jtoxcore.ToxException;
import im.tox.jtoxcore.callbacks.OnConnectionStatusCallback;

public class AntoxOnConnectionStatusCallback implements OnConnectionStatusCallback<AntoxFriend> {

private final static String TAG = "im.tox.antox.TAG";
private Context ctx;

ToxSingleton toxSingleton = ToxSingleton.getInstance();

public AntoxOnConnectionStatusCallback(Context ctx) {
this.ctx = ctx;
}
Expand All @@ -23,14 +31,44 @@ public AntoxOnConnectionStatusCallback(Context ctx) {
public void execute(AntoxFriend friend, boolean online) {
AntoxDB db = new AntoxDB(ctx);
db.updateUserOnline(friend.getId(), online);
db.close();
Intent update = new Intent(Constants.BROADCAST_ACTION);
update.putExtra("action", Constants.UPDATE);
LocalBroadcastManager.getInstance(ctx).sendBroadcast(update);
if (online) {
Intent intent = new Intent(this.ctx, ToxService.class);
intent.setAction(Constants.SEND_UNSENT_MESSAGES);
this.ctx.startService(intent);
db = new AntoxDB(this.ctx);
ArrayList<Message> unsentMessageList = db.getUnsentMessageList();
for (int i = 0; i<unsentMessageList.size(); i++) {
friend = null;
int id = unsentMessageList.get(i).message_id;
boolean sendingSucceeded = true;
try {
friend = toxSingleton.friendsList.getById(unsentMessageList.get(i).key);
} catch (Exception e) {
Log.d(TAG, e.toString());
}
try {
if (friend != null) {
toxSingleton.jTox.sendMessage(friend, unsentMessageList.get(i).message, id);
}
} catch (ToxException e) {
Log.d(TAG, e.toString());
e.printStackTrace();
sendingSucceeded = false;
}
if (sendingSucceeded) {
db.updateUnsentMessage(id);
}
}

if (toxSingleton.activeFriendKey != null) {
/* Broadcast to update UI */
Intent notify = new Intent(Constants.BROADCAST_ACTION);
notify.putExtra("action", Constants.UPDATE_MESSAGES);
notify.putExtra("key", toxSingleton.activeFriendKey);
LocalBroadcastManager.getInstance(this.ctx).sendBroadcast(notify);
}
}

db.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package im.tox.antox.callbacks;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import im.tox.antox.tox.ToxService;
import im.tox.antox.R;
import im.tox.antox.activities.MainActivity;
import im.tox.antox.data.AntoxDB;
import im.tox.antox.tox.ToxSingleton;
import im.tox.antox.utils.Constants;
import im.tox.antox.tox.ToxService;
import im.tox.antox.utils.FriendRequest;
import im.tox.jtoxcore.callbacks.OnFriendRequestCallback;

public class AntoxOnFriendRequestCallback implements OnFriendRequestCallback {
Expand All @@ -15,16 +27,48 @@ public class AntoxOnFriendRequestCallback implements OnFriendRequestCallback {

private Context ctx;

ToxSingleton toxSingleton = ToxSingleton.getInstance();

public AntoxOnFriendRequestCallback(Context ctx) {
this.ctx = ctx;
}

@Override
public void execute(String publicKey, String message){
Intent intent = new Intent(this.ctx, ToxService.class);
intent.setAction(Constants.FRIEND_REQUEST);
intent.putExtra(FRIEND_KEY, publicKey);
intent.putExtra(FRIEND_MESSAGE, message);
this.ctx.startService(intent);

toxSingleton.friend_requests.add(new FriendRequest(publicKey, message));
/* Add friend request to database */
AntoxDB db = new AntoxDB(this.ctx);
if(!db.isFriendBlocked(publicKey))
db.addFriendRequest(publicKey, message);
db.close();

/* Notifications for friend requests */
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.ctx);
/* Check user accepts friend request notifcations in their settings */
if(preferences.getBoolean("notifications_enable_notifications", true) != false
&& preferences.getBoolean("notifications_friend_request", true) != false) {
/* Notification */
if (!toxSingleton.leftPaneActive) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this.ctx)
.setSmallIcon(R.drawable.ic_actionbar)
.setContentTitle(this.ctx.getString(R.string.friend_request))
.setContentText(message)

.setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true);

int ID = toxSingleton.friend_requests.size();
Intent targetIntent = new Intent(this.ctx, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.ctx, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
toxSingleton.mNotificationManager.notify(ID, mBuilder.build());
}
}

/* Update friends list */
Intent update = new Intent(Constants.BROADCAST_ACTION);
update.putExtra("action", Constants.UPDATE);
LocalBroadcastManager.getInstance(this.ctx).sendBroadcast(update);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
package im.tox.antox.callbacks;

import android.content.Context;
import android.content.Intent;

import im.tox.antox.tox.ToxService;
import im.tox.antox.utils.AntoxFriend;
import im.tox.antox.R;
import im.tox.antox.activities.MainActivity;
import im.tox.antox.data.AntoxDB;
import im.tox.antox.tox.ToxSingleton;
import im.tox.antox.utils.Constants;
import im.tox.antox.utils.AntoxFriend;
import im.tox.antox.tox.ToxService;
import im.tox.jtoxcore.callbacks.OnMessageCallback;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

public class AntoxOnMessageCallback implements OnMessageCallback<AntoxFriend> {
public static final String TAG = "AntoxOnMessageCallback";
public static final String MESSAGE = "im.tox.antox.AntoxOnMessageCallback.MESSAGE";
public static final String KEY = "im.tox.antox.AntoxOnMessageCallback.KEY";
public static final String FRIEND_NUMBER = "im.tox.antox.AntoxOnMessageCallback.FRIEND_NUMBER";
public static final String INTENT_ACTION = "im.tox.antox.AntoxOnMessageCallback.INTENT_ACTION";

private Context ctx;

ToxSingleton toxSingleton = ToxSingleton.getInstance();

public AntoxOnMessageCallback(Context ctx) {
this.ctx = ctx;
}
Expand All @@ -29,5 +42,62 @@ public void execute(AntoxFriend friend, String message) {
intent.putExtra(FRIEND_NUMBER, friend.getFriendnumber());
intent.putExtra(KEY, friend.getId());
this.ctx.startService(intent);

/* Add message to database */
AntoxDB db = new AntoxDB(this.ctx);
if(!db.isFriendBlocked(friend.getId()))
db.addMessage(-1, friend.getId(), message, false, true, false, true);
db.close();

/* Broadcast to main activity to tell it to refresh */
Intent notify = new Intent(Constants.BROADCAST_ACTION);
notify.putExtra("action", Constants.UPDATE_MESSAGES);
notify.putExtra("key", friend.getId());
LocalBroadcastManager.getInstance(this.ctx).sendBroadcast(notify);

/* Notifications for messages */
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.ctx);

/* Check user accepts notifications in their settings */
if(preferences.getBoolean("notifications_enable_notifications", true) != false
&& preferences.getBoolean("notifications_new_message", true) != false) {

/* Check user isn't actively looking at the friends list or the user's chat */
if (!(toxSingleton.rightPaneActive && toxSingleton.activeFriendKey.equals(friend.getId()))
&& !(toxSingleton.leftPaneActive)) {

String name = toxSingleton.friendsList.getById(friend.getId()).getName();

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this.ctx)
.setSmallIcon(R.drawable.ic_actionbar)
.setContentTitle(name)
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this.ctx, MainActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
resultIntent.setAction(Constants.SWITCH_TO_FRIEND);
resultIntent.putExtra("key", friend.getId());
resultIntent.putExtra("name", name);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.ctx);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
toxSingleton.mNotificationManager.notify(friend.getFriendnumber(), mBuilder.build());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import im.tox.antox.data.AntoxDB;
import im.tox.antox.utils.AntoxFriend;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import im.tox.antox.tox.ToxService;
import im.tox.antox.data.AntoxDB;
import im.tox.antox.utils.AntoxFriend;
import im.tox.antox.utils.Constants;
import im.tox.antox.tox.ToxService;
import im.tox.jtoxcore.callbacks.OnReadReceiptCallback;

public class AntoxOnReadReceiptCallback implements OnReadReceiptCallback<AntoxFriend> {
Expand All @@ -19,9 +22,15 @@ public AntoxOnReadReceiptCallback(Context ctx) {

@Override
public void execute(AntoxFriend friend, int receipt) {
Intent intent = new Intent(this.ctx, ToxService.class);
intent.setAction(Constants.DELIVERY_RECEIPT);
intent.putExtra("receipt", receipt);
this.ctx.startService(intent);
AntoxDB db = new AntoxDB(this.ctx);
String key = db.setMessageReceived(receipt);
db.close();

/* Broadcast */
Intent notify;
notify = new Intent(Constants.BROADCAST_ACTION);
notify.putExtra("action", Constants.UPDATE_MESSAGES);
notify.putExtra("key", key);
LocalBroadcastManager.getInstance(this.ctx).sendBroadcast(notify);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import im.tox.antox.data.AntoxDB;
import im.tox.antox.utils.AntoxFriend;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package im.tox.antox.callbacks;

import android.content.Context;
import android.util.Log;

import im.tox.antox.utils.AntoxFriend;
import im.tox.jtoxcore.callbacks.OnTypingChangeCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import im.tox.antox.data.AntoxDB;
import im.tox.antox.utils.AntoxFriend;
Expand Down

0 comments on commit 859ec0e

Please sign in to comment.