package androidx.print; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.pdf.PdfDocument; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.CancellationSignal; import android.os.ParcelFileDescriptor; import android.print.PageRange; import android.print.PrintAttributes; import android.print.PrintDocumentAdapter; import android.print.PrintDocumentInfo; import android.print.PrintManager; import android.print.pdf.PrintedPdfDocument; import android.util.Log; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /* loaded from: classes2.dex */ public final class PrintHelper { public static final int COLOR_MODE_COLOR = 2; public static final int COLOR_MODE_MONOCHROME = 1; private static final String LOG_TAG = "PrintHelper"; private static final int MAX_PRINT_SIZE = 3500; public static final int ORIENTATION_LANDSCAPE = 1; public static final int ORIENTATION_PORTRAIT = 2; public static final int SCALE_MODE_FILL = 2; public static final int SCALE_MODE_FIT = 1; final Context mContext; static final boolean PRINT_ACTIVITY_RESPECTS_ORIENTATION = true; static final boolean IS_MIN_MARGINS_HANDLING_CORRECT = true; BitmapFactory.Options mDecodeOptions = null; final Object mLock = new Object(); int mScaleMode = 2; int mColorMode = 2; int mOrientation = 1; /* loaded from: classes2.dex */ public interface OnPrintFinishCallback { void onFinish(); } public static boolean systemSupportsPrint() { return true; } public int getColorMode() { return this.mColorMode; } public int getOrientation() { int i = this.mOrientation; if (i == 0) { return 1; } return i; } public int getScaleMode() { return this.mScaleMode; } public void setColorMode(int i) { this.mColorMode = i; } public void setOrientation(int i) { this.mOrientation = i; } public void setScaleMode(int i) { this.mScaleMode = i; } public PrintHelper(Context context) { this.mContext = context; } public void printBitmap(String str, Bitmap bitmap) { printBitmap(str, bitmap, (OnPrintFinishCallback) null); } public void printBitmap(String str, Bitmap bitmap, OnPrintFinishCallback onPrintFinishCallback) { PrintAttributes.MediaSize mediaSize; if (bitmap == null) { return; } PrintManager printManager = (PrintManager) this.mContext.getSystemService("print"); if (isPortrait(bitmap)) { mediaSize = PrintAttributes.MediaSize.UNKNOWN_PORTRAIT; } else { mediaSize = PrintAttributes.MediaSize.UNKNOWN_LANDSCAPE; } printManager.print(str, new PrintBitmapAdapter(str, this.mScaleMode, bitmap, onPrintFinishCallback), new PrintAttributes.Builder().setMediaSize(mediaSize).setColorMode(this.mColorMode).build()); } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public class PrintBitmapAdapter extends PrintDocumentAdapter { private PrintAttributes mAttributes; private final Bitmap mBitmap; private final OnPrintFinishCallback mCallback; private final int mFittingMode; private final String mJobName; PrintBitmapAdapter(String str, int i, Bitmap bitmap, OnPrintFinishCallback onPrintFinishCallback) { this.mJobName = str; this.mFittingMode = i; this.mBitmap = bitmap; this.mCallback = onPrintFinishCallback; } @Override // android.print.PrintDocumentAdapter public void onLayout(PrintAttributes printAttributes, PrintAttributes printAttributes2, CancellationSignal cancellationSignal, PrintDocumentAdapter.LayoutResultCallback layoutResultCallback, Bundle bundle) { this.mAttributes = printAttributes2; layoutResultCallback.onLayoutFinished(new PrintDocumentInfo.Builder(this.mJobName).setContentType(1).setPageCount(1).build(), true ^ printAttributes2.equals(printAttributes)); } @Override // android.print.PrintDocumentAdapter public void onWrite(PageRange[] pageRangeArr, ParcelFileDescriptor parcelFileDescriptor, CancellationSignal cancellationSignal, PrintDocumentAdapter.WriteResultCallback writeResultCallback) { PrintHelper.this.writeBitmap(this.mAttributes, this.mFittingMode, this.mBitmap, parcelFileDescriptor, cancellationSignal, writeResultCallback); } @Override // android.print.PrintDocumentAdapter public void onFinish() { OnPrintFinishCallback onPrintFinishCallback = this.mCallback; if (onPrintFinishCallback != null) { onPrintFinishCallback.onFinish(); } } } public void printBitmap(String str, Uri uri) throws FileNotFoundException { printBitmap(str, uri, (OnPrintFinishCallback) null); } public void printBitmap(String str, Uri uri, OnPrintFinishCallback onPrintFinishCallback) throws FileNotFoundException { PrintUriAdapter printUriAdapter = new PrintUriAdapter(str, uri, onPrintFinishCallback, this.mScaleMode); PrintManager printManager = (PrintManager) this.mContext.getSystemService("print"); PrintAttributes.Builder builder = new PrintAttributes.Builder(); builder.setColorMode(this.mColorMode); int i = this.mOrientation; if (i == 1 || i == 0) { builder.setMediaSize(PrintAttributes.MediaSize.UNKNOWN_LANDSCAPE); } else if (i == 2) { builder.setMediaSize(PrintAttributes.MediaSize.UNKNOWN_PORTRAIT); } printManager.print(str, printUriAdapter, builder.build()); } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public class PrintUriAdapter extends PrintDocumentAdapter { PrintAttributes mAttributes; Bitmap mBitmap = null; final OnPrintFinishCallback mCallback; final int mFittingMode; final Uri mImageFile; final String mJobName; AsyncTask mLoadBitmap; PrintUriAdapter(String str, Uri uri, OnPrintFinishCallback onPrintFinishCallback, int i) { this.mJobName = str; this.mImageFile = uri; this.mCallback = onPrintFinishCallback; this.mFittingMode = i; } /* JADX WARN: Type inference failed for: r11v3, types: [androidx.print.PrintHelper$PrintUriAdapter$1] */ @Override // android.print.PrintDocumentAdapter public void onLayout(final PrintAttributes printAttributes, final PrintAttributes printAttributes2, final CancellationSignal cancellationSignal, final PrintDocumentAdapter.LayoutResultCallback layoutResultCallback, Bundle bundle) { synchronized (this) { this.mAttributes = printAttributes2; } if (cancellationSignal.isCanceled()) { layoutResultCallback.onLayoutCancelled(); } else if (this.mBitmap != null) { layoutResultCallback.onLayoutFinished(new PrintDocumentInfo.Builder(this.mJobName).setContentType(1).setPageCount(1).build(), true ^ printAttributes2.equals(printAttributes)); } else { this.mLoadBitmap = new AsyncTask() { // from class: androidx.print.PrintHelper.PrintUriAdapter.1 @Override // android.os.AsyncTask protected void onPreExecute() { cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() { // from class: androidx.print.PrintHelper.PrintUriAdapter.1.1 @Override // android.os.CancellationSignal.OnCancelListener public void onCancel() { PrintUriAdapter.this.cancelLoad(); cancel(false); } }); } /* JADX INFO: Access modifiers changed from: protected */ @Override // android.os.AsyncTask public Bitmap doInBackground(Uri... uriArr) { try { return PrintHelper.this.loadConstrainedBitmap(PrintUriAdapter.this.mImageFile); } catch (FileNotFoundException unused) { return null; } } /* JADX INFO: Access modifiers changed from: protected */ @Override // android.os.AsyncTask public void onPostExecute(Bitmap bitmap) { PrintAttributes.MediaSize mediaSize; super.onPostExecute((AnonymousClass1) bitmap); if (bitmap != null && (!PrintHelper.PRINT_ACTIVITY_RESPECTS_ORIENTATION || PrintHelper.this.mOrientation == 0)) { synchronized (this) { mediaSize = PrintUriAdapter.this.mAttributes.getMediaSize(); } if (mediaSize != null && mediaSize.isPortrait() != PrintHelper.isPortrait(bitmap)) { Matrix matrix = new Matrix(); matrix.postRotate(90.0f); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); } } PrintUriAdapter.this.mBitmap = bitmap; if (bitmap != null) { layoutResultCallback.onLayoutFinished(new PrintDocumentInfo.Builder(PrintUriAdapter.this.mJobName).setContentType(1).setPageCount(1).build(), true ^ printAttributes2.equals(printAttributes)); } else { layoutResultCallback.onLayoutFailed(null); } PrintUriAdapter.this.mLoadBitmap = null; } /* JADX INFO: Access modifiers changed from: protected */ @Override // android.os.AsyncTask public void onCancelled(Bitmap bitmap) { layoutResultCallback.onLayoutCancelled(); PrintUriAdapter.this.mLoadBitmap = null; } }.execute(new Uri[0]); } } void cancelLoad() { synchronized (PrintHelper.this.mLock) { if (PrintHelper.this.mDecodeOptions != null) { PrintHelper.this.mDecodeOptions = null; } } } @Override // android.print.PrintDocumentAdapter public void onFinish() { super.onFinish(); cancelLoad(); AsyncTask asyncTask = this.mLoadBitmap; if (asyncTask != null) { asyncTask.cancel(true); } OnPrintFinishCallback onPrintFinishCallback = this.mCallback; if (onPrintFinishCallback != null) { onPrintFinishCallback.onFinish(); } Bitmap bitmap = this.mBitmap; if (bitmap != null) { bitmap.recycle(); this.mBitmap = null; } } @Override // android.print.PrintDocumentAdapter public void onWrite(PageRange[] pageRangeArr, ParcelFileDescriptor parcelFileDescriptor, CancellationSignal cancellationSignal, PrintDocumentAdapter.WriteResultCallback writeResultCallback) { PrintHelper.this.writeBitmap(this.mAttributes, this.mFittingMode, this.mBitmap, parcelFileDescriptor, cancellationSignal, writeResultCallback); } } static boolean isPortrait(Bitmap bitmap) { return bitmap.getWidth() <= bitmap.getHeight(); } private static PrintAttributes.Builder copyAttributes(PrintAttributes printAttributes) { PrintAttributes.Builder minMargins = new PrintAttributes.Builder().setMediaSize(printAttributes.getMediaSize()).setResolution(printAttributes.getResolution()).setMinMargins(printAttributes.getMinMargins()); if (printAttributes.getColorMode() != 0) { minMargins.setColorMode(printAttributes.getColorMode()); } if (printAttributes.getDuplexMode() != 0) { minMargins.setDuplexMode(printAttributes.getDuplexMode()); } return minMargins; } static Matrix getMatrix(int i, int i2, RectF rectF, int i3) { float min; Matrix matrix = new Matrix(); float f = i; float width = rectF.width() / f; if (i3 == 2) { min = Math.max(width, rectF.height() / i2); } else { min = Math.min(width, rectF.height() / i2); } matrix.postScale(min, min); matrix.postTranslate((rectF.width() - (f * min)) / 2.0f, (rectF.height() - (i2 * min)) / 2.0f); return matrix; } /* JADX WARN: Type inference failed for: r0v4, types: [androidx.print.PrintHelper$1] */ void writeBitmap(final PrintAttributes printAttributes, final int i, final Bitmap bitmap, final ParcelFileDescriptor parcelFileDescriptor, final CancellationSignal cancellationSignal, final PrintDocumentAdapter.WriteResultCallback writeResultCallback) { final PrintAttributes build = IS_MIN_MARGINS_HANDLING_CORRECT ? printAttributes : copyAttributes(printAttributes).setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0)).build(); new AsyncTask() { // from class: androidx.print.PrintHelper.1 /* JADX INFO: Access modifiers changed from: protected */ @Override // android.os.AsyncTask public Throwable doInBackground(Void... voidArr) { RectF rectF; try { if (cancellationSignal.isCanceled()) { return null; } PrintedPdfDocument printedPdfDocument = new PrintedPdfDocument(PrintHelper.this.mContext, build); Bitmap convertBitmapForColorMode = PrintHelper.convertBitmapForColorMode(bitmap, build.getColorMode()); if (cancellationSignal.isCanceled()) { return null; } try { PdfDocument.Page startPage = printedPdfDocument.startPage(1); if (PrintHelper.IS_MIN_MARGINS_HANDLING_CORRECT) { rectF = new RectF(startPage.getInfo().getContentRect()); } else { PrintedPdfDocument printedPdfDocument2 = new PrintedPdfDocument(PrintHelper.this.mContext, printAttributes); PdfDocument.Page startPage2 = printedPdfDocument2.startPage(1); RectF rectF2 = new RectF(startPage2.getInfo().getContentRect()); printedPdfDocument2.finishPage(startPage2); printedPdfDocument2.close(); rectF = rectF2; } Matrix matrix = PrintHelper.getMatrix(convertBitmapForColorMode.getWidth(), convertBitmapForColorMode.getHeight(), rectF, i); if (!PrintHelper.IS_MIN_MARGINS_HANDLING_CORRECT) { matrix.postTranslate(rectF.left, rectF.top); startPage.getCanvas().clipRect(rectF); } startPage.getCanvas().drawBitmap(convertBitmapForColorMode, matrix, null); printedPdfDocument.finishPage(startPage); if (!cancellationSignal.isCanceled()) { printedPdfDocument.writeTo(new FileOutputStream(parcelFileDescriptor.getFileDescriptor())); printedPdfDocument.close(); ParcelFileDescriptor parcelFileDescriptor2 = parcelFileDescriptor; if (parcelFileDescriptor2 != null) { try { parcelFileDescriptor2.close(); } catch (IOException unused) { } } if (convertBitmapForColorMode != bitmap) { convertBitmapForColorMode.recycle(); } return null; } printedPdfDocument.close(); ParcelFileDescriptor parcelFileDescriptor3 = parcelFileDescriptor; if (parcelFileDescriptor3 != null) { try { parcelFileDescriptor3.close(); } catch (IOException unused2) { } } if (convertBitmapForColorMode != bitmap) { convertBitmapForColorMode.recycle(); } return null; } finally { } } catch (Throwable th) { return th; } } /* JADX INFO: Access modifiers changed from: protected */ @Override // android.os.AsyncTask public void onPostExecute(Throwable th) { if (cancellationSignal.isCanceled()) { writeResultCallback.onWriteCancelled(); } else if (th == null) { writeResultCallback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES}); } else { Log.e(PrintHelper.LOG_TAG, "Error writing printed content", th); writeResultCallback.onWriteFailed(null); } } }.execute(new Void[0]); } Bitmap loadConstrainedBitmap(Uri uri) throws FileNotFoundException { BitmapFactory.Options options; if (uri == null || this.mContext == null) { throw new IllegalArgumentException("bad argument to getScaledBitmap"); } BitmapFactory.Options options2 = new BitmapFactory.Options(); options2.inJustDecodeBounds = true; loadBitmap(uri, options2); int i = options2.outWidth; int i2 = options2.outHeight; if (i > 0 && i2 > 0) { int max = Math.max(i, i2); int i3 = 1; while (max > MAX_PRINT_SIZE) { max >>>= 1; i3 <<= 1; } if (i3 > 0 && Math.min(i, i2) / i3 > 0) { synchronized (this.mLock) { BitmapFactory.Options options3 = new BitmapFactory.Options(); this.mDecodeOptions = options3; options3.inMutable = true; this.mDecodeOptions.inSampleSize = i3; options = this.mDecodeOptions; } try { Bitmap loadBitmap = loadBitmap(uri, options); synchronized (this.mLock) { this.mDecodeOptions = null; } return loadBitmap; } catch (Throwable th) { synchronized (this.mLock) { this.mDecodeOptions = null; throw th; } } } } return null; } private Bitmap loadBitmap(Uri uri, BitmapFactory.Options options) throws FileNotFoundException { Context context; if (uri == null || (context = this.mContext) == null) { throw new IllegalArgumentException("bad argument to loadBitmap"); } InputStream inputStream = null; try { InputStream openInputStream = context.getContentResolver().openInputStream(uri); try { Bitmap decodeStream = BitmapFactory.decodeStream(openInputStream, null, options); if (openInputStream != null) { try { openInputStream.close(); } catch (IOException e) { Log.w(LOG_TAG, "close fail ", e); } } return decodeStream; } catch (Throwable th) { th = th; inputStream = openInputStream; if (inputStream != null) { try { inputStream.close(); } catch (IOException e2) { Log.w(LOG_TAG, "close fail ", e2); } } throw th; } } catch (Throwable th2) { th = th2; } } static Bitmap convertBitmapForColorMode(Bitmap bitmap, int i) { if (i != 1) { return bitmap; } Bitmap createBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(createBitmap); Paint paint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0.0f); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint); canvas.setBitmap(null); return createBitmap; } }