Skip to content

Commit

Permalink
Addition of remove duplicate feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivamdhuria committed Dec 19, 2018
1 parent f875e2b commit 7fc8528
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import swati4star.createpdf.fragment.MergeFilesFragment;
import swati4star.createpdf.fragment.PdfToImageFragment;
import swati4star.createpdf.fragment.QrBarcodeScanFragment;
import swati4star.createpdf.fragment.RemoveDuplicatePagesFragment;
import swati4star.createpdf.fragment.RemovePagesFragment;
import swati4star.createpdf.fragment.SettingsFragment;
import swati4star.createpdf.fragment.SplitFilesFragment;
Expand Down Expand Up @@ -369,6 +370,9 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Intent intent = new Intent(this, WelcomeActivity.class);
intent.putExtra(SHOW_WELCOME_ACT, true);
startActivity(intent);
case R.id.nav_remove_duplicate_pages:
fragment = new RemoveDuplicatePagesFragment();
break;
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class HomeFragment extends Fragment implements View.OnClickListener {
MyCardView addWatermark;
@BindView(R.id.add_images)
MyCardView addImages;
@BindView(R.id.remove_duplicates_pages_pdf)
MyCardView removeDuplicatePages;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand All @@ -85,6 +87,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
rotatePdf.setOnClickListener(this);
addWatermark.setOnClickListener(this);
addImages.setOnClickListener(this);
removeDuplicatePages.setOnClickListener(this);
return rootview;
}

Expand Down Expand Up @@ -191,6 +194,10 @@ public void onClick(View v) {
fragment.setArguments(bundle);
setNavigationViewSelection(3);
break;
case R.id.remove_duplicates_pages_pdf:
fragment = new RemoveDuplicatePagesFragment();
setNavigationViewSelection(12);
break;
}

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package swati4star.createpdf.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.afollestad.materialdialogs.MaterialDialog;
import com.airbnb.lottie.LottieAnimationView;
import com.dd.morphingbutton.MorphingButton;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import swati4star.createpdf.R;
import swati4star.createpdf.adapter.FilesListAdapter;
import swati4star.createpdf.adapter.MergeFilesAdapter;
import swati4star.createpdf.database.DatabaseHelper;
import swati4star.createpdf.interfaces.BottomSheetPopulate;
import swati4star.createpdf.interfaces.OnPDFCreatedInterface;
import swati4star.createpdf.util.BottomSheetCallback;
import swati4star.createpdf.util.BottomSheetUtils;
import swati4star.createpdf.util.FileUtils;
import swati4star.createpdf.util.MorphButtonUtility;
import swati4star.createpdf.util.PDFUtils;
import swati4star.createpdf.util.RemoveDuplicates;
import swati4star.createpdf.util.ViewFilesDividerItemDecoration;

import static android.app.Activity.RESULT_OK;
import static swati4star.createpdf.util.DialogUtils.createAnimationDialog;
import static swati4star.createpdf.util.FileUriUtils.getFilePath;
import static swati4star.createpdf.util.StringUtils.getSnackbarwithAction;
import static swati4star.createpdf.util.StringUtils.showSnackbar;

public class RemoveDuplicatePagesFragment extends Fragment implements MergeFilesAdapter.OnClickListener,
FilesListAdapter.OnFileItemClickedListener, BottomSheetPopulate, OnPDFCreatedInterface {

private Activity mActivity;
private String mPath;
private MorphButtonUtility mMorphButtonUtility;
private FileUtils mFileUtils;
private PDFUtils mPDFUtils;
private BottomSheetUtils mBottomSheetUtils;
private static final int INTENT_REQUEST_PICKFILE_CODE = 10;
private MaterialDialog mMaterialDialog;

@BindView(R.id.lottie_progress)
LottieAnimationView mLottieProgress;
@BindView(R.id.selectFile)
MorphingButton selectFileButton;
//TODO id name
@BindView(R.id.remove)
MorphingButton removeDuplicateButton;
BottomSheetBehavior sheetBehavior;
@BindView(R.id.bottom_sheet)
LinearLayout layoutBottomSheet;
@BindView(R.id.upArrow)
ImageView mUpArrow;
@BindView(R.id.downArrow)
ImageView mDownArrow;
@BindView(R.id.layout)
RelativeLayout mLayout;
@BindView(R.id.recyclerViewFiles)
RecyclerView mRecyclerViewFiles;
@BindView(R.id.view_pdf)
Button mViewPdf;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_remove_duplicate_pages, container, false);
ButterKnife.bind(this, rootview);
sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet);
sheetBehavior.setBottomSheetCallback(new BottomSheetCallback(mUpArrow, isAdded()));
mLottieProgress.setVisibility(View.VISIBLE);
mBottomSheetUtils.populateBottomSheetWithPDFs(this);

resetValues();
return rootview;
}
@OnClick(R.id.viewFiles)
void onViewFilesClick(View view) {
mBottomSheetUtils.showHideSheet(sheetBehavior);
}

