Skip to content

Commit

Permalink
Refactor usage of Html.fromHtml to handle deprecation (#6074)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalyadav1 authored Dec 24, 2024
1 parent a9058d1 commit e7864ac
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -167,7 +168,11 @@ public View onCreateView(
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) {
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/fr/free/nrw/commons/feedback/FeedbackDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -511,7 +512,12 @@ public boolean onZoom(ZoomEvent event) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e7864ac

Please sign in to comment.