Reputation: 1105
package com.pack.epubreader;
/* all the imports */
public abstract class EpubWebView extends WebView { private final static float FLING_THRESHOLD_VELOCITY = 200;
/*
* The book view will show
*/
public static SQLiteDatabase db;
DatabaseManipulation dbManipulation = new DatabaseManipulation();
//MainActivity lastChapterAddress= new MainActivity();
MySQLiteHelper dbManager;
private Book mBook;
String lastPage;
private GestureDetector mGestureDetector;
/*
* "root" page we're currently showing
*/
private Uri mCurrentResourceUri, extraUri, extraUri_start, extraUri_lastPos,extraUri_chapter ;
/*
* Position of document
*/
private float mScrollY = 0.0f;
public String sentFilename=null;
/*
* Note that we're loading from a bookmark
*/
private boolean mScrollWhenPageLoaded = false;
/*
* To speak the text
*/
private TextToSpeechWrapper mTtsWrapper;
/*
* The page, as text
*/
private ArrayList<String> mText;
private WebViewClient mWebViewClient;
/*
* Current line being spoken
*/
private int mTextLine;
/*
* Pick an initial default
*/
private float mFlingMinDistance = 320;
public EpubWebView(Context context) {
this(context, null);
}
public EpubWebView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, mGestureListener);
WebSettings settings = getSettings();
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
setWebViewClient(mWebViewClient = createWebViewClient());
}
/*
* @ return Android version appropriate WebViewClient
*/
abstract protected WebViewClient createWebViewClient();
public void testDataURL(AssetManager assetManager) {
setBook(Environment.getExternalStorageDirectory().getPath());
// String resourceName = "content/calibre_title_page.html";
// String resourceName = "content/074349881X__p__split_0.html";
// gingerbreadLoadUrl(Book.resourceName2Url(resourceName));
loadChapter(null);
}
/*
* Book to show
*/
public void setBook(String fileName) {
//String Count=Intent.getExtra
System.out.println(fileName+" book going to load");
sentFilename=fileName;
// if book already loaded, don't load again
if ((mBook == null) || !mBook.getFileName().equals(fileName)) {
mBook = new Book(fileName);
}
}
public Book getBook() {
return mBook;
}
protected WebViewClient getWebViewClient() {
return mWebViewClient;
}
/*
* Chapter of book to show
*/
public void loadChapter(Uri resourceUri)
{
if (mBook != null)
{
// if no chapter resourceName supplied, default to first one.
if (resourceUri == null)
{
resourceUri = mBook.firstChapter();
extraUri=resourceUri;
extraUri_start=resourceUri;
String extra_uriStart=extraUri.toString();
//String extraUri_start_str=resourceUri.toString();
//dbManipulation.updatingBookdetails(sentFilename, extraUri_start);
//lastChapterAddress.findLastAddress(extraUri_start, sentFilename);
System.out.println(sentFilename+" name of book going to load first");
System.out.println(extraUri_start+" address of book going to load first");
//db = openOrCreateDatabase("epub.db", EpubWebView.this.MODE_PRIVATE,null); error here
//db = openOrCreateDatabase("epub.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);// and here
try
{
System.out.println("BLOW FILE111");
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
System.out.println("BLOW FILE222");
ContentValues val=new ContentValues();
val.put("bookpath",fileName );
val.put("lastchapter", strAddress);
db.update("Bookdetails", val, "bookpath="+fileName, null);
System.out.println("BLOW FILE333");
}
catch(Exception e)
{
System.out.println(e+" errors happens");
}
finally
{
db.close();
}
//dbManager.updateBook(sentFilename, extra_uriStart);
System.out.println(sentFilename+" name of book going to load");
//(extraUri_start);
System.out.println(resourceUri+" actual Uri from top");
String resuristr = resourceUri.toString();
System.out.println(resuristr+" actual string from top");
}
if (resourceUri != null)
{
if(extraUri==null)
{
extraUri=resourceUri;
}
String extraUristr=extraUri.toString();
//extraUristr=extraUristr.substring(extraUristr.lastIndexOf("/") + 1);
//extraUristr= extraUristr.substring(8);
int p=extraUristr.lastIndexOf("/");
extraUristr=extraUristr.substring(0,p);
System.out.println(extraUristr+" cutted string_1");
String extraUri2=resourceUri.toString();
int q=extraUri2.lastIndexOf("/");
extraUri2=extraUri2.substring(q);
extraUri2=extraUristr+extraUri2;
System.out.println(extraUri2+" Joined Uri string_2");
Uri myUri = Uri.parse(extraUri2);
System.out.println(myUri+"--> Joined Uri ");
resourceUri= myUri;
System.out.println(resourceUri+"actual string");
mCurrentResourceUri = resourceUri;
// prevent cache, because WebSettings.LOAD_NO_CACHE doesn't always work.
clearCache(false);
LoadUri(resourceUri);
}
}
}
/*
* @ return load contents of URI into WebView,
* implementation is android version dependent
*/
protected abstract void LoadUri(Uri uri);
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event) ||
super.onTouchEvent(event);
}
GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
// if no book, nothing to do
if (mBook == null) {
return false;
}
// also ignore swipes that are vertical, too slow, or too short.
float deltaX = event2.getX() - event1.getX();
float deltaY = event2.getY() - event1.getY();
if ((Math.abs(deltaX) < Math.abs(deltaY))
|| (Math.abs(deltaX) < mFlingMinDistance)
|| (Math.abs(velocityX) < FLING_THRESHOLD_VELOCITY)) {
return false;
} else {
if (deltaX < 0) {
return changeChapter(mBook.nextResource(mCurrentResourceUri));
} else {
return changeChapter(mBook.previousResource(mCurrentResourceUri));
}
}
}
};
private boolean changeChapter(Uri resourceUri) {
if (resourceUri != null) {
loadChapter(resourceUri);
System.out.println(resourceUri+ " next chapter uri");
return true;
} else {
Utility.showToast(getContext(), R.string.end_of_book);
return false;
}
}
/*
* Store current view into bookmark
*/
public Bookmark getBookmark() {
if ((mBook != null) && (mCurrentResourceUri != null)) {
float contentHeight = (float)getContentHeight();
contentHeight = (contentHeight == 0.0f) ? 0.0f : getScrollY() / contentHeight;
return new Bookmark (
mBook.getFileName(),
mCurrentResourceUri,
contentHeight
);
}
return null;
}
public void gotoBookmark(Bookmark bookmark) {
if (!bookmark.isEmpty()) {
mScrollY = bookmark.mScrollY;
// get notify when content height is available, for setting Y scroll position
mScrollWhenPageLoaded = true;
setBook(bookmark.getFileName());
loadChapter(bookmark.getResourceUri());
}
}
public void speak(TextToSpeechWrapper ttsWrapper) {
mTtsWrapper = ttsWrapper;
if ((mBook != null) && (mCurrentResourceUri != null)) {
mText = new ArrayList<String>();
XmlUtil.parseXmlResource(
mBook.fetch(mCurrentResourceUri).getData(),
new XhtmlToText(mText), null);
mTextLine = 0;
mTtsWrapper.setOnUtteranceCompletedListener(mCompletedListener);
if (0 < mText.size()) {
mTtsWrapper.speak(mText.get(0));
}
}
}
/*
* Send next piece of text to Text to speech
*/
private TextToSpeech.OnUtteranceCompletedListener mCompletedListener =
new TextToSpeech.OnUtteranceCompletedListener() {
@Override
public void onUtteranceCompleted(String utteranceId) {
if (mTextLine < mText.size() - 1) {
mTtsWrapper.speak(mText.get(++mTextLine));
}
}
};
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mFlingMinDistance = w / 2;
}
/*
* Called when page is loaded,
* if we're scrolling to a bookmark, we need to set the
* page size listener here. Otherwise it can be called
* for pages other than the one we're interested in
*/
@SuppressWarnings("deprecation")
protected void onPageLoaded() {
if (mScrollWhenPageLoaded) {
setPictureListener(mPictureListener);
mScrollWhenPageLoaded = false;
}
}
/*
* Need to wait until view has figured out how big web page is
* Otherwise, we can't scroll to last position because
* getContentHeight() returns 0.
* At current time, there is no replacement for PictureListener
*/
@SuppressWarnings("deprecation")
private PictureListener mPictureListener = new PictureListener() {
@Override
@Deprecated
public void onNewPicture(WebView view, Picture picture) {
// stop listening
setPictureListener(null);
scrollTo(0, (int)(getContentHeight() * mScrollY));
mScrollY = 0.0f;
}
};
}
Error is showing where i have mentioned why is this happening? please help i am stuck here for two days
Upvotes: 0
Views: 88
Reputation: 18923
Change here.
Context ctx;
public EpubWebView(Context context) {
this(context, null);
this.ctx = context;
}
public EpubWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.ctx = context;
mGestureDetector = new GestureDetector(context, mGestureListener);
WebSettings settings = getSettings();
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
setWebViewClient(mWebViewClient = createWebViewClient());
}
Now change here
db = ctx.openOrCreateDatabase("epub.db", EpubWebView.this.MODE_PRIVATE,null);
db = ctx.openOrCreateDatabase("epub.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
Upvotes: 1