aboutsummaryrefslogtreecommitdiffstats
path: root/libraries
diff options
context:
space:
mode:
authorDominik Schürmann <dominik@dominikschuermann.de>2013-09-09 13:16:54 +0200
committerDominik Schürmann <dominik@dominikschuermann.de>2013-09-09 13:16:54 +0200
commit5b6880d2e34f4c8a4bfba87b5ce53d3c6727b744 (patch)
tree0e9c9f0c3b9cd9f0a7d5a15512a70b36001a87de /libraries
parent5dc693c64c14000a3a03903736d000a45795bcee (diff)
downloadopen-keychain-5b6880d2e34f4c8a4bfba87b5ce53d3c6727b744.tar.gz
open-keychain-5b6880d2e34f4c8a4bfba87b5ce53d3c6727b744.tar.bz2
open-keychain-5b6880d2e34f4c8a4bfba87b5ce53d3c6727b744.zip
Switch from HtmlSpanner to HtmlTextView
Diffstat (limited to 'libraries')
-rw-r--r--libraries/HtmlTextView/.gitignore30
-rw-r--r--libraries/HtmlTextView/AndroidManifest.xml13
-rw-r--r--libraries/HtmlTextView/LICENSE202
-rw-r--r--libraries/HtmlTextView/README.md45
-rw-r--r--libraries/HtmlTextView/build.gradle24
-rw-r--r--libraries/HtmlTextView/project.properties15
-rw-r--r--libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/HtmlTagHandler.java92
-rw-r--r--libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/HtmlTextView.java83
-rw-r--r--libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/JellyBeanSpanFixTextView.java212
-rw-r--r--libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/UrlImageGetter.java128
10 files changed, 844 insertions, 0 deletions
diff --git a/libraries/HtmlTextView/.gitignore b/libraries/HtmlTextView/.gitignore
new file mode 100644
index 000000000..71c11b159
--- /dev/null
+++ b/libraries/HtmlTextView/.gitignore
@@ -0,0 +1,30 @@
+#Android specific
+bin
+gen
+obj
+libs/armeabi
+lint.xml
+local.properties
+release.properties
+ant.properties
+*.class
+*.apk
+
+#Gradle
+.gradle
+build
+gradle.properties
+
+#Maven
+target
+pom.xml.*
+
+#Eclipse
+.project
+.classpath
+.settings
+.metadata
+
+#IntelliJ IDEA
+.idea
+*.iml
diff --git a/libraries/HtmlTextView/AndroidManifest.xml b/libraries/HtmlTextView/AndroidManifest.xml
new file mode 100644
index 000000000..1c747c1b8
--- /dev/null
+++ b/libraries/HtmlTextView/AndroidManifest.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="org.sufficientlysecure.htmltextview"
+ android:versionCode="1"
+ android:versionName="1.0">
+
+ <uses-sdk
+ android:minSdkVersion="7"
+ android:targetSdkVersion="17"/>
+
+ <application/>
+
+</manifest> \ No newline at end of file
diff --git a/libraries/HtmlTextView/LICENSE b/libraries/HtmlTextView/LICENSE
new file mode 100644
index 000000000..d64569567
--- /dev/null
+++ b/libraries/HtmlTextView/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/libraries/HtmlTextView/README.md b/libraries/HtmlTextView/README.md
new file mode 100644
index 000000000..05714f79c
--- /dev/null
+++ b/libraries/HtmlTextView/README.md
@@ -0,0 +1,45 @@
+# HtmlTextView for Android
+
+This HtmlTextView supports all HTML tags supported by Android's Html class (see [The CommonsBlog](http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html) and [history of Html class](https://github.com/android/platform_frameworks_base/commits/master/core/java/android/text/Html.java) for newer additions).
+Additionally, list tags are supported (``<ul>``, ``<ol>``, ``<dd>``) and code tags with ``<code>``.
+
+This also includes a workaround to prevent TextView crashing on [specific Android versions](http://code.google.com/p/android/issues/detail?id=35466).
+
+This library is kept very tiny with no external dependencies.
+I am using it to provide Help/About Activities in my apps.
+
+## Example
+
+```
+HtmlTextView text = new HtmlTextView(this);
+
+// loads html from string
+text.setHtmlFromString("<b>Hello</b><ul><li>world</li><li>cats</li></ul>");
+```
+or
+```
+HtmlTextView text = new HtmlTextView(this);
+
+// loads html from raw resource, i.e., a html file in res/raw/, this allows translatable resource (e.g., res/raw-de/ for german)
+text.setHtmlFromRawResource(this, R.raw.help);
+```
+
+## Use library as Gradle dependency (Android library project)
+
+1. Copy this folder to your project and include it in ``settings.gradle`` with ``include ':html-textview'``
+2. Add dependency ``compile project(':html-textview')`` to your project's ``build.gradle``.
+
+## License
+
+Apache License v2
+
+## Authors
+- This library was hacked together by Dominik Schürmann
+- Original [TagHandler](https://gist.github.com/mlakkadshaw/5983704) developed by [Mohammed Lakkadshaw](http://blog.mohammedlakkadshaw.com/)
+- Original [UrlImageGetter](https://gist.github.com/Antarix/4167655) developed by Antarix Tandon
+- [JellyBeanSpanFixTextView](https://gist.github.com/pyricau/3424004) (with fix from comment) developed by Pierre-Yves Ricau
+
+## Contributions
+
+Feel free to fork and do pull requests. I am more than happy to merge them.
+Please do not introduce external dependencies. \ No newline at end of file
diff --git a/libraries/HtmlTextView/build.gradle b/libraries/HtmlTextView/build.gradle
new file mode 100644
index 000000000..40a586dab
--- /dev/null
+++ b/libraries/HtmlTextView/build.gradle
@@ -0,0 +1,24 @@
+buildscript {
+ repositories {
+ mavenCentral()
+ }
+ dependencies {
+ classpath 'com.android.tools.build:gradle:0.5.+'
+ }
+}
+
+apply plugin: 'android-library'
+
+
+android {
+ compileSdkVersion 17
+ buildToolsVersion '17'
+
+ sourceSets {
+ main {
+ manifest.srcFile 'AndroidManifest.xml'
+ java.srcDirs = ['src']
+ res.srcDirs = ['res']
+ }
+ }
+}
diff --git a/libraries/HtmlTextView/project.properties b/libraries/HtmlTextView/project.properties
new file mode 100644
index 000000000..5ca3505c8
--- /dev/null
+++ b/libraries/HtmlTextView/project.properties
@@ -0,0 +1,15 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system edit
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+#
+# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
+#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+
+# Project target.
+target=android-7
+android.library=true
diff --git a/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/HtmlTagHandler.java b/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/HtmlTagHandler.java
new file mode 100644
index 000000000..c40c8dec3
--- /dev/null
+++ b/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/HtmlTagHandler.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2013 Mohammed Lakkadshaw
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.sufficientlysecure.htmltextview;
+
+import java.util.Vector;
+
+import org.xml.sax.XMLReader;
+
+import android.text.Editable;
+import android.text.Html;
+import android.text.Spannable;
+import android.text.style.BulletSpan;
+import android.text.style.LeadingMarginSpan;
+import android.text.style.TypefaceSpan;
+import android.util.Log;
+
+public class HtmlTagHandler implements Html.TagHandler {
+ private int mListItemCount = 0;
+ private Vector<String> mListParents = new Vector<String>();
+
+ @Override
+ public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {
+
+ if (tag.equals("ul") || tag.equals("ol") || tag.equals("dd")) {
+ if (opening) {
+ mListParents.add(tag);
+ } else mListParents.remove(tag);
+
+ mListItemCount = 0;
+ } else if (tag.equals("li") && !opening) {
+ handleListTag(output);
+ } else if (tag.equalsIgnoreCase("code")) {
+ if (opening) {
+ output.setSpan(new TypefaceSpan("monospace"), output.length(), output.length(), Spannable.SPAN_MARK_MARK);
+ } else {
+ Log.d(HtmlTextView.TAG, "Code tag encountered");
+ Object obj = getLast(output, TypefaceSpan.class);
+ int where = output.getSpanStart(obj);
+ output.setSpan(new TypefaceSpan("monospace"), where, output.length(), 0);
+ }
+ }
+ }
+
+ private Object getLast(Editable text, Class kind) {
+ Object[] objs = text.getSpans(0, text.length(), kind);
+ if (objs.length == 0) {
+ return null;
+ } else {
+ for (int i = objs.length; i > 0; i--) {
+ if (text.getSpanFlags(objs[i - 1]) == Spannable.SPAN_MARK_MARK) {
+ return objs[i - 1];
+ }
+ }
+ return null;
+ }
+ }
+
+ private void handleListTag(Editable output) {
+ if (mListParents.lastElement().equals("ul")) {
+ output.append("\n");
+ String[] split = output.toString().split("\n");
+
+ int lastIndex = split.length - 1;
+ int start = output.length() - split[lastIndex].length() - 1;
+ output.setSpan(new BulletSpan(15 * mListParents.size()), start, output.length(), 0);
+ } else if (mListParents.lastElement().equals("ol")) {
+ mListItemCount++;
+
+ output.append("\n");
+ String[] split = output.toString().split("\n");
+
+ int lastIndex = split.length - 1;
+ int start = output.length() - split[lastIndex].length() - 1;
+ output.insert(start, mListItemCount + ". ");
+ output.setSpan(new LeadingMarginSpan.Standard(15 * mListParents.size()), start, output.length(), 0);
+ }
+ }
+}
diff --git a/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/HtmlTextView.java b/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/HtmlTextView.java
new file mode 100644
index 000000000..317c25aaf
--- /dev/null
+++ b/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/HtmlTextView.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2013 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.sufficientlysecure.htmltextview;
+
+import android.content.Context;
+import android.text.Html;
+import android.text.method.LinkMovementMethod;
+import android.util.AttributeSet;
+
+import java.io.InputStream;
+
+public class HtmlTextView extends JellyBeanSpanFixTextView {
+
+ public static final String TAG = "HtmlTextView";
+
+ public HtmlTextView(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+ }
+
+ public HtmlTextView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public HtmlTextView(Context context) {
+ super(context);
+ }
+
+ /**
+ * http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
+ *
+ * @param is
+ * @return
+ */
+ static private String convertStreamToString(java.io.InputStream is) {
+ java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
+ return s.hasNext() ? s.next() : "";
+ }
+
+ /**
+ * Loads HTML from a raw resource, i.e., a HTML file in res/raw/.
+ * This allows translatable resource (e.g., res/raw-de/ for german).
+ * The containing HTML is parsed to Android's Spannable format and then displayed.
+ *
+ * @param context
+ * @param id for example: R.raw.help
+ */
+ public void setHtmlFromRawResource(Context context, int id) {
+ // load html from html file from /res/raw
+ InputStream inputStreamText = context.getResources().openRawResource(id);
+
+ setHtmlFromString(convertStreamToString(inputStreamText));
+ }
+
+ /**
+ * Parses String containing HTML to Android's Spannable format and displays it in this TextView.
+ *
+ * @param html String containing HTML, for example: "<b>Hello world!</b>"
+ */
+ public void setHtmlFromString(String html) {
+ // this uses Android's Html class for basic parsing, and HtmlTagHandler
+ setText(Html.fromHtml(html, new UrlImageGetter(this, getContext()), new HtmlTagHandler()));
+
+ // make links work
+ setMovementMethod(LinkMovementMethod.getInstance());
+
+ // no flickering when clicking textview for Android < 4
+// text.setTextColor(getResources().getColor(android.R.color.secondary_text_dark_nodisable));
+ }
+}
diff --git a/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/JellyBeanSpanFixTextView.java b/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/JellyBeanSpanFixTextView.java
new file mode 100644
index 000000000..94bf45849
--- /dev/null
+++ b/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/JellyBeanSpanFixTextView.java
@@ -0,0 +1,212 @@
+/*
+ * Copyright (C) 2013 Dominik Schürmann <dominik@dominikschuermann.de>
+ * Copyright (C) 2012 Pierre-Yves Ricau <py.ricau@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.sufficientlysecure.htmltextview;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import android.content.Context;
+import android.text.SpannableStringBuilder;
+import android.text.Spanned;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.widget.TextView;
+
+/**
+ * <p>
+ * A {@link android.widget.TextView} that insert spaces around its text spans where needed to prevent
+ * {@link IndexOutOfBoundsException} in {@link #onMeasure(int, int)} on Jelly Bean.
+ * <p>
+ * When {@link #onMeasure(int, int)} throws an exception, we try to fix the text by adding spaces
+ * around spans, until it works again. We then try removing some of the added spans, to minimize the
+ * insertions.
+ * <p>
+ * The fix is time consuming (a few ms, it depends on the size of your text), but it should only
+ * happen once per text change.
+ * <p>
+ * See http://code.google.com/p/android/issues/detail?id=35466
+ *
+ */
+public class JellyBeanSpanFixTextView extends TextView {
+
+ private static class FixingResult {
+ public final boolean fixed;
+ public final List<Object> spansWithSpacesBefore;
+ public final List<Object> spansWithSpacesAfter;
+
+ public static FixingResult fixed(List<Object> spansWithSpacesBefore,
+ List<Object> spansWithSpacesAfter) {
+ return new FixingResult(true, spansWithSpacesBefore, spansWithSpacesAfter);
+ }
+
+ public static FixingResult notFixed() {
+ return new FixingResult(false, null, null);
+ }
+
+ private FixingResult(boolean fixed, List<Object> spansWithSpacesBefore,
+ List<Object> spansWithSpacesAfter) {
+ this.fixed = fixed;
+ this.spansWithSpacesBefore = spansWithSpacesBefore;
+ this.spansWithSpacesAfter = spansWithSpacesAfter;
+ }
+ }
+
+ public JellyBeanSpanFixTextView(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+ }
+
+ public JellyBeanSpanFixTextView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public JellyBeanSpanFixTextView(Context context) {
+ super(context);
+ }
+
+ @Override
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+ try {
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+ } catch (IndexOutOfBoundsException e) {
+ fixOnMeasure(widthMeasureSpec, heightMeasureSpec);
+ }
+ }
+
+ /**
+ * If possible, fixes the Spanned text by adding spaces around spans when needed.
+ */
+ private void fixOnMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+ CharSequence text = getText();
+ if (text instanceof Spanned) {
+ SpannableStringBuilder builder = new SpannableStringBuilder(text);
+ fixSpannedWithSpaces(builder, widthMeasureSpec, heightMeasureSpec);
+ } else {
+ if (BuildConfig.DEBUG) {
+ Log.d(HtmlTextView.TAG, "The text isn't a Spanned");
+ }
+ fallbackToString(widthMeasureSpec, heightMeasureSpec);
+ }
+ }
+
+ /**
+ * Add spaces around spans until the text is fixed, and then removes the unneeded spaces
+ */
+ private void fixSpannedWithSpaces(SpannableStringBuilder builder, int widthMeasureSpec,
+ int heightMeasureSpec) {
+ long startFix = System.currentTimeMillis();
+
+ FixingResult result = addSpacesAroundSpansUntilFixed(builder, widthMeasureSpec,
+ heightMeasureSpec);
+
+ if (result.fixed) {
+ removeUnneededSpaces(widthMeasureSpec, heightMeasureSpec, builder, result);
+ } else {
+ fallbackToString(widthMeasureSpec, heightMeasureSpec);
+ }
+
+ if (BuildConfig.DEBUG) {
+ long fixDuration = System.currentTimeMillis() - startFix;
+ Log.d(HtmlTextView.TAG, "fixSpannedWithSpaces() duration in ms: " + fixDuration);
+ }
+ }
+
+ private FixingResult addSpacesAroundSpansUntilFixed(SpannableStringBuilder builder,
+ int widthMeasureSpec, int heightMeasureSpec) {
+
+ Object[] spans = builder.getSpans(0, builder.length(), Object.class);
+ List<Object> spansWithSpacesBefore = new ArrayList<Object>(spans.length);
+ List<Object> spansWithSpacesAfter = new ArrayList<Object>(spans.length);
+
+ for (Object span : spans) {
+ int spanStart = builder.getSpanStart(span);
+ if (isNotSpace(builder, spanStart - 1)) {
+ builder.insert(spanStart, " ");
+ spansWithSpacesBefore.add(span);
+ }
+
+ int spanEnd = builder.getSpanEnd(span);
+ if (isNotSpace(builder, spanEnd)) {
+ builder.insert(spanEnd, " ");
+ spansWithSpacesAfter.add(span);
+ }
+
+ try {
+ setTextAndMeasure(builder, widthMeasureSpec, heightMeasureSpec);
+ return FixingResult.fixed(spansWithSpacesBefore, spansWithSpacesAfter);
+ } catch (IndexOutOfBoundsException notFixed) {
+ }
+ }
+ if (BuildConfig.DEBUG) {
+ Log.d(HtmlTextView.TAG, "Could not fix the Spanned by adding spaces around spans");
+ }
+ return FixingResult.notFixed();
+ }
+
+ private boolean isNotSpace(CharSequence text, int where) {
+ if (where < 0)
+ return true;
+ return text.charAt(where) != ' ';
+ }
+
+ private void setTextAndMeasure(CharSequence text, int widthMeasureSpec, int heightMeasureSpec) {
+ setText(text);
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+ }
+
+ private void removeUnneededSpaces(int widthMeasureSpec, int heightMeasureSpec,
+ SpannableStringBuilder builder, FixingResult result) {
+
+ for (Object span : result.spansWithSpacesAfter) {
+ int spanEnd = builder.getSpanEnd(span);
+ builder.delete(spanEnd, spanEnd + 1);
+ try {
+ setTextAndMeasure(builder, widthMeasureSpec, heightMeasureSpec);
+ } catch (IndexOutOfBoundsException ignored) {
+ builder.insert(spanEnd, " ");
+ }
+ }
+
+ boolean needReset = true;
+ for (Object span : result.spansWithSpacesBefore) {
+ int spanStart = builder.getSpanStart(span);
+ builder.delete(spanStart - 1, spanStart);
+ try {
+ setTextAndMeasure(builder, widthMeasureSpec, heightMeasureSpec);
+ needReset = false;
+ } catch (IndexOutOfBoundsException ignored) {
+ needReset = true;
+ int newSpanStart = spanStart - 1;
+ builder.insert(newSpanStart, " ");
+ }
+ }
+
+ if (needReset) {
+ setText(builder);
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+ }
+ }
+
+ private void fallbackToString(int widthMeasureSpec, int heightMeasureSpec) {
+ if (BuildConfig.DEBUG) {
+ Log.d(HtmlTextView.TAG, "Fallback to unspanned text");
+ }
+ String fallbackText = getText().toString();
+ setTextAndMeasure(fallbackText, widthMeasureSpec, heightMeasureSpec);
+ }
+
+} \ No newline at end of file
diff --git a/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/UrlImageGetter.java b/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/UrlImageGetter.java
new file mode 100644
index 000000000..e4fc41c05
--- /dev/null
+++ b/libraries/HtmlTextView/src/org/sufficientlysecure/htmltextview/UrlImageGetter.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2013 Antarix Tandon
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.sufficientlysecure.htmltextview;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
+import android.os.AsyncTask;
+import android.text.Html.ImageGetter;
+import android.view.View;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+
+public class UrlImageGetter implements ImageGetter {
+ Context c;
+ View container;
+
+ /**
+ * Construct the URLImageParser which will execute AsyncTask and refresh the container
+ *
+ * @param t
+ * @param c
+ */
+ public UrlImageGetter(View t, Context c) {
+ this.c = c;
+ this.container = t;
+ }
+
+ public Drawable getDrawable(String source) {
+ UrlDrawable urlDrawable = new UrlDrawable();
+
+ // get the actual source
+ ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);
+
+ asyncTask.execute(source);
+
+ // return reference to URLDrawable where I will change with actual image from
+ // the src tag
+ return urlDrawable;
+ }
+
+ public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
+ UrlDrawable urlDrawable;
+
+ public ImageGetterAsyncTask(UrlDrawable d) {
+ this.urlDrawable = d;
+ }
+
+ @Override
+ protected Drawable doInBackground(String... params) {
+ String source = params[0];
+ return fetchDrawable(source);
+ }
+
+ @Override
+ protected void onPostExecute(Drawable result) {
+ // set the correct bound according to the result from HTTP call
+ urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0 + result.getIntrinsicHeight());
+
+ // change the reference of the current drawable to the result
+ // from the HTTP call
+ urlDrawable.drawable = result;
+
+ // redraw the image by invalidating the container
+ UrlImageGetter.this.container.invalidate();
+ }
+
+ /**
+ * Get the Drawable from URL
+ *
+ * @param urlString
+ * @return
+ */
+ public Drawable fetchDrawable(String urlString) {
+ try {
+ InputStream is = fetch(urlString);
+ Drawable drawable = Drawable.createFromStream(is, "src");
+ drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 + drawable.getIntrinsicHeight());
+ return drawable;
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ private InputStream fetch(String urlString) throws MalformedURLException, IOException {
+ DefaultHttpClient httpClient = new DefaultHttpClient();
+ HttpGet request = new HttpGet(urlString);
+ HttpResponse response = httpClient.execute(request);
+ return response.getEntity().getContent();
+ }
+ }
+
+ @SuppressWarnings("deprecation")
+ public class UrlDrawable extends BitmapDrawable {
+ // the drawable that you need to set, you could set the initial drawing
+ // with the loading image if you need to
+ protected Drawable drawable;
+
+ @Override
+ public void draw(Canvas canvas) {
+ // override the draw to facilitate refresh function later
+ if (drawable != null) {
+ drawable.draw(canvas);
+ }
+ }
+ }
+}