diff --git a/app/src/main/java/swati4star/createpdf/fragment/PdfToImageFragment.java b/app/src/main/java/swati4star/createpdf/fragment/PdfToImageFragment.java index 5ab067dc5..c7796d59a 100644 --- a/app/src/main/java/swati4star/createpdf/fragment/PdfToImageFragment.java +++ b/app/src/main/java/swati4star/createpdf/fragment/PdfToImageFragment.java @@ -10,6 +10,7 @@ import android.support.design.widget.BottomSheetBehavior; import android.support.v4.app.Fragment; import android.support.v7.widget.RecyclerView; +import android.text.InputType; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -41,8 +42,10 @@ import swati4star.createpdf.util.ExtractImages; import swati4star.createpdf.util.FileUtils; import swati4star.createpdf.util.MorphButtonUtility; +import swati4star.createpdf.util.PDFUtils; import swati4star.createpdf.util.PdfToImages; import swati4star.createpdf.util.RealPathUtil; +import swati4star.createpdf.util.StringUtils; import static android.app.Activity.RESULT_OK; import static swati4star.createpdf.util.CommonCodeUtils.checkSheetBehaviourUtil; @@ -51,6 +54,7 @@ import static swati4star.createpdf.util.Constants.BUNDLE_DATA; import static swati4star.createpdf.util.Constants.PDF_TO_IMAGES; import static swati4star.createpdf.util.DialogUtils.createAnimationDialog; +import static swati4star.createpdf.util.DialogUtils.createOverwriteDialog; import static swati4star.createpdf.util.StringUtils.showSnackbar; public class PdfToImageFragment extends Fragment implements BottomSheetPopulate, MergeFilesAdapter.OnClickListener, @@ -67,6 +71,9 @@ public class PdfToImageFragment extends Fragment implements BottomSheetPopulate, private ArrayList mOutputFilePaths; private MaterialDialog mMaterialDialog; private String mOperation; + private Context mContext; + private PDFUtils mPDFUtils; + private String[] mInputPassword; @BindView(R.id.lottie_progress) LottieAnimationView mLottieProgress; @@ -177,13 +184,45 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) throw } /** - * invokes generation of images for pdf pages in the background + * invokes generation of images for pdf pages in the background by checking + * for encryption first. */ @OnClick(R.id.createImages) public void parse() { - if (mOperation.equals(PDF_TO_IMAGES)) - new PdfToImages(mPath, mUri, this).execute(); - else + if (mPDFUtils.isPDFEncrypted(mPath)) { + mInputPassword = new String[1]; + new MaterialDialog.Builder(mActivity) + .title(R.string.enter_password) + .content(R.string.decrypt_protected_file) + .inputType(InputType.TYPE_TEXT_VARIATION_PASSWORD) + .input(null, null, (dialog, input) -> { + if (StringUtils.isEmpty(input)) { + showSnackbar(mActivity, R.string.snackbar_name_not_blank); + } else { + final String inputName = input.toString(); + if (inputName != null) { + mInputPassword[0] = inputName; + pdfToImage(mInputPassword); + } + } + }) + .show(); + } else { + pdfToImage(mInputPassword); + } + } + + /** + * Thia method handles the call to the Async process of conversion + * from PDF to Image. + * + * @param mInputPassword - the password if the file is encrypted. + */ + private void pdfToImage(String[] mInputPassword) { + if (mOperation.equals(PDF_TO_IMAGES)) { + new PdfToImages(mContext, mInputPassword, mPath, mUri, this) + .execute(); + } else new ExtractImages(mPath, this).execute(); } @@ -194,6 +233,8 @@ public void onAttach(Context context) { mMorphButtonUtility = new MorphButtonUtility(mActivity); mFileUtils = new FileUtils(mActivity); mBottomSheetUtils = new BottomSheetUtils(mActivity); + mContext = context; + mPDFUtils = new PDFUtils(mActivity); } /** diff --git a/app/src/main/java/swati4star/createpdf/util/PDFEncryptionUtility.java b/app/src/main/java/swati4star/createpdf/util/PDFEncryptionUtility.java index 2bd1ce15f..6adacf282 100644 --- a/app/src/main/java/swati4star/createpdf/util/PDFEncryptionUtility.java +++ b/app/src/main/java/swati4star/createpdf/util/PDFEncryptionUtility.java @@ -191,6 +191,36 @@ public void afterTextChanged(Editable input) { }); } + /** + * This function removes the password for encrypted files. + * @param file - the path of the actual encrypted file. + * @param inputPassword - the password of the encrypted file. + * @return + */ + public String removeDefPasswordForImages(final String file, + final String[] inputPassword) { + String finalOutputFile; + try { + String masterpwd = mSharedPrefs.getString(MASTER_PWD_STRING, appName); + PdfReader reader = new PdfReader(file, masterpwd.getBytes()); + byte[] password; + finalOutputFile = mFileUtils.getUniqueFileName + (file.replace(mContext.getResources().getString(R.string.pdf_ext), + mContext.getString(R.string.decrypted_file))); + password = reader.computeUserPassword(); + byte[] input = inputPassword[0].getBytes(); + if (Arrays.equals(input, password)) { + PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(finalOutputFile)); + stamper.close(); + reader.close(); + return finalOutputFile; + } + } catch (DocumentException | IOException e) { + e.printStackTrace(); + } + return null; + } + private boolean removePasswordUsingDefMasterPAssword(final String file, final DataSetChanged dataSetChanged, final String[] inputPassword) { diff --git a/app/src/main/java/swati4star/createpdf/util/PdfToImages.java b/app/src/main/java/swati4star/createpdf/util/PdfToImages.java index 2c25ac6cf..14dc4c688 100644 --- a/app/src/main/java/swati4star/createpdf/util/PdfToImages.java +++ b/app/src/main/java/swati4star/createpdf/util/PdfToImages.java @@ -1,5 +1,7 @@ package swati4star.createpdf.util; +import android.app.Activity; +import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; @@ -26,35 +28,51 @@ public class PdfToImages extends AsyncTask { private ExtractImagesListener mExtractImagesListener; private int mImagesCount = 0; private ArrayList mOutputFilePaths; + private String[] mPassword; + private PDFEncryptionUtility mPDFEncryptionUtility; + private Context mContext; + private String mDecryptedPath; - public PdfToImages(String mPath, Uri mUri, ExtractImagesListener mExtractImagesListener) { + public PdfToImages(Context context, String[] password, String mPath, Uri mUri, + ExtractImagesListener mExtractImagesListener) { this.mPath = mPath; this.mUri = mUri; this.mExtractImagesListener = mExtractImagesListener; mOutputFilePaths = new ArrayList<>(); + this.mPassword = password; + this.mContext = context; } @Override protected void onPreExecute() { super.onPreExecute(); + mPDFEncryptionUtility = new PDFEncryptionUtility((Activity) mContext); mExtractImagesListener.extractionStarted(); } @Override protected Void doInBackground(Void... voids) { + if (mPassword != null) { + mDecryptedPath = mPDFEncryptionUtility.removeDefPasswordForImages(mPath, mPassword); + } mOutputFilePaths = new ArrayList<>(); mImagesCount = 0; // Render pdf pages as bitmap ParcelFileDescriptor fileDescriptor = null; try { - if (mUri != null) - // resolve pdf file path based on uri - fileDescriptor = ((PdfToImageFragment) mExtractImagesListener).getContext() - .getContentResolver().openFileDescriptor(mUri, "r"); - else if (mPath != null) - // resolve pdf file path based on relative path - fileDescriptor = ParcelFileDescriptor.open(new File(mPath), MODE_READ_ONLY); + if (mDecryptedPath != null) + fileDescriptor = ParcelFileDescriptor.open(new File(mDecryptedPath), MODE_READ_ONLY); + else { + if (mUri != null) { + // resolve pdf file path based on uri + fileDescriptor = ((PdfToImageFragment) mExtractImagesListener).getContext() + .getContentResolver().openFileDescriptor(mUri, "r"); + } else if (mPath != null) { + // resolve pdf file path based on relative path + fileDescriptor = ParcelFileDescriptor.open(new File(mPath), MODE_READ_ONLY); + } + } if (fileDescriptor != null) { PdfRenderer renderer = new PdfRenderer(fileDescriptor); final int pageCount = renderer.getPageCount(); @@ -88,8 +106,6 @@ else if (mPath != null) } catch (IOException | SecurityException e) { e.printStackTrace(); } - - return null; } @@ -97,5 +113,7 @@ else if (mPath != null) protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); mExtractImagesListener.updateView(mImagesCount, mOutputFilePaths); + if (mDecryptedPath != null) + new File(mDecryptedPath).delete(); } } diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 567ad06d5..02c4913db 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -241,6 +241,7 @@ Extract Text Text extracted! PDF file selected! + Selected pdf is password protected. Enter the password to continue... Creating Text File Pincel Imagen guardada diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index ab80ac1a7..6c999ca94 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -405,6 +405,7 @@ PDF file selected: Text extracted! PDF file selected! + Selected pdf is password protected. Enter the password to continue... Creating Text File Font Color diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 3a2be1690..3aeb9be6a 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -403,6 +403,7 @@ PDF file selected: Text extracted! PDF file selected! + Selected pdf is password protected. Enter the password to continue... Creating Text File Font Color diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index a24c72f24..0f1567063 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -446,6 +446,7 @@ PDF file selected: Text extracted! PDF file selected! + Selected pdf is password protected. Enter the password to continue... Creating Text File Font Color diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e054ccf2d..dadb58b45 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -200,6 +200,7 @@ Set as default _unencrypted.pdf Enter the password to continue + Selected pdf is password protected. Enter the password to continue... PDF is not Encrypted Save Save