/**
* Displays file chooser intent
*/
@OnClick(R.id.selectFile)
public void showFileChooser() {
startActivityForResult(mFileUtils.getFileChooser(),
INTENT_REQUEST_PICKFILE_CODE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) throws NullPointerException {
if (data == null || resultCode != RESULT_OK || data.getData() == null)
return;
if (requestCode == INTENT_REQUEST_PICKFILE_CODE)
setTextAndActivateButtons(getFilePath(data.getData()));
}


//On click remove duplicate button
@OnClick(R.id.remove)
public void parse() {
new RemoveDuplicates(mPath, this).execute();
}


private void resetValues() {
mPath = null;
mMorphButtonUtility.initializeButton(selectFileButton, removeDuplicateButton);
}

private void setTextAndActivateButtons(String path) {
mPath = path;
// mCompressionInfoText.setVisibility(View.GONE);
mMorphButtonUtility.setTextAndActivateButtons(path,
selectFileButton, removeDuplicateButton);
}

@Override
public void onPopulate(ArrayList<String> paths) {
if (paths == null || paths.size() == 0) {
mLayout.setVisibility(View.GONE);
} else {
// Init recycler view
mRecyclerViewFiles.setVisibility(View.VISIBLE);
MergeFilesAdapter mergeFilesAdapter = new MergeFilesAdapter(mActivity, paths, false, this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mActivity);
mRecyclerViewFiles.setLayoutManager(mLayoutManager);
mRecyclerViewFiles.setAdapter(mergeFilesAdapter);
mRecyclerViewFiles.addItemDecoration(new ViewFilesDividerItemDecoration(mActivity));
}
mLottieProgress.setVisibility(View.GONE);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = (Activity) context;
mMorphButtonUtility = new MorphButtonUtility(mActivity);
mFileUtils = new FileUtils(mActivity);
mPDFUtils = new PDFUtils(mActivity);
mBottomSheetUtils = new BottomSheetUtils(mActivity);
}

@Override
public void onItemClick(String path) {
sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
setTextAndActivateButtons(path);
}

@Override
public void onFileItemClick(String path) {
mFileUtils.openFile(path);
}

private void viewPdfButton(String path) {
mViewPdf.setVisibility(View.VISIBLE);
mViewPdf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mFileUtils.openFile(path);
}
});
}

@Override
public void onPDFCreationStarted() {
mMaterialDialog = createAnimationDialog(mActivity);
mMaterialDialog.show();
}

@Override
public void onPDFCreated(boolean isNewPdfCreated, String path) {
mMaterialDialog.dismiss();
if (!isNewPdfCreated) {
showSnackbar(mActivity, R.string.snackbar_no_duplicate_pdf);
//Hiding View PDF button
mViewPdf.setVisibility(View.GONE);
return;
}
new DatabaseHelper(mActivity).insertRecord(path, mActivity.getString(R.string.snackbar_no_duplicate_pdf));
getSnackbarwithAction(mActivity, R.string.snackbar_pdfCreated)
.setAction(R.string.snackbar_viewAction, v -> mFileUtils.openFile(path)).show();
viewPdfButton(path);
resetValues();
}


}

134 changes: 134 additions & 0 deletions app/src/main/java/swati4star/createpdf/util/RemoveDuplicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package swati4star.createpdf.util;

import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.AsyncTask;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import swati4star.createpdf.interfaces.OnPDFCreatedInterface;

import static android.os.ParcelFileDescriptor.MODE_READ_ONLY;

public class RemoveDuplicates extends AsyncTask<Void, Void, Void> {
private String mPath;
private OnPDFCreatedInterface mOnPDFCreatedInterface;
private ArrayList<Bitmap> mBitmaps;
private ArrayList<Integer> mSequence;
private String mPages;
private Boolean mIsNewPDFCreated;
public RemoveDuplicates(String mPath, OnPDFCreatedInterface onPDFCreatedInterface) {
this.mPath = mPath;
mSequence = new ArrayList<>();
mBitmaps = new ArrayList<>();
this.mOnPDFCreatedInterface = onPDFCreatedInterface;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
mOnPDFCreatedInterface.onPDFCreationStarted();
mIsNewPDFCreated = false;
}


@Override
protected Void doInBackground(Void... voids) {
//mImagesCount = 0;
// Render pdf pages as bitmap
ParcelFileDescriptor fileDescriptor = null;
try {

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();
for (int i = 0; i < pageCount; i++) {
PdfRenderer.Page page = renderer.openPage(i);
// generate bitmaps for individual pdf pages
Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),
Bitmap.Config.ARGB_8888);
// say we render for showing on the screen

page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
// close the page
page.close();
//Adding bitmap to arrayList if not same
Boolean add = true;
if (mBitmaps.size() == 0) {
mBitmaps.add(bitmap);
mSequence.add(1);
} else {
for (int j = 0; j < mBitmaps.size(); j++) {
if (mBitmaps.get(j).sameAs(bitmap)) {
add = false;
}
}
if (add) {
mBitmaps.add(bitmap);
mSequence.add(i + 1);
}
}

}
// close the renderer
renderer.close();
if (mBitmaps.size() == pageCount) {
//No repetition found
} else {
StringBuilder pages = new StringBuilder();
for ( int x : mSequence)
pages.append(x).append(",");
mPages = pages.toString();
String outputPath = mPath.replace(".pdf", "_edited_" + mPages + ".pdf");
if (createPDF(mPath, outputPath, mPages)) {
mPath = outputPath;
mIsNewPDFCreated = true;
}
}


}
} catch (IOException | SecurityException e) {
e.printStackTrace();
mIsNewPDFCreated = false;
}

return null;
}

@Override
protected void onPostExecute(Void avoid) {
// execution of result of Long time consuming operation
super.onPostExecute(avoid);
mOnPDFCreatedInterface.onPDFCreated(mIsNewPDFCreated, mPath);
}
public boolean createPDF(String inputPath, String output, String pages) {
try {
Log.e("create ", pages);
PdfReader reader = new PdfReader(inputPath);
reader.selectPages(pages);
//if (reader.getNumberOfPages() )
PdfStamper pdfStamper = new PdfStamper(reader,
new FileOutputStream(output));
pdfStamper.close();
return true;

} catch (IOException | DocumentException e) {
e.printStackTrace();
return false;
}
}

}
Loading

0 comments on commit 7fc8528

Please sign in to comment.