Search in sources :

Example 11 with MasterSecret

use of org.thoughtcrime.securesms.crypto.MasterSecret in project Signal-Android by WhisperSystems.

the class DirectShareService method onGetChooserTargets.

@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 && results.size() < 10) {
            Recipients recipients = RecipientFactory.getRecipientsForIds(this, record.getRecipients().getIds(), false);
            String name = recipients.toShortString();
            Drawable drawable = recipients.getContactPhoto().asDrawable(this, recipients.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, recipients.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();
    }
}
Also used : MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) Recipients(org.thoughtcrime.securesms.recipients.Recipients) Bundle(android.os.Bundle) ThreadRecord(org.thoughtcrime.securesms.database.model.ThreadRecord) Drawable(android.graphics.drawable.Drawable) MasterCipher(org.thoughtcrime.securesms.crypto.MasterCipher) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) Bitmap(android.graphics.Bitmap) ChooserTarget(android.service.chooser.ChooserTarget) ComponentName(android.content.ComponentName)

Example 12 with MasterSecret

use of org.thoughtcrime.securesms.crypto.MasterSecret in project Signal-Android by WhisperSystems.

the class SaveAttachmentTask method doInBackground.

@Override
protected Integer doInBackground(SaveAttachmentTask.Attachment... attachments) {
    if (attachments == null || attachments.length == 0) {
        throw new AssertionError("must pass in at least one attachment");
    }
    try {
        Context context = contextReference.get();
        MasterSecret masterSecret = masterSecretReference.get();
        if (!Environment.getExternalStorageDirectory().canWrite()) {
            return WRITE_ACCESS_FAILURE;
        }
        if (context == null) {
            return FAILURE;
        }
        for (Attachment attachment : attachments) {
            if (attachment != null && !saveAttachment(context, masterSecret, attachment)) {
                return FAILURE;
            }
        }
        return SUCCESS;
    } catch (IOException ioe) {
        Log.w(TAG, ioe);
        return FAILURE;
    }
}
Also used : Context(android.content.Context) MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) IOException(java.io.IOException)

Example 13 with MasterSecret

use of org.thoughtcrime.securesms.crypto.MasterSecret in project Signal-Android by WhisperSystems.

the class BaseUnitTest method setUp.

@Before
public void setUp() throws Exception {
    masterSecret = new MasterSecret(new SecretKeySpec(new byte[16], "AES"), new SecretKeySpec(new byte[16], "HmacSHA1"));
    mockStatic(Looper.class);
    mockStatic(Log.class);
    mockStatic(Handler.class);
    mockStatic(TextUtils.class);
    mockStatic(PreferenceManager.class);
    when(PreferenceManager.getDefaultSharedPreferences(any(Context.class))).thenReturn(sharedPreferences);
    when(Looper.getMainLooper()).thenReturn(null);
    PowerMockito.whenNew(Handler.class).withAnyArguments().thenReturn(null);
    Answer<?> logAnswer = new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            final String tag = (String) invocation.getArguments()[0];
            final String msg = (String) invocation.getArguments()[1];
            System.out.println(invocation.getMethod().getName().toUpperCase() + "/[" + tag + "] " + msg);
            return null;
        }
    };
    PowerMockito.doAnswer(logAnswer).when(Log.class, "d", anyString(), anyString());
    PowerMockito.doAnswer(logAnswer).when(Log.class, "i", anyString(), anyString());
    PowerMockito.doAnswer(logAnswer).when(Log.class, "w", anyString(), anyString());
    PowerMockito.doAnswer(logAnswer).when(Log.class, "e", anyString(), anyString());
    PowerMockito.doAnswer(logAnswer).when(Log.class, "wtf", anyString(), anyString());
    PowerMockito.doAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            final String s = (String) invocation.getArguments()[0];
            return s == null || s.length() == 0;
        }
    }).when(TextUtils.class, "isEmpty", anyString());
    when(sharedPreferences.getString(anyString(), anyString())).thenReturn("");
    when(sharedPreferences.getLong(anyString(), anyLong())).thenReturn(0L);
    when(sharedPreferences.getInt(anyString(), anyInt())).thenReturn(0);
    when(sharedPreferences.getBoolean(anyString(), anyBoolean())).thenReturn(false);
    when(sharedPreferences.getFloat(anyString(), anyFloat())).thenReturn(0f);
    when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPreferences);
    when(context.getPackageName()).thenReturn("org.thoughtcrime.securesms");
}
Also used : Context(android.content.Context) MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) Answer(org.mockito.stubbing.Answer) SecretKeySpec(javax.crypto.spec.SecretKeySpec) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) Before(org.junit.Before)

Example 14 with MasterSecret

use of org.thoughtcrime.securesms.crypto.MasterSecret in project Signal-Android by WhisperSystems.

the class AttachmentBitmapDecoder method decode.

@Override
public Bitmap decode(Context context, Uri uri) throws Exception {
    if (!PartAuthority.isLocalUri(uri)) {
        return new SkiaImageDecoder().decode(context, uri);
    }
    MasterSecret masterSecret = KeyCachingService.getMasterSecret(context);
    if (masterSecret == null) {
        throw new IllegalStateException("Can't decode without secret");
    }
    InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, uri);
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
        if (bitmap == null) {
            throw new RuntimeException("Skia image region decoder returned null bitmap - image format may not be supported");
        }
        return bitmap;
    } finally {
        if (inputStream != null)
            inputStream.close();
    }
}
Also used : MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) Bitmap(android.graphics.Bitmap) InputStream(java.io.InputStream) SkiaImageDecoder(com.davemorrissey.labs.subscaleview.decoder.SkiaImageDecoder) BitmapFactory(android.graphics.BitmapFactory)

Example 15 with MasterSecret

use of org.thoughtcrime.securesms.crypto.MasterSecret in project Signal-Android by WhisperSystems.

the class AttachmentRegionDecoder method init.

@RequiresApi(api = Build.VERSION_CODES.GINGERBREAD_MR1)
@Override
public Point init(Context context, Uri uri) throws Exception {
    Log.w(TAG, "Init!");
    if (!PartAuthority.isLocalUri(uri)) {
        passthrough = new SkiaImageRegionDecoder();
        return passthrough.init(context, uri);
    }
    MasterSecret masterSecret = KeyCachingService.getMasterSecret(context);
    if (masterSecret == null) {
        throw new IllegalStateException("No master secret available...");
    }
    InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, uri);
    this.bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false);
    inputStream.close();
    return new Point(bitmapRegionDecoder.getWidth(), bitmapRegionDecoder.getHeight());
}
Also used : MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) InputStream(java.io.InputStream) Point(android.graphics.Point) SkiaImageRegionDecoder(com.davemorrissey.labs.subscaleview.decoder.SkiaImageRegionDecoder) RequiresApi(android.support.annotation.RequiresApi)

Aggregations

MasterSecret (org.thoughtcrime.securesms.crypto.MasterSecret)16 MasterCipher (org.thoughtcrime.securesms.crypto.MasterCipher)4 IOException (java.io.IOException)3 InvalidPassphraseException (org.thoughtcrime.securesms.crypto.InvalidPassphraseException)3 PendingIntent (android.app.PendingIntent)2 Context (android.content.Context)2 Intent (android.content.Intent)2 Bitmap (android.graphics.Bitmap)2 InputStream (java.io.InputStream)2 MasterSecretUnion (org.thoughtcrime.securesms.crypto.MasterSecretUnion)2 ThreadDatabase (org.thoughtcrime.securesms.database.ThreadDatabase)2 ComponentName (android.content.ComponentName)1 Cursor (android.database.Cursor)1 BitmapFactory (android.graphics.BitmapFactory)1 Point (android.graphics.Point)1 Drawable (android.graphics.drawable.Drawable)1 Bundle (android.os.Bundle)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 ChooserTarget (android.service.chooser.ChooserTarget)1 Nullable (android.support.annotation.Nullable)1