Refactor usage of Html.fromHtml to handle deprecation (#6074)

This commit is contained in:
Sonal Yadav 2024-12-24 21:14:25 +05:30 committed by GitHub
parent a9058d129e
commit e7864ac1dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 33 additions and 9 deletions

View file

@ -17,6 +17,7 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
@ -167,7 +168,11 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setSearchThisAreaButtonVisibility(false);
binding.tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
binding.tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution), Html.FROM_HTML_MODE_LEGACY));
} else {
binding.tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution)));
}
initNetworkBroadCastReceiver();
locationPermissionsHelper = new LocationPermissionsHelper(getActivity(),locationManager,this);
if (presenter == null) {

View file

@ -2,6 +2,7 @@ package fr.free.nrw.commons.feedback
import android.app.Dialog
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.text.Spanned
@ -26,11 +27,13 @@ class FeedbackDialog(
private val onFeedbackSubmitCallback: OnFeedbackSubmitCallback) : Dialog(context) {
private var _binding: DialogFeedbackBinding? = null
private val binding get() = _binding!!
// TODO("Remove Deprecation") Issue : #6002
// 'fromHtml(String!): Spanned!' is deprecated. Deprecated in Java
@Suppress("DEPRECATION")
private var feedbackDestinationHtml: Spanned = Html.fromHtml(
context.getString(R.string.feedback_destination_note))
// Refactored to handle deprecation for Html.fromHtml()
private var feedbackDestinationHtml: Spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(context.getString(R.string.feedback_destination_note), Html.FROM_HTML_MODE_LEGACY)
} else {
@Suppress("DEPRECATION")
Html.fromHtml(context.getString(R.string.feedback_destination_note))
}
public override fun onCreate(savedInstanceState: Bundle?) {

View file

@ -23,6 +23,7 @@ import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
@ -511,7 +512,12 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
moveCameraToPosition(lastMapFocus);
initRvNearbyList();
onResume();
binding.tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
binding.tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution), Html.FROM_HTML_MODE_LEGACY));
} else {
//noinspection deprecation
binding.tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution)));
}
binding.tvAttribution.setMovementMethod(LinkMovementMethod.getInstance());
binding.nearbyFilterList.btnAdvancedOptions.setOnClickListener(v -> {
binding.nearbyFilter.searchViewLayout.searchView.clearFocus();

View file

@ -61,7 +61,12 @@ class ReviewImageFragment : CommonsDaggerSupportFragment() {
R.string.review_category_explanation,
formattedCatString
)
return Html.fromHtml(stringToConvertHtml).toString()
val formattedString = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
Html.fromHtml(stringToConvertHtml, Html.FROM_HTML_MODE_LEGACY).toString()
} else {
Html.fromHtml(stringToConvertHtml).toString()
}
return formattedString
}
}
return getString(R.string.review_no_category)

View file

@ -2,6 +2,7 @@ package fr.free.nrw.commons.upload.license
import android.app.Activity
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.text.SpannableStringBuilder
@ -151,7 +152,11 @@ class MediaLicenseFragment : UploadBaseFragment(), MediaLicenseContract.View {
}
private fun setTextViewHTML(textView: TextView, text: String) {
val sequence: CharSequence = Html.fromHtml(text)
val sequence: CharSequence = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY)
} else {
Html.fromHtml(text)
}
val strBuilder = SpannableStringBuilder(sequence)
val urls = strBuilder.getSpans(
0, sequence.length,