Permalink
Browse files

Page generation working.

  • Loading branch information...
1 parent d24cd5f commit dbb885246b3a7daac86233b5772b1a9cae2ae908 @fidanov committed Dec 21, 2011
Showing with 131 additions and 12 deletions.
  1. +1 −0 AndroidManifest.xml
  2. +19 −0 assets/js/paginate.js
  3. +1 −2 gen/com/terlici/reader/R.java
  4. +10 −1 res/layout/main.xml
  5. +100 −9 src/com/terlici/reader/StartScreen.java
View
@@ -5,6 +5,7 @@
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="13" android:targetSdkVersion="14"/>
+ <uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
View
@@ -0,0 +1,19 @@
+var body = document.getElementsByTagName('body')[0];
+var desiredHeight = window.innerHeight - 50;
+var desiredWidth = window.innerWidth - 30;
+
+totalHeight = body.offsetHeight;
+pageCount = Math.floor(totalHeight/desiredHeight) + 1;
+
+body.style.padding = "10px";
+body.style.width = desiredWidth * pageCount + "px";
+body.style.height = desiredHeight + "px";
+body.style.WebkitColumnCount = pageCount;
+body.style.WebkitColumnWidth = desiredWidth + "px";
+body.style.WebkitColumnGap = "10px";
+
+window.scrollBy(desiredWidth + 10,0);
+
+document.body.addEventListener('touchmove', function(e) {
+ e.preventDefault();
+}, false);
@@ -16,8 +16,7 @@
public static final class id {
public static final int button1=0x7f050001;
public static final int spineIndex=0x7f050000;
- public static final int text=0x7f050002;
- public static final int webtext=0x7f050003;
+ public static final int webtext=0x7f050002;
}
public static final class layout {
public static final int main=0x7f030000;
View
@@ -20,15 +20,24 @@
android:text="Load"
android:onClick="load" />
+ <!--
+ <ScrollView
+ android:layout_width="fill_parent"
+ android:layout_height="400dp"
+ >
+
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
+ </ScrollView>
+ -->
<WebView
android:id="@+id/webtext"
android:layout_width="fill_parent"
- android:layout_height="400dp" />
+ android:layout_height="420dp" />
+
</LinearLayout>
@@ -1,7 +1,9 @@
package com.terlici.reader;
+import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.util.List;
import nl.siegmann.epublib.domain.Book;
@@ -15,21 +17,25 @@
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
+import android.view.MotionEvent;
import android.view.View;
-import android.webkit.WebSettings;
+import android.webkit.WebResourceResponse;
import android.webkit.WebView;
+import android.webkit.WebViewClient;
import android.widget.EditText;
public class StartScreen extends Activity {
/** Called when the activity is first created. */
-
+ String paginatejs;
Book book;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
+ loadjs();
+ prepare();
logBook();
}
@@ -67,20 +73,105 @@ private void logBook() {
}
}
+ public String read(InputStream input) throws IOException {
+ BufferedReader r = new BufferedReader(new InputStreamReader(input));
+ StringBuilder total = new StringBuilder();
+ String line;
+
+ while ((line = r.readLine()) != null) {
+ total.append(line);
+ }
+
+ return total.toString();
+ }
+
+ public void loadjs() {
+ Log.i("epublib", "Loading JS");
+ AssetManager assetManager = getAssets();
+
+
+ try {
+ paginatejs = read(assetManager.open("js/paginate.js"))
+ .replace('\r', ' ')
+ .replace('\n', ' ')
+ ;
+ Log.i("epublib", paginatejs);
+ } catch (IOException e) {
+ }
+ }
+
+ public void prepare() {
+ WebView wbox = (WebView)findViewById(R.id.webtext);
+
+ wbox.setWebViewClient(new WebViewClient() {
+ @Override
+ public WebResourceResponse shouldInterceptRequest(WebView view,
+ String url) {
+ Log.i("epublib", "Loading something: " + url);
+
+ String prefix = "http://www.terlici.com/";
+ if (url.startsWith(prefix)) {
+ url = url.substring(prefix.length());
+ }
+
+ Log.i("epublib", "Real url: " + url);
+
+ Resource r = book.getResources().getByHref(url);
+
+ if (r != null) {
+ WebResourceResponse response = null;
+
+ try {
+ response = new WebResourceResponse(r.getMediaType().toString(),
+ r.getInputEncoding(), r.getInputStream());
+ } catch (IOException e) {
+ Log.i("epublib", "Problem loading resource: " + url);
+ }
+
+ if (response != null) {
+ Log.i("epublib", "Loading: " + url);
+ return response;
+ }
+ }
+
+ return super.shouldInterceptRequest(view, url);
+ }
+
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ Log.i("epublib", "Page Loaded");
+ super.onPageFinished(view, url);
+
+ view.loadUrl("javascript:(function() { " + paginatejs + "})()");
+ }
+ });
+ wbox.setInitialScale(100);
+ // Big - 42
+ // Normal - 36
+ // Small - 30
+ wbox.getSettings().setMinimumFontSize(36);
+ wbox.getSettings().setJavaScriptEnabled(true);
+ wbox.setVerticalScrollBarEnabled(false);
+ wbox.setHorizontalScrollBarEnabled(false);
+ wbox.setOnTouchListener(new View.OnTouchListener() {
+
+ public boolean onTouch(View v, MotionEvent event) {
+ return (event.getAction() == MotionEvent.ACTION_MOVE);
+ }
+ });
+ }
+
public void load(View v) {
String value = ((EditText)findViewById(R.id.spineIndex)).getText().toString();
int index = Integer.parseInt(value);
Resource r = book.getSpine().getResource(index);
-
String text = new String(r.getData());
-
- //TextView box = (TextView)findViewById(R.id.text);
- //box.setText(text);
-
-
+
WebView wbox = (WebView)findViewById(R.id.webtext);
- wbox.loadDataWithBaseURL(null, text, "text/html", "utf-8", null);
+ // The "http://www.terlici.com/" is necessary for shouldInterceptRequest
+ // to be called.
+ wbox.loadDataWithBaseURL("http://www.terlici.com/", text, "text/html", "utf-8", null);
}
private void logSpineTypes(List<SpineReference> spineReferences) {

0 comments on commit dbb8852

Please sign in to comment.