Skip to content

Commit

Permalink
fixes #485 - add an option to maintain aspect ratio/stretch image (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swati4star committed Oct 11, 2018
1 parent 3882de9 commit 8d10607
Show file tree
Hide file tree
Showing 23 changed files with 198 additions and 230 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
minSdkVersion 21
multiDexEnabled true
targetSdkVersion 27
versionCode 63
versionName "8.3.6"
versionCode 65
versionName "8.3.8"
}
buildTypes {
release {
Expand Down
27 changes: 25 additions & 2 deletions app/src/main/java/swati4star/createpdf/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package swati4star.createpdf.activity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
Expand Down Expand Up @@ -84,13 +88,13 @@ protected void onCreate(Bundle savedInstanceState) {
// Check if images are received
handleReceivedImagesIntent(fragment);



mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int count = mSharedPreferences.getInt(LAUNCH_COUNT, 0);
if (count > 0 && count % 15 == 0)
mFeedbackUtils.rateUs();
mSharedPreferences.edit().putInt(LAUNCH_COUNT, count + 1).apply();

getRuntimePermissions();
}

/**
Expand Down Expand Up @@ -324,4 +328,23 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
public void setNavigationViewSelection(int index) {
mNavigationView.getMenu().getItem(index).setChecked(true);
}

private boolean getRuntimePermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)) {
requestPermissions(new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA},
0);
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@
import static swati4star.createpdf.util.Constants.DEFAULT_BORDER_WIDTH;
import static swati4star.createpdf.util.Constants.DEFAULT_COMPRESSION;
import static swati4star.createpdf.util.Constants.DEFAULT_IMAGE_BORDER_TEXT;
import static swati4star.createpdf.util.Constants.DEFAULT_IMAGE_SCALETYPE_TEXT;
import static swati4star.createpdf.util.Constants.DEFAULT_PAGE_SIZE;
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.OPEN_SELECT_IMAGES;
import static swati4star.createpdf.util.Constants.RESULT;
import static swati4star.createpdf.util.Constants.STORAGE_LOCATION;
Expand All @@ -81,6 +83,8 @@
import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent;
import static swati4star.createpdf.util.DialogUtils.createOverwriteDialog;
import static swati4star.createpdf.util.ImageEnhancementOptionsUtils.getEnhancementOptions;
import static swati4star.createpdf.util.ImageUtils.mImageScaleType;
import static swati4star.createpdf.util.ImageUtils.showImageScaleTypeDialog;
import static swati4star.createpdf.util.StringUtils.getDefaultStorageLocation;
import static swati4star.createpdf.util.StringUtils.getSnackbarwithAction;
import static swati4star.createpdf.util.StringUtils.showSnackbar;
Expand Down Expand Up @@ -218,6 +222,7 @@ void startAddingImages() {
void createPdf() {
mPdfOptions.setImagesUri(mImagesUri);
mPdfOptions.setPageSize(PageSizeUtils.mPageSize);
mPdfOptions.setImageScaleType(mImageScaleType);
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 Expand Up @@ -367,24 +372,27 @@ public void onItemClick(int position) {
INTENT_REQUEST_APPLY_FILTER);
break;
case 4:
mPageSizeUtils.showPageSizeDialog(R.layout.set_page_size_dialog, false);
mPageSizeUtils.showPageSizeDialog(false);
break;
case 5:
showImageScaleTypeDialog(mActivity, false);
break;
case 6:
startActivityForResult(PreviewActivity.getStartIntent(mActivity, mImagesUri),
INTENT_REQUEST_PREVIEW_IMAGE);
break;
case 6:
case 7:
addBorder();
break;
case 7:
case 8:
startActivityForResult(RearrangeImages.getStartIntent(mActivity, mImagesUri),
INTENT_REQUEST_REARRANGE_IMAGE);
break;
case 8:
case 9:
saveCurrentImage();
createPdf();
break;
case 9:
case 10:
addMargins();
break;
}
Expand Down Expand Up @@ -602,6 +610,9 @@ private void resetValues() {
mImageCounter = 0;
showEnhancementOptions();
mNoOfImages.setVisibility(View.GONE);
mImageScaleType = mSharedPreferences.getString(DEFAULT_IMAGE_SCALETYPE_TEXT,
IMAGE_SCALE_TYPE_ASPECT_RATIO);
mPdfOptions.setMargins(0, 0, 0, 0);
}

/**
Expand Down Expand Up @@ -667,15 +678,6 @@ void addMargins() {
mMarginLeft = Integer.parseInt(left.getText().toString());
mPdfOptions.setMargins(mMarginTop, mMarginBottom, mMarginRight, mMarginLeft);
})).build();
View view = materialDialog.getCustomView();
EditText top = view.findViewById(R.id.topMarginEditText);
EditText bottom = view.findViewById(R.id.bottomMarginEditText);
EditText right = view.findViewById(R.id.rightMarginEditText);
EditText left = view.findViewById(R.id.leftMarginEditText);
top.setText(Integer.toString(mMarginTop));
bottom.setText(Integer.toString(mMarginBottom));
right.setText(Integer.toString(mMarginRight));
left.setText(Integer.toString(mMarginLeft));
materialDialog.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static swati4star.createpdf.util.Constants.DEFAULT_COMPRESSION;
import static swati4star.createpdf.util.Constants.STORAGE_LOCATION;
import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent;
import static swati4star.createpdf.util.ImageUtils.showImageScaleTypeDialog;
import static swati4star.createpdf.util.SettingsOptions.ImageEnhancementOptionsUtils.getEnhancementOptions;
import static swati4star.createpdf.util.StringUtils.getDefaultStorageLocation;
import static swati4star.createpdf.util.StringUtils.showSnackbar;
Expand Down Expand Up @@ -95,8 +96,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
showSnackbar(mActivity, R.string.storage_location_modified);
storageLocation.setText(mSharedPreferences.getString(STORAGE_LOCATION,
getDefaultStorageLocation()));
} else {
showSnackbar(mActivity, R.string.error_occurred);
}
break;
}
Expand Down Expand Up @@ -130,6 +129,9 @@ public void onItemClick(int position) {
case 4:
setTheme();
break;
case 5:
showImageScaleTypeDialog(mActivity, true);
break;
}
}

Expand All @@ -138,8 +140,8 @@ public void onItemClick(int position) {
*/
private void changeCompressImage() {

createCustomDialogWithoutContent(mActivity, R.string.compression_image_edit)
.customView(R.layout.compress_image_default, true)
MaterialDialog dialog = createCustomDialogWithoutContent(mActivity, R.string.compression_image_edit)
.customView(R.layout.compress_image_dialog, true)
.onPositive((dialog1, which) -> {
final EditText qualityInput = dialog1.getCustomView().findViewById(R.id.quality);
int check;
Expand All @@ -156,7 +158,10 @@ private void changeCompressImage() {
} catch (NumberFormatException e) {
showSnackbar(mActivity, R.string.invalid_entry);
}
}).show();
}).build();
View customView = dialog.getCustomView();
customView.findViewById(R.id.cbSetDefault).setVisibility(View.GONE);
dialog.show();
}


Expand All @@ -166,9 +171,9 @@ private void changeCompressImage() {
private void editFontSize() {
MaterialDialog.Builder builder = createCustomDialogWithoutContent(mActivity,
R.string.font_size_edit);
builder.customView(R.layout.dialog_font_size_default, true)
.onPositive((dialog, which) -> {
final EditText fontInput = dialog.getCustomView().findViewById(R.id.fontInput);
MaterialDialog dialog = builder.customView(R.layout.dialog_font_size, true)
.onPositive((dialog1, which) -> {
final EditText fontInput = dialog1.getCustomView().findViewById(R.id.fontInput);
try {
int check = Integer.parseInt(String.valueOf(fontInput.getText()));
if (check > 1000 || check < 0) {
Expand All @@ -185,7 +190,10 @@ private void editFontSize() {
showSnackbar(mActivity, R.string.invalid_entry);
}
})
.show();
.build();
View customView = dialog.getCustomView();
customView.findViewById(R.id.cbSetFontDefault).setVisibility(View.GONE);
dialog.show();
}

/**
Expand All @@ -197,7 +205,7 @@ private void changeFontFamily() {
int ordinal = Font.FontFamily.valueOf(fontFamily).ordinal();
MaterialDialog.Builder builder = createCustomDialogWithoutContent(mActivity,
R.string.font_family_edit);
MaterialDialog materialDialog = builder.customView(R.layout.dialog_font_family_default, true)
MaterialDialog materialDialog = builder.customView(R.layout.dialog_font_family, true)
.onPositive((dialog, which) -> {
View view = dialog.getCustomView();
RadioGroup radioGroup = view.findViewById(R.id.radio_group_font_family);
Expand All @@ -210,9 +218,11 @@ private void changeFontFamily() {
showSettingsOptions();
})
.build();
RadioGroup radioGroup = materialDialog.getCustomView().findViewById(R.id.radio_group_font_family);
View customView = materialDialog.getCustomView();
RadioGroup radioGroup = customView.findViewById(R.id.radio_group_font_family);
RadioButton rb = (RadioButton) radioGroup.getChildAt(ordinal);
rb.setChecked(true);
customView.findViewById(R.id.cbSetDefault).setVisibility(View.GONE);
materialDialog.show();
}

Expand All @@ -221,7 +231,7 @@ private void changeFontFamily() {
*/
public void setPageSize() {
PageSizeUtils utils = new PageSizeUtils(mActivity);
MaterialDialog materialDialog = utils.showPageSizeDialog(R.layout.set_page_size_dialog_default, true);
MaterialDialog materialDialog = utils.showPageSizeDialog(true);
materialDialog.setOnDismissListener(dialog -> showSettingsOptions());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void onPopulate(ArrayList<String> paths) {
// Init recycler view
mRecyclerViewFiles.setVisibility(View.VISIBLE);
MergeFilesAdapter mergeFilesAdapter = new MergeFilesAdapter(mActivity,
paths, this::onFileItemClick);
paths, this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mActivity);
mRecyclerViewFiles.setLayoutManager(mLayoutManager);
mRecyclerViewFiles.setAdapter(mergeFilesAdapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void afterTextChanged(Editable input) {

private void setPageSize() {
PageSizeUtils utils = new PageSizeUtils(mActivity);
utils.showPageSizeDialog(R.layout.set_page_size_dialog, false);
utils.showPageSizeDialog(false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ImageToPDFOptions extends PDFOptions {
private int mMarginBottom = 0;
private int mMarginRight = 0;
private int mMarginLeft = 0;
private String mImageScaleType;

public ImageToPDFOptions() {
super();
Expand Down Expand Up @@ -63,4 +64,12 @@ public int getMarginRight() {
public int getMarginLeft() {
return mMarginLeft;
}

public String getImageScaleType() {
return mImageScaleType;
}

public void setImageScaleType(String mImageScaleType) {
this.mImageScaleType = mImageScaleType;
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/swati4star/createpdf/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class Constants {
public static final int DEFAULT_QUALITY_VALUE = 30;
public static final int DEFAULT_BORDER_WIDTH = 0;
public static final String STORAGE_LOCATION = "storage_location";
public static final String DEFAULT_IMAGE_SCALETYPE_TEXT = "image_scale_type";
public static final String IMAGE_SCALE_TYPE_STRETCH = "stretch_image";
public static final String IMAGE_SCALE_TYPE_ASPECT_RATIO = "maintain_aspect_ratio";

public static final String BUNDLE_DATA = "bundle_data";
public static final String REORDER_PAGES = "reorder_pages";
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/swati4star/createpdf/util/CreatePdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import swati4star.createpdf.interfaces.OnPDFCreatedInterface;
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 @@ -41,6 +42,7 @@ public class CreatePdf extends AsyncTask<String, String, String> {
private int mMarginBottom;
private int mMarginRight;
private int mMarginLeft;
private String mImagescaleType;

public CreatePdf(ImageToPDFOptions mImageToPDFOptions, String parentPath,
OnPDFCreatedInterface onPDFCreated) {
Expand All @@ -57,6 +59,7 @@ public CreatePdf(ImageToPDFOptions mImageToPDFOptions, String parentPath,
this.mMarginBottom = mImageToPDFOptions.getMarginBottom();
this.mMarginRight = mImageToPDFOptions.getMarginRight();
this.mMarginLeft = mImageToPDFOptions.getMarginLeft();
this.mImagescaleType = mImageToPDFOptions.getImageScaleType();
mPath = parentPath;
}

Expand Down Expand Up @@ -126,21 +129,20 @@ protected String doInBackground(String... params) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(mImagesUri.get(i), bmOptions);

Rectangle imageSize = ImageUtils.calculateFitSize(bitmap.getWidth() + mBorderWidth,
bitmap.getHeight() + mBorderWidth, documentRect);
image.scaleAbsolute(imageSize);

Log.v("Stage 6", "Image path adding");

float pageWidth = document.getPageSize().getWidth() - (mMarginLeft + mMarginRight);
float pageHeight = document.getPageSize().getHeight() - (mMarginBottom + mMarginTop);
image.scaleToFit(pageWidth, pageHeight);
if (mImagescaleType.equals(IMAGE_SCALE_TYPE_ASPECT_RATIO))
image.scaleToFit(pageWidth, pageHeight);
else
image.scaleAbsolute(pageWidth, pageHeight);

image.setAbsolutePosition(
(documentRect.getWidth() - image.getScaledWidth()) / 2,
(documentRect.getHeight() - image.getScaledHeight()) / 2);
Log.v("Stage 7", "Image Alignments");

Log.v("Stage 7", "Image Alignments");
document.add(image);

document.newPage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public static ArrayList<EnhancementOptionsEntity> getEnhancementOptions(Context
context.getResources().getDrawable(R.drawable.ic_page_size_24dp),
context.getResources().getString(R.string.set_page_size_text)));

options.add(new EnhancementOptionsEntity(
context.getResources().getDrawable(R.drawable.ic_aspect_ratio_black_24dp),
context.getResources().getString(R.string.image_scale_type)));

options.add(new EnhancementOptionsEntity(
context.getResources().getDrawable(R.drawable.ic_play_circle_outline_black_24dp),
context.getResources().getString(R.string.preview_image_to_pdf)));
Expand Down
Loading

0 comments on commit 8d10607

Please sign in to comment.