Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ ownCloud admins and users.

https://github.com/owncloud/android/issues/4607
https://github.com/owncloud/android/issues/4688
https://github.com/owncloud/android/issues/4695
https://github.com/owncloud/android/pull/4687
https://github.com/owncloud/android/pull/4694
https://github.com/owncloud/android/pull/4713

* Enhancement - Disable/Remove a space: [#4611](https://github.com/owncloud/android/issues/4611)

Expand Down
2 changes: 2 additions & 0 deletions changelog/unreleased/4687
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ with the required permissions when the three-dot menu button is tapped.

https://github.com/owncloud/android/issues/4607
https://github.com/owncloud/android/issues/4688
https://github.com/owncloud/android/issues/4695
https://github.com/owncloud/android/pull/4687
https://github.com/owncloud/android/pull/4694
https://github.com/owncloud/android/pull/4713
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CreateSpaceDialogFragment : DialogFragment() {
updateUI()
createSpaceDialogQuotaNoRestrictionLabel.isVisible = !isChecked
createSpaceDialogQuotaLayout.isVisible = isChecked
createSpaceDialogQuotaGbLabel.isVisible = isChecked
createSpaceDialogQuotaUnitLabel.isVisible = isChecked
}

if (isEditMode) {
Expand All @@ -78,7 +78,9 @@ class CreateSpaceDialogFragment : DialogFragment() {
val totalQuota = it.quota?.total ?: 0L
if (totalQuota != 0L) {
createSpaceDialogQuotaSwitch.isChecked = true
createSpaceDialogQuotaValue.setText(DisplayUtils.formatFromBytesToGb(totalQuota))
val formattedQuota = DisplayUtils.formatFromBytes(totalQuota)
createSpaceDialogQuotaValue.setText(formattedQuota.first)
createSpaceDialogQuotaUnitLabel.text = formattedQuota.second
}
}

Expand All @@ -91,7 +93,8 @@ class CreateSpaceDialogFragment : DialogFragment() {
createSpaceButton.setOnClickListener {
val spaceName = createSpaceDialogNameValue.text.toString()
val spaceSubtitle = createSpaceDialogSubtitleValue.text.toString()
val spaceQuota = if (createSpaceDialogQuotaSwitch.isChecked) convertToBytes(createSpaceDialogQuotaValue.text.toString()) else 0L
val spaceQuota = if (createSpaceDialogQuotaSwitch.isChecked) convertToBytes(createSpaceDialogQuotaValue.text.toString(),
createSpaceDialogQuotaUnitLabel.text.toString()) else 0L

if (isEditMode) {
currentSpace?.let {
Expand Down Expand Up @@ -127,15 +130,16 @@ class CreateSpaceDialogFragment : DialogFragment() {

return when {
spaceQuota.isEmpty() -> getString(R.string.create_space_dialog_quota_empty_error)
spaceQuota.toDouble() == MIN_SPACE_QUOTA_GB -> getString(R.string.create_space_dialog_quota_zero_error)
spaceQuota.toDouble() > MAX_SPACE_QUOTA_GB -> getString(R.string.create_space_dialog_quota_too_large_error)
spaceQuota.toDouble() == MIN_SPACE_QUOTA_LIMIT -> getString(R.string.create_space_dialog_quota_zero_error)
spaceQuota.toDouble() > MAX_SPACE_QUOTA_LIMIT -> getString(R.string.create_space_dialog_quota_too_large_error)
else -> null
}
}

private fun updateUI() {
val nameError = validateName(binding.createSpaceDialogNameValue.text.toString())
val quotaError = validateQuota(binding.createSpaceDialogQuotaValue.text.toString())
val quotaValue = convertToBytes(binding.createSpaceDialogQuotaValue.text.toString(), binding.createSpaceDialogQuotaUnitLabel.text.toString())
val quotaError = validateQuota(quotaValue.toString())
val noErrors = nameError == null && quotaError == null

val colorButton = if (noErrors) {
Expand All @@ -152,9 +156,18 @@ class CreateSpaceDialogFragment : DialogFragment() {
}
}

private fun convertToBytes(spaceQuota: String): Long {
val quotaNumber = spaceQuota.toDoubleOrNull() ?: return 0L
return (quotaNumber * 1_000_000_000L).toLong()
private fun convertToBytes(spaceQuotaValue: String, spaceQuotaUnit: String): Long {
val quotaNumber = spaceQuotaValue.toDoubleOrNull() ?: return 0L
val multiplier = when (spaceQuotaUnit) {
DisplayUtils.sizeSuffixes[0] -> B_MULTIPLIER
DisplayUtils.sizeSuffixes[1] -> KB_MULTIPLIER
DisplayUtils.sizeSuffixes[2] -> MB_MULTIPLIER
DisplayUtils.sizeSuffixes[3] -> GB_MULTIPLIER
DisplayUtils.sizeSuffixes[4] -> TB_MULTIPLIER
DisplayUtils.sizeSuffixes[5] -> PB_MULTIPLIER
else -> B_MULTIPLIER
}
return (quotaNumber * multiplier).toLong()
}

interface CreateSpaceListener {
Expand All @@ -167,8 +180,14 @@ class CreateSpaceDialogFragment : DialogFragment() {
private const val ARG_CAN_EDIT_SPACE_QUOTA = "CAN_EDIT_SPACE_QUOTA"
private const val ARG_CURRENT_SPACE = "CURRENT_SPACE"
private const val FORBIDDEN_CHARACTERS = """[/\\.:?*"'><|]"""
private const val MIN_SPACE_QUOTA_GB = 0.0
private const val MAX_SPACE_QUOTA_GB = 1_000_000.0
private const val MIN_SPACE_QUOTA_LIMIT = 0.0
private const val MAX_SPACE_QUOTA_LIMIT = 1_000_000_000_000_000.0
private const val B_MULTIPLIER = 1L
private const val KB_MULTIPLIER = 1_000L
private const val MB_MULTIPLIER = 1_000_000L
private const val GB_MULTIPLIER = 1_000_000_000L
private const val TB_MULTIPLIER = 1_000_000_000_000L
private const val PB_MULTIPLIER = 1_000_000_000_000_000L

fun newInstance(
isEditMode: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
* @author Bartek Przybylski
* @author David A. Velasco
* @author David González Verdugo
* @author Jorge Aguado Recio
*
* Copyright (C) 2011 Bartek Przybylski
* Copyright (C) 2020 ownCloud GmbH.
* Copyright (C) 2025 ownCloud GmbH.
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
Expand All @@ -27,6 +29,7 @@
import android.content.res.Resources;
import android.text.format.DateUtils;
import android.util.DisplayMetrics;
import android.util.Pair;

import androidx.core.content.ContextCompat;
import com.google.android.material.snackbar.Snackbar;
Expand All @@ -35,6 +38,8 @@

import java.io.File;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.net.IDN;
import java.text.DateFormat;
import java.util.Calendar;
Expand All @@ -49,7 +54,7 @@ public class DisplayUtils {

private static final String OWNCLOUD_APP_NAME = "ownCloud";

private static final String[] sizeSuffixes = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
public static final String[] sizeSuffixes = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
private static final int[] sizeScales = {0, 0, 1, 1, 1, 2, 2, 2, 2};

private static Map<String, String> mimeType2HumanReadable;
Expand Down Expand Up @@ -256,8 +261,22 @@ public static int getDrawerHeaderHeight(int displayCutout, Resources resources)
}
}

public static String formatFromBytesToGb(long bytes) {
BigDecimal valueInGB = new BigDecimal(bytes).divide(BigDecimal.valueOf(1_000_000_000L)).stripTrailingZeros();
return valueInGB.toPlainString();
public static Pair<String, String> formatFromBytes(long bytes) {
BigDecimal value = new BigDecimal(bytes);
BigDecimal baseUnit = new BigDecimal(1000L);
int unitIndex = 0;

while (value.compareTo(baseUnit) >= 0 && unitIndex < sizeSuffixes.length - 1) {
value = value.divide(baseUnit);
unitIndex++;
}

if (value.compareTo(BigDecimal.ONE) >= 0) {
value = value.setScale(1, RoundingMode.HALF_UP);
} else {
value = value.round(new MathContext(1, RoundingMode.HALF_UP));
}

return new Pair<>(value.stripTrailingZeros().toPlainString(), sizeSuffixes[unitIndex]);
}
}
2 changes: 1 addition & 1 deletion owncloudApp/src/main/res/layout/create_space_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
</com.google.android.material.textfield.TextInputLayout>

<TextView
android:id="@+id/create_space_dialog_quota_gb_label"
android:id="@+id/create_space_dialog_quota_unit_label"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/standard_half_margin"
Expand Down
Loading