Don't crash direct share service if avatar photo is missing

Fixes #7541
This commit is contained in:
Moxie Marlinspike 2018-03-19 13:17:57 -07:00
parent 26a16bf0ca
commit 261296e41b

View File

@ -11,7 +11,9 @@ import android.os.Bundle;
import android.os.Parcel;
import android.service.chooser.ChooserTarget;
import android.service.chooser.ChooserTargetService;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.util.Log;
import org.thoughtcrime.securesms.ShareActivity;
import org.thoughtcrime.securesms.database.DatabaseFactory;
@ -27,6 +29,9 @@ import java.util.concurrent.ExecutionException;
@RequiresApi(api = Build.VERSION_CODES.M)
public class DirectShareService extends ChooserTargetService {
private static final String TAG = DirectShareService.class.getSimpleName();
@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
IntentFilter matchedFilter)
@ -41,13 +46,13 @@ public class DirectShareService extends ChooserTargetService {
ThreadRecord record;
while ((record = reader.getNext()) != null && results.size() < 10) {
try {
Recipient recipient = Recipient.from(this, record.getRecipient().getAddress(), false);
String name = recipient.toShortString();
Bitmap avatar;
if (recipient.getContactPhoto() != null) {
try {
avatar = GlideApp.with(this)
.asBitmap()
.load(recipient.getContactPhoto())
@ -55,10 +60,12 @@ public class DirectShareService extends ChooserTargetService {
.submit(getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width))
.get();
} catch (InterruptedException | ExecutionException e) {
Log.w(TAG, e);
avatar = getFallbackDrawable(recipient);
}
} else {
avatar = BitmapUtil.createFromDrawable(recipient.getFallbackContactPhotoDrawable(this, false),
getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height));
avatar = getFallbackDrawable(recipient);
}
Parcel parcel = Parcel.obtain();
@ -72,9 +79,7 @@ public class DirectShareService extends ChooserTargetService {
results.add(new ChooserTarget(name, Icon.createWithBitmap(avatar), 1.0f, componentName, bundle));
parcel.recycle();
} catch (InterruptedException | ExecutionException e) {
throw new AssertionError(e);
}
}
return results;
@ -82,4 +87,10 @@ public class DirectShareService extends ChooserTargetService {
if (cursor != null) cursor.close();
}
}
private Bitmap getFallbackDrawable(@NonNull Recipient recipient) {
return BitmapUtil.createFromDrawable(recipient.getFallbackContactPhotoDrawable(this, false),
getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height));
}
}