2017-01-12 03:54:19 +00:00
|
|
|
package org.thoughtcrime.securesms.service;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.drawable.Icon;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
2017-07-26 16:59:15 +00:00
|
|
|
import android.os.Parcel;
|
2017-01-12 03:54:19 +00:00
|
|
|
import android.service.chooser.ChooserTarget;
|
|
|
|
import android.service.chooser.ChooserTargetService;
|
2018-03-19 20:17:57 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2017-01-12 03:54:19 +00:00
|
|
|
import android.support.annotation.RequiresApi;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ShareActivity;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2017-10-16 20:11:42 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2017-01-12 03:54:19 +00:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2017-10-16 20:11:42 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2017-01-12 03:54:19 +00:00
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
|
|
|
public class DirectShareService extends ChooserTargetService {
|
2018-03-19 20:17:57 +00:00
|
|
|
|
|
|
|
private static final String TAG = DirectShareService.class.getSimpleName();
|
|
|
|
|
2017-01-12 03:54:19 +00:00
|
|
|
@Override
|
|
|
|
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
|
|
|
|
IntentFilter matchedFilter)
|
|
|
|
{
|
|
|
|
List<ChooserTarget> results = new LinkedList<>();
|
2018-01-25 03:17:44 +00:00
|
|
|
ComponentName componentName = new ComponentName(this, ShareActivity.class);
|
|
|
|
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(this);
|
|
|
|
Cursor cursor = threadDatabase.getDirectShareList();
|
2017-01-12 03:54:19 +00:00
|
|
|
|
|
|
|
try {
|
2018-01-25 03:17:44 +00:00
|
|
|
ThreadDatabase.Reader reader = threadDatabase.readerFor(cursor);
|
2017-01-12 03:54:19 +00:00
|
|
|
ThreadRecord record;
|
|
|
|
|
2017-01-12 17:52:24 +00:00
|
|
|
while ((record = reader.getNext()) != null && results.size() < 10) {
|
2017-10-16 20:11:42 +00:00
|
|
|
Recipient recipient = Recipient.from(this, record.getRecipient().getAddress(), false);
|
|
|
|
String name = recipient.toShortString();
|
2017-01-12 03:54:19 +00:00
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
Bitmap avatar;
|
2017-07-26 16:59:15 +00:00
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
if (recipient.getContactPhoto() != null) {
|
2018-03-19 20:17:57 +00:00
|
|
|
try {
|
|
|
|
avatar = GlideApp.with(this)
|
|
|
|
.asBitmap()
|
|
|
|
.load(recipient.getContactPhoto())
|
|
|
|
.circleCrop()
|
|
|
|
.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);
|
|
|
|
}
|
2017-10-16 20:11:42 +00:00
|
|
|
} else {
|
2018-03-19 20:17:57 +00:00
|
|
|
avatar = getFallbackDrawable(recipient);
|
2017-10-16 20:11:42 +00:00
|
|
|
}
|
2017-01-12 03:54:19 +00:00
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
Parcel parcel = Parcel.obtain();
|
|
|
|
parcel.writeParcelable(recipient.getAddress(), 0);
|
|
|
|
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
bundle.putLong(ShareActivity.EXTRA_THREAD_ID, record.getThreadId());
|
|
|
|
bundle.putByteArray(ShareActivity.EXTRA_ADDRESS_MARSHALLED, parcel.marshall());
|
|
|
|
bundle.putInt(ShareActivity.EXTRA_DISTRIBUTION_TYPE, record.getDistributionType());
|
|
|
|
bundle.setClassLoader(getClassLoader());
|
|
|
|
|
|
|
|
results.add(new ChooserTarget(name, Icon.createWithBitmap(avatar), 1.0f, componentName, bundle));
|
|
|
|
parcel.recycle();
|
2018-03-19 20:17:57 +00:00
|
|
|
|
2017-01-12 03:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 20:17:57 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2017-01-12 03:54:19 +00:00
|
|
|
}
|