-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve NewPipe's share on some devices + fix crash when no browser i…
…s set on some devices Catching ActivityNotFoundException when trying to open the default browser Use an ACTION_CHOOSER intent and put as an extra intent the intent to open an URI / share an URI when no default app is set. Add a LinkHelper class which set a custom action when clicking web links in the description of a content. This class also helps to implement a confirmation dialog when trying to open web links in an external app.
- Loading branch information
Showing
13 changed files
with
246 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.schabi.newpipe.util; | ||
|
||
import android.content.Context; | ||
import android.text.SpannableStringBuilder; | ||
import android.text.method.LinkMovementMethod; | ||
import android.text.style.ClickableSpan; | ||
import android.text.style.URLSpan; | ||
import android.text.util.Linkify; | ||
import android.view.View; | ||
import android.widget.TextView; | ||
|
||
import androidx.core.text.HtmlCompat; | ||
|
||
import io.noties.markwon.Markwon; | ||
import io.noties.markwon.linkify.LinkifyPlugin; | ||
|
||
public final class LinkHelper { | ||
private LinkHelper() { | ||
} | ||
|
||
public static SpannableStringBuilder createLinksFromHtmlBlock(final Context context, | ||
final String htmlBlock, | ||
final TextView textView, | ||
final int fromHtmlOption) { | ||
return changeIntentOfDescriptionLinks(context, HtmlCompat.fromHtml(htmlBlock, | ||
fromHtmlOption), textView); | ||
} | ||
|
||
public static void createLinksFromPlainText(final Context context, | ||
final String plainTextBlock, | ||
final TextView textView) { | ||
textView.setAutoLinkMask(Linkify.WEB_URLS); | ||
textView.setText(plainTextBlock, TextView.BufferType.SPANNABLE); | ||
changeIntentOfDescriptionLinks(context, textView.getText(), textView); | ||
} | ||
|
||
public static void createLinksFromMarkdownText(final Context context, | ||
final String markdownBlock, | ||
final TextView textView) { | ||
final Markwon markwon = Markwon.builder(context).usePlugin(LinkifyPlugin.create()).build(); | ||
markwon.setMarkdown(textView, markdownBlock); | ||
|
||
changeIntentOfDescriptionLinks(context, textView.getText(), textView); | ||
} | ||
|
||
private static SpannableStringBuilder changeIntentOfDescriptionLinks(final Context context, | ||
final CharSequence chars, | ||
final TextView textView) { | ||
final SpannableStringBuilder textBlockLinked = new SpannableStringBuilder(chars); | ||
final URLSpan[] urls = textBlockLinked.getSpans(0, chars.length(), URLSpan.class); | ||
|
||
for (final URLSpan span : urls) { | ||
final ClickableSpan clickableSpan = new ClickableSpan() { | ||
public void onClick(final View view) { | ||
ShareUtils.openUrlInBrowser(context, span.getURL(), false); | ||
} | ||
}; | ||
textBlockLinked.setSpan(clickableSpan, textBlockLinked.getSpanStart(span), | ||
textBlockLinked.getSpanEnd(span), textBlockLinked.getSpanFlags(span)); | ||
textBlockLinked.removeSpan(span); | ||
} | ||
|
||
textView.setText(textBlockLinked); | ||
textView.setMovementMethod(LinkMovementMethod.getInstance()); | ||
|
||
return textBlockLinked; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.