Move license utilities into their own file

This commit is contained in:
Paul Hawke 2025-07-02 16:02:55 -05:00
parent a45edbd1da
commit 544e43a7ef
5 changed files with 61 additions and 53 deletions

View file

@ -21,6 +21,7 @@ import fr.free.nrw.commons.databinding.FragmentMediaLicenseBinding
import fr.free.nrw.commons.upload.UploadActivity import fr.free.nrw.commons.upload.UploadActivity
import fr.free.nrw.commons.upload.UploadBaseFragment import fr.free.nrw.commons.upload.UploadBaseFragment
import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog
import fr.free.nrw.commons.utils.Licenses
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@ -126,20 +127,20 @@ class MediaLicenseFragment : UploadBaseFragment(), MediaLicenseContract.View {
} }
override fun setSelectedLicense(license: String?) { override fun setSelectedLicense(license: String?) {
var position = licenses!!.indexOf(getString(Utils.licenseNameFor(license))) var position = licenses!!.indexOf(getString(Licenses.licenseNameFor(license)))
// Check if position is valid // Check if position is valid
if (position < 0) { if (position < 0) {
Timber.d("Invalid position: %d. Using default licenses", position) Timber.d("Invalid position: %d. Using default licenses", position)
position = licenses!!.size - 1 position = licenses!!.size - 1
} else { } else {
Timber.d("Position: %d %s", position, getString(Utils.licenseNameFor(license))) Timber.d("Position: %d %s", position, getString(Licenses.licenseNameFor(license)))
} }
binding.spinnerLicenseList.setSelection(position) binding.spinnerLicenseList.setSelection(position)
} }
override fun updateLicenseSummary(selectedLicense: String?, numberOfItems: Int) { override fun updateLicenseSummary(selectedLicense: String?, numberOfItems: Int) {
val licenseHyperLink = "<a href='" + Utils.licenseUrlFor(selectedLicense) + "'>" + val licenseHyperLink = "<a href='" + Licenses.licenseUrlFor(selectedLicense) + "'>" +
getString(Utils.licenseNameFor(selectedLicense)) + "</a><br>" getString(Licenses.licenseNameFor(selectedLicense)) + "</a><br>"
setTextViewHTML( setTextViewHTML(
binding.tvShareLicenseSummary, resources binding.tvShareLicenseSummary, resources

View file

@ -1,9 +1,9 @@
package fr.free.nrw.commons.upload.license package fr.free.nrw.commons.upload.license
import fr.free.nrw.commons.utils.Utils
import fr.free.nrw.commons.kvstore.JsonKvStore import fr.free.nrw.commons.kvstore.JsonKvStore
import fr.free.nrw.commons.repository.UploadRepository import fr.free.nrw.commons.repository.UploadRepository
import fr.free.nrw.commons.settings.Prefs import fr.free.nrw.commons.settings.Prefs
import fr.free.nrw.commons.utils.Licenses
import timber.log.Timber import timber.log.Timber
import java.lang.reflect.Method import java.lang.reflect.Method
import java.lang.reflect.Proxy import java.lang.reflect.Proxy
@ -39,7 +39,7 @@ class MediaLicensePresenter @Inject constructor(
Prefs.Licenses.CC_BY_SA_4 Prefs.Licenses.CC_BY_SA_4
) //CC_BY_SA_4 is the default one used by the commons web app ) //CC_BY_SA_4 is the default one used by the commons web app
try { //I have to make sure that the stored default license was not one of the deprecated one's try { //I have to make sure that the stored default license was not one of the deprecated one's
Utils.licenseNameFor(selectedLicense) Licenses.licenseNameFor(selectedLicense)
} catch (exception: IllegalStateException) { } catch (exception: IllegalStateException) {
Timber.e(exception) Timber.e(exception)
selectedLicense = Prefs.Licenses.CC_BY_SA_4 selectedLicense = Prefs.Licenses.CC_BY_SA_4

View file

@ -0,0 +1,52 @@
package fr.free.nrw.commons.utils;
import androidx.annotation.NonNull;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.settings.Prefs;
public final class Licenses {
/**
* Generates licence name with given ID
* @param license License ID
* @return Name of license
*/
public static int licenseNameFor(String license) {
switch (license) {
case Prefs.Licenses.CC_BY_3:
return R.string.license_name_cc_by;
case Prefs.Licenses.CC_BY_4:
return R.string.license_name_cc_by_four;
case Prefs.Licenses.CC_BY_SA_3:
return R.string.license_name_cc_by_sa;
case Prefs.Licenses.CC_BY_SA_4:
return R.string.license_name_cc_by_sa_four;
case Prefs.Licenses.CC0:
return R.string.license_name_cc0;
}
throw new IllegalStateException("Unrecognized license value: " + license);
}
/**
* Generates license url with given ID
* @param license License ID
* @return Url of license
*/
@NonNull
public static String licenseUrlFor(String license) {
switch (license) {
case Prefs.Licenses.CC_BY_3:
return "https://creativecommons.org/licenses/by/3.0/";
case Prefs.Licenses.CC_BY_4:
return "https://creativecommons.org/licenses/by/4.0/";
case Prefs.Licenses.CC_BY_SA_3:
return "https://creativecommons.org/licenses/by-sa/3.0/";
case Prefs.Licenses.CC_BY_SA_4:
return "https://creativecommons.org/licenses/by-sa/4.0/";
case Prefs.Licenses.CC0:
return "https://creativecommons.org/publicdomain/zero/1.0/";
default:
throw new IllegalStateException("Unrecognized license value: " + license);
}
}
}

View file

@ -11,7 +11,6 @@ import android.text.style.UnderlineSpan;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.browser.customtabs.CustomTabColorSchemeParams; import androidx.browser.customtabs.CustomTabColorSchemeParams;
import androidx.browser.customtabs.CustomTabsIntent; import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -25,55 +24,10 @@ import java.util.Locale;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.settings.Prefs;
import timber.log.Timber; import timber.log.Timber;
public class Utils { public class Utils {
/**
* Generates licence name with given ID
* @param license License ID
* @return Name of license
*/
public static int licenseNameFor(String license) {
switch (license) {
case Prefs.Licenses.CC_BY_3:
return R.string.license_name_cc_by;
case Prefs.Licenses.CC_BY_4:
return R.string.license_name_cc_by_four;
case Prefs.Licenses.CC_BY_SA_3:
return R.string.license_name_cc_by_sa;
case Prefs.Licenses.CC_BY_SA_4:
return R.string.license_name_cc_by_sa_four;
case Prefs.Licenses.CC0:
return R.string.license_name_cc0;
}
throw new IllegalStateException("Unrecognized license value: " + license);
}
/**
* Generates license url with given ID
* @param license License ID
* @return Url of license
*/
@NonNull
public static String licenseUrlFor(String license) {
switch (license) {
case Prefs.Licenses.CC_BY_3:
return "https://creativecommons.org/licenses/by/3.0/";
case Prefs.Licenses.CC_BY_4:
return "https://creativecommons.org/licenses/by/4.0/";
case Prefs.Licenses.CC_BY_SA_3:
return "https://creativecommons.org/licenses/by-sa/3.0/";
case Prefs.Licenses.CC_BY_SA_4:
return "https://creativecommons.org/licenses/by-sa/4.0/";
case Prefs.Licenses.CC0:
return "https://creativecommons.org/publicdomain/zero/1.0/";
default:
throw new IllegalStateException("Unrecognized license value: " + license);
}
}
/** /**
* Adds extension to filename. Converts to .jpg if system provides .jpeg, adds .jpg if no extension detected * Adds extension to filename. Converts to .jpg if system provides .jpeg, adds .jpg if no extension detected
* @param title File name * @param title File name

View file

@ -6,6 +6,7 @@ import fr.free.nrw.commons.kvstore.JsonKvStore
import fr.free.nrw.commons.repository.UploadRepository import fr.free.nrw.commons.repository.UploadRepository
import fr.free.nrw.commons.upload.license.MediaLicenseContract import fr.free.nrw.commons.upload.license.MediaLicenseContract
import fr.free.nrw.commons.upload.license.MediaLicensePresenter import fr.free.nrw.commons.upload.license.MediaLicensePresenter
import fr.free.nrw.commons.utils.Licenses
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -50,7 +51,7 @@ class MediaLicensePresenterTest {
MockitoAnnotations.openMocks(this) MockitoAnnotations.openMocks(this)
mediaLicensePresenter.onAttachView(view) mediaLicensePresenter.onAttachView(view)
mockedUtil = Mockito.mockStatic(Utils::class.java) mockedUtil = Mockito.mockStatic(Utils::class.java)
`when`(Utils.licenseNameFor(ArgumentMatchers.anyString())).thenReturn(1) `when`(Licenses.licenseNameFor(ArgumentMatchers.anyString())).thenReturn(1)
} }
@After @After