directory changes to use number instead of token, group ui progress
parent
0af473d880
commit
9cd5a67ec5
@ -0,0 +1,31 @@
|
||||
package org.whispersystems.textsecure.push;
|
||||
|
||||
import com.google.thoughtcrimegson.Gson;
|
||||
|
||||
public abstract class ContactDetails {
|
||||
|
||||
private String relay;
|
||||
private boolean supportsSms;
|
||||
|
||||
public ContactDetails() {}
|
||||
|
||||
public ContactDetails(String relay) {
|
||||
this.relay = relay;
|
||||
}
|
||||
|
||||
public String getRelay() {
|
||||
return relay;
|
||||
}
|
||||
|
||||
public void setRelay(String relay) {
|
||||
this.relay = relay;
|
||||
}
|
||||
|
||||
public boolean isSupportsSms() {
|
||||
return supportsSms;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return new Gson().toJson(this);
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package org.whispersystems.textsecure.push;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ContactNumberDetails extends ContactDetails {
|
||||
private static final String TAG = "ContactNumberDetails";
|
||||
|
||||
private String number;
|
||||
|
||||
public ContactNumberDetails() { super(); }
|
||||
|
||||
public ContactNumberDetails(String number) {
|
||||
super();
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public ContactNumberDetails(String number, String relay) {
|
||||
super(relay);
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public static List<ContactNumberDetails> fromContactTokenDetailsList(List<ContactTokenDetails> contactTokenDetails, final Map<String, String> tokenMap) {
|
||||
if (contactTokenDetails == null || tokenMap == null) return null;
|
||||
|
||||
List<ContactNumberDetails> contactNumberDetails = new ArrayList<ContactNumberDetails>(contactTokenDetails.size());
|
||||
for (ContactTokenDetails tokenDetails : contactTokenDetails) {
|
||||
if (tokenMap.containsKey(tokenDetails.getToken()))
|
||||
contactNumberDetails.add(new ContactNumberDetails(tokenMap.get(tokenDetails.getToken()), tokenDetails.getRelay()));
|
||||
else
|
||||
Log.w(TAG, "tokenMap was missing a contact.");
|
||||
}
|
||||
return contactNumberDetails;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package org.whispersystems.textsecure.util;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class DirectoryUtil {
|
||||
|
||||
public static String getDirectoryServerToken(String e164number) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA1");
|
||||
byte[] token = Util.trim(digest.digest(e164number.getBytes()), 10);
|
||||
return Base64.encodeBytesWithoutPadding(token);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a mapping of directory server tokens to their requested number.
|
||||
* @param e164numbers
|
||||
* @return map with token as key, E164 number as value
|
||||
*/
|
||||
public static Map<String, String> getDirectoryServerTokenMap(Collection<String> e164numbers) {
|
||||
final Map<String,String> tokenMap = new HashMap<String,String>(e164numbers.size());
|
||||
for (String number : e164numbers) {
|
||||
tokenMap.put(getDirectoryServerToken(number), number);
|
||||
}
|
||||
return tokenMap;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/recipients_panel"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical" >
|
||||
|
||||
<org.thoughtcrime.securesms.contacts.RecipientsEditor android:id="@+id/recipients_text"
|
||||
android:layout_height="wrap_content"
|
||||
android:capitalize="sentences"
|
||||
android:autoText="true"
|
||||
android:singleLine="true"
|
||||
android:hint="@string/recipients_panel__to"
|
||||
android:paddingRight="45dip"
|
||||
android:textColor="?conversation_editor_text_color"
|
||||
android:layout_width="fill_parent"/>
|
||||
|
||||
<ImageButton android:id="@+id/contacts_button"
|
||||
android:background="#00000000"
|
||||
android:layout_width="40dip"
|
||||
android:layout_height="35dip"
|
||||
android:layout_marginRight="5dip"
|
||||
android:layout_marginTop="4dip"
|
||||
android:src="@drawable/ic_menu_add_field_holo_light"
|
||||
android:layout_alignRight="@id/recipients_text"
|
||||
android:maxWidth="32dip"
|
||||
android:maxHeight="32dip" />
|
||||
|
||||
</RelativeLayout>
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue