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

@ -7,39 +7,41 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.telephony.TelephonyManager;
import android.util.Log;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.util.Base64;
import org.whispersystems.textsecure.push.ContactNumberDetails;
import org.whispersystems.textsecure.util.DirectoryUtil;
import org.whispersystems.textsecure.util.InvalidNumberException;
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
import org.whispersystems.textsecure.util.Util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Directory {
private static final int INTRODUCED_CHANGE_FROM_TOKEN_TO_E164_NUMBER = 2;
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "whisper_directory.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "directory";
private static final String ID = "_id";
private static final String TOKEN = "token";
private static final String NUMBER = "number";
private static final String REGISTERED = "registered";
private static final String RELAY = "relay";
private static final String SUPPORTS_SMS = "supports_sms";
private static final String TIMESTAMP = "timestamp";
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY, " +
TOKEN + " TEXT UNIQUE, " +
REGISTERED + " INTEGER, " +
RELAY + " TEXT, " +
SUPPORTS_SMS + " INTEGER, " +
TIMESTAMP + " INTEGER);";
NUMBER + " TEXT UNIQUE, " +
REGISTERED + " INTEGER, " +
RELAY + " TEXT, " +
SUPPORTS_SMS + " INTEGER, " +
TIMESTAMP + " INTEGER);";
private static final Object instanceLock = new Object();
private static volatile Directory instance;
@ -60,7 +62,7 @@ public class Directory {
private final Context context;
private Directory(Context context) {
this.context = context;
this.context = context;
this.databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@ -69,14 +71,13 @@ public class Directory {
return false;
}
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String token = getToken(e164number);
Cursor cursor = null;
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.query(TABLE_NAME,
new String[] {REGISTERED}, TOKEN + " = ?",
new String[] {token}, null, null, null);
new String[]{REGISTERED}, NUMBER + " = ?",
new String[] {e164number}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
return cursor.getInt(0) == 1;
@ -91,12 +92,11 @@ public class Directory {
}
public String getRelay(String e164number) {
String token = getToken(e164number);
SQLiteDatabase database = databaseHelper.getReadableDatabase();
Cursor cursor = null;
try {
cursor = database.query(TABLE_NAME, null, TOKEN + " = ?", new String[]{token}, null, null, null);
cursor = database.query(TABLE_NAME, null, NUMBER + " = ?", new String[]{e164number}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
return cursor.getString(cursor.getColumnIndexOrThrow(RELAY));
@ -109,10 +109,10 @@ public class Directory {
}
}
public void setToken(ContactTokenDetails token, boolean active) {
public void setNumber(ContactNumberDetails token, boolean active) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TOKEN, token.getToken());
values.put(NUMBER, token.getNumber());
values.put(RELAY, token.getRelay());
values.put(REGISTERED, active ? 1 : 0);
values.put(SUPPORTS_SMS, token.isSupportsSms() ? 1 : 0);
@ -120,16 +120,16 @@ public class Directory {
db.replace(TABLE_NAME, null, values);
}
public void setTokens(List<ContactTokenDetails> activeTokens, Collection<String> inactiveTokens) {
public void setNumbers(List<ContactNumberDetails> activeTokens, Collection<String> inactiveTokens) {
long timestamp = System.currentTimeMillis();
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.beginTransaction();
try {
for (ContactTokenDetails token : activeTokens) {
for (ContactNumberDetails token : activeTokens) {
Log.w("Directory", "Adding active token: " + token);
ContentValues values = new ContentValues();
values.put(TOKEN, token.getToken());
values.put(NUMBER, token.getNumber());
values.put(REGISTERED, 1);
values.put(TIMESTAMP, timestamp);
values.put(RELAY, token.getRelay());
@ -139,7 +139,7 @@ public class Directory {
for (String token : inactiveTokens) {
ContentValues values = new ContentValues();
values.put(TOKEN, token);
values.put(NUMBER, token);
values.put(REGISTERED, 0);
values.put(TIMESTAMP, timestamp);
db.replace(TABLE_NAME, null, values);
@ -151,21 +151,20 @@ public class Directory {
}
}
public Set<String> getPushEligibleContactTokens(String localNumber) {
Uri uri = Phone.CONTENT_URI;
Set<String> results = new HashSet<String>();
Cursor cursor = null;
public Set<String> getPushEligibleContactNumbers(String localNumber) {
final Uri uri = Phone.CONTENT_URI;
final Set<String> results = new HashSet<String>();
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, new String[] {Phone.NUMBER}, null, null, null);
while (cursor != null && cursor.moveToNext()) {
String rawNumber = cursor.getString(0);
final String rawNumber = cursor.getString(0);
if (rawNumber != null) {
try {
String e164Number = PhoneNumberFormatter.formatNumber(rawNumber, localNumber);
results.add(getToken(e164Number));
final String e164Number = PhoneNumberFormatter.formatNumber(rawNumber, localNumber);
results.add(e164Number);
} catch (InvalidNumberException e) {
Log.w("Directory", "Invalid number: " + rawNumber);
}
@ -175,11 +174,14 @@ public class Directory {
if (cursor != null)
cursor.close();
cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, new String[] {TOKEN},
null, null, null, null, null);
final SQLiteDatabase readableDb = databaseHelper.getReadableDatabase();
if (readableDb != null) {
cursor = readableDb.query(TABLE_NAME, new String[]{NUMBER},
null, null, null, null, null);
while (cursor != null && cursor.moveToNext()) {
results.add(cursor.getString(0));
while (cursor != null && cursor.moveToNext()) {
results.add(cursor.getString(0));
}
}
return results;
@ -189,13 +191,20 @@ public class Directory {
}
}
public String getToken(String e164number) {
public List<String> getActiveNumbers() {
final List<String> results = new ArrayList<String>();
Cursor cursor = null;
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);
cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, new String[]{NUMBER},
REGISTERED + " = 1", null, null, null, null);
while (cursor != null && cursor.moveToNext()) {
results.add(cursor.getString(0));
}
return results;
} finally {
if (cursor != null)
cursor.close();
}
}
@ -215,7 +224,10 @@ public class Directory {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < INTRODUCED_CHANGE_FROM_TOKEN_TO_E164_NUMBER) {
db.execSQL("DROP TABLE directory;");
onCreate(db);
}
}
}

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -2,40 +2,24 @@ package org.whispersystems.textsecure.push;
import com.google.thoughtcrimegson.Gson;
public class ContactTokenDetails {
public class ContactTokenDetails extends ContactDetails {
private String token;
private String relay;
private boolean supportsSms;
public ContactTokenDetails() {}
public ContactTokenDetails() { super(); }
public ContactTokenDetails(String token) {
super();
this.token = token;
}
public ContactTokenDetails(String token, String relay) {
super(relay);
this.token = token;
this.relay = relay;
}
public String getToken() {
return token;
}
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);
}
}

View file

@ -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;
}
}

View file

@ -6,36 +6,32 @@
android:layout_height="match_parent"
android:padding="20dp">
<EditText
android:id="@+id/group_name"
android:layout_width="match_parent"
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="Group name"
android:layout_marginBottom="20dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginBottom="20dp" >
<ImageView
android:id="@+id/avatar"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/icon"/>
<EditText
android:id="@+id/group_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Add people"
android:padding="10dp" />
android:padding="10dp"
android:hint="Group name"
android:layout_gravity="center_vertical" />
</LinearLayout>
<ImageButton
android:id="@+id/add_people_button"
android:background="#00ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="15dp"
android:src="@drawable/ic_action_add_person"
android:layout_alignParentRight="true" />
</RelativeLayout>
<org.thoughtcrime.securesms.components.PushRecipientsPanel
android:id="@+id/recipients"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/selected_contacts_list"

View file

@ -3,9 +3,16 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingLeft="14dip"
android:paddingRight="25dip">
<View android:id="@+id/push_support_label"
android:layout_height="fill_parent"
android:layout_width="3dip"
android:layout_alignParentLeft="true"
android:background="#ff64a926"
android:visibility="visible"
/>
<TextView android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -13,6 +20,7 @@
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dip"
android:layout_marginTop="-8dip"
android:layout_marginLeft="14dip"
android:singleLine="true"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceSmall"
@ -26,6 +34,7 @@
android:layout_height="wrap_content"
android:layout_marginBottom="8dip"
android:layout_marginTop="-8dip"
android:layout_marginLeft="14dip"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:singleLine="true"
@ -41,6 +50,7 @@
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="1dip"
android:layout_marginLeft="14dip"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:singleLine="true"
android:ellipsize="marquee"

View file

@ -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>

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();