Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix attribute class redefined error on some epub files #457

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3e99160
Fix attribute class redefined error on some epub files
marcqtan Jul 6, 2020
80c3fc5
Rename menu_main to avoid conflict with flutter_inappwebview menu
marcqtan Jul 15, 2020
18f3247
Implement pinch to zoom feature
marcqtan Aug 14, 2020
baa8cb2
Add audio controls when playing audio
marcqtan Aug 18, 2020
85a27cb
Always show app bar
marcqtan Aug 18, 2020
f4d517f
Modify parsing of html for injecting of audio control
marcqtan Sep 18, 2020
b46f307
Initial changes to make notification work when playing epub
marcqtan Oct 29, 2020
8786e47
Add new buttons and more ui enhancement
marcqtan Oct 29, 2020
9182f2d
Remove notification when exiting the webview
marcqtan Oct 29, 2020
eccfa25
Fix undefined exception showing in console log
marcqtan Oct 29, 2020
59fcf28
Dont destroy webview/viewpager while audio is still active
marcqtan Oct 30, 2020
865f86a
Remove white background
marcqtan Oct 30, 2020
9aee468
Fix unable to open epub file
marcqtan Dec 2, 2020
5cb9af1
Fix audio control not showing on some epub file
marcqtan Dec 9, 2020
1bd2f37
Handle nbsp entity
marcqtan Dec 10, 2020
8eec2ca
Don't show download button in audio control
marcqtan Dec 11, 2020
1db1988
Remove download button in audio controls
marcqtan Dec 14, 2020
1a11f44
Add tap edge to go to nextpage feature
marcqtan Jan 6, 2021
0f24d8c
Add slide bar to easily change the page content
marcqtan Jan 14, 2021
ef00cf9
Modify into a vertical seekbar
marcqtan Jan 14, 2021
551bea1
Change slider color
marcqtan Jan 18, 2021
832b2f4
Change progress color to be consistent with the theme color
marcqtan Jan 19, 2021
f7a5df9
Change position of prev and next button
marcqtan Jan 22, 2021
ed57f45
Increase tap area of next button to 10% of the screen
marcqtan Jan 26, 2021
4f57e3e
Increase tap area of prev button
marcqtan Jan 27, 2021
46fed96
Lock orientation mode of epub to portrait
marcqtan Jan 29, 2021
e5f607f
Merge branch 'feature_TapEdge' into master
marcqtan Feb 2, 2021
1ec96b4
Remove top button to go to previous page
marcqtan Mar 19, 2021
56067de
Remove go to next button and also readjust the position of audio control
marcqtan Mar 19, 2021
dfb247f
Don't initialize tts since it's disabled
marcqtan Mar 24, 2021
95a675c
Fix audio player not showing on some epub
marcqtan Apr 7, 2021
12c2f19
Fix DBAdapter crash
marcqtan Apr 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions folioreader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,7 @@ dependencies {

// Lifecycle
implementation "androidx.lifecycle:lifecycle-extensions:$versions.lifecycle"

implementation 'org.jsoup:jsoup:1.13.1'
}
apply from: '../folioreader/bintray/bintrayv1.gradle'
2 changes: 1 addition & 1 deletion folioreader/res/layout/folio_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.folioreader.ui.activity.FolioActivity"
tools:menu="@menu/menu_main">
tools:menu="@menu/menu_main_folio">

<com.folioreader.ui.view.DirectionalViewpager
android:id="@+id/folioPageViewPager"
Expand Down
1 change: 1 addition & 0 deletions folioreader/res/layout/folio_page_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
android:orientation="vertical">

<FrameLayout
android:paddingTop="?android:actionBarSize"
android:id="@+id/webViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class FolioActivity : AppCompatActivity(), FolioActivityCallback, MediaControlle
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

setConfig(savedInstanceState)
initDistractionFreeMode(savedInstanceState)
//initDistractionFreeMode(savedInstanceState)

setContentView(R.layout.folio_activity)
this.savedInstanceState = savedInstanceState
Expand Down Expand Up @@ -359,7 +359,7 @@ class FolioActivity : AppCompatActivity(), FolioActivityCallback, MediaControlle
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
menuInflater.inflate(R.menu.menu_main_folio, menu)

val config = AppUtil.getSavedConfig(applicationContext)!!
UiUtil.setColorIntToDrawable(config.themeColor, menu.findItem(R.id.itemSearch).icon)
Expand Down
82 changes: 79 additions & 3 deletions folioreader/src/main/java/com/folioreader/ui/base/HtmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import com.folioreader.Constants;
import com.folioreader.R;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

