This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Html Tag Handler can handle basic tables by storing the HTML to be la…
…ter used by a ClickTableSpan. Applications wanting to take advantage of this capability need to implement a ClickTableSpan and do whatever they'd like with the raw HTML (i.e. pass it into a webview). In addition, a DrawTableSpan has been provided for applications that want to display text other than "[tap for table]", which should be useful for localization.
- Loading branch information
Showing
11 changed files
with
380 additions
and
15 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
40 changes: 40 additions & 0 deletions
40
HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/ClickableTableSpan.java
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,40 @@ | ||
/* | ||
* Copyright (C) 2016 Richard Thai | ||
* | ||
* 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.text.style.ClickableSpan; | ||
|
||
/** | ||
* This span defines what should happen if a table is clicked. This abstract class is defined so | ||
* that applications can access the raw table HTML and do whatever they'd like to render it (e.g. | ||
* show it in a WebView). | ||
*/ | ||
public abstract class ClickableTableSpan extends ClickableSpan { | ||
protected String mTableHtml; | ||
|
||
// This sucks, but we need this so that each table can get its own ClickableTableSpan. | ||
// Otherwise, we end up removing the clicking from earlier tables. | ||
public abstract ClickableTableSpan newInstance(); | ||
|
||
public void setTableHtml(String tableHtml) { | ||
this.mTableHtml = tableHtml; | ||
} | ||
|
||
public String getTableHtml() { | ||
return mTableHtml; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/DrawTableLinkSpan.java
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,83 @@ | ||
/* | ||
* Copyright (C) 2016 Richard Thai | ||
* | ||
* 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.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Paint; | ||
import android.text.style.ReplacementSpan; | ||
|
||
/** | ||
* This span defines how a table should be rendered in the HtmlTextView. The default implementation | ||
* is a cop-out which replaces the HTML table with some text ("[tap for table]" is the default). | ||
* | ||
* This is to be used in conjunction with the ClickableTableSpan which will redirect a click to the | ||
* text some application-defined action (i.e. render the raw HTML in a WebView). | ||
*/ | ||
public class DrawTableLinkSpan extends ReplacementSpan { | ||
private int mWidth; | ||
|
||
private static final String DEFAULT_TABLE_LINK_TEXT = "[tap for table]"; | ||
private static float DEFAULT_TEXT_SIZE = 80f; | ||
private static int DEFAULT_TEXT_COLOR = Color.BLUE; | ||
|
||
protected String mTableLinkText = DEFAULT_TABLE_LINK_TEXT; | ||
protected float mTextSize = DEFAULT_TEXT_SIZE; | ||
protected int mTextColor = DEFAULT_TEXT_COLOR; | ||
|
||
@Override | ||
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { | ||
mWidth = (int) paint.measureText(mTableLinkText, 0, mTableLinkText.length()); | ||
mTextSize = paint.getTextSize(); | ||
return mWidth; | ||
} | ||
|
||
@Override | ||
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { | ||
final Paint paint2 = new Paint(); | ||
paint2.setStyle(Paint.Style.STROKE); | ||
paint2.setColor(Color.BLUE); | ||
paint2.setAntiAlias(true); | ||
paint2.setTextSize(mTextSize); | ||
|
||
canvas.drawText(mTableLinkText, x, bottom, paint2); | ||
} | ||
|
||
public void setTableLinkText(String tableLinkText) { | ||
this.mTableLinkText = tableLinkText; | ||
} | ||
|
||
public void setTextSize(float textSize) { | ||
this.mTextSize = textSize; | ||
} | ||
|
||
public void setTextColor(int textColor) { | ||
this.mTextColor = textColor; | ||
} | ||
|
||
public String getTableLinkText() { | ||
return mTableLinkText; | ||
} | ||
|
||
public float getTextSize() { | ||
return mTextSize; | ||
} | ||
|
||
public int getTextColor() { | ||
return mTextColor; | ||
} | ||
} |
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
Oops, something went wrong.