content: 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.Rect;
  import android.graphics.RectF;
  import android.graphics.pdf.PdfDocument;
  import android.net.Uri;
  import android.os.AsyncTask;
  import android.os.Build;
  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;
  
  public final class PrintHelper {
      public static final int COLOR_MODE_COLOR = 2;
      public static final int COLOR_MODE_MONOCHROME = 1;
      static final boolean IS_MIN_MARGINS_HANDLING_CORRECT;
      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;
      static final boolean PRINT_ACTIVITY_RESPECTS_ORIENTATION = (Build.VERSION.SDK_INT < 20 || Build.VERSION.SDK_INT > 23);
      public static final int SCALE_MODE_FILL = 2;
      public static final int SCALE_MODE_FIT = 1;
      int mColorMode = 2;
      final Context mContext;
      BitmapFactory.Options mDecodeOptions = null;
      final Object mLock = new Object();
      int mOrientation = 1;
      int mScaleMode = 2;
  
      public interface OnPrintFinishCallback {
          void onFinish();
      }
  
      private 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 jobName, int fittingMode, Bitmap bitmap, OnPrintFinishCallback callback) {
              this.mJobName = jobName;
              this.mFittingMode = fittingMode;
              this.mBitmap = bitmap;
              this.mCallback = callback;
          }
  
          public void onFinish() {
              OnPrintFinishCallback onPrintFinishCallback = this.mCallback;
              if (onPrintFinishCallback != null) {
                  onPrintFinishCallback.onFinish();
              }
          }
  
          public void onLayout(PrintAttributes oldPrintAttributes, PrintAttributes newPrintAttributes, CancellationSignal cancellationSignal, PrintDocumentAdapter.LayoutResultCallback layoutResultCallback, Bundle bundle) {
              this.mAttributes = newPrintAttributes;
              layoutResultCallback.onLayoutFinished(new PrintDocumentInfo.Builder(this.mJobName).setContentType(1).setPageCount(1).build(), true ^ newPrintAttributes.equals(oldPrintAttributes));
          }
  
          public void onWrite(PageRange[] pageRanges, ParcelFileDescriptor fileDescriptor, CancellationSignal cancellationSignal, PrintDocumentAdapter.WriteResultCallback writeResultCallback) {
              PrintHelper.this.writeBitmap(this.mAttributes, this.mFittingMode, this.mBitmap, fileDescriptor, cancellationSignal, writeResultCallback);
          }
      }
  
      private class PrintUriAdapter extends PrintDocumentAdapter {
          PrintAttributes mAttributes;
          Bitmap mBitmap = null;
          final OnPrintFinishCallback mCallback;
          final int mFittingMode;
          final Uri mImageFile;
          final String mJobName;
          AsyncTask<Uri, Boolean, Bitmap> mLoadBitmap;
  
          PrintUriAdapter(String jobName, Uri imageFile, OnPrintFinishCallback callback, int fittingMode) {
              this.mJobName = jobName;
              this.mImageFile = imageFile;
              this.mCallback = callback;
              this.mFittingMode = fittingMode;
          }
  
          /* access modifiers changed from: package-private */
          public void cancelLoad() {
              synchronized (PrintHelper.this.mLock) {
                  if (PrintHelper.this.mDecodeOptions != null) {
                      if (Build.VERSION.SDK_INT < 24) {
                          PrintHelper.this.mDecodeOptions.requestCancelDecode();
                      }
                      PrintHelper.this.mDecodeOptions = null;
                  }
              }
          }
  
          public void onFinish() {
              super.onFinish();
              cancelLoad();
              AsyncTask<Uri, Boolean, Bitmap> 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;
              }
          }
  
          public void onLayout(PrintAttributes oldPrintAttributes, PrintAttributes newPrintAttributes, CancellationSignal cancellationSignal, PrintDocumentAdapter.LayoutResultCallback layoutResultCallback, Bundle bundle) {
              synchronized (this) {
                  this.mAttributes = newPrintAttributes;
              }
              if (cancellationSignal.isCanceled()) {
                  layoutResultCallback.onLayoutCancelled();
              } else if (this.mBitmap != null) {
                  layoutResultCallback.onLayoutFinished(new PrintDocumentInfo.Builder(this.mJobName).setContentType(1).setPageCount(1).build(), true ^ newPrintAttributes.equals(oldPrintAttributes));
              } else {
                  final CancellationSignal cancellationSignal2 = cancellationSignal;
                  final PrintAttributes printAttributes = newPrintAttributes;
                  final PrintAttributes printAttributes2 = oldPrintAttributes;
                  final PrintDocumentAdapter.LayoutResultCallback layoutResultCallback2 = layoutResultCallback;
                  this.mLoadBitmap = new AsyncTask<Uri, Boolean, Bitmap>() {
                      /* access modifiers changed from: protected */
                      public Bitmap doInBackground(Uri... uris) {
                          try {
                              return PrintHelper.this.loadConstrainedBitmap(PrintUriAdapter.this.mImageFile);
                          } catch (FileNotFoundException e) {
                              return null;
                          }
                      }
  
                      /* access modifiers changed from: protected */
                      public void onCancelled(Bitmap result) {
                          layoutResultCallback2.onLayoutCancelled();
                          PrintUriAdapter.this.mLoadBitmap = null;
                      }
  
                      /* access modifiers changed from: protected */
                      public void onPostExecute(Bitmap bitmap) {
                          PrintAttributes.MediaSize mediaSize;
                          super.onPostExecute(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) {
                              layoutResultCallback2.onLayoutFinished(new PrintDocumentInfo.Builder(PrintUriAdapter.this.mJobName).setContentType(1).setPageCount(1).build(), true ^ printAttributes.equals(printAttributes2));
                          } else {
                              layoutResultCallback2.onLayoutFailed((CharSequence) null);
                          }
                          PrintUriAdapter.this.mLoadBitmap = null;
                      }
  
                      /* access modifiers changed from: protected */
                      public void onPreExecute() {
                          cancellationSignal2.setOnCancelListener(new CancellationSignal.OnCancelListener() {
                              public void onCancel() {
                                  PrintUriAdapter.this.cancelLoad();
                                  AnonymousClass1.this.cancel(false);
                              }
                          });
                      }
                  }.execute(new Uri[0]);
              }
          }
  
          public void onWrite(PageRange[] pageRanges, ParcelFileDescriptor fileDescriptor, CancellationSignal cancellationSignal, PrintDocumentAdapter.WriteResultCallback writeResultCallback) {
              PrintHelper.this.writeBitmap(this.mAttributes, this.mFittingMode, this.mBitmap, fileDescriptor, cancellationSignal, writeResultCallback);
          }
      }
  
      static {
          boolean z = false;
          if (Build.VERSION.SDK_INT != 23) {
              z = true;
          }
          IS_MIN_MARGINS_HANDLING_CORRECT = z;
      }
  
      public PrintHelper(Context context) {
          this.mContext = context;
      }
  
      static Bitmap convertBitmapForColorMode(Bitmap original, int colorMode) {
          if (colorMode != 1) {
              return original;
          }
          Bitmap createBitmap = Bitmap.createBitmap(original.getWidth(), original.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(original, 0.0f, 0.0f, paint);
          canvas.setBitmap((Bitmap) null);
          return createBitmap;
      }
  
      private static PrintAttributes.Builder copyAttributes(PrintAttributes other) {
          PrintAttributes.Builder minMargins = new PrintAttributes.Builder().setMediaSize(other.getMediaSize()).setResolution(other.getResolution()).setMinMargins(other.getMinMargins());
          if (other.getColorMode() != 0) {
              minMargins.setColorMode(other.getColorMode());
          }
          if (Build.VERSION.SDK_INT >= 23 && other.getDuplexMode() != 0) {
              minMargins.setDuplexMode(other.getDuplexMode());
          }
          return minMargins;
      }
  
      static Matrix getMatrix(int imageWidth, int imageHeight, RectF content, int fittingMode) {
          Matrix matrix = new Matrix();
          float width = content.width() / ((float) imageWidth);
          float max = fittingMode == 2 ? Math.max(width, content.height() / ((float) imageHeight)) : Math.min(width, content.height() / ((float) imageHeight));
          matrix.postScale(max, max);
          matrix.postTranslate((content.width() - (((float) imageWidth) * max)) / 2.0f, (content.height() - (((float) imageHeight) * max)) / 2.0f);
          return matrix;
      }
  
      static boolean isPortrait(Bitmap bitmap) {
          return bitmap.getWidth() <= bitmap.getHeight();
      }
  
      private Bitmap loadBitmap(Uri uri, BitmapFactory.Options o) throws FileNotFoundException {
          Context context;
          if (uri == null || (context = this.mContext) == null) {
              throw new IllegalArgumentException("bad argument to loadBitmap");
          }
          InputStream inputStream = null;
          try {
              inputStream = context.getContentResolver().openInputStream(uri);
              return BitmapFactory.decodeStream(inputStream, (Rect) null, o);
          } finally {
              if (inputStream != null) {
                  try {
                      inputStream.close();
                  } catch (IOException e) {
                      Log.w(LOG_TAG, "close fail ", e);
                  }
              }
          }
      }
  
      public static boolean systemSupportsPrint() {
          return Build.VERSION.SDK_INT >= 19;
      }
  
      public int getColorMode() {
          return this.mColorMode;
      }
  
      public int getOrientation() {
          if (Build.VERSION.SDK_INT < 19 || this.mOrientation != 0) {
              return this.mOrientation;
          }
          return 1;
      }
  
      public int getScaleMode() {
          return this.mScaleMode;
      }
  
      /* access modifiers changed from: package-private */
      public Bitmap loadConstrainedBitmap(Uri uri) throws FileNotFoundException {
          if (uri == null || this.mContext == null) {
              throw new IllegalArgumentException("bad argument to getScaledBitmap");
          }
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          loadBitmap(uri, options);
          int i = options.outWidth;
          int i2 = options.outHeight;
          if (i <= 0 || i2 <= 0) {
              return null;
          }
          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) {
              return null;
          }
          synchronized (this.mLock) {
              try {
                  BitmapFactory.Options options2 = new BitmapFactory.Options();
                  this.mDecodeOptions = options2;
                  options2.inMutable = true;
                  this.mDecodeOptions.inSampleSize = i3;
                  BitmapFactory.Options options3 = this.mDecodeOptions;
                  try {
                      try {
                          Bitmap loadBitmap = loadBitmap(uri, options3);
                          synchronized (this.mLock) {
                              this.mDecodeOptions = null;
                          }
                          return loadBitmap;
                      } catch (Throwable th) {
                          synchronized (this.mLock) {
                              this.mDecodeOptions = null;
                              throw th;
                          }
                      }
                  } catch (Throwable th2) {
                      th = th2;
                      throw th;
                  }
              } catch (Throwable th3) {
                  th = th3;
                  throw th;
              }
          }
      }
  
      public void printBitmap(String jobName, Bitmap bitmap) {
          printBitmap(jobName, bitmap, (OnPrintFinishCallback) null);
      }
  
      public void printBitmap(String jobName, Bitmap bitmap, OnPrintFinishCallback callback) {
          if (Build.VERSION.SDK_INT >= 19 && bitmap != null) {
              ((PrintManager) this.mContext.getSystemService("print")).print(jobName, new PrintBitmapAdapter(jobName, this.mScaleMode, bitmap, callback), new PrintAttributes.Builder().setMediaSize(isPortrait(bitmap) ? PrintAttributes.MediaSize.UNKNOWN_PORTRAIT : PrintAttributes.MediaSize.UNKNOWN_LANDSCAPE).setColorMode(this.mColorMode).build());
          }
      }
  
      public void printBitmap(String jobName, Uri imageFile) throws FileNotFoundException {
          printBitmap(jobName, imageFile, (OnPrintFinishCallback) null);
      }
  
      public void printBitmap(String jobName, Uri imageFile, OnPrintFinishCallback callback) throws FileNotFoundException {
          if (Build.VERSION.SDK_INT >= 19) {
              PrintUriAdapter printUriAdapter = new PrintUriAdapter(jobName, imageFile, callback, 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(jobName, printUriAdapter, builder.build());
          }
      }
  
      public void setColorMode(int colorMode) {
          this.mColorMode = colorMode;
      }
  
      public void setOrientation(int orientation) {
          this.mOrientation = orientation;
      }
  
      public void setScaleMode(int scaleMode) {
          this.mScaleMode = scaleMode;
      }
  
      /* access modifiers changed from: package-private */
      public void writeBitmap(PrintAttributes attributes, int fittingMode, Bitmap bitmap, ParcelFileDescriptor fileDescriptor, CancellationSignal cancellationSignal, PrintDocumentAdapter.WriteResultCallback writeResultCallback) {
          final CancellationSignal cancellationSignal2 = cancellationSignal;
          final PrintAttributes build = IS_MIN_MARGINS_HANDLING_CORRECT ? attributes : copyAttributes(attributes).setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0)).build();
          final Bitmap bitmap2 = bitmap;
          final PrintAttributes printAttributes = attributes;
          final int i = fittingMode;
          final ParcelFileDescriptor parcelFileDescriptor = fileDescriptor;
          final PrintDocumentAdapter.WriteResultCallback writeResultCallback2 = writeResultCallback;
          new AsyncTask<Void, Void, Throwable>() {
              /* access modifiers changed from: protected */
              public Throwable doInBackground(Void... params) {
                  PrintedPdfDocument printedPdfDocument;
                  Bitmap convertBitmapForColorMode;
                  RectF rectF;
                  try {
                      if (cancellationSignal2.isCanceled()) {
                          return null;
                      }
                      printedPdfDocument = new PrintedPdfDocument(PrintHelper.this.mContext, build);
                      convertBitmapForColorMode = PrintHelper.convertBitmapForColorMode(bitmap2, build.getColorMode());
                      if (cancellationSignal2.isCanceled()) {
                          return null;
                      }
                      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, (Paint) null);
                      printedPdfDocument.finishPage(startPage);
                      if (cancellationSignal2.isCanceled()) {
                          printedPdfDocument.close();
                          ParcelFileDescriptor parcelFileDescriptor = parcelFileDescriptor;
                          if (parcelFileDescriptor != null) {
                              try {
                                  parcelFileDescriptor.close();
                              } catch (IOException e) {
                              }
                          }
                          if (convertBitmapForColorMode != bitmap2) {
                              convertBitmapForColorMode.recycle();
                          }
                          return null;
                      }
                      printedPdfDocument.writeTo(new FileOutputStream(parcelFileDescriptor.getFileDescriptor()));
                      printedPdfDocument.close();
                      ParcelFileDescriptor parcelFileDescriptor2 = parcelFileDescriptor;
                      if (parcelFileDescriptor2 != null) {
                          try {
                              parcelFileDescriptor2.close();
                          } catch (IOException e2) {
                          }
                      }
                      if (convertBitmapForColorMode != bitmap2) {
                          convertBitmapForColorMode.recycle();
                      }
                      return null;
                  } catch (Throwable th) {
                      return th;
                  }
              }
  
              /* access modifiers changed from: protected */
              public void onPostExecute(Throwable throwable) {
                  if (cancellationSignal2.isCanceled()) {
                      writeResultCallback2.onWriteCancelled();
                  } else if (throwable == null) {
                      writeResultCallback2.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
                  } else {
                      Log.e(PrintHelper.LOG_TAG, "Error writing printed content", throwable);
                      writeResultCallback2.onWriteFailed((CharSequence) null);
                  }
              }
          }.execute(new Void[0]);
      }
  }
  