Fix issue #2164 Links open with handleWebUrl. (#2220)

* AboutActivity links now open with the in-app tabbed browser.
* UploadActivity license and policy links now open with the in-app tabbed browser.
This commit is contained in:
DylanRobson 2019-01-01 02:51:33 -08:00 committed by Vivek Maskara
parent af291a5fcc
commit 55113b3018
2 changed files with 55 additions and 7 deletions

View file

@ -188,6 +188,7 @@ public class Utils {
}
/**
* Opens Custom Tab Activity with in-app browser for the specified URL.
* Launches intent for web URL
* @param context
* @param url
@ -206,7 +207,8 @@ public class Utils {
builder.setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.primaryDarkColor));
builder.setExitAnimations(context, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Clear previous browser tasks, so that back/exit buttons work as intended.
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
customTabsIntent.launchUrl(context, url);
}

View file

@ -15,8 +15,11 @@ import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
@ -143,6 +146,7 @@ public class UploadActivity extends AuthenticatedActivity implements UploadView,
configureNavigationButtons();
configureCategories();
configureLicenses();
configurePolicy();
presenter.init();
@ -257,11 +261,8 @@ public class UploadActivity extends AuthenticatedActivity implements UploadView,
public void updateLicenseSummary(String selectedLicense, int imageCount) {
String licenseHyperLink = "<a href='" + Utils.licenseUrlFor(selectedLicense) + "'>" +
getString(Utils.licenseNameFor(selectedLicense)) + "</a><br>";
licenseSummary.setMovementMethod(LinkMovementMethod.getInstance());
licenseSummary.setText(
Html.fromHtml(
getResources().getQuantityString(R.plurals.share_license_summary,
imageCount, licenseHyperLink)));
setTextViewHTML(licenseSummary, getResources().getQuantityString(R.plurals.share_license_summary, imageCount, licenseHyperLink));
}
@Override
@ -437,6 +438,47 @@ public class UploadActivity extends AuthenticatedActivity implements UploadView,
finish();
}
/**
* Parses links from HTML string, and makes the links clickable in the specified TextView.<br>
* Uses {@link #makeLinkClickable(SpannableStringBuilder, URLSpan)}.
* @see <a href="https://stackoverflow.com/questions/12418279/android-textview-with-clickable-links-how-to-capture-clicks">Source</a>
*/
private void setTextViewHTML(TextView text, String html)
{
CharSequence sequence = Html.fromHtml(html);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for (URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
text.setText(strBuilder);
text.setMovementMethod(LinkMovementMethod.getInstance());
}
/**
* Sets onClick handler to launch browser for the specified URLSpan.
* @see <a href="https://stackoverflow.com/questions/12418279/android-textview-with-clickable-links-how-to-capture-clicks">Source</a>
*/
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
{
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
// Handle hyperlink click
String hyperLink = span.getURL();
launchBrowser(hyperLink);
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
private void launchBrowser(String hyperLink) {
Utils.handleWebUrl(this, Uri.parse(hyperLink));
}
private void configureLicenses() {
licenseSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
@ -515,6 +557,10 @@ public class UploadActivity extends AuthenticatedActivity implements UploadView,
categoriesList.setAdapter(categoriesAdapter);
}
private void configurePolicy() {
setTextViewHTML(licensePolicy, getString(R.string.media_upload_policy));
}
@SuppressLint("CheckResult")
private void updateCategoryList(String filter) {
List<String> imageTitleList = presenter.getImageTitleList();