Support for direct share targets

Fixes #4333
// FREEBIE
This commit is contained in:
Moxie Marlinspike 2017-01-11 19:54:19 -08:00
parent bd819d7d10
commit a125fcb8fa
4 changed files with 110 additions and 11 deletions

View File

@ -167,6 +167,10 @@
<data android:mimeType="video/*" />
</intent-filter>
<meta-data
android:name="android.service.chooser.chooser_target_service"
android:value=".service.DirectShareService" />
</activity>
<activity android:name=".ConversationListActivity"
@ -412,6 +416,13 @@
<meta-data android:name="android.provider.CONTACTS_STRUCTURE" android:resource="@xml/contactsformat" />
</service>
<service android:name=".service.DirectShareService"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
<intent-filter>
<action android:name="android.service.chooser.ChooserTargetService" />
</intent-filter>
</service>
<receiver android:name=".gcm.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />

View File

@ -21,15 +21,10 @@ import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.system.ErrnoException;
import android.system.Os;
import android.system.StructStat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
@ -40,6 +35,7 @@ import android.view.ViewGroup;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.mms.PartAuthority;
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.util.DynamicTheme;
@ -47,9 +43,7 @@ import org.thoughtcrime.securesms.util.FileUtils;
import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.ViewUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@ -63,6 +57,10 @@ public class ShareActivity extends PassphraseRequiredActionBarActivity
{
private static final String TAG = ShareActivity.class.getSimpleName();
public static final String EXTRA_THREAD_ID = "thread_id";
public static final String EXTRA_RECIPIENT_IDS = "recipient_ids";
public static final String EXTRA_DISTRIBUTION_TYPE = "distribution_type";
private final DynamicTheme dynamicTheme = new DynamicTheme ();
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
@ -123,11 +121,11 @@ public class ShareActivity extends PassphraseRequiredActionBarActivity
Uri streamExtra = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
mimeType = getMimeType(streamExtra);
if (streamExtra != null && PartAuthority.isLocalUri(streamExtra)) {
isPassingAlongMedia = true;
resolvedExtra = streamExtra;
fragmentContainer.setVisibility(View.VISIBLE);
progressWheel.setVisibility(View.GONE);
handleResolvedMedia(getIntent(), false);
} else {
fragmentContainer.setVisibility(View.GONE);
progressWheel.setVisibility(View.VISIBLE);
@ -166,6 +164,24 @@ public class ShareActivity extends PassphraseRequiredActionBarActivity
createConversation(threadId, recipients, distributionType);
}
private void handleResolvedMedia(Intent intent, boolean animate) {
long threadId = intent.getLongExtra(EXTRA_THREAD_ID, -1);
long[] recipientIds = intent.getLongArrayExtra(EXTRA_RECIPIENT_IDS);
int distributionType = intent.getIntExtra(EXTRA_DISTRIBUTION_TYPE, -1);
boolean hasResolvedDestination = threadId != -1 && recipientIds != null && distributionType != -1;
if (!hasResolvedDestination && animate) {
ViewUtil.fadeIn(fragmentContainer, 300);
ViewUtil.fadeOut(progressWheel, 300);
} else if (!hasResolvedDestination) {
fragmentContainer.setVisibility(View.VISIBLE);
progressWheel.setVisibility(View.GONE);
} else {
createConversation(threadId, RecipientFactory.getRecipientsForIds(this, recipientIds, true), distributionType);
}
}
private void createConversation(long threadId, Recipients recipients, int distributionType) {
final Intent intent = getBaseShareIntent(ConversationActivity.class);
intent.putExtra(ConversationActivity.RECIPIENTS_EXTRA, recipients.getIds());
@ -229,8 +245,7 @@ public class ShareActivity extends PassphraseRequiredActionBarActivity
@Override
protected void onPostExecute(Uri uri) {
resolvedExtra = uri;
ViewUtil.fadeIn(fragmentContainer, 300);
ViewUtil.fadeOut(progressWheel, 300);
handleResolvedMedia(getIntent(), true);
}
private InputStream openFileUri(Uri uri) throws IOException {

View File

@ -349,6 +349,11 @@ public class ThreadDatabase extends Database {
return cursor;
}
public Cursor getDirectShareList() {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
return db.query(TABLE_NAME, null, null, null, null, null, DATE + " DESC");
}
public int getArchivedConversationListCount() {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = null;

View File

@ -0,0 +1,68 @@
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.Drawable;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.Bundle;
import android.service.chooser.ChooserTarget;
import android.service.chooser.ChooserTargetService;
import android.support.annotation.RequiresApi;
import org.thoughtcrime.securesms.ShareActivity;
import org.thoughtcrime.securesms.crypto.MasterCipher;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.database.model.ThreadRecord;
import org.thoughtcrime.securesms.util.BitmapUtil;
import java.util.LinkedList;
import java.util.List;
@RequiresApi(api = Build.VERSION_CODES.M)
public class DirectShareService extends ChooserTargetService {
@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
IntentFilter matchedFilter)
{
List<ChooserTarget> results = new LinkedList<>();
MasterSecret masterSecret = KeyCachingService.getMasterSecret(this);
if (masterSecret == null) {
return results;
}
ComponentName componentName = new ComponentName(this, ShareActivity.class);
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(this);
Cursor cursor = threadDatabase.getDirectShareList();
try {
ThreadDatabase.Reader reader = threadDatabase.readerFor(cursor, new MasterCipher(masterSecret));
ThreadRecord record;
while ((record = reader.getNext()) != null) {
String name = record.getRecipients().toShortString();
Drawable drawable = record.getRecipients().getContactPhoto()
.asDrawable(this, record.getRecipients().getColor()
.toConversationColor(this));
Bitmap avatar = BitmapUtil.createFromDrawable(drawable, 500, 500);
Bundle bundle = new Bundle();
bundle.putLong(ShareActivity.EXTRA_THREAD_ID, record.getThreadId());
bundle.putLongArray(ShareActivity.EXTRA_RECIPIENT_IDS, record.getRecipients().getIds());
bundle.putInt(ShareActivity.EXTRA_DISTRIBUTION_TYPE, record.getDistributionType());
results.add(new ChooserTarget(name, Icon.createWithBitmap(avatar), 1.0f, componentName, bundle));
}
return results;
} finally {
if (cursor != null) cursor.close();
}
}
}