|
@@ -22,7 +22,9 @@ import com.compdfkit.pdfviewer.R;
|
|
|
import com.compdfkit.pdfviewer.home.HomeFunBean;
|
|
|
import com.compdfkit.pdfviewer.home.SelectWatermarkFunFragment;
|
|
|
import com.compdfkit.tools.common.utils.CFileUtils;
|
|
|
+import com.compdfkit.tools.common.utils.CToastUtil;
|
|
|
import com.compdfkit.tools.common.utils.dialog.CAlertDialog;
|
|
|
+import com.compdfkit.tools.common.views.CVerifyPasswordDialogFragment;
|
|
|
import com.compdfkit.tools.common.views.directory.CFileDirectoryDialog;
|
|
|
import com.compdfkit.tools.security.watermark.CWatermarkEditDialog;
|
|
|
|
|
@@ -40,7 +42,7 @@ public class WatermarkSamplesImpl extends OpenPDFSamplesImpl {
|
|
|
watermark(filePath, uri);
|
|
|
}
|
|
|
|
|
|
- private CPDFDocument getDocument(String filePath, Uri uri) {
|
|
|
+ private void verifyDocument(String filePath, Uri uri, CVerifyPasswordDialogFragment.CVerifyCompleteListener listener) {
|
|
|
CPDFDocument document = new CPDFDocument(fragment.getContext());
|
|
|
CPDFDocument.PDFDocumentError error;
|
|
|
if (!TextUtils.isEmpty(filePath)) {
|
|
@@ -48,88 +50,87 @@ public class WatermarkSamplesImpl extends OpenPDFSamplesImpl {
|
|
|
} else {
|
|
|
error = document.open(uri);
|
|
|
}
|
|
|
- return error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess ? document : null;
|
|
|
+ if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
|
|
|
+ if (listener != null) {
|
|
|
+ listener.complete(document);
|
|
|
+ }
|
|
|
+ } else if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
|
|
|
+ showVerifyPasswordDialog(document, listener);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Demonstrate watermark function
|
|
|
+ *
|
|
|
* @param filePath
|
|
|
* @param uri
|
|
|
*/
|
|
|
private void watermark(String filePath, Uri uri) {
|
|
|
SelectWatermarkFunFragment watermarkFunFragment = SelectWatermarkFunFragment.newInstance();
|
|
|
watermarkFunFragment.setAddWatermarkClickListener(v -> {
|
|
|
- CPDFDocument document = getDocument(filePath, uri);
|
|
|
- if (document != null) {
|
|
|
+ verifyDocument(filePath, uri, document -> {
|
|
|
CWatermarkEditDialog watermarkEditDialog = CWatermarkEditDialog.newInstance();
|
|
|
watermarkEditDialog.setDocument(document);
|
|
|
watermarkEditDialog.setPageIndex(0);
|
|
|
watermarkEditDialog.setCompleteListener(pdfFile -> {
|
|
|
watermarkEditDialog.dismiss();
|
|
|
- showResultDialog(fragment.getString(R.string.tools_watermark_add_success),
|
|
|
- fragment.getString(R.string.tools_already_saved_as) + pdfFile,
|
|
|
- pdfFile, null);
|
|
|
+ CToastUtil.showLongToast(fragment.getContext(), R.string.tools_watermark_add_success);
|
|
|
+ startPDFActivity(pdfFile, null, password);
|
|
|
});
|
|
|
watermarkEditDialog.show(fragment.getChildFragmentManager(), "watermarkEditDialog");
|
|
|
- }
|
|
|
- watermarkFunFragment.dismiss();
|
|
|
+ watermarkFunFragment.dismiss();
|
|
|
+ });
|
|
|
+
|
|
|
});
|
|
|
watermarkFunFragment.setRemoveWatermarkClickListener(v -> {
|
|
|
watermarkFunFragment.dismiss();
|
|
|
- CPDFDocument document = getDocument(filePath, uri);
|
|
|
- if (document != null) {
|
|
|
- // Display the secondary confirmation pop-up window to remove the watermark
|
|
|
- CAlertDialog alertDialog = CAlertDialog.newInstance(fragment.getString(R.string.tools_remove_watermark),
|
|
|
- fragment.getString(R.string.tools_remove_watermark_info));
|
|
|
- alertDialog.setCancelClickListener(v1 -> {
|
|
|
- alertDialog.dismiss();
|
|
|
- });
|
|
|
- alertDialog.setConfirmClickListener(v1 -> {
|
|
|
- alertDialog.dismiss();
|
|
|
- String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
|
|
|
- CFileDirectoryDialog directoryDialog = CFileDirectoryDialog.newInstance(dir,
|
|
|
- fragment.getString(R.string.tools_save_location), fragment.getString(R.string.tools_save_to_this_directory));
|
|
|
- directoryDialog.setSelectFolderListener(dir1 -> {
|
|
|
- try {
|
|
|
- // remove all watermark
|
|
|
- for (int i = 0; i < document.getWatermarkCount(); i++) {
|
|
|
- document.getWatermark(i).clear();
|
|
|
+ verifyDocument(filePath, uri, document -> {
|
|
|
+ if (document != null) {
|
|
|
+ // Display the secondary confirmation pop-up window to remove the watermark
|
|
|
+ CAlertDialog alertDialog = CAlertDialog.newInstance(fragment.getString(R.string.tools_remove_watermark),
|
|
|
+ fragment.getString(R.string.tools_remove_watermark_info));
|
|
|
+ alertDialog.setCancelClickListener(v1 -> {
|
|
|
+ alertDialog.dismiss();
|
|
|
+ });
|
|
|
+ alertDialog.setConfirmClickListener(v1 -> {
|
|
|
+ alertDialog.dismiss();
|
|
|
+ String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
|
|
|
+ CFileDirectoryDialog directoryDialog = CFileDirectoryDialog.newInstance(dir,
|
|
|
+ fragment.getString(R.string.tools_save_location), fragment.getString(R.string.tools_save_to_this_directory));
|
|
|
+ directoryDialog.setSelectFolderListener(dir1 -> {
|
|
|
+ try {
|
|
|
+ // remove all watermark
|
|
|
+ for (int i = 0; i < document.getWatermarkCount(); i++) {
|
|
|
+ document.getWatermark(i).clear();
|
|
|
+ }
|
|
|
+ File file = new File(dir1, document.getFileName());
|
|
|
+ file = CFileUtils.renameNameSuffix(file);
|
|
|
+ document.saveAs(file.getAbsolutePath(), false);
|
|
|
+ document.close();
|
|
|
+ CToastUtil.showLongToast(fragment.getContext(), R.string.tools_watermark_removed_success);
|
|
|
+ startPDFActivity(file.getAbsolutePath(), null, password);
|
|
|
+ } catch (CPDFDocumentException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
- File file = new File(dir1, document.getFileName());
|
|
|
- file = CFileUtils.renameNameSuffix(file);
|
|
|
- document.saveAs(file.getAbsolutePath(), false);
|
|
|
- document.close();
|
|
|
- showResultDialog(fragment.getString(R.string.tools_watermark_removed_success),
|
|
|
- fragment.getString(R.string.tools_already_saved_as) + file.getAbsolutePath(),
|
|
|
- file.getAbsolutePath(), null);
|
|
|
-
|
|
|
- } catch (CPDFDocumentException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
+ });
|
|
|
+ directoryDialog.show(fragment.getChildFragmentManager(), "dirDialog");
|
|
|
});
|
|
|
- directoryDialog.show(fragment.getChildFragmentManager(), "dirDialog");
|
|
|
- });
|
|
|
- alertDialog.show(fragment.getChildFragmentManager(), "removeWatermarkAlertDialog");
|
|
|
- }
|
|
|
+ alertDialog.show(fragment.getChildFragmentManager(), "removeWatermarkAlertDialog");
|
|
|
+ }
|
|
|
+ });
|
|
|
});
|
|
|
watermarkFunFragment.show(fragment.getChildFragmentManager(), "selectFunDialog");
|
|
|
}
|
|
|
|
|
|
- private void showResultDialog(String title, String msg, String filePath, String password) {
|
|
|
- Bundle bundle = new Bundle();
|
|
|
- bundle.putString(CAlertDialog.EXTRA_TITLE, title);
|
|
|
- bundle.putString(CAlertDialog.EXTRA_MESSAGE, msg);
|
|
|
- bundle.putString(CAlertDialog.EXTRA_CONFIRM_BUTTON_TEXT, fragment.getString(R.string.tools_check));
|
|
|
- bundle.putString(CAlertDialog.EXTRA_CANCEL_BUTTON_TEXT, fragment.getString(R.string.tools_open_folder));
|
|
|
- CAlertDialog alertDialog = CAlertDialog.newInstance(bundle);
|
|
|
- alertDialog.setCancelClickListener(v -> {
|
|
|
- CFileUtils.openFolder(fragment.getContext(), new File(filePath).getParentFile());
|
|
|
- alertDialog.dismiss();
|
|
|
- });
|
|
|
- alertDialog.setConfirmClickListener(v -> {
|
|
|
- startPDFActivity(filePath, null, password);
|
|
|
- alertDialog.dismiss();
|
|
|
+ private void showVerifyPasswordDialog(CPDFDocument document, CVerifyPasswordDialogFragment.CVerifyCompleteListener documentListener) {
|
|
|
+ CVerifyPasswordDialogFragment verifyPasswordDialogFragment;
|
|
|
+ verifyPasswordDialogFragment = CVerifyPasswordDialogFragment.newInstance(document, filePath, uri);
|
|
|
+ verifyPasswordDialogFragment.setVerifyCompleteListener(document1 -> {
|
|
|
+ if (documentListener != null) {
|
|
|
+ documentListener.complete(document1);
|
|
|
+ }
|
|
|
});
|
|
|
- alertDialog.show(fragment.getParentFragmentManager(), "tips");
|
|
|
+ verifyPasswordDialogFragment.show(fragment.getChildFragmentManager(), "verifyPasswordDialog");
|
|
|
}
|
|
|
+
|
|
|
}
|