Skip to content

Commit

Permalink
#513 - change master pwd, remove directory operations in view files, …
Browse files Browse the repository at this point in the history
…decrypt pdf by master pwd (#517)
  • Loading branch information
Swati4star authored Oct 28, 2018
1 parent ef7c271 commit cc8e22d
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@
import static swati4star.createpdf.util.Constants.DEFAULT_PAGE_SIZE_TEXT;
import static swati4star.createpdf.util.Constants.DEFAULT_QUALITY_VALUE;
import static swati4star.createpdf.util.Constants.IMAGE_SCALE_TYPE_ASPECT_RATIO;
import static swati4star.createpdf.util.Constants.MASTER_PWD_STRING;
import static swati4star.createpdf.util.Constants.OPEN_SELECT_IMAGES;
import static swati4star.createpdf.util.Constants.RESULT;
import static swati4star.createpdf.util.Constants.STORAGE_LOCATION;
import static swati4star.createpdf.util.Constants.appName;
import static swati4star.createpdf.util.DialogUtils.createAnimationDialog;
import static swati4star.createpdf.util.DialogUtils.createCustomDialog;
import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent;
Expand Down Expand Up @@ -232,6 +234,7 @@ void createPdf(boolean isgrayScale) {
mPdfOptions.setPageSize(PageSizeUtils.mPageSize);
mPdfOptions.setImageScaleType(mImageScaleType);
mPdfOptions.setPageNumStyle(mPageNumStyle);
mPdfOptions.setMasterPwd(mSharedPreferences.getString(MASTER_PWD_STRING, appName));
MaterialDialog.Builder builder = createCustomDialog(mActivity,
R.string.creating_pdf, R.string.enter_file_name);
builder.input(getString(R.string.example), null, (dialog, input) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -53,7 +54,9 @@
import swati4star.createpdf.util.ViewFilesDividerItemDecoration;

import static android.app.Activity.RESULT_OK;
import static swati4star.createpdf.util.Constants.MASTER_PWD_STRING;
import static swati4star.createpdf.util.Constants.STORAGE_LOCATION;
import static swati4star.createpdf.util.Constants.appName;
import static swati4star.createpdf.util.DialogUtils.createAnimationDialog;
import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent;
import static swati4star.createpdf.util.DialogUtils.createOverwriteDialog;
Expand All @@ -79,6 +82,7 @@ public class MergeFilesFragment extends Fragment implements MergeFilesAdapter.On
private EnhancementOptionsAdapter mEnhancementOptionsAdapter;
private boolean mPasswordProtected = false;
private String mPassword;
private SharedPreferences mSharedPrefs;

@BindView(R.id.lottie_progress)
LottieAnimationView mLottieProgress;
Expand Down Expand Up @@ -115,8 +119,8 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
mFilePaths = new ArrayList<>();
mMergeSelectedFilesAdapter = new MergeSelectedFilesAdapter(mActivity, mFilePaths, this);
mMorphButtonUtility = new MorphButtonUtility(mActivity);
mHomePath = PreferenceManager.getDefaultSharedPreferences(mActivity)
.getString(STORAGE_LOCATION,
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
mHomePath = mSharedPrefs.getString(STORAGE_LOCATION,
getDefaultStorageLocation());
mLottieProgress.setVisibility(View.VISIBLE);
mBottomSheetUtils.populateBottomSheetWithPDFs(this);
Expand Down Expand Up @@ -229,6 +233,7 @@ void startAddingPDF(View v) {
@OnClick(R.id.mergebtn)
void mergeFiles(final View view) {
String[] pdfpaths = mFilePaths.toArray(new String[0]);
String masterpwd = mSharedPrefs.getString(MASTER_PWD_STRING, appName);
new MaterialDialog.Builder(mActivity)
.title(R.string.creating_pdf)
.content(R.string.enter_file_name)
Expand All @@ -238,12 +243,12 @@ void mergeFiles(final View view) {
} else {
if (!mFileUtils.isFileExist(input + getString(R.string.pdf_ext))) {
new MergePdf(input.toString(), mHomePath, mPasswordProtected,
mPassword, this).execute(pdfpaths);
mPassword, this, masterpwd).execute(pdfpaths);
} else {
MaterialDialog.Builder builder = createOverwriteDialog(mActivity);
builder.onPositive((dialog12, which) -> new MergePdf(input.toString(),
mHomePath, mPasswordProtected, mPassword,
this).execute(pdfpaths))
this, masterpwd).execute(pdfpaths))
.onNegative((dialog1, which) -> mergeFiles(view)).show();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import swati4star.createpdf.util.PageSizeUtils;

import static swati4star.createpdf.util.Constants.DEFAULT_COMPRESSION;
import static swati4star.createpdf.util.Constants.MASTER_PWD_STRING;
import static swati4star.createpdf.util.Constants.STORAGE_LOCATION;
import static swati4star.createpdf.util.Constants.appName;
import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent;
import static swati4star.createpdf.util.ImageUtils.showImageScaleTypeDialog;
import static swati4star.createpdf.util.SettingsOptions.ImageEnhancementOptionsUtils.getEnhancementOptions;
Expand Down Expand Up @@ -132,9 +134,39 @@ public void onItemClick(int position) {
case 5:
showImageScaleTypeDialog(mActivity, true);
break;
case 6:
changeMasterPassword();
break;
}
}

/**
* To modify master password of PDFs
*/
private void changeMasterPassword() {

MaterialDialog.Builder builder = createCustomDialogWithoutContent(mActivity,
R.string.change_master_pwd);
MaterialDialog materialDialog =
builder.customView(R.layout.dialog_change_master_pwd, true)
.onPositive((dialog1, which) -> {
View view = dialog1.getCustomView();
EditText et = view.findViewById(R.id.value);
String value = et.getText().toString();
if (!value.isEmpty())
mSharedPreferences.edit().putString(MASTER_PWD_STRING, value).apply();
else
showSnackbar(mActivity, R.string.invalid_entry);


}).build();
View view = materialDialog.getCustomView();
TextView tv = view.findViewById(R.id.content);
tv.setText(String.format(mActivity.getString(R.string.current_master_pwd),
mSharedPreferences.getString(MASTER_PWD_STRING, appName)));
materialDialog.show();
}

/**
* To modify default image compression value
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

Expand Down Expand Up @@ -69,8 +68,6 @@ public class ViewFilesFragment extends Fragment

private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE_RESULT = 10;

@BindView(R.id.layout_main)
public LinearLayout mainLayout;
@BindView(R.id.getStarted)
public Button getStarted;
@BindView(R.id.filesRecyclerView)
Expand Down Expand Up @@ -326,21 +323,18 @@ private void displaySortDialog() {
@Override
public void setEmptyStateVisible() {
emptyView.setVisibility(View.VISIBLE);
mainLayout.setVisibility(View.GONE);
noPermissionsLayout.setVisibility(View.GONE);
}

@Override
public void setEmptyStateInvisible() {
emptyView.setVisibility(View.GONE);
mainLayout.setVisibility(View.VISIBLE);
noPermissionsLayout.setVisibility(View.GONE);
}

@Override
public void showNoPermissionsView() {
emptyView.setVisibility(View.GONE);
mainLayout.setVisibility(View.GONE);
noPermissionsLayout.setVisibility(View.VISIBLE);
}

Expand All @@ -349,88 +343,6 @@ public void hideNoPermissionsView() {
noPermissionsLayout.setVisibility(View.GONE);
}

// DIRECTORY OPERATIONS

@OnClick(R.id.new_dir)
void moveToNewDirectory() {
if (mViewFilesAdapter.areItemsSelected())
moveFilesToDirectory(NEW_DIR);
else
showSnackbar(mActivity, R.string.snackbar_no_pdfs_selected);
}

@OnClick(R.id.move_to_dir)
void moveToDirectory() {
if (mViewFilesAdapter.areItemsSelected())
moveFilesToDirectory(EXISTING_DIR);
else
showSnackbar(mActivity, R.string.snackbar_no_pdfs_selected);
}

@OnClick(R.id.move_to_home_dir)
void moveFilesToHomeDirectory() {
if (!mViewFilesAdapter.areItemsSelected()) {
showSnackbar(mActivity, R.string.snackbar_no_pdfs_selected);
return;
}
final ArrayList<String> filePath = mViewFilesAdapter.getSelectedFilePath();
if (filePath == null) {
showSnackbar(mActivity, R.string.snackbar_no_pdfs_selected);
} else {
final File[] files = mDirectoryUtils.getOrCreatePdfDirectory().listFiles();
for (File pdf : mDirectoryUtils.getPdfsFromPdfFolder(files)) {
//remove the files already present in home directory
filePath.remove(pdf.getPath());
}
new MoveFilesToDirectory(mActivity
, filePath
, null
, MoveFilesToDirectory.HOME_DIRECTORY)
.execute();
populatePdfList();
}
}

@OnClick(R.id.delete_dir)
void deleteDirectory() {
LayoutInflater inflater = getLayoutInflater();
View alertView = inflater.inflate(R.layout.directory_dialog, null);
final ArrayList<String> pdfFiles = new ArrayList<>();
final EditText input = alertView.findViewById(R.id.directory_editText);
TextView message = alertView.findViewById(R.id.directory_textView);
message.setText(R.string.dialog_delete_dir);
mAlertDialogBuilder.setTitle(R.string.delete_directory)
.setView(alertView)
.setPositiveButton(R.string.ok, (dialogInterface, i) -> {
final String dirName = input.getText().toString();
final File directory = mDirectoryUtils.getDirectory(dirName);
if (directory == null || dirName.trim().isEmpty()) {
showSnackbar(mActivity, R.string.dir_does_not_exists);
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setTitle(R.string.delete)
.setMessage(R.string.delete_dialog)
.setCancelable(true)
.setPositiveButton(R.string.yes, (dialogInterface1, i12) -> {
for (File pdf : directory.listFiles()) {
pdfFiles.add(pdf.getPath());
}
new MoveFilesToDirectory(mActivity, pdfFiles,
dirName, MoveFilesToDirectory.DELETE_DIRECTORY)
.execute();
populatePdfList();
})
.setNegativeButton(R.string.no, (dialog, i1) -> {
dialog.dismiss();
dialogInterface.dismiss();
});
builder.create().show();
}
});
mAlertDialogBuilder.create().show();
}


//When the "GET STARTED" button is clicked, the user is taken to home
@OnClick(R.id.getStarted)
public void loadHome() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ImageToPDFOptions extends PDFOptions {
private int mMarginLeft = 0;
private String mImageScaleType;
private String mPageNumStyle;
private String mMasterPwd;

public ImageToPDFOptions() {
super();
Expand All @@ -21,10 +22,12 @@ public ImageToPDFOptions() {

public ImageToPDFOptions(String mFileName, String mPageSize, boolean mPasswordProtected,
String mPassword, String mQualityString, int mBorderWidth,
String masterPwd,
ArrayList<String> mImagesUri) {
super(mFileName, mPageSize, mPasswordProtected, mPassword, mBorderWidth);
this.mQualityString = mQualityString;
this.mImagesUri = mImagesUri;
this.mMasterPwd = masterPwd;
}

public String getQualityString() {
Expand All @@ -50,6 +53,10 @@ public void setMargins(int top, int bottom, int right, int left) {
mMarginLeft = left;
}

public void setMasterPwd(String pwd) {
this.mMasterPwd = pwd;
}

public int getMarginTop() {
return mMarginTop;
}
Expand Down Expand Up @@ -80,4 +87,8 @@ public String getPageNumStyle() {
public void setPageNumStyle(String mPageNumStyle) {
this.mPageNumStyle = mPageNumStyle;
}

public String getMasterPwd() {
return mMasterPwd;
}
}
1 change: 1 addition & 0 deletions app/src/main/java/swati4star/createpdf/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Constants {
public static final String PG_NUM_STYLE_PAGE_X_OF_N = "pg_num_style_page_x_of_n";
public static final String PG_NUM_STYLE_X_OF_N = "pg_num_style_x_of_n";
public static final String PG_NUM_STYLE_X = "pg_num_style_x";
public static final String MASTER_PWD_STRING = "master_password";

public static final String BUNDLE_DATA = "bundle_data";
public static final String REORDER_PAGES = "reorder_pages";
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/swati4star/createpdf/util/CreatePdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import swati4star.createpdf.model.ImageToPDFOptions;

import static swati4star.createpdf.util.Constants.IMAGE_SCALE_TYPE_ASPECT_RATIO;
import static swati4star.createpdf.util.Constants.appName;
import static swati4star.createpdf.util.Constants.pdfExtension;

/**
Expand All @@ -48,6 +47,7 @@ public class CreatePdf extends AsyncTask<String, String, String> {
private int mMarginLeft;
private String mImagescaleType;
private String mPageNumStyle;
private String mMasterPwd;

public CreatePdf(ImageToPDFOptions mImageToPDFOptions, String parentPath,
OnPDFCreatedInterface onPDFCreated) {
Expand All @@ -66,6 +66,7 @@ public CreatePdf(ImageToPDFOptions mImageToPDFOptions, String parentPath,
this.mMarginLeft = mImageToPDFOptions.getMarginLeft();
this.mImagescaleType = mImageToPDFOptions.getImageScaleType();
this.mPageNumStyle = mImageToPDFOptions.getPageNumStyle();
this.mMasterPwd = mImageToPDFOptions.getMasterPwd();
mPath = parentPath;
}

Expand Down Expand Up @@ -102,7 +103,7 @@ protected String doInBackground(String... params) {
Log.v("Stage 3", "Pdf writer");

if (mPasswordProtected) {
writer.setEncryption(mPassword.getBytes(), appName.getBytes(),
writer.setEncryption(mPassword.getBytes(), mMasterPwd.getBytes(),
PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY,
PdfWriter.ENCRYPTION_AES_128);

Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/swati4star/createpdf/util/MergeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.afollestad.materialdialogs.MaterialDialog;
Expand All @@ -11,7 +12,9 @@
import swati4star.createpdf.database.DatabaseHelper;
import swati4star.createpdf.interfaces.MergeFilesListener;

import static swati4star.createpdf.util.Constants.MASTER_PWD_STRING;
import static swati4star.createpdf.util.Constants.STORAGE_LOCATION;
import static swati4star.createpdf.util.Constants.appName;
import static swati4star.createpdf.util.DialogUtils.createAnimationDialog;
import static swati4star.createpdf.util.DialogUtils.createOverwriteDialog;
import static swati4star.createpdf.util.StringUtils.getDefaultStorageLocation;
Expand All @@ -27,6 +30,7 @@ public class MergeHelper implements MergeFilesListener {
private String mHomePath;
private Context mContext;
private ViewFilesAdapter mViewFilesAdapter;
private SharedPreferences mSharedPrefs;

public MergeHelper(Activity activity, ViewFilesAdapter viewFilesAdapter) {
mActivity = activity;
Expand All @@ -36,10 +40,12 @@ public MergeHelper(Activity activity, ViewFilesAdapter viewFilesAdapter) {
getDefaultStorageLocation());
mContext = mActivity;
mViewFilesAdapter = viewFilesAdapter;
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
}

public void mergeFiles() {
String[] pdfpaths = mViewFilesAdapter.getSelectedFilePath().toArray(new String[0]);
String masterpwd = mSharedPrefs.getString(MASTER_PWD_STRING, appName);
new MaterialDialog.Builder(mActivity)
.title(R.string.creating_pdf)
.content(R.string.enter_file_name)
Expand All @@ -49,12 +55,12 @@ public void mergeFiles() {
} else {
if (!mFileUtils.isFileExist(input + mContext.getResources().getString(R.string.pdf_ext))) {
new MergePdf(input.toString(), mHomePath, mPasswordProtected,
mPassword, this).execute(pdfpaths);
mPassword, this, masterpwd).execute(pdfpaths);
} else {
MaterialDialog.Builder builder = createOverwriteDialog(mActivity);
builder.onPositive((dialog12, which) -> new MergePdf(input.toString(),
mHomePath, mPasswordProtected, mPassword,
this).execute(pdfpaths))
this, masterpwd).execute(pdfpaths))
.onNegative((dialog1, which) -> mergeFiles()).show();
}
}
Expand Down
Loading

0 comments on commit cc8e22d

Please sign in to comment.