package S0;

import G0.d;
import J0.c;
import S0.a;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.media.Image;
import android.media.ImageReader;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
public class b {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static b f1654a = new b();

    private b() {
    }

    private Bitmap a(ImageReader imageReader) {
        Image imageAcquireLatestImage = null;
        int i2 = 0;
        while (imageAcquireLatestImage == null && i2 < 10) {
            i2++;
            try {
                Thread.sleep(250L);
                imageAcquireLatestImage = imageReader.acquireLatestImage();
            } catch (InterruptedException unused) {
            }
        }
        if (imageAcquireLatestImage == null) {
            return null;
        }
        try {
            Image.Plane[] planes = imageAcquireLatestImage.getPlanes();
            ByteBuffer buffer = planes[0].getBuffer();
            int pixelStride = planes[0].getPixelStride();
            Bitmap bitmapCreateBitmap = Bitmap.createBitmap(imageReader.getWidth() + ((planes[0].getRowStride() - (imageReader.getWidth() * pixelStride)) / pixelStride), imageReader.getHeight(), Bitmap.Config.ARGB_8888);
            bitmapCreateBitmap.copyPixelsFromBuffer(buffer);
            imageAcquireLatestImage.close();
            return bitmapCreateBitmap;
        } catch (Exception e2) {
            Log.d("SDKVirtualDisplay", "captureImage error", e2);
            return null;
        }
    }

    private boolean b(Bitmap bitmap) {
        for (int i2 = 0; i2 < bitmap.getWidth(); i2 += 10) {
            for (int i3 = 0; i3 < bitmap.getHeight(); i3 += 10) {
                int pixel = bitmap.getPixel(i2, i3);
                if (pixel == 0) {
                    Log.d("SDKVirtualDisplay", "checkBitmap failed, bitmap is Translucent");
                    return false;
                }
                if (pixel != -16777216) {
                    return true;
                }
            }
        }
        Log.d("SDKVirtualDisplay", "checkBitmap failed, bitmap is total black");
        return false;
    }

    private ImageReader c(Context context) {
        Display defaultDisplay = ((WindowManager) context.getSystemService("window")).getDefaultDisplay();
        Point point = new Point();
        defaultDisplay.getRealSize(point);
        return ImageReader.newInstance(point.x, point.y, 1, 2);
    }

    public static b e() {
        return f1654a;
    }

    private a f() {
        IBinder iBinderF = c.g().f("virtual_display");
        if (iBinderF != null) {
            return a.AbstractBinderC0035a.E(iBinderF);
        }
        return null;
    }

    public String d(Surface surface, int i2, int i3) {
        a aVarF = f();
        if (aVarF == null) {
            return null;
        }
        try {
            return aVarF.g(d.o().p(), surface, i2, i3);
        } catch (RemoteException e2) {
            Log.e("SDKVirtualDisplay", "createVirtualDisplay failed", e2);
            return null;
        }
    }

    public void g(String str) {
        a aVarF = f();
        if (aVarF == null) {
            return;
        }
        try {
            aVarF.A(str);
        } catch (RemoteException e2) {
            Log.e("SDKVirtualDisplay", "createVirtualDisplay failed", e2);
        }
    }

    public Bitmap h(Context context) {
        ImageReader imageReaderC = c(context);
        if (imageReaderC == null) {
            return null;
        }
        String strD = d(imageReaderC.getSurface(), imageReaderC.getWidth(), imageReaderC.getHeight());
        Bitmap bitmapA = a(imageReaderC);
        int i2 = 0;
        while (bitmapA != null && !b(bitmapA) && i2 < 3) {
            bitmapA = a(imageReaderC);
            i2++;
            Log.d("SDKVirtualDisplay", "try read bitmap " + i2);
        }
        imageReaderC.close();
        g(strD);
        return bitmapA;
    }
}
