Support for static RecipientDetails for certain shortcodes.

Closes #3813
// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-07-27 10:49:14 -07:00
parent e2e5aa32a8
commit bbf33f88e0
9 changed files with 26 additions and 8 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 704 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

@ -6,6 +6,8 @@ import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.provider.ContactsContract;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
@ -17,6 +19,7 @@ import org.thoughtcrime.securesms.util.BitmapUtil;
import java.io.InputStream;
public class ContactPhotoFactory {
private static final String TAG = ContactPhotoFactory.class.getSimpleName();
public static ContactPhoto getLoadingPhoto() {
@ -28,6 +31,10 @@ public class ContactPhotoFactory {
else return new GeneratedContactPhoto("#");
}
public static ContactPhoto getResourceContactPhoto(@DrawableRes int resourceId) {
return new ResourceContactPhoto(resourceId);
}
public static ContactPhoto getDefaultGroupPhoto() {
return new ResourceContactPhoto(R.drawable.ic_group_white_24dp);
}

View File

@ -6,6 +6,7 @@ import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.annotation.DrawableRes;
import android.support.v4.graphics.ColorUtils;
import android.widget.ImageView;
@ -16,7 +17,7 @@ public class ResourceContactPhoto implements ContactPhoto {
private final int resourceId;
ResourceContactPhoto(int resourceId) {
ResourceContactPhoto(@DrawableRes int resourceId) {
this.resourceId = resourceId;
}

View File

@ -22,6 +22,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.util.Log;
@ -97,7 +98,7 @@ public class CanonicalAddressDatabase {
}
}
public String getAddressFromId(long id) {
public @NonNull String getAddressFromId(long id) {
String cachedAddress = idCache.get(id);
if (cachedAddress != null)

View File

@ -25,7 +25,9 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.color.MaterialColor;
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
@ -40,7 +42,7 @@ import org.whispersystems.libaxolotl.util.guava.Optional;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -62,6 +64,12 @@ public class RecipientProvider {
PhoneLookup.NUMBER
};
private static final Map<String, RecipientDetails> STATIC_DETAILS = new HashMap<String, RecipientDetails>() {{
put("262966", new RecipientDetails("Amazon", "262966", null,
ContactPhotoFactory.getResourceContactPhoto(R.drawable.ic_amazon),
ContactColors.UNKNOWN_COLOR));
}};
Recipient getRecipient(Context context, long recipientId, boolean asynchronous) {
Recipient cachedRecipient = recipientCache.get(recipientId);
if (cachedRecipient != null && !cachedRecipient.isStale()) return cachedRecipient;
@ -102,7 +110,7 @@ public class RecipientProvider {
private @NonNull ListenableFutureTask<RecipientDetails> getRecipientDetailsAsync(final Context context,
final long recipientId,
final String number)
final @NonNull String number)
{
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
@Override
@ -116,12 +124,12 @@ public class RecipientProvider {
return future;
}
private @NonNull RecipientDetails getRecipientDetailsSync(Context context, long recipientId, String number) {
private @NonNull RecipientDetails getRecipientDetailsSync(Context context, long recipientId, @NonNull String number) {
if (GroupUtil.isEncodedGroup(number)) return getGroupRecipientDetails(context, number);
else return getIndividualRecipientDetails(context, recipientId, number);
}
private @NonNull RecipientDetails getIndividualRecipientDetails(Context context, long recipientId, String number) {
private @NonNull RecipientDetails getIndividualRecipientDetails(Context context, long recipientId, @NonNull String number) {
Optional<RecipientsPreferences> preferences = DatabaseFactory.getRecipientPreferenceDatabase(context).getRecipientsPreferences(new long[]{recipientId});
MaterialColor color = preferences.isPresent() ? preferences.get().getColor() : null;
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
@ -143,7 +151,8 @@ public class RecipientProvider {
cursor.close();
}
return new RecipientDetails(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(null), color);
if (STATIC_DETAILS.containsKey(number)) return STATIC_DETAILS.get(number);
else return new RecipientDetails(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(null), color);
}
private @NonNull RecipientDetails getGroupRecipientDetails(Context context, String groupId) {