From fd05989c0cd1b859dd9222170d51fbb68fae1a54 Mon Sep 17 00:00:00 2001 From: Paul Konstantin Gerke Date: Tue, 1 Feb 2022 16:52:10 +0100 Subject: [PATCH] Add license text to about screen --- app/build.gradle | 11 ++- .../babybuddywidgets/AboutFragment.java | 69 ++++++++++++++++++- .../babybuddywidgets/BaseFragment.java | 6 ++ app/src/main/res/layout/about_fragment.xml | 24 ++++++- app/src/main/res/values/about_strings.xml | 2 + 5 files changed, 106 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f00b764..d54f517 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,8 +5,8 @@ plugins { } task generateAboutStrings { - String fileContents = new File("ATTRIBUTIONS.md").text - String[] sections = fileContents.replaceAll("\n", " ").split("##") + String attributionsText = new File("ATTRIBUTIONS.md").text + String[] sections = attributionsText.replaceAll("\n", " ").split("##") def outList = [] @@ -31,6 +31,11 @@ task generateAboutStrings { outList.add(["icons": tagsList, "title": aHrefTitle, "link": aHrefAddress]) } + String licenseText = new File("LICENSE.md").text.trim() + String[] splits = licenseText.split("\n", 2) + String licenseTitle = splits[0].replace("#", "").trim() + licenseText = splits[1].trim().replaceAll("\n\n", "\\\\\\n\\\\\\n").replaceAll("\n", " ") + def xmlout = new StringWriter() def xml = new groovy.xml.MarkupBuilder(xmlout) xml.resources { @@ -49,6 +54,8 @@ task generateAboutStrings { xml.item(item["link"]) } } + xml.string(name:"autostring_about_license_title", "License under ${licenseTitle}") + xml.string(name:"autostring_about_license_text", licenseText) } FileWriter about_strings_file_writer = new FileWriter("app/src/main/res/values/about_strings.xml") diff --git a/app/src/main/java/eu/pkgsoftware/babybuddywidgets/AboutFragment.java b/app/src/main/java/eu/pkgsoftware/babybuddywidgets/AboutFragment.java index 961bcdb..bf713f3 100644 --- a/app/src/main/java/eu/pkgsoftware/babybuddywidgets/AboutFragment.java +++ b/app/src/main/java/eu/pkgsoftware/babybuddywidgets/AboutFragment.java @@ -1,15 +1,29 @@ package eu.pkgsoftware.babybuddywidgets; +import android.graphics.Color; import android.graphics.drawable.Icon; +import android.media.Image; import android.os.Bundle; +import android.text.SpannableString; +import android.text.Spanned; +import android.text.TextPaint; +import android.text.method.LinkMovementMethod; +import android.text.style.ClickableSpan; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import com.squareup.phrase.Phrase; + import java.util.ArrayList; +import androidx.core.content.ContextCompat; +import androidx.core.content.res.ResourcesCompat; +import androidx.core.widget.TextViewCompat; import androidx.fragment.app.Fragment; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -28,6 +42,10 @@ public IconData(String icons, String title, String link) { } } + private int dpToPx(float dp) { + return (int) (getContext().getResources().getDisplayMetrics().density * dp + 0.5f); + } + @Override public View onCreateView( @NonNull LayoutInflater inflater, @@ -52,13 +70,58 @@ public View onCreateView( )); } - for (IconData iconData : iconDataList) { + for (final IconData iconData : iconDataList) { + LinearLayout group = new LinearLayout(getContext()); + group.setPadding(0, dpToPx(8), 0, dpToPx(8)); + group.setOrientation(LinearLayout.VERTICAL); + group.setGravity(Gravity.START); + + LinearLayout iconsList = new LinearLayout(getContext()); + group.addView(iconsList); + + for (String icon : iconData.icons) { + ImageView iView = new ImageView(getContext()); + int id = getResources().getIdentifier(icon, "drawable", getActivity().getPackageName()); + iView.setImageDrawable(ContextCompat.getDrawable(getActivity(), id)); + iView.setMinimumWidth(dpToPx(48)); + iView.setMinimumHeight(dpToPx(48)); + iconsList.addView(iView); + } + TextView tv = new TextView(getContext()); - tv.setText(iconData.title); + tv.setMovementMethod(LinkMovementMethod.getInstance()); + TextViewCompat.setTextAppearance(tv, R.style.TextAppearance_AppCompat_Body1); + String rawText = Phrase.from( + "Images based on or derived from works from:\n{title}" + ).put( + "title", iconData.title + ).format().toString(); + SpannableString spanText = new SpannableString(rawText + "\n" + "Open creator's website"); + spanText.setSpan( + new ClickableSpan() { + @Override + public void onClick(@NonNull View view) { + showUrlInBrowser(iconData.link); + } + + @Override + public void updateDrawState(@NonNull TextPaint ds) { + ds.setUnderlineText(true); + ds.setColor(Color.BLUE); + } + }, + rawText.length() + 1, + spanText.length(), + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE + ); + tv.setText(spanText); tv.setLayoutParams(new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT )); - binding.root.addView(tv); + + group.addView(tv); + + binding.root.addView(group); } return binding.root; diff --git a/app/src/main/java/eu/pkgsoftware/babybuddywidgets/BaseFragment.java b/app/src/main/java/eu/pkgsoftware/babybuddywidgets/BaseFragment.java index b7e7b16..801aec5 100644 --- a/app/src/main/java/eu/pkgsoftware/babybuddywidgets/BaseFragment.java +++ b/app/src/main/java/eu/pkgsoftware/babybuddywidgets/BaseFragment.java @@ -4,6 +4,8 @@ import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -94,4 +96,8 @@ public void onDestroy() { protected MainActivity getMainActivity() { return (MainActivity) getActivity(); } + + protected void showUrlInBrowser(String url) { + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); + } } diff --git a/app/src/main/res/layout/about_fragment.xml b/app/src/main/res/layout/about_fragment.xml index e21d9ad..2dd1250 100644 --- a/app/src/main/res/layout/about_fragment.xml +++ b/app/src/main/res/layout/about_fragment.xml @@ -5,4 +5,26 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" - tools:context=".AboutFragment" /> \ No newline at end of file + android:padding="8dp" + tools:context=".AboutFragment"> + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/about_strings.xml b/app/src/main/res/values/about_strings.xml index b8d542e..4fc7b10 100644 --- a/app/src/main/res/values/about_strings.xml +++ b/app/src/main/res/values/about_strings.xml @@ -12,4 +12,6 @@ https://www.flaticon.com/free-icons/poop https://www.flaticon.com/free-icons/humidity + License under The MIT License (MIT) + 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. \ No newline at end of file