/**
* @author gautam chibde on 14/6/17.
*/
Expand Down Expand Up @@ -54,7 +59,15 @@ public static String getHtmlContent(Context context, String htmlContent, Config
"setMediaOverlayStyleColors('#C0ED72','#C0ED72')") + "\n";

jsPath = jsPath
+ "<meta name=\"viewport\" content=\"height=device-height, user-scalable=no\" />";
+ "<meta name=\"viewport\" content=\"height=device-height, user-scalable=yes\" />" + "\n";

jsPath = jsPath + String.format(context.getString(R.string.script_tag_method_call), "function playAudio(src) {\n" +
" var audioElement = $('#player')[0];\n" +
" audioElement.setAttribute('src',src);\n" +
" audioElement.load();\n" +
" audioElement.play();\n" +
" $(audioElement).show();\n" +
"}") + "\n";

String toInject = "\n" + cssPath + "\n" + jsPath + "\n</head>";
htmlContent = htmlContent.replace("</head>", toInject);
Expand Down Expand Up @@ -101,8 +114,71 @@ public static String getHtmlContent(Context context, String htmlContent, Config
break;
}

htmlContent = htmlContent.replace("<html", "<html class=\"" + classes + "\"" +
" onclick=\"onClickHtml()\"");
htmlContent = checkClassAttr(htmlContent, classes);


Document doc = Jsoup.parse(htmlContent, "", Parser.xmlParser());

Elements audios = doc.getElementsByTag("audio");
if(audios.size() > 0) {
Elements rect = doc.getElementsByTag("rect");
for (int i = 0; i < rect.size(); i++) {
for(int y = 0; y < audios.size(); y++) {
String src = null;
String id = audios.get(y).attr("id");
String onclick = rect.get(i).attr("onclick"); //$("#TRAC_126")[0].play()
if(onclick.split("\"").length > 1) {//split by quotation
String rectAudioId = onclick.split("\"")[1].substring(1); //to extract TRAC_126
if (id.equals(rectAudioId)) {
src = audios.get(y).attr("src");
rect.get(i).attr("onclick", "playAudio('" + src + "')");
break;
}
}
}
}

if(rect.size() > 0) {
doc.getElementsByTag("body").append("<audio id=\"player\" controls=\"controls\" style=\"width:100%; " +
"margin: 0 auto;display: table;\"" + "\n</body>");
}

return doc.html();
}
return htmlContent;
}

//This will fix the "Attribute Class Redefined" error
private static String checkClassAttr(String html, String classes) {
//get the entry of <html>
String s1 = html.substring(html.indexOf("<html"));
String s2= s1.substring(0, s1.indexOf(">"));
String classValue = "";

//check if class attribute exists
if(s2.contains("class=")) {
String classVal = s2.substring(s2.indexOf("class="));
String[] kvPairs = classVal.split(" ");

//get the value
for(String kvPair: kvPairs) {
String[] kv = kvPair.split("=");
String key = kv[0];

if(key.equals("class")) {
classValue = kv[1]; //get the value of class attribute
break;
}
}

//remove the class attribute since it will be readded later to avoid "attribute redefined error"
html = html.replaceFirst("class=" + classValue, "");
classValue = classValue.substring(1); //remove the first quotation


}
classValue = classValue.equals("") ? "\"" : classValue;
html = html.replace("<html", "<html class=\"" + classes + " " + classValue + " onclick=\"onClickHtml()\"");
return html;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ class FolioPageFragment : Fragment(),
mWebview!!.webChromeClient = webChromeClient

mWebview!!.settings.defaultTextEncodingName = "utf-8"
mWebview!!.settings.builtInZoomControls = true;
mWebview!!.settings.displayZoomControls = false;


HtmlTask(this).execute(chapterUrl.toString())
}

Expand Down Expand Up @@ -657,9 +661,13 @@ class FolioPageFragment : Fragment(),
if (mConfig!!.isNightMode) {
mRootView!!.findViewById<View>(R.id.indicatorLayout)
.setBackgroundColor(Color.parseColor("#131313"))
mRootView!!.findViewById<FrameLayout>(R.id.webViewLayout)
.setBackgroundColor(Color.parseColor("#131313"))
} else {
mRootView!!.findViewById<View>(R.id.indicatorLayout)
.setBackgroundColor(Color.WHITE)
mRootView!!.findViewById<FrameLayout>(R.id.webViewLayout)
.setBackgroundColor(Color.WHITE)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ class FolioWebView : WebView {

@JavascriptInterface
fun toggleSystemUI() {
uiHandler.post {
folioActivityCallback.toggleSystemUI()
}
// uiHandler.post {
// folioActivityCallback.toggleSystemUI()
// }
}

@JavascriptInterface
Expand Down