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 | |
parent | 7ae471d95d832d0f6f3460666ae7be76a9d714bc (diff) |
Fix #587 Compress images before uploading as attachment
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app')
6 files changed, 49 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); }); diff --git a/app/src/main/res/drawable/ic_baseline_photo_size_select_small_24.xml b/app/src/main/res/drawable/ic_baseline_photo_size_select_small_24.xml new file mode 100644 index 000000000..948e77c03 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_photo_size_select_small_24.xml @@ -0,0 +1,5 @@ +<vector android:height="24dp" android:tint="#757575" + android:viewportHeight="24" android:viewportWidth="24" + android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> + <path android:fillColor="@android:color/white" android:pathData="M23,15h-2v2h2v-2zM23,11h-2v2h2v-2zM23,19h-2v2c1,0 2,-1 2,-2zM15,3h-2v2h2L15,3zM23,7h-2v2h2L23,7zM21,3v2h2c0,-1 -1,-2 -2,-2zM3,21h8v-6L1,15v4c0,1.1 0.9,2 2,2zM3,7L1,7v2h2L3,7zM15,19h-2v2h2v-2zM19,3h-2v2h2L19,3zM19,19h-2v2h2v-2zM3,3C2,3 1,4 1,5h2L3,3zM3,11L1,11v2h2v-2zM11,3L9,3v2h2L11,3zM7,3L5,3v2h2L7,3z"/> +</vector> diff --git a/app/src/main/res/values/setup.xml b/app/src/main/res/values/setup.xml index f9fc4f697..1ca6063cb 100644 --- a/app/src/main/res/values/setup.xml +++ b/app/src/main/res/values/setup.xml @@ -8,6 +8,7 @@ <string name="pref_key_wifi_only" translatable="false">wifiOnly</string> <string name="pref_key_dark_theme" translatable="false">darkTheme</string> <string name="pref_key_compact" translatable="false">compact</string> + <string name="pref_key_compress_image_attachments" translatable="false">compressImageAttachments</string> <string name="pref_key_background_sync" translatable="false">backgroundSync</string> <string name="pref_key_debugging" translatable="false">debugging</string> <string name="pref_key_etags" translatable="false">eTags</string> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 94a85f592..56e96734a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -148,6 +148,8 @@ <string name="settings_etags">Use ETags</string> <string name="settings_etags_summary">Speeds up synchronization</string> <string name="settings_background_sync">Background synchronization</string> + <string name="settings_compress_image_attachments">Compress images on upload</string> + <string name="settings_compress_image_attachments_summary">Faster upload, less quality</string> <string name="pref_value_wifi_and_mobile">Sync on Wi-Fi and mobile data</string> <string name="pref_value_wifi_only">Sync only on Wi-Fi</string> <string-array name="darkmode_entries"> diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index a75c1630e..d3bb41252 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -16,6 +16,13 @@ android:key="@string/pref_key_background_sync" android:summary="%s" android:title="@string/settings_background_sync" /> + + <it.niedermann.nextcloud.deck.ui.branding.BrandedSwitchPreference + android:icon="@drawable/ic_baseline_photo_size_select_small_24" + android:key="@string/pref_key_compress_image_attachments" + android:title="@string/settings_compress_image_attachments" + android:summary="@string/settings_compress_image_attachments_summary" + app:defaultValue="true" /> </it.niedermann.nextcloud.deck.ui.branding.BrandedPreferenceCategory> <it.niedermann.nextcloud.deck.ui.branding.BrandedPreferenceCategory android:title="@string/simple_appearance"> |