forked from Swati4star/Images-to-PDF
/
PdfToImageFragment.java
335 lines (304 loc) · 11.3 KB
/
PdfToImageFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package swati4star.createpdf.fragment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
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.RecyclerView;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.afollestad.materialdialogs.MaterialDialog;
import com.airbnb.lottie.LottieAnimationView;
import com.dd.morphingbutton.MorphingButton;
import java.io.File;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import swati4star.createpdf.R;
import swati4star.createpdf.activity.ImagesPreviewActivity;
import swati4star.createpdf.adapter.ExtractImagesAdapter;
import swati4star.createpdf.adapter.MergeFilesAdapter;
import swati4star.createpdf.interfaces.BottomSheetPopulate;
import swati4star.createpdf.interfaces.ExtractImagesListener;
import swati4star.createpdf.interfaces.OnBackPressedInterface;
import swati4star.createpdf.util.BottomSheetCallback;
import swati4star.createpdf.util.BottomSheetUtils;
import swati4star.createpdf.util.CommonCodeUtils;
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;
import static swati4star.createpdf.util.CommonCodeUtils.closeBottomSheetUtil;
import static swati4star.createpdf.util.CommonCodeUtils.populateUtil;
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,
ExtractImagesListener, ExtractImagesAdapter.OnFileItemClickedListener, OnBackPressedInterface {
private static final int INTENT_REQUEST_PICKFILE_CODE = 10;
private Activity mActivity;
private String mPath;
private Uri mUri;
private MorphButtonUtility mMorphButtonUtility;
private FileUtils mFileUtils;
private BottomSheetBehavior mSheetBehavior;
private BottomSheetUtils mBottomSheetUtils;
private ArrayList<String> mOutputFilePaths;
private MaterialDialog mMaterialDialog;
private String mOperation;
private Context mContext;
private PDFUtils mPDFUtils;
private String[] mInputPassword;
@BindView(R.id.lottie_progress)
LottieAnimationView mLottieProgress;
@BindView(R.id.bottom_sheet)
LinearLayout mLayoutBottomSheet;
@BindView(R.id.upArrow)
ImageView mUpArrow;
@BindView(R.id.selectFile)
MorphingButton mSelectFileButton;
@BindView(R.id.createImages)
MorphingButton mCreateImagesButton;
@BindView(R.id.created_images)
RecyclerView mCreatedImages;
@BindView(R.id.pdfToImagesText)
TextView mCreateImagesSuccessText;
@BindView(R.id.options)
LinearLayout options;
@BindView(R.id.layout)
RelativeLayout mLayout;
@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) {
View rootView = inflater.inflate(R.layout.fragment_pdf_to_image, container, false);
ButterKnife.bind(this, rootView);
mOperation = getArguments().getString(BUNDLE_DATA);
mSheetBehavior = BottomSheetBehavior.from(mLayoutBottomSheet);
mSheetBehavior.setBottomSheetCallback(new BottomSheetCallback(mUpArrow, isAdded()));
mLottieProgress.setVisibility(View.VISIBLE);
mBottomSheetUtils.populateBottomSheetWithPDFs(this);
resetView();
return rootView;
}
@OnClick(R.id.viewImagesInGallery)
void onImagesInGalleryClick() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imagesUri = Uri.parse("content:///storage/emulated/0/PDFfiles/");
intent.setDataAndType(imagesUri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
/**
* called when user chooses to share generated images
*/
@OnClick(R.id.shareImages)
void onShareFilesClick() {
if (mOutputFilePaths != null) {
ArrayList<File> fileArrayList = new ArrayList<>();
for (String path : mOutputFilePaths) {
fileArrayList.add(new File(path));
}
mFileUtils.shareMultipleFiles(fileArrayList);
}
}
/**
* 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;
if (requestCode == INTENT_REQUEST_PICKFILE_CODE) {
mUri = data.getData();
//Getting Absolute Path
String path = RealPathUtil.getRealPath(getContext(), data.getData());
setTextAndActivateButtons(path);
}
}
/**
* invokes generation of images for pdf pages in the background by checking
* for encryption first.
*/
@OnClick(R.id.createImages)
public void parse() {
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();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = (Activity) context;
mMorphButtonUtility = new MorphButtonUtility(mActivity);
mFileUtils = new FileUtils(mActivity);
mBottomSheetUtils = new BottomSheetUtils(mActivity);
mContext = context;
mPDFUtils = new PDFUtils(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) {
if (path == null) {
showSnackbar(mActivity, R.string.error_occurred);
resetView();
return;
}
mCreatedImages.setVisibility(View.GONE);
options.setVisibility(View.GONE);
mCreateImagesSuccessText.setVisibility(View.GONE);
mPath = path;
mMorphButtonUtility.setTextAndActivateButtons(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) {
mMaterialDialog.dismiss();
resetView();
mOutputFilePaths = outputFilePaths;
CommonCodeUtils.updateView(mActivity, imageCount, outputFilePaths,
mCreateImagesSuccessText, options, mCreatedImages, this);
}
/**
* populates bottom sheet list with pdf files
*
* @param paths paths for pdf files on the device
*/
@Override
public void onPopulate(ArrayList<String> paths) {
populateUtil(mActivity, paths, this, mLayout, mLottieProgress, mRecyclerViewFiles);
}
@Override
public void closeBottomSheet() {
closeBottomSheetUtil(mSheetBehavior);
}
@Override
public boolean checkSheetBehaviour() {
return checkSheetBehaviourUtil(mSheetBehavior);
}
}