Skip to content

Commit

Permalink
add javadoc comments for PdfToImages flow
Browse files Browse the repository at this point in the history
  • Loading branch information
amy6 committed Oct 16, 2018
1 parent bcc6ae4 commit e64d820
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public class PdfToImageFragment extends Fragment implements BottomSheetPopulate,
@BindView(R.id.recyclerViewFiles)
RecyclerView mRecyclerViewFiles;

/**
* inflates the layout for the fragment
* @param inflater reference to inflater object
* @param container parent for the inflated view
* @param savedInstanceState bundle with saved data, if any
* @return inflated view
*/
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand All @@ -95,6 +102,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
return rootView;
}

/**
* called when user chooses to share generated images
*/
@OnClick(R.id.shareImages)
void onShareFilesClick() {
if (mOutputFilePaths != null) {
Expand All @@ -106,22 +116,35 @@ void onShareFilesClick() {
}
}

/**
* called on click of bottom sheet
*/
@OnClick(R.id.viewFiles)
void onViewFilesClick() {
mBottomSheetUtils.showHideSheet(mSheetBehavior);
}

/**
* called when user chooses to view generated images
*/
@OnClick(R.id.viewImages)
void onViewImagesClicked() {
mActivity.startActivity(ImagesPreviewActivity.getStartIntent(mActivity, mOutputFilePaths));
}

/**
* invoked when user chooses to select a pdf file
* initiates an intent to pick a pdf file
*/
@OnClick(R.id.selectFile)
public void showFileChooser() {
startActivityForResult(mFileUtils.getFileChooser(),
INTENT_REQUEST_PICKFILE_CODE);
}

/**
* receives intent response for selecting a pdf file
*/
public void onActivityResult(int requestCode, int resultCode, Intent data) throws NullPointerException {
if (data == null || resultCode != RESULT_OK || data.getData() == null)
return;
Expand All @@ -131,6 +154,9 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) throw
}
}

/**
* invokes generation of images for pdf pages in the background
*/
@OnClick(R.id.createImages)
public void parse() {
new PdfToImages(mPath, mUri, this).execute();
Expand All @@ -145,13 +171,21 @@ public void onAttach(Context context) {
mBottomSheetUtils = new BottomSheetUtils(mActivity);
}

/**
* handles choosing a file from bottom sheet list
* @param path path of the file on the device
*/
@Override
public void onItemClick(String path) {
mUri = null;
mSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
setTextAndActivateButtons(path);
}

/**
* handles text and button states
* @param path path of the file on the device
*/
private void setTextAndActivateButtons(String path) {
mCreatedImages.setVisibility(View.GONE);
options.setVisibility(View.GONE);
Expand All @@ -161,23 +195,38 @@ private void setTextAndActivateButtons(String path) {
mSelectFileButton, mCreateImagesButton);
}

/**
* handles opening a generated image for the given pdf
* @param path path of the file on the device
*/
@Override
public void onFileItemClick(String path) {
mFileUtils.openImage(path);
}

/**
* initializes interactive views
*/
@Override
public void resetView() {
mPath = null;
mMorphButtonUtility.initializeButton(mSelectFileButton, mCreateImagesButton);
}

/**
* displays progress indicator
*/
@Override
public void extractionStarted() {
mMaterialDialog = createAnimationDialog(mActivity);
mMaterialDialog.show();
}

/**
* updates recycler view list items based with the generated images
* @param imageCount number of generated images
* @param outputFilePaths path for each generated image
*/
@Override
public void updateView(int imageCount, ArrayList<String> outputFilePaths) {

Expand All @@ -194,25 +243,32 @@ public void updateView(int imageCount, ArrayList<String> outputFilePaths) {
options.setVisibility(View.VISIBLE);
mOutputFilePaths = outputFilePaths;
ExtractImagesAdapter extractImagesAdapter = new ExtractImagesAdapter(mActivity, outputFilePaths, this);
// init recycler view for displaying generated image list
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mActivity);
mCreateImagesSuccessText.setText(text);
mCreatedImages.setVisibility(View.VISIBLE);
mCreatedImages.setLayoutManager(mLayoutManager);
// set up adapter
mCreatedImages.setAdapter(extractImagesAdapter);
mCreatedImages.addItemDecoration(new ViewFilesDividerItemDecoration(mActivity));
}

/**
* populates bottom sheet list with pdf files
* @param paths paths for pdf files on the device
*/
@Override
public void onPopulate(ArrayList<String> paths) {
if (paths == null || paths.size() == 0) {
mLayout.setVisibility(View.GONE);
} else {
// Init recycler view
// init recycler view for bottom sheet
mRecyclerViewFiles.setVisibility(View.VISIBLE);
MergeFilesAdapter mergeFilesAdapter = new MergeFilesAdapter(mActivity,
paths, this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mActivity);
mRecyclerViewFiles.setLayoutManager(mLayoutManager);
// set up adapter
mRecyclerViewFiles.setAdapter(mergeFilesAdapter);
mRecyclerViewFiles.addItemDecoration(new ViewFilesDividerItemDecoration(mActivity));
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/swati4star/createpdf/util/PdfToImages.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ protected Void doInBackground(Void... voids) {
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 (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
Expand All @@ -64,6 +67,7 @@ else if (mPath != null)
// close the page
page.close();

// generate numbered image file names
String filename = getFileNameWithoutExtension(mPath) +
"_" + Integer.toString(i + 1);
String path = saveImage(filename, bitmap);
Expand Down

0 comments on commit e64d820

Please sign in to comment.