diff --git a/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt b/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt index 3b1972ffbc..59629743b5 100644 --- a/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt @@ -20,7 +20,6 @@ package im.vector.riotx.features.attachments.preview import android.app.Activity.RESULT_CANCELED import android.app.Activity.RESULT_OK import android.content.Intent -import android.graphics.Color import android.os.Bundle import android.os.Parcelable import android.view.Menu @@ -38,7 +37,6 @@ import com.airbnb.mvrx.args import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState import com.yalantis.ucrop.UCrop -import com.yalantis.ucrop.UCropActivity import im.vector.matrix.android.api.extensions.orFalse import im.vector.matrix.android.api.session.content.ContentAttachmentData import im.vector.riotx.R @@ -52,6 +50,7 @@ import im.vector.riotx.core.utils.SnapOnScrollListener import im.vector.riotx.core.utils.allGranted import im.vector.riotx.core.utils.attachSnapHelperWithListener import im.vector.riotx.core.utils.checkPermissions +import im.vector.riotx.features.media.createUCropWithDefaultSettings import kotlinx.android.parcel.Parcelize import kotlinx.android.synthetic.main.fragment_attachments_preview.* import timber.log.Timber @@ -203,32 +202,7 @@ class AttachmentsPreviewFragment @Inject constructor( val currentAttachment = it.attachments.getOrNull(it.currentAttachmentIndex) ?: return@withState val destinationFile = File(requireContext().cacheDir, "${currentAttachment.name}_edited_image_${System.currentTimeMillis()}") val uri = currentAttachment.queryUri - UCrop.of(uri, destinationFile.toUri()) - .withOptions( - UCrop.Options() - .apply { - setAllowedGestures( - /* tabScale = */ UCropActivity.SCALE, - /* tabRotate = */ UCropActivity.ALL, - /* tabAspectRatio = */ UCropActivity.SCALE - ) - setToolbarTitle(currentAttachment.name) - // Disable freestyle crop, usability was not easy - // setFreeStyleCropEnabled(true) - // Color used for toolbar icon and text - setToolbarColor(colorProvider.getColorFromAttribute(R.attr.riotx_background)) - setToolbarWidgetColor(colorProvider.getColorFromAttribute(R.attr.vctr_toolbar_primary_text_color)) - // Background - setRootViewBackgroundColor(colorProvider.getColorFromAttribute(R.attr.riotx_background)) - // Status bar color (pb in dark mode, icon of the status bar are dark) - setStatusBarColor(colorProvider.getColorFromAttribute(R.attr.riotx_header_panel_background)) - // Known issue: there is still orange color used by the lib - // https://github.com/Yalantis/uCrop/issues/602 - setActiveControlsWidgetColor(colorProvider.getColor(R.color.riotx_accent)) - // Hide the logo (does not work) - setLogoColor(Color.TRANSPARENT) - } - ) + createUCropWithDefaultSettings(requireContext(), uri, destinationFile.toUri(), currentAttachment.name) .start(requireContext(), this) } diff --git a/vector/src/main/java/im/vector/riotx/features/media/BigImageViewerActivity.kt b/vector/src/main/java/im/vector/riotx/features/media/BigImageViewerActivity.kt index 10e752c8a7..8513d64da9 100644 --- a/vector/src/main/java/im/vector/riotx/features/media/BigImageViewerActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/media/BigImageViewerActivity.kt @@ -19,7 +19,6 @@ package im.vector.riotx.features.media import android.app.Activity import android.content.Context import android.content.Intent -import android.graphics.Color import android.net.Uri import android.os.Bundle import android.view.Menu @@ -27,7 +26,6 @@ import android.view.MenuItem import android.widget.Toast import androidx.core.net.toUri import com.yalantis.ucrop.UCrop -import com.yalantis.ucrop.UCropActivity import im.vector.riotx.R import im.vector.riotx.core.di.ActiveSessionHolder import im.vector.riotx.core.di.ScreenComponent @@ -115,32 +113,8 @@ class BigImageViewerActivity : VectorBaseActivity(), AvatarSelectorView.Callback private fun onRoomAvatarSelected(image: MultiPickerImageType) { val destinationFile = File(cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}") val uri = image.contentUri - UCrop.of(uri, destinationFile.toUri()) - .withOptions( - UCrop.Options() - .apply { - setAllowedGestures( - /* tabScale = */ UCropActivity.SCALE, - /* tabRotate = */ UCropActivity.ALL, - /* tabAspectRatio = */ UCropActivity.SCALE - ) - setToolbarTitle(image.displayName) - // Disable freestyle crop, usability was not easy - // setFreeStyleCropEnabled(true) - // Color used for toolbar icon and text - setToolbarColor(colorProvider.getColorFromAttribute(R.attr.riotx_background)) - setToolbarWidgetColor(colorProvider.getColorFromAttribute(R.attr.vctr_toolbar_primary_text_color)) - // Background - setRootViewBackgroundColor(colorProvider.getColorFromAttribute(R.attr.riotx_background)) - // Status bar color (pb in dark mode, icon of the status bar are dark) - setStatusBarColor(colorProvider.getColorFromAttribute(R.attr.riotx_header_panel_background)) - // Known issue: there is still orange color used by the lib - // https://github.com/Yalantis/uCrop/issues/602 - setActiveControlsWidgetColor(colorProvider.getColor(R.color.riotx_accent)) - // Hide the logo (does not work) - setLogoColor(Color.TRANSPARENT) - } - ) + createUCropWithDefaultSettings(this, uri, destinationFile.toUri(), image.displayName) + .apply { withAspectRatio(1f, 1f) } .start(this) } diff --git a/vector/src/main/java/im/vector/riotx/features/media/UCropHelper.kt b/vector/src/main/java/im/vector/riotx/features/media/UCropHelper.kt new file mode 100644 index 0000000000..5e3ca5f483 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/media/UCropHelper.kt @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotx.features.media + +import android.content.Context +import android.graphics.Color +import android.net.Uri +import androidx.core.content.ContextCompat +import com.yalantis.ucrop.UCrop +import com.yalantis.ucrop.UCropActivity +import im.vector.riotx.R +import im.vector.riotx.features.themes.ThemeUtils + +fun createUCropWithDefaultSettings(context: Context, source: Uri, destination: Uri, toolbarTitle: String?): UCrop { + return UCrop.of(source, destination) + .withOptions( + UCrop.Options() + .apply { + setAllowedGestures( + /* tabScale = */ UCropActivity.SCALE, + /* tabRotate = */ UCropActivity.ALL, + /* tabAspectRatio = */ UCropActivity.SCALE + ) + setToolbarTitle(toolbarTitle) + // Disable freestyle crop, usability was not easy + // setFreeStyleCropEnabled(true) + // Color used for toolbar icon and text + setToolbarColor(ThemeUtils.getColor(context, R.attr.riotx_background)) + setToolbarWidgetColor(ThemeUtils.getColor(context, R.attr.vctr_toolbar_primary_text_color)) + // Background + setRootViewBackgroundColor(ThemeUtils.getColor(context, R.attr.riotx_background)) + // Status bar color (pb in dark mode, icon of the status bar are dark) + setStatusBarColor(ThemeUtils.getColor(context, R.attr.riotx_header_panel_background)) + // Known issue: there is still orange color used by the lib + // https://github.com/Yalantis/uCrop/issues/602 + setActiveControlsWidgetColor(ContextCompat.getColor(context, R.color.riotx_accent)) + // Hide the logo (does not work) + setLogoColor(Color.TRANSPARENT) + } + ) +} diff --git a/vector/src/main/java/im/vector/riotx/features/roomprofile/RoomProfileFragment.kt b/vector/src/main/java/im/vector/riotx/features/roomprofile/RoomProfileFragment.kt index a1e9c2613b..2558c2bd32 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomprofile/RoomProfileFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomprofile/RoomProfileFragment.kt @@ -19,7 +19,6 @@ package im.vector.riotx.features.roomprofile import android.app.Activity import android.content.Intent -import android.graphics.Color import android.net.Uri import android.os.Bundle import android.os.Parcelable @@ -35,7 +34,6 @@ import com.airbnb.mvrx.args import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState import com.yalantis.ucrop.UCrop -import com.yalantis.ucrop.UCropActivity import im.vector.matrix.android.api.session.room.notification.RoomNotificationState import im.vector.matrix.android.api.util.MatrixItem import im.vector.matrix.android.api.util.toMatrixItem @@ -58,6 +56,7 @@ import im.vector.riotx.features.home.room.list.actions.RoomListQuickActionsBotto import im.vector.riotx.features.home.room.list.actions.RoomListQuickActionsSharedAction import im.vector.riotx.features.home.room.list.actions.RoomListQuickActionsSharedActionViewModel import im.vector.riotx.features.media.BigImageViewerActivity +import im.vector.riotx.features.media.createUCropWithDefaultSettings import im.vector.riotx.multipicker.MultiPicker import im.vector.riotx.multipicker.entity.MultiPickerImageType import kotlinx.android.parcel.Parcelize @@ -273,32 +272,8 @@ class RoomProfileFragment @Inject constructor( private fun onRoomAvatarSelected(image: MultiPickerImageType) { val destinationFile = File(requireContext().cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}") val uri = image.contentUri - UCrop.of(uri, destinationFile.toUri()) - .withOptions( - UCrop.Options() - .apply { - setAllowedGestures( - /* tabScale = */ UCropActivity.SCALE, - /* tabRotate = */ UCropActivity.ALL, - /* tabAspectRatio = */ UCropActivity.SCALE - ) - setToolbarTitle(image.displayName) - // Disable freestyle crop, usability was not easy - // setFreeStyleCropEnabled(true) - // Color used for toolbar icon and text - setToolbarColor(colorProvider.getColorFromAttribute(R.attr.riotx_background)) - setToolbarWidgetColor(colorProvider.getColorFromAttribute(R.attr.vctr_toolbar_primary_text_color)) - // Background - setRootViewBackgroundColor(colorProvider.getColorFromAttribute(R.attr.riotx_background)) - // Status bar color (pb in dark mode, icon of the status bar are dark) - setStatusBarColor(colorProvider.getColorFromAttribute(R.attr.riotx_header_panel_background)) - // Known issue: there is still orange color used by the lib - // https://github.com/Yalantis/uCrop/issues/602 - setActiveControlsWidgetColor(colorProvider.getColor(R.color.riotx_accent)) - // Hide the logo (does not work) - setLogoColor(Color.TRANSPARENT) - } - ) + createUCropWithDefaultSettings(requireContext(), uri, destinationFile.toUri(), image.displayName) + .apply { withAspectRatio(1f, 1f) } .start(requireContext(), this) } diff --git a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt index c344d7bf2f..69ca6f56ca 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt @@ -20,7 +20,6 @@ package im.vector.riotx.features.settings import android.app.Activity import android.content.Intent -import android.graphics.Color import android.net.Uri import android.text.Editable import android.util.Patterns @@ -41,7 +40,6 @@ import com.bumptech.glide.load.engine.cache.DiskCache import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputLayout import com.yalantis.ucrop.UCrop -import com.yalantis.ucrop.UCropActivity import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.NoOpMatrixCallback import im.vector.matrix.android.api.failure.isInvalidPassword @@ -65,6 +63,7 @@ import im.vector.riotx.core.utils.getSizeOfFiles import im.vector.riotx.core.utils.toast import im.vector.riotx.features.MainActivity import im.vector.riotx.features.MainActivityArgs +import im.vector.riotx.features.media.createUCropWithDefaultSettings import im.vector.riotx.features.roomprofile.AvatarSelectorView import im.vector.riotx.features.themes.ThemeUtils import im.vector.riotx.features.workers.signout.SignOutUiWorker @@ -429,32 +428,8 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment(), AvatarSelect private fun onAvatarSelected(image: MultiPickerImageType) { val destinationFile = File(requireContext().cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}") val uri = image.contentUri - UCrop.of(uri, destinationFile.toUri()) - .withOptions( - UCrop.Options() - .apply { - setAllowedGestures( - /* tabScale = */ UCropActivity.SCALE, - /* tabRotate = */ UCropActivity.ALL, - /* tabAspectRatio = */ UCropActivity.SCALE - ) - setToolbarTitle(image.displayName) - // Disable freestyle crop, usability was not easy - // setFreeStyleCropEnabled(true) - // Color used for toolbar icon and text - setToolbarColor(ThemeUtils.getColor(requireContext(), R.attr.riotx_background)) - setToolbarWidgetColor(ThemeUtils.getColor(requireContext(), R.attr.vctr_toolbar_primary_text_color)) - // Background - setRootViewBackgroundColor(ThemeUtils.getColor(requireContext(), R.attr.riotx_background)) - // Status bar color (pb in dark mode, icon of the status bar are dark) - setStatusBarColor(ThemeUtils.getColor(requireContext(), R.attr.riotx_header_panel_background)) - // Known issue: there is still orange color used by the lib - // https://github.com/Yalantis/uCrop/issues/602 - setActiveControlsWidgetColor(ContextCompat.getColor(requireContext(), R.color.riotx_accent)) - // Hide the logo (does not work) - setLogoColor(Color.TRANSPARENT) - } - ) + createUCropWithDefaultSettings(requireContext(), uri, destinationFile.toUri(), image.displayName) + .apply { withAspectRatio(1f, 1f) } .start(requireContext(), this) }