Search in sources :

Example 26 with Stopwatch

use of org.thoughtcrime.securesms.util.Stopwatch in project Signal-Android by WhisperSystems.

the class CameraXUtil method transformByteArray.

private static byte[] transformByteArray(@NonNull byte[] data, @Nullable Rect cropRect, int rotation, boolean flip) throws IOException {
    Stopwatch stopwatch = new Stopwatch("transform");
    Bitmap in;
    if (cropRect != null) {
        BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(data, 0, data.length, false);
        in = decoder.decodeRegion(cropRect, new BitmapFactory.Options());
        decoder.recycle();
        stopwatch.split("crop");
    } else {
        in = BitmapFactory.decodeByteArray(data, 0, data.length);
    }
    Bitmap out = in;
    if (rotation != 0 || flip) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        if (flip) {
            matrix.postScale(-1, 1);
            matrix.postTranslate(in.getWidth(), 0);
        }
        out = Bitmap.createBitmap(in, 0, 0, in.getWidth(), in.getHeight(), matrix, true);
    }
    byte[] transformedData = toJpegBytes(out);
    stopwatch.split("transcode");
    in.recycle();
    out.recycle();
    stopwatch.stop(TAG);
    return transformedData;
}
Also used : Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) BitmapRegionDecoder(android.graphics.BitmapRegionDecoder)

Example 27 with Stopwatch

use of org.thoughtcrime.securesms.util.Stopwatch in project Signal-Android by WhisperSystems.

the class Camera1Fragment method onCaptureClicked.

private void onCaptureClicked() {
    orderEnforcer.reset();
    Stopwatch fastCaptureTimer = new Stopwatch("Capture");
    camera.capture((jpegData, frontFacing) -> {
        fastCaptureTimer.split("captured");
        Transformation<Bitmap> transformation = frontFacing ? new MultiTransformation<>(new CenterCrop(), new FlipTransformation()) : new CenterCrop();
        GlideApp.with(this).asBitmap().load(jpegData).transform(transformation).override(cameraPreview.getWidth(), cameraPreview.getHeight()).into(new SimpleTarget<Bitmap>() {

            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                fastCaptureTimer.split("transform");
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                resource.compress(Bitmap.CompressFormat.JPEG, 80, stream);
                fastCaptureTimer.split("compressed");
                byte[] data = stream.toByteArray();
                fastCaptureTimer.split("bytes");
                fastCaptureTimer.stop(TAG);
                controller.onImageCaptured(data, resource.getWidth(), resource.getHeight());
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                controller.onCameraError();
            }
        });
    });
}
Also used : Bitmap(android.graphics.Bitmap) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) Drawable(android.graphics.drawable.Drawable) CenterCrop(com.bumptech.glide.load.resource.bitmap.CenterCrop) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 28 with Stopwatch

use of org.thoughtcrime.securesms.util.Stopwatch in project Signal-Android by WhisperSystems.

the class MediaRepository method getMediaInBucket.

@WorkerThread
@NonNull
private List<Media> getMediaInBucket(@NonNull Context context, @NonNull String bucketId) {
    Stopwatch stopwatch = new Stopwatch("getMediaInBucket");
    List<Media> images = getMediaInBucket(context, bucketId, Images.Media.EXTERNAL_CONTENT_URI, true);
    List<Media> videos = getMediaInBucket(context, bucketId, Video.Media.EXTERNAL_CONTENT_URI, false);
    List<Media> media = new ArrayList<>(images.size() + videos.size());
    stopwatch.split("post fetch");
    media.addAll(images);
    media.addAll(videos);
    Collections.sort(media, (o1, o2) -> Long.compare(o2.getDate(), o1.getDate()));
    stopwatch.split("post sort");
    stopwatch.stop(TAG);
    return media;
}
Also used : Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) ArrayList(java.util.ArrayList) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Example 29 with Stopwatch

use of org.thoughtcrime.securesms.util.Stopwatch in project Signal-Android by WhisperSystems.

the class LiveRecipientCache method warmUp.

@AnyThread
public void warmUp() {
    if (warmedUp.getAndSet(true)) {
        return;
    }
    Stopwatch stopwatch = new Stopwatch("recipient-warm-up");
    SignalExecutors.BOUNDED.execute(() -> {
        ThreadDatabase threadDatabase = SignalDatabase.threads();
        List<Recipient> recipients = new ArrayList<>();
        try (ThreadDatabase.Reader reader = threadDatabase.readerFor(threadDatabase.getRecentConversationList(THREAD_CACHE_WARM_MAX, false, false))) {
            int i = 0;
            ThreadRecord record = null;
            while ((record = reader.getNext()) != null && i < THREAD_CACHE_WARM_MAX) {
                recipients.add(record.getRecipient());
                i++;
            }
        }
        Log.d(TAG, "Warming up " + recipients.size() + " thread recipients.");
        addToCache(recipients);
        stopwatch.split("thread");
        if (SignalStore.registrationValues().isRegistrationComplete() && SignalStore.account().getAci() != null) {
            try (Cursor cursor = SignalDatabase.recipients().getNonGroupContacts(false)) {
                int count = 0;
                while (cursor != null && cursor.moveToNext() && count < CONTACT_CACHE_WARM_MAX) {
                    RecipientId id = RecipientId.from(CursorUtil.requireLong(cursor, RecipientDatabase.ID));
                    Recipient.resolved(id);
                    count++;
                }
                Log.d(TAG, "Warmed up " + count + " contact recipient.");
                stopwatch.split("contact");
            }
        }
        stopwatch.stop(TAG);
    });
}
Also used : Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) ArrayList(java.util.ArrayList) ThreadRecord(org.thoughtcrime.securesms.database.model.ThreadRecord) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) Cursor(android.database.Cursor) SuppressLint(android.annotation.SuppressLint) AnyThread(androidx.annotation.AnyThread)

Example 30 with Stopwatch

use of org.thoughtcrime.securesms.util.Stopwatch in project Signal-Android by WhisperSystems.

the class QrCode method create.

@NonNull
public static Bitmap create(@Nullable String data, @ColorInt int foregroundColor, @ColorInt int backgroundColor) {
    if (data == null || data.length() == 0) {
        Log.w(TAG, "No data");
        return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
    }
    try {
        Stopwatch stopwatch = new Stopwatch("QrGen");
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix qrData = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, 512, 512);
        int qrWidth = qrData.getWidth();
        int qrHeight = qrData.getHeight();
        int[] pixels = new int[qrWidth * qrHeight];
        for (int y = 0; y < qrHeight; y++) {
            int offset = y * qrWidth;
            for (int x = 0; x < qrWidth; x++) {
                pixels[offset + x] = qrData.get(x, y) ? foregroundColor : backgroundColor;
            }
        }
        stopwatch.split("Write pixels");
        Bitmap bitmap = Bitmap.createBitmap(pixels, qrWidth, qrHeight, Bitmap.Config.ARGB_8888);
        stopwatch.split("Create bitmap");
        stopwatch.stop(TAG);
        return bitmap;
    } catch (WriterException e) {
        Log.w(TAG, e);
        return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
    }
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) Bitmap(android.graphics.Bitmap) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) BitMatrix(com.google.zxing.common.BitMatrix) WriterException(com.google.zxing.WriterException) NonNull(androidx.annotation.NonNull)

Aggregations

Stopwatch (org.thoughtcrime.securesms.util.Stopwatch)42 NonNull (androidx.annotation.NonNull)20 IOException (java.io.IOException)16 Nullable (androidx.annotation.Nullable)14 Context (android.content.Context)12 ArrayList (java.util.ArrayList)12 Log (org.signal.core.util.logging.Log)12 WorkerThread (androidx.annotation.WorkerThread)10 List (java.util.List)10 Recipient (org.thoughtcrime.securesms.recipients.Recipient)10 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)10 Util (org.thoughtcrime.securesms.util.Util)10 Stream (com.annimon.stream.Stream)8 Collections (java.util.Collections)8 LinkedList (java.util.LinkedList)8 Set (java.util.Set)8 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)8 SignalStore (org.thoughtcrime.securesms.keyvalue.SignalStore)8 Optional (org.whispersystems.libsignal.util.guava.Optional)8 Cursor (android.database.Cursor)6