Skip to content

Commit

Permalink
group updates do things
Browse files Browse the repository at this point in the history
// FREEBIE
  • Loading branch information
mcginty committed Feb 24, 2014
1 parent 0ae1004 commit 86b3de2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/org/thoughtcrime/securesms/ConversationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
import org.whispersystems.textsecure.crypto.MasterSecret;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.directory.NotInDirectoryException;
import org.whispersystems.textsecure.push.PushMessageProtos;
import org.whispersystems.textsecure.storage.RecipientDevice;
import org.whispersystems.textsecure.storage.Session;
import org.whispersystems.textsecure.storage.SessionRecordV2;
Expand Down Expand Up @@ -226,7 +225,7 @@ protected void onDestroy() {

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
Log.w("ComposeMessageActivity", "onActivityResult called: " + resultCode + " , " + data);
Log.w(TAG, "onActivityResult called: " + reqCode + ", " + resultCode + " , " + data);
super.onActivityResult(reqCode, resultCode, data);

if (data == null || resultCode != RESULT_OK) return;
Expand All @@ -249,7 +248,7 @@ public void onActivityResult(int reqCode, int resultCode, Intent data) {
addContactInfo(data.getData());
break;
case GROUP_EDIT:
this.recipients = RecipientFactory.getRecipientsForIds(this, String.valueOf(getRecipients().getPrimaryRecipient().getRecipientId()), false);
this.recipients = data.getParcelableExtra(GroupCreateActivity.GROUP_RECIPIENT_EXTRA);
initializeTitleBar();
break;
}
Expand Down Expand Up @@ -947,7 +946,7 @@ private boolean isActiveGroup() {
byte[] groupId = GroupUtil.getDecodedId(getRecipients().getPrimaryRecipient().getNumber());
GroupRecord record = DatabaseFactory.getGroupDatabase(this).getGroup(groupId);

return record.isActive();
return record != null && record.isActive();
} catch (IOException e) {
Log.w("ConversationActivity", e);
return false;
Expand Down
21 changes: 20 additions & 1 deletion src/org/thoughtcrime/securesms/GroupCreateActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.recipients.RecipientProvider;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.sms.MessageSender;
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage;
Expand Down Expand Up @@ -583,6 +582,26 @@ protected Pair<Long, Recipients> doInBackground(Void... params) {
return new Pair<Long,Recipients>(RES_BAD_NUMBER, null);
}
}

@Override
protected void onPostExecute(Pair<Long, Recipients> groupInfo) {
final long threadId = groupInfo.first;
final Recipients recipients = groupInfo.second;
if (threadId > -1) {
Intent intent = getIntent();
intent.putExtra(GROUP_THREAD_EXTRA, threadId);
intent.putExtra(GROUP_RECIPIENT_EXTRA, recipients);
setResult(RESULT_OK, intent);
finish();
} else if (threadId == RES_BAD_NUMBER) {
Toast.makeText(getApplicationContext(), R.string.GroupCreateActivity_contacts_invalid_number, Toast.LENGTH_LONG).show();
disableWhisperGroupCreatingUi();
} else if (threadId == RES_MMS_EXCEPTION) {
Toast.makeText(getApplicationContext(), R.string.GroupCreateActivity_contacts_mms_exception, Toast.LENGTH_LONG).show();
setResult(RESULT_CANCELED);
finish();
}
}
}

private class CreateWhisperGroupAsyncTask extends AsyncTask<Void,Void,Pair<Long,Recipients>> {
Expand Down
40 changes: 38 additions & 2 deletions src/org/thoughtcrime/securesms/database/GroupDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import org.thoughtcrime.securesms.recipients.Recipient;
Expand All @@ -27,6 +28,7 @@
import static org.whispersystems.textsecure.push.PushMessageProtos.PushMessageContent.AttachmentPointer;

public class GroupDatabase extends Database {
private static final String TAG = GroupDatabase.class.getSimpleName();

private static final String TABLE_NAME = "groups";
private static final String ID = "_id";
Expand Down Expand Up @@ -128,7 +130,7 @@ public void create(byte[] groupId, String owner, String title,

public void update(byte[] groupId, String title, AttachmentPointer avatar) {
ContentValues contentValues = new ContentValues();
if (title != null) contentValues.put(TITLE, title);
if (title != null) contentValues.put(TITLE, title);

if (avatar != null) {
contentValues.put(AVATAR_ID, avatar.getId());
Expand All @@ -139,20 +141,30 @@ public void update(byte[] groupId, String title, AttachmentPointer avatar) {
databaseHelper.getWritableDatabase().update(TABLE_NAME, contentValues,
GROUP_ID + " = ?",
new String[] {GroupUtil.getEncodedId(groupId)});

if (title != null) updateGroupRecipientTitle(groupId, title);
}

public void updateTitle(byte[] groupId, String title) {
ContentValues contentValues = new ContentValues();
contentValues.put(TITLE, title);
databaseHelper.getWritableDatabase().update(TABLE_NAME, contentValues, GROUP_ID + " = ?",
new String[] {GroupUtil.getEncodedId(groupId)});

if (title != null) updateGroupRecipientTitle(groupId, title);
}

public void updateAvatar(byte[] groupId, Bitmap avatar) {
updateAvatar(groupId, BitmapUtil.toByteArray(avatar));
updateAvatarInDatabase(groupId, BitmapUtil.toByteArray(avatar));
updateGroupRecipientAvatar(groupId, avatar);
}

public void updateAvatar(byte[] groupId, byte[] avatar) {
updateAvatarInDatabase(groupId, avatar);
updateGroupRecipientAvatar(groupId, BitmapFactory.decodeByteArray(avatar, 0, avatar.length));
}

private void updateAvatarInDatabase(byte[] groupId, byte[] avatar) {
ContentValues contentValues = new ContentValues();
contentValues.put(AVATAR, avatar);

Expand Down Expand Up @@ -330,4 +342,28 @@ public boolean isActive() {
return active;
}
}

private Recipient getGroupRecipient(byte[] groupId) {
try {
return RecipientFactory.getRecipientsFromString(context, GroupUtil.getEncodedId(groupId), true)
.getPrimaryRecipient();
} catch (RecipientFormattingException e) {
Log.w(TAG, e);
return null;
}
}

private void updateGroupRecipientTitle(byte[] groupId, String title) {
Recipient groupRecipient = getGroupRecipient(groupId);
Log.i(TAG, "updating group recipient title for recipient " + System.identityHashCode(groupRecipient));
if (groupRecipient != null) groupRecipient.setName(title);
else Log.w(TAG, "Couldn't update group title because recipient couldn't be found.");
}

private void updateGroupRecipientAvatar(byte[] groupId, Bitmap photo) {
Recipient groupRecipient = getGroupRecipient(groupId);
if (groupRecipient != null) groupRecipient.setContactPhoto(photo);
else Log.w(TAG, "Couldn't update group title because recipient couldn't be found.");
}

}
7 changes: 6 additions & 1 deletion src/org/thoughtcrime/securesms/recipients/Recipient.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public synchronized Uri getContactUri() {

public synchronized void setContactPhoto(Bitmap bitmap) {
this.contactPhoto = bitmap;
notifyListeners();
}

public synchronized void setName(String name) {
this.name = name;
notifyListeners();
}

public synchronized String getName() {
Expand Down Expand Up @@ -203,5 +209,4 @@ public int hashCode() {
public static interface RecipientModifiedListener {
public void onModified(Recipient recipient);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.util.NumberUtil;
import org.whispersystems.textsecure.push.IncomingPushMessage;
import org.whispersystems.textsecure.util.Util;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public void process(MasterSecret masterSecret, Intent intent) {
Recipient groupRecipient = RecipientFactory.getRecipientsFromString(context, GroupUtil.getEncodedId(groupId), true)
.getPrimaryRecipient();
groupRecipient.setContactPhoto(avatar);
groupRecipient.notifyListeners();
} catch (RecipientFormattingException e) {
Log.w("AvatarDownloader", e);
}
Expand Down

0 comments on commit 86b3de2

Please sign in to comment.