directory changes to use number instead of token, group ui progress

This commit is contained in:
Jake McGinty
2014-02-06 18:06:23 -08:00
parent 0af473d880
commit 9cd5a67ec5
21 changed files with 581 additions and 316 deletions

View File

@@ -2,20 +2,18 @@ package org.thoughtcrime.securesms;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import org.thoughtcrime.securesms.components.PushRecipientsPanel;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
@@ -28,7 +26,6 @@ import org.thoughtcrime.securesms.util.SelectedRecipientsAdapter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
@@ -37,12 +34,17 @@ import static org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData;
public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActivity {
private final static String TAG = "GroupCreateActivity";
private final DynamicTheme dynamicTheme = new DynamicTheme();
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
private static final int PICK_CONTACT = 1;
private static final int SELECT_PHOTO = 100;
private ListView lv;
private static final int PICK_AVATAR = 2;
private ListView lv;
private PushRecipientsPanel recipientsPanel;
private ImageView avatar;
private Set<Recipient> selectedContacts;
@@ -69,7 +71,35 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv
private void initializeResources() {
lv = (ListView) findViewById(R.id.selected_contacts_list);
lv.setAdapter(new SelectedRecipientsAdapter(this, android.R.id.text1, new ArrayList<Recipient>()));
(findViewById(R.id.add_people_button)).setOnClickListener(new AddRecipientButtonListener());
recipientsPanel = (PushRecipientsPanel) findViewById(R.id.recipients);
recipientsPanel.setPanelChangeListener(new PushRecipientsPanel.RecipientsPanelChangedListener() {
@Override
public void onRecipientsPanelUpdate(Recipients recipients) {
Log.i(TAG, "onRecipientsPanelUpdate received.");
if (recipients != null) {
selectedContacts.addAll(recipients.getRecipientsList());
syncAdapterWithSelectedContacts();
}
}
});
(findViewById(R.id.contacts_button)).setOnClickListener(new AddRecipientButtonListener());
avatar = (ImageView) findViewById(R.id.avatar);
avatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra("aspectX", 1);
photoPickerIntent.putExtra("aspectY", 1);
photoPickerIntent.putExtra("outputX", 256);
photoPickerIntent.putExtra("outputY", 256);
photoPickerIntent.putExtra("return-data", "true");
startActivityForResult(photoPickerIntent, PICK_AVATAR);
}
});
}
@Override
@@ -103,6 +133,16 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv
return false;
}
private void syncAdapterWithSelectedContacts() {
SelectedRecipientsAdapter adapter = (SelectedRecipientsAdapter)lv.getAdapter();
adapter.clear();
for (Recipient contact : selectedContacts) {
adapter.add(contact);
Log.i("GroupCreateActivity", "Adding " + contact.getName() + "/" + contact.getNumber());
}
adapter.notifyDataSetChanged();
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
Log.w("ComposeMessageActivity", "onActivityResult called: " + resultCode + " , " + data);
@@ -114,10 +154,13 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv
switch (reqCode) {
case PICK_CONTACT:
List<ContactData> selected = data.getParcelableArrayListExtra("contacts");
for (ContactData cdata : selected) {
Log.i("PushContactSelect", "selected report: " + cdata.name);
}
for (ContactData contact : selected) {
for (ContactAccessor.NumberData numberData : contact.numbers) {
try {
Recipient recipient = RecipientFactory.getRecipientsFromString(this, numberData.number, true)
Recipient recipient = RecipientFactory.getRecipientsFromString(this, numberData.number, false)
.getPrimaryRecipient();
if (!selectedContacts.contains(recipient)) {
@@ -128,28 +171,14 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv
}
}
}
SelectedRecipientsAdapter adapter = (SelectedRecipientsAdapter)lv.getAdapter();
adapter.clear();
Iterator<Recipient> selectedContactsIter = selectedContacts.iterator();
while (selectedContactsIter.hasNext()) {
adapter.add(selectedContactsIter.next());
}
syncAdapterWithSelectedContacts();
break;
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap selectedBitmap = BitmapFactory.decodeFile(filePath);
case PICK_AVATAR:
if(resultCode == RESULT_OK) {
Bitmap avatarBmp = data.getParcelableExtra("data");
avatar.setImageBitmap(avatarBmp);
//Uri selectedImage = data.getData();
//avatar.setImageURI(selectedImage);
break;
}
}

View File

@@ -18,6 +18,7 @@ package org.thoughtcrime.securesms;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.recipients.Recipients;

View File

@@ -21,6 +21,8 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.MergeCursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
@@ -42,11 +44,7 @@ import com.actionbarsherlock.view.MenuItem;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData;
import org.thoughtcrime.securesms.contacts.ContactAccessor.NumberData;
import org.thoughtcrime.securesms.contacts.PushFilterCursorWrapper;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipients;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@@ -105,7 +103,6 @@ public class PushContactSelectionListFragment extends SherlockListFragment
return selected;
}
private void handleUnselectAll() {
selectedContacts.clear();
((CursorAdapter)getListView().getAdapter()).notifyDataSetChanged();
@@ -188,19 +185,35 @@ public class PushContactSelectionListFragment extends SherlockListFragment
@Override
public void bindView(View view, Context context, Cursor cursor) {
PushFilterCursorWrapper wrappedCursor = (PushFilterCursorWrapper) cursor;
boolean isPushUser = wrappedCursor.getPushCount() > wrappedCursor.getPosition();
boolean isPushUser;
try {
isPushUser = (cursor.getInt(cursor.getColumnIndexOrThrow(ContactAccessor.PUSH_COLUMN)) > 0);
} catch (IllegalArgumentException iae) {
isPushUser = false;
}
ContactData contactData = ContactAccessor.getInstance().getContactData(context, cursor);
((ContactItemView)view).set(contactData, isPushUser);
PushContactData pushContactData = new PushContactData(contactData, isPushUser);
((ContactItemView)view).set(pushContactData);
}
}
private class PushContactData {
private final ContactData contactData;
private final boolean pushSupport;
public PushContactData(ContactData contactData, boolean pushSupport) {
this.contactData = contactData;
this.pushSupport = pushSupport;
}
}
private class ContactItemView extends RelativeLayout {
private ContactData contactData;
private boolean pushSupport;
private CheckBox checkBox;
private TextView name;
private TextView number;
private TextView label;
private View pushLabel;
public ContactItemView(Context context) {
super(context);
@@ -211,6 +224,7 @@ public class PushContactSelectionListFragment extends SherlockListFragment
this.number = (TextView) findViewById(R.id.number);
this.label = (TextView) findViewById(R.id.label);
this.checkBox = (CheckBox) findViewById(R.id.check_box);
this.pushLabel = findViewById(R.id.push_support_label);
}
public void selected() {
@@ -225,17 +239,18 @@ public class PushContactSelectionListFragment extends SherlockListFragment
}
}
public void set(ContactData contactData, boolean isPushUser) {
this.contactData = contactData;
public void set(PushContactData pushContactData) {
this.contactData = pushContactData.contactData;
this.pushSupport = pushContactData.pushSupport;
if (!isPushUser) {
if (!pushSupport) {
this.name.setTextColor(0xa0000000);
this.number.setTextColor(0xa0000000);
this.checkBox.setVisibility(View.GONE);
this.pushLabel.setBackgroundColor(0x99000000);
} else {
this.name.setTextColor(0xff000000);
this.number.setTextColor(0xff000000);
this.checkBox.setVisibility(View.VISIBLE);
this.pushLabel.setBackgroundColor(0xff64a926);
}
if (selectedContacts.containsKey(contactData.id))
@@ -322,7 +337,8 @@ public class PushContactSelectionListFragment extends SherlockListFragment
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
((CursorAdapter) getListAdapter()).changeCursor(new PushFilterCursorWrapper(cursor, getActivity()));
Cursor pushCursor = ContactAccessor.getInstance().getCursorForContactsWithPush(getActivity());
((CursorAdapter) getListAdapter()).changeCursor(new MergeCursor(new Cursor[]{pushCursor,cursor}));
}
@Override

View File

@@ -9,6 +9,9 @@ import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.service.ApplicationMigrationService;
import org.thoughtcrime.securesms.service.DirectoryRefreshListener;
import org.thoughtcrime.securesms.service.DirectoryRefreshService;
import org.thoughtcrime.securesms.util.DirectoryHelper;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.textsecure.crypto.MasterSecret;

View File

@@ -0,0 +1,177 @@
/**
* Copyright (C) 2011 Whisper Systems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.components;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.contacts.RecipientsAdapter;
import org.thoughtcrime.securesms.contacts.RecipientsEditor;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.recipients.Recipients;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* Panel component combining both an editable field with a button for
* a list-based contact selector.
*
* @author Moxie Marlinspike
*/
public class PushRecipientsPanel extends RelativeLayout {
private final String TAG = "PushRecipientsPanel";
private RecipientsPanelChangedListener panelChangeListener;
private RecipientsEditor recipientsText;
private View panel;
private static final int RECIPIENTS_MAX_LENGTH = 312;
public PushRecipientsPanel(Context context) {
super(context);
initialize();
}
public PushRecipientsPanel(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public PushRecipientsPanel(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
public void addRecipient(String name, String number) {
Log.i(TAG, "addRecipient for " + name + "/" + number);
if (name != null) recipientsText.append(name + "< " + number + ">, ");
else recipientsText.append(number + ", ");
}
public void addContacts(List<ContactAccessor.ContactData> contacts) {
for (ContactAccessor.ContactData contact : contacts) {
for (ContactAccessor.NumberData number : contact.numbers) {
addRecipient(contact.name, number.number);
}
}
}
public void addRecipients(Recipients recipients) {
List<Recipient> recipientList = recipients.getRecipientsList();
Iterator<Recipient> iterator = recipientList.iterator();
while (iterator.hasNext()) {
Recipient recipient = iterator.next();
addRecipient(recipient.getName(), recipient.getNumber());
}
}
public Recipients getRecipients() throws RecipientFormattingException {
String rawText = recipientsText.getText().toString();
Recipients recipients = RecipientFactory.getRecipientsFromString(getContext(), rawText, false);
if (recipients.isEmpty())
throw new RecipientFormattingException("Recipient List Is Empty!");
return recipients;
}
public void disable() {
recipientsText.setText("");
panel.setVisibility(View.GONE);
}
public void setPanelChangeListener(RecipientsPanelChangedListener panelChangeListener) {
this.panelChangeListener = panelChangeListener;
}
private void initialize() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.push_recipients_panel, this, true);
View imageButton = findViewById(R.id.contacts_button);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
((MarginLayoutParams) imageButton.getLayoutParams()).topMargin = 0;
panel = findViewById(R.id.recipients_panel);
initRecipientsEditor();
}
private void initRecipientsEditor() {
Recipients recipients = null;
recipientsText = (RecipientsEditor)findViewById(R.id.recipients_text);
try {
recipients = getRecipients();
} catch (RecipientFormattingException e) {
recipients = new Recipients( new LinkedList<Recipient>() );
}
recipientsText.setAdapter(new RecipientsAdapter(this.getContext()));
recipientsText.populate(recipients);
recipientsText.setOnFocusChangeListener(new FocusChangedListener());
recipientsText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (panelChangeListener != null) {
try {
panelChangeListener.onRecipientsPanelUpdate(getRecipients());
} catch (RecipientFormattingException rfe) {
panelChangeListener.onRecipientsPanelUpdate(null);
}
}
recipientsText.setText("");
}
});
}
private class FocusChangedListener implements View.OnFocusChangeListener {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus && (panelChangeListener != null)) {
try {
panelChangeListener.onRecipientsPanelUpdate(getRecipients());
} catch (RecipientFormattingException rfe) {
panelChangeListener.onRecipientsPanelUpdate(null);
}
}
}
}
public interface RecipientsPanelChangedListener {
public void onRecipientsPanelUpdate(Recipients recipients);
}
}

View File

@@ -20,7 +20,9 @@ import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
@@ -36,6 +38,7 @@ import android.util.Log;
import org.whispersystems.textsecure.crypto.IdentityKey;
import org.whispersystems.textsecure.crypto.InvalidKeyException;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.util.Base64;
import java.io.IOException;
@@ -58,6 +61,8 @@ import java.util.List;
public class ContactAccessor {
public static final String PUSH_COLUMN = "push";
private static final ContactAccessor instance = new ContactAccessor();
public static synchronized ContactAccessor getInstance() {
@@ -85,6 +90,27 @@ public class ContactAccessor {
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
}
public Cursor getCursorForContactsWithPush(Context context) {
final ContentResolver resolver = context.getContentResolver();
final String[] inProjection = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME};
final String[] outProjection = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME, PUSH_COLUMN};
MatrixCursor cursor = new MatrixCursor(outProjection);
List<String> pushNumbers = Directory.getInstance(context).getActiveNumbers();
for (String pushNumber : pushNumbers) {
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(pushNumber));
Cursor lookupCursor = resolver.query(uri, inProjection, null, null, null);
try {
if (lookupCursor != null && lookupCursor.moveToFirst()) {
cursor.addRow(new Object[]{lookupCursor.getLong(0), lookupCursor.getString(1), 1});
}
} finally {
if (lookupCursor != null)
lookupCursor.close();
}
}
return cursor;
}
public String getNameFromContact(Context context, Uri uri) {
Cursor cursor = null;

View File

@@ -1,106 +0,0 @@
package org.thoughtcrime.securesms.contacts;
import android.content.Context;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.os.Debug;
import android.provider.ContactsContract;
import android.util.Log;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.InvalidNumberException;
import java.util.List;
public class PushFilterCursorWrapper extends CursorWrapper {
private int[] pushIndex;
private int[] normalIndex;
private int count = 0;
private int pushCount = 0;
private int pos = 0;
private final ContactAccessor contactAccessor = ContactAccessor.getInstance();
/*
* Don't know of a better way to do this without a large filtering through the entire dataset at first
*/
public PushFilterCursorWrapper(Cursor cursor, Context context) {
super(cursor);
this.count = super.getCount();
this.pushIndex = new int[this.count];
this.normalIndex = new int[this.count];
int pushPos = 0;
int normalPos = 0;
for (int i = 0; i < this.count; i++) {
super.moveToPosition(i);
List<ContactAccessor.NumberData> numbers = contactAccessor.getContactData(context, cursor).numbers;
if (numbers.size() > 0) {
try {
if (Util.isPushTransport(context, Util.canonicalizeNumber(context, numbers.get(0).number)))
this.pushIndex[pushPos++] = i;
else
this.normalIndex[normalPos++] = i;
} catch (InvalidNumberException ine) {
}
}
}
this.pushCount = pushPos;
super.moveToFirst();
}
@Override
public boolean move(int offset) {
return this.moveToPosition(this.pos + offset);
}
@Override
public boolean moveToNext() {
return this.moveToPosition(this.pos + 1);
}
@Override
public boolean moveToPrevious() {
return this.moveToPosition(this.pos - 1);
}
@Override
public boolean moveToFirst() {
return this.moveToPosition(0);
}
@Override
public boolean moveToLast() {
return this.moveToPosition(this.count - 1);
}
private int getPostFilteredPosition(int preFilteredPosition) {
return preFilteredPosition < this.pushCount
? this.pushIndex[preFilteredPosition]
: this.normalIndex[preFilteredPosition - pushCount];
}
@Override
public boolean moveToPosition(int position) {
if (position >= this.count || position < 0)
return false;
pos = position;
return super.moveToPosition(getPostFilteredPosition(position));
}
@Override
public int getCount() {
return this.count;
}
public int getPushCount() {
return this.pushCount;
}
@Override
public int getPosition() {
return this.pos;
}
}

View File

@@ -14,6 +14,7 @@ import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.textsecure.crypto.InvalidVersionException;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.directory.NotInDirectoryException;
import org.whispersystems.textsecure.push.ContactNumberDetails;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.push.IncomingEncryptedPushMessage;
import org.whispersystems.textsecure.push.IncomingPushMessage;
@@ -68,9 +69,9 @@ public class GcmIntentService extends GCMBaseIntentService {
if (!isActiveNumber(context, message.getSource())) {
Directory directory = Directory.getInstance(context);
String contactToken = directory.getToken(message.getSource());
ContactTokenDetails contactTokenDetails = new ContactTokenDetails(contactToken, message.getRelay());
directory.setToken(contactTokenDetails, true);
String contactNumber = message.getSource();
ContactNumberDetails contactNumberDetails = new ContactNumberDetails(contactNumber, message.getRelay());
directory.setNumber(contactNumberDetails, true);
}
Intent service = new Intent(context, SendReceiveService.class);

View File

@@ -7,16 +7,9 @@ import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;
import org.thoughtcrime.securesms.Release;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.DirectoryHelper;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.push.PushServiceSocket;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@@ -59,19 +52,8 @@ public class DirectoryRefreshService extends Service {
public void run() {
try {
Log.w("DirectoryRefreshService", "Refreshing directory...");
Directory directory = Directory.getInstance(context);
PushServiceSocket socket = PushServiceSocketFactory.create(context);
Set<String> eligibleContactTokens = directory.getPushEligibleContactTokens(TextSecurePreferences.getLocalNumber(context));
List<ContactTokenDetails> activeTokens = socket.retrieveDirectory(eligibleContactTokens);
if (activeTokens != null) {
for (ContactTokenDetails activeToken : activeTokens) {
eligibleContactTokens.remove(activeToken.getToken());
}
directory.setTokens(activeTokens, eligibleContactTokens);
}
DirectoryHelper.refreshDirectory(context);
Log.w("DirectoryRefreshService", "Directory refresh complete...");
} finally {

View File

@@ -17,13 +17,12 @@ import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.gcm.GcmIntentService;
import org.thoughtcrime.securesms.gcm.GcmRegistrationTimeoutException;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.util.DirectoryHelper;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.textsecure.crypto.IdentityKey;
import org.whispersystems.textsecure.crypto.MasterSecret;
import org.whispersystems.textsecure.crypto.PreKeyUtil;
import org.whispersystems.textsecure.crypto.ecc.Curve;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.storage.PreKeyRecord;
import org.whispersystems.textsecure.util.Util;
@@ -286,15 +285,7 @@ public class RegistrationService extends Service {
String gcmRegistrationId = waitForGcmRegistrationId();
socket.registerGcmId(gcmRegistrationId);
Set<String> contactTokens = Directory.getInstance(this).getPushEligibleContactTokens(number);
List<ContactTokenDetails> activeTokens = socket.retrieveDirectory(contactTokens);
if (activeTokens != null) {
for (ContactTokenDetails activeToken : activeTokens) {
contactTokens.remove(activeToken.getToken());
}
Directory.getInstance(this).setTokens(activeTokens, contactTokens);
}
DirectoryHelper.refreshDirectory(this, socket, number);
DirectoryRefreshListener.schedule(this);
}

View File

@@ -29,8 +29,10 @@ import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.crypto.MasterSecret;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.directory.NotInDirectoryException;
import org.whispersystems.textsecure.push.ContactNumberDetails;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.util.DirectoryUtil;
import org.whispersystems.textsecure.util.InvalidNumberException;
import java.io.IOException;
@@ -138,18 +140,13 @@ public class UniversalTransport {
return directory.isActiveNumber(destination);
} catch (NotInDirectoryException e) {
try {
PushServiceSocket socket = PushServiceSocketFactory.create(context);
String contactToken = directory.getToken(destination);
ContactTokenDetails registeredUser = socket.getContactTokenDetails(contactToken);
PushServiceSocket socket = PushServiceSocketFactory.create(context);
ContactTokenDetails registeredUser = socket.getContactTokenDetails(DirectoryUtil.getDirectoryServerToken(destination));
boolean registeredFound = !(registeredUser == null);
ContactNumberDetails numberDetails = new ContactNumberDetails(destination, registeredUser == null ? null : registeredUser.getRelay());
if (registeredUser == null) {
registeredUser = new ContactTokenDetails(contactToken);
directory.setToken(registeredUser, false);
return false;
} else {
directory.setToken(registeredUser, true);
return true;
}
directory.setNumber(numberDetails, registeredFound);
return registeredFound;
} catch (IOException e1) {
Log.w("UniversalTransport", e1);
return false;

View File

@@ -0,0 +1,43 @@
package org.thoughtcrime.securesms.util;
import android.content.Context;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.push.ContactNumberDetails;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.util.DirectoryUtil;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DirectoryHelper {
public static void refreshDirectory(final Context context) {
refreshDirectory(context, PushServiceSocketFactory.create(context));
}
public static void refreshDirectory(final Context context, final PushServiceSocket socket) {
refreshDirectory(context, socket, TextSecurePreferences.getLocalNumber(context));
}
public static void refreshDirectory(final Context context, final PushServiceSocket socket, final String localNumber) {
final Directory directory = Directory.getInstance(context);
final Set<String> eligibleContactNumbers = directory.getPushEligibleContactNumbers(localNumber);
final Map<String, String> tokenMap = DirectoryUtil.getDirectoryServerTokenMap(eligibleContactNumbers);
final List<ContactTokenDetails> activeTokens = socket.retrieveDirectory(tokenMap.keySet());
if (activeTokens != null) {
final List<ContactNumberDetails> activeNumbers = ContactNumberDetails.fromContactTokenDetailsList(activeTokens, tokenMap);
for (ContactTokenDetails activeToken : activeTokens) {
eligibleContactNumbers.remove(tokenMap.get(activeToken.getToken()));
}
directory.setNumbers(activeNumbers, eligibleContactNumbers);
}
}
}

View File

@@ -23,16 +23,9 @@ import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.Log;
import android.widget.EditText;
import android.os.Build;
import android.provider.Telephony;
import org.thoughtcrime.securesms.mms.MmsRadio;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.directory.NotInDirectoryException;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.util.InvalidNumberException;
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
@@ -40,7 +33,6 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@@ -156,31 +148,6 @@ public class Util {
(context.getPackageName().equals(Telephony.Sms.getDefaultSmsPackage(context)));
}
public static boolean isPushTransport(Context context, String destination) {
Directory directory = Directory.getInstance(context);
try {
return directory.isActiveNumber(destination);
} catch (NotInDirectoryException e) {
try {
PushServiceSocket socket = PushServiceSocketFactory.create(context);
String contactToken = directory.getToken(destination);
ContactTokenDetails registeredUser = socket.getContactTokenDetails(contactToken);
if (registeredUser == null) {
registeredUser = new ContactTokenDetails(contactToken);
directory.setToken(registeredUser, false);
return false;
} else {
directory.setToken(registeredUser, true);
return true;
}
} catch (IOException e1) {
Log.w("UniversalTransport", e1);
return false;
}
}
}
// public static Bitmap loadScaledBitmap(InputStream src, int targetWidth, int targetHeight) {
// return BitmapFactory.decodeStream(src);
//// BitmapFactory.Options options = new BitmapFactory.Options();