Skip to content

Commit

Permalink
Add general about text
Browse files Browse the repository at this point in the history
  • Loading branch information
MrApplejuice committed Feb 1, 2022
1 parent fd05989 commit 3077394
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ task generateAboutStrings {
xml.item(item["link"])
}
}
xml.string(name:"autostring_about_license_title", "License under ${licenseTitle}")
xml.string(name:"autostring_about_license_title", "Licensed under ${licenseTitle}")
xml.string(name:"autostring_about_license_text", licenseText)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.media.Image;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
Expand All @@ -19,7 +20,11 @@

import com.squareup.phrase.Phrase;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
Expand All @@ -42,16 +47,92 @@ public IconData(String icons, String title, String link) {
}
}

private class ClickableLinkSpan extends ClickableSpan {
private String url;

public ClickableLinkSpan(String url) {
this.url = url;
}

@Override
public void onClick(@NonNull View view) {
showUrlInBrowser(url);
}

@Override
public void updateDrawState(@NonNull TextPaint ds) {
ds.setUnderlineText(true);
ds.setColor(Color.BLUE);
}
}

private AboutFragmentBinding binding = null;

private int dpToPx(float dp) {
return (int) (getContext().getResources().getDisplayMetrics().density * dp + 0.5f);
}

private class Range {
public int start, end;
public Range(int start, int end) {
this.start = start;
this.end = end;
}
}

private static Pattern HREF_DETECTOR_PATTERN = Pattern.compile("<a .*href=[\"']([^\"]+)[\"'][^>]*>([^<]*)</a>");

private void filterLinksFromTextFields(ViewGroup root) {
for (int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
if (!(v instanceof TextView)) {
continue;
}

TextView tv = (TextView) v;

String orgText = tv.getText().toString();
System.out.println("orgText: " + orgText);
Matcher matcher = HREF_DETECTOR_PATTERN.matcher(orgText);

SpannableStringBuilder builder = null;
int prevMatchEnd = 0;
while (matcher.find()) {
if (builder == null) {
builder = new SpannableStringBuilder();
}

builder.append(orgText.substring(prevMatchEnd, matcher.start()));

String linkUrl = matcher.group(1);
String linkText = matcher.group(2);
int startSpanIndex = builder.length();
builder.append(linkText);
builder.setSpan(
new ClickableLinkSpan(linkUrl),
startSpanIndex,
builder.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);

prevMatchEnd = matcher.end();
}

if (builder != null) {
builder.append(orgText.substring(prevMatchEnd));

tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(builder);
}
}
}

@Override
public View onCreateView(
@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
AboutFragmentBinding binding = AboutFragmentBinding.inflate(inflater);
binding = AboutFragmentBinding.inflate(inflater);

String[] aboutIconLists = getResources().getStringArray(R.array.autostring_about_icon_iconlists);
String[] aboutIconTitles = getResources().getStringArray(R.array.autostring_about_icon_titles);
Expand Down Expand Up @@ -124,6 +205,8 @@ public void updateDrawState(@NonNull TextPaint ds) {
binding.root.addView(group);
}

filterLinksFromTextFields(binding.root);

return binding.root;
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/layout/about_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
android:padding="8dp"
tools:context=".AboutFragment">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/about_text" />

<TextView
android:id="@+id/aboutLicenseTitle"
android:layout_width="match_parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/about_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
<item>https://www.flaticon.com/free-icons/poop</item>
<item>https://www.flaticon.com/free-icons/humidity</item>
</string-array>
<string name='autostring_about_license_title'>License under The MIT License (MIT)</string>
<string name='autostring_about_license_title'>Licensed under The MIT License (MIT)</string>
<string name='autostring_about_license_text'>Copyright © 2022 Paul Konstantin Gerke (and contributors)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

<string name="app_name">Baby Buddy</string>

<string name="about_text">This Android application is Open Source software licensed under the MIT License (see below).\n\nThe source code of the application is available at &lt;a href=\'https://github.com/MrApplejuice/BabyBuddyAndroid/\'&gt;https://github.com/MrApplejuice/BabyBuddyAndroid/&lt;/a&gt;.\n\nThe application uses third-party media under attribution licenes listed below.</string>

<!-- Strings used for fragments for navigation -->
<string name="action_settings">Settings</string>
<string name="first_fragment_label">Login to Baby Buddy</string>
Expand Down

0 comments on commit 3077394

Please sign in to comment.