@@ -28,7 +28,7 @@ public class DataAccessObject extends SQLiteOpenHelper {

public static final String TAG = "DataAccessObject";

public static final int DATABASE_VERSION = 10;
public static final int DATABASE_VERSION = 11;
public static final String DATABASE_NAME = "areaChat";

public static final String CONVERSATION_TABLE = "conversations";
@@ -60,6 +60,7 @@ public class DataAccessObject extends SQLiteOpenHelper {
public static final String U_MAIL = "mail";
public static final String U_SAFE = "safe";
public static final String U_FRIEND = "friend";
public static final String U_IMAGE = "icon";

public static final String EVENT_COLLISIONS_TABLE = "eventCollisions";
public static final String FORUM_COLLISIONS_TABLE = "forumCollisions";
@@ -364,33 +365,58 @@ public Long CreateUnownedForum(List<LocoUser> uList, GeoPt pt, String name, Stri

}

public void WriteMessageToForum(long forumId, String owner, Message message) {
public void RemoveUserFromForum(LocoForum forum, String user) {

SQLiteDatabase db = this.getWritableDatabase();
SQLiteDatabase db =this.getWritableDatabase();

Cursor collision = db.rawQuery("SELECT * FROM " + FORUM_COLLISIONS_TABLE + " WHERE " + COL_FROM + "=" + forumId, null);
Cursor collision = db.rawQuery("SELECT * FROM " + FORUM_COLLISIONS_TABLE + " WHERE " + COL_FROM + "=" + forum.getId(), null);

if(collision.moveToFirst()) {

do {

long collidingForumId = collision.getLong(collision.getColumnIndex(COL_FROM));
if(collidingForumId == forumId) {
if (collision.getString(collision.getColumnIndex(OWNER)).compareTo(forum.getOwner()) == 0) {

if (collision.getString(collision.getColumnIndex(OWNER)).compareTo(owner) == 0) {
long resolvedForumId = collision.getLong(collision.getColumnIndex(COL_TO));
db.delete(FORUM_USERS_TABLE, F_KEY + "=" + resolvedForumId + " AND " + U_MAIL + "='" + user + "'", null);
break;

long resolvedForumId = collision.getLong(collision.getColumnIndex(COL_TO));
Cursor forum = db.rawQuery("SELECT * FROM " + FORUMS_TABLE + " WHERE " + F_KEY + "=" + resolvedForumId, null);
if(forum.moveToFirst()) {
WriteMessageToConversation(message, forum.getLong(forum.getColumnIndex(C_KEY)));
AppController.NewForumMessage(resolvedForumId);
break;
}
}

} while(collision.moveToNext());

forum.close();
} else {

db.delete(FORUM_USERS_TABLE, F_KEY + "=" + forum.getId() + " AND " + U_MAIL + "='" + user + "'", null);

}

collision.close();

}

public void WriteMessageToForum(long forumId, String owner, Message message) {

SQLiteDatabase db = this.getWritableDatabase();

Cursor collision = db.rawQuery("SELECT * FROM " + FORUM_COLLISIONS_TABLE + " WHERE " + COL_FROM + "=" + forumId, null);

if(collision.moveToFirst()) {

do {

if (collision.getString(collision.getColumnIndex(OWNER)).compareTo(owner) == 0) {

long resolvedForumId = collision.getLong(collision.getColumnIndex(COL_TO));
Cursor forum = db.rawQuery("SELECT * FROM " + FORUMS_TABLE + " WHERE " + F_KEY + "=" + resolvedForumId, null);
if(forum.moveToFirst()) {
WriteMessageToConversation(message, forum.getLong(forum.getColumnIndex(C_KEY)));
AppController.NewForumMessage(resolvedForumId);
break;
}

forum.close();

}

} while(collision.moveToNext());
@@ -582,9 +608,7 @@ public void WriteMessageToEvent(long eventId, String name, String owner, GeoPt l
}

public void WriteMessageToEvent(LocoEvent event, Message message) {

WriteMessageToEvent(event.getId(), event.getName(), event.getOwner(), event.getLocation(), event.getRadius(), message);

}

public void AddUser(LocoUser user) {
@@ -604,6 +628,7 @@ public void AddUser(LocoUser user) {
values.put(LOC_LON, user.getLocation().getLongitude());
values.put(U_SAFE, user.getSafe());
values.put(U_FRIEND, user.getFriend());
values.put(U_IMAGE, user.getIcon());

db.insert(USERS_TABLE, null, values);

@@ -626,6 +651,7 @@ public void AddUsers(Collection<LocoUser> users) {
values.put(LOC_LON, user.getLocation().getLongitude());
values.put(U_SAFE, user.getSafe());
values.put(U_FRIEND, user.getFriend());
values.put(U_IMAGE, user.getIcon());

db.insert(USERS_TABLE, null, values);

@@ -679,15 +705,14 @@ public List<LocoUser> GetAllUsers() {

do {

DatabaseUtils.dumpCurrentRow(cursor);

LocoUser user = new LocoUser();
GeoPt loc = new GeoPt();
loc.setLatitude(cursor.getFloat(cursor.getColumnIndex(LOC_LAT)));
loc.setLongitude(cursor.getFloat(cursor.getColumnIndex(LOC_LON)));
user.setLocation(loc);
user.setMail(cursor.getString(cursor.getColumnIndex(U_MAIL)));
user.setName(cursor.getString(cursor.getColumnIndex(NAME)));
user.setIcon(cursor.getString(cursor.getColumnIndex(U_IMAGE)));
int friend = cursor.getInt(cursor.getColumnIndex(U_FRIEND));
if(friend == 1) {
user.setFriend(true);
@@ -713,7 +738,7 @@ public List<Message> GetAllMessagesFromForum(long forumId) {

SQLiteDatabase db = this.getWritableDatabase();

List<Message> retVal = new ArrayList<Message>();
List<Message> retVal = new ArrayList<>();
Cursor forum = db.rawQuery("SELECT * FROM " + FORUMS_TABLE + " WHERE " + F_KEY + "=" + forumId, null);

if(forum.moveToFirst()) {
@@ -730,7 +755,7 @@ public List<Message> GetAllMessagesFromEvent(long eventId) {

SQLiteDatabase db = this.getWritableDatabase();

List<Message> retVal = new ArrayList<Message>();
List<Message> retVal = new ArrayList<>();
Cursor forum = db.rawQuery("SELECT * FROM " + EVENTS_TABLE + " WHERE " + E_KEY + "=" + eventId, null);

if(forum.moveToFirst()) {
@@ -749,7 +774,7 @@ public List<LocoUser> GetAllSafeUsers() {

Cursor cursor = db.rawQuery("SELECT * FROM " + USERS_TABLE + " WHERE " + U_SAFE + "=1", null);

List<LocoUser> retVal = new ArrayList<LocoUser>();
List<LocoUser> retVal = new ArrayList<>();

if(cursor.moveToFirst()) {

@@ -762,6 +787,7 @@ public List<LocoUser> GetAllSafeUsers() {
u.setLocation(loc);
u.setMail(cursor.getString(cursor.getColumnIndex(U_MAIL)));
u.setName(cursor.getString(cursor.getColumnIndex(NAME)));
u.setIcon(cursor.getString(cursor.getColumnIndex(U_IMAGE)));
int friend = cursor.getInt(cursor.getColumnIndex(U_FRIEND));
if(friend == 1) {
u.setFriend(true);
@@ -786,7 +812,7 @@ public List<LocoUser> GetAllFriends() {

Cursor cursor = db.rawQuery("SELECT * FROM " + USERS_TABLE + " WHERE " + U_FRIEND + "=1", null);

List<LocoUser> retVal = new ArrayList<LocoUser>();
List<LocoUser> retVal = new ArrayList<>();

if(cursor.moveToFirst()) {

@@ -799,6 +825,7 @@ public List<LocoUser> GetAllFriends() {
u.setLocation(loc);
u.setMail(cursor.getString(cursor.getColumnIndex(U_MAIL)));
u.setName(cursor.getString(cursor.getColumnIndex(NAME)));
u.setIcon(cursor.getString(cursor.getColumnIndex(U_IMAGE)));
int safe = cursor.getInt(cursor.getColumnIndex(U_SAFE));
if(safe != 0) {
u.setSafe(true);
@@ -821,7 +848,7 @@ public List<LocoForum> GetAllForums() {

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + FORUMS_TABLE + " WHERE " + DELETED + "=0", null);
List<LocoForum> retVal = new ArrayList<LocoForum>();
List<LocoForum> retVal = new ArrayList<>();

if(cursor.moveToFirst()) {

@@ -838,7 +865,7 @@ public List<LocoForum> GetAllForums() {
forum.setConversation(cursor.getLong(cursor.getColumnIndex(C_KEY)));

Cursor users = db.rawQuery("SELECT * FROM " + FORUM_USERS_TABLE + " WHERE " + F_KEY + "=" + forum.getId(), null);
List<LocoUser> uList = new ArrayList<LocoUser>();
List<LocoUser> uList = new ArrayList<>();

if(users.moveToFirst()) {

@@ -871,7 +898,7 @@ public List<LocoEvent> GetAllEvents() {

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + EVENTS_TABLE + " WHERE " + DELETED + "=0", null);
List<LocoEvent> retVal = new ArrayList<LocoEvent>();
List<LocoEvent> retVal = new ArrayList<>();

if(cursor.moveToFirst()) {

@@ -905,7 +932,7 @@ public List<Message> GetMessagesFromDirectConversation(String mail) {

SQLiteDatabase db = this.getWritableDatabase();

List<Message> retVal = new ArrayList<Message>();
List<Message> retVal = new ArrayList<>();
Cursor directCov = db.rawQuery("SELECT * FROM " + DIRECT_CONVERSATIONS_TABLE + " WHERE " + M_FROM + "='" + mail + "'", null);

if(directCov.moveToFirst()) {
@@ -938,7 +965,7 @@ public LocoForum GetForum(long forumId) {
newForum.setConversation(cursor.getLong(cursor.getColumnIndex(C_KEY)));

Cursor users = db.rawQuery("SELECT * FROM " + FORUM_USERS_TABLE + " WHERE " + F_KEY + "=" + newForum.getId(), null);
List<LocoUser> uList = new ArrayList<LocoUser>();
List<LocoUser> uList = new ArrayList<>();

if(users.moveToFirst()) {

@@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.List;

import il.co.nolife.locotalk.DataTypes.LocoForum;
import il.co.nolife.locotalk.DataTypes.LocoUser;

/**
@@ -112,7 +113,21 @@ public static void HandleMessage(Context context, JSONObject message, String ori
DataAccessObject dao = new DataAccessObject(context);
dao.WriteMessageToEvent(eventId, name, owner, loc, radius, actualMessage);

} else if(dataType.compareTo("removeForum") == 0) {

long forumId = message.getLong("forumId");
String owner = message.getString("owner");
String toRemove = message.getString("user");

LocoForum forum = new LocoForum();
forum.setId(forumId);
forum.setOwner(owner);

DataAccessObject dao = new DataAccessObject(context);
dao.RemoveUserFromForum(forum, toRemove);

}

} catch(JSONException e) {
Log.e(TAG, "Could not parse GCM Message of type '" + dataType + "': " + e.getMessage() + ", original message: " + orig);
}
@@ -330,33 +330,47 @@ void LongMapClicked (LatLng latLng){
void MarkerClicked(Marker marker){

LocoUser user = currentUserMap.get(marker);
if(user != null){
if(user != null) {

Intent chatIntent = new Intent(this, ChatActivity.class);
chatIntent.putExtra("type", EChatType.PRIVATE.ordinal());
chatIntent.putExtra("from", user.getMail());
startActivity(chatIntent);

} else {

LocoEvent event = eventMarkerMap.get(marker);
if(event != null) {
user = friendsMarkersMap.get(marker);
if(user != null) {

Intent chatIntent = new Intent(this, ChatActivity.class);
chatIntent.putExtra("type", EChatType.EVENT.ordinal());
chatIntent.putExtra("eventId", event.getId());
chatIntent.putExtra("type", EChatType.PRIVATE.ordinal());
chatIntent.putExtra("from", user.getMail());
startActivity(chatIntent);

} else {

LocoForum forum = forumMarkerMap.get(marker);
if(forum != null) {
LocoEvent event = eventMarkerMap.get(marker);
if (event != null) {

Intent chatIntent = new Intent(this, ChatActivity.class);
chatIntent.putExtra("type", EChatType.FORUM.ordinal());
chatIntent.putExtra("forumId", forum.getId());
chatIntent.putExtra("type", EChatType.EVENT.ordinal());
chatIntent.putExtra("eventId", event.getId());
startActivity(chatIntent);

} else {
Log.e(TAG, "Could not find marker in the current maps");

LocoForum forum = forumMarkerMap.get(marker);
if (forum != null) {

Intent chatIntent = new Intent(this, ChatActivity.class);
chatIntent.putExtra("type", EChatType.FORUM.ordinal());
chatIntent.putExtra("forumId", forum.getId());
startActivity(chatIntent);

} else {
Log.e(TAG, "Could not find marker in the current maps");
}

}

}
@@ -647,8 +661,8 @@ public void run() {
m.remove();
}

currentUserMap = new HashMap<Marker, LocoUser>();
reverseMarkersMap = new HashMap<String, Marker>();
currentUserMap = new HashMap<>();
reverseMarkersMap = new HashMap<>();

List<LocoUser> usersAroundMe = AppController.GetKnownUsersAround(myLoc, myRange);
inUserDrawingPhase = true;
@@ -717,8 +731,8 @@ public void Invoke(List<UserAroundMe> result) {
if (result != null) {

List<UserAroundMe> fResult = result;
List<LocoUser> users = new ArrayList<LocoUser>();
List<LocoUser> newUsers = new ArrayList<LocoUser>();
List<LocoUser> users = new ArrayList<>();
List<LocoUser> newUsers = new ArrayList<>();

for (UserAroundMe u : fResult) {
if (u.getMail().compareTo(AppController.GetMyUser().getMail()) != 0) {