diff options
author | Stefan Niedermann <info@niedermann.it> | 2021-03-25 22:50:06 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2021-03-25 22:50:06 +0300 |
commit | a118ee3da56b977be2476c712f075125d78fd524 (patch) | |
tree | c89eb1e0a77914114d2c1086450e98fdc7637dfa /app/src/main/java | |
parent | 7ae471d95d832d0f6f3460666ae7be76a9d714bc (diff) |
Fix #587 Compress images before uploading as attachment
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src/main/java')
2 files changed, 34 insertions, 12 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java index 0cb59c006..1da79a067 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java @@ -3,6 +3,7 @@ package it.niedermann.nextcloud.deck.ui.card.attachments; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.ColorStateList; import android.graphics.Bitmap; import android.net.Uri; @@ -24,6 +25,7 @@ import androidx.core.app.SharedElementCallback; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; +import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -96,6 +98,7 @@ public class CardAttachmentsFragment extends BrandedFragment implements Attachme private EditCardViewModel editViewModel; private PreviewDialogViewModel previewViewModel; private BottomSheetBehavior<LinearLayout> mBottomSheetBehaviour; + private boolean compressImagesOnUpload = true; private RecyclerView.ItemDecoration galleryItemDecoration; @@ -235,6 +238,8 @@ public class CardAttachmentsFragment extends BrandedFragment implements Attachme } @Nullable Context context = requireContext(); applyBrand(readBrandMainColor(context)); + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()); + compressImagesOnUpload = sharedPreferences.getBoolean(getString(R.string.pref_key_compress_image_attachments), true); return binding.getRoot(); } @@ -413,16 +418,21 @@ public class CardAttachmentsFragment extends BrandedFragment implements Attachme try { final File originalFile = copyContentUriToTempFile(requireContext(), sourceUri, editViewModel.getAccount().getId(), editViewModel.getFullCard().getLocalId()); requireActivity().runOnUiThread(() -> { - if (MimeTypeUtil.isImage(mimeType)) { - JavaCompressor.compress( - (AppCompatActivity) requireActivity(), - originalFile, - (status, file) -> uploadNewAttachmentFromFile(status ? file : originalFile, mimeType), - new ResolutionConstraint(1920, 1920), - new SizeConstraint(1_000_000, 10, 10, 10), - new FormatConstraint(Bitmap.CompressFormat.JPEG), - new QualityConstraint(80) - ); + if (compressImagesOnUpload && MimeTypeUtil.isImage(mimeType)) { + try { + JavaCompressor.compress( + (AppCompatActivity) requireActivity(), + originalFile, + (status, file) -> uploadNewAttachmentFromFile(status && file != null ? file : originalFile, mimeType), + new ResolutionConstraint(1920, 1920), + new SizeConstraint(1_000_000, 10, 10, 10), + new FormatConstraint(Bitmap.CompressFormat.JPEG), + new QualityConstraint(80) + ); + } catch (Throwable t) { + DeckLog.logError(t); + uploadNewAttachmentFromFile(originalFile, mimeType); + } } else { uploadNewAttachmentFromFile(originalFile, mimeType); } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java index 91c7ad995..ef9e7ccc0 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java @@ -23,6 +23,7 @@ public class SettingsFragment extends PreferenceFragmentCompat { private BrandedSwitchPreference wifiOnlyPref; private BrandedSwitchPreference compactPref; + private BrandedSwitchPreference compressImageAttachmentsPref; private BrandedSwitchPreference debuggingPref; private BrandedSwitchPreference eTagPref; @@ -31,7 +32,6 @@ public class SettingsFragment extends PreferenceFragmentCompat { setPreferencesFromResource(R.xml.settings, rootKey); wifiOnlyPref = findPreference(getString(R.string.pref_key_wifi_only)); - if (wifiOnlyPref != null) { wifiOnlyPref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> { final Boolean syncOnWifiOnly = (Boolean) newValue; @@ -54,8 +54,19 @@ public class SettingsFragment extends PreferenceFragmentCompat { DeckLog.error("Could not find preference with key:", getString(R.string.pref_key_dark_theme)); } - compactPref = findPreference(getString(R.string.pref_key_compact)); + compressImageAttachmentsPref = findPreference(getString(R.string.pref_key_compress_image_attachments)); + if (compressImageAttachmentsPref != null) { + compressImageAttachmentsPref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> { + setAppTheme(Integer.parseInt((String) newValue)); + requireActivity().setResult(Activity.RESULT_OK); + ActivityCompat.recreate(requireActivity()); + return true; + }); + } else { + DeckLog.error("Could not find preference with key:", getString(R.string.pref_key_compress_image_attachments)); + } + compactPref = findPreference(getString(R.string.pref_key_compact)); final ListPreference backgroundSyncPref = findPreference(getString(R.string.pref_key_background_sync)); if (backgroundSyncPref != null) { backgroundSyncPref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> { @@ -87,6 +98,7 @@ public class SettingsFragment extends PreferenceFragmentCompat { DeckApplication.readCurrentAccountColor().observe(getViewLifecycleOwner(), (mainColor) -> { wifiOnlyPref.applyBrand(mainColor); compactPref.applyBrand(mainColor); + compressImageAttachmentsPref.applyBrand(mainColor); debuggingPref.applyBrand(mainColor); eTagPref.applyBrand(mainColor); }); |