Skip to content

Commit fcbd89c

Browse files
committed
Merge branch 'master' of github.com:codetoart/FolioReader-Android
2 parents e57c544 + 5181cf3 commit fcbd89c

File tree

14 files changed

+372
-209
lines changed

14 files changed

+372
-209
lines changed

.travis.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
language: android
22

3+
before_install:
4+
- echo yes | android update sdk --filter extra-android-support --no-ui --force > /dev/null
5+
- echo yes | android update sdk --filter extra-android-m2repository --no-ui --force > /dev/null
6+
37
android:
48
components:
5-
- tools
6-
- build-tools-27.0.0
79
- build-tools-26.0.2
8-
- android-27
10+
- android-26
911
- android-19
1012
- extra-android-m2repository
1113

12-
script:
13-
./gradlew checkstyle build
14+
licenses:
15+
- android-sdk-license-.+
16+
17+
script:
18+
./gradlew checkstyle build

folioreader/src/main/assets/js/Bridge.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -574,32 +574,57 @@ function isElementVisible(element, isHorizontal) {
574574
return rect.top > 0;
575575
}
576576

577-
function getFirstVisibleSpanIndex(isHorizontal) {
577+
/**
578+
Gets the first visible span from the displayed chapter and if it has id then usingId is true with
579+
value as span id else usingId is false with value as span index. usingId and value is forwarded to
580+
FolioPageFragment#storeFirstVisibleSpan(boolean, String) JavascriptInterface.
581+
582+
@param {boolean} isHorizontal - scrolling type of DirectionalViewpager#mDirection
583+
*/
584+
function getFirstVisibleSpan(isHorizontal) {
578585

579-
//Can be more specific with document.querySelectorAll('span.sentence')
580586
var spanCollection = document.getElementsByTagName("span");
581-
var json = {usingId: false, value: 0};
582587

583-
for (var i = 0 ; i < spanCollection.length ; i++) {
588+
if (spanCollection.length == 0) {
589+
FolioPageFragment.storeFirstVisibleSpan(false, 0);
590+
return;
591+
}
584592

585-
if (isElementVisible(spanCollection[i], isHorizontal)) {
593+
var spanIndex = 0;
594+
var spanElement;
586595

587-
json.value = i;
596+
for (var i = 0 ; i < spanCollection.length ; i++) {
597+
if (isElementVisible(spanCollection[i], isHorizontal)) {
598+
spanIndex = i;
599+
spanElement = spanCollection[i];
588600
break;
589601
}
590602
}
591603

592-
FolioPageFragment.storeFirstVisibleSpanIndex(JSON.stringify(json));
604+
var usingId = spanElement.id ? true : false;
605+
var value = usingId ? spanElement.id : spanIndex;
606+
FolioPageFragment.storeFirstVisibleSpan(usingId, value);
593607
}
594608

595-
function scrollToSpanIndex(index) {
609+
/**
610+
Scrolls the web page to particular span using id or index
596611
597-
var spanCollection = document.getElementsByTagName("span");
612+
@param {boolean} usingId - if span tag has id then true or else false
613+
@param {number} value - if usingId true then span id else span index
614+
*/
615+
function scrollToSpan(usingId, value) {
598616

599-
if (spanCollection.length == 0 || index < 0 || index >= spanCollection.length)
600-
return;
601-
602-
goToEl(spanCollection[index]);
617+
if (usingId) {
618+
var spanElement = document.getElementById(value);
619+
if (spanElement)
620+
goToEl(spanElement);
621+
} else {
622+
var spanCollection = document.getElementsByTagName("span");
623+
if (spanCollection.length == 0 || value < 0 || value >= spanCollection.length
624+
|| value == null)
625+
return;
626+
goToEl(spanCollection[value]);
627+
}
603628
}
604629

605630
// Class based onClick listener

folioreader/src/main/java/com/folioreader/Constants.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ public class Constants {
1111
public static final String CHAPTER_SELECTED = "chapter_selected";
1212
public static final String HIGHLIGHT_SELECTED = "highlight_selected";
1313
public static final String BOOK_TITLE = "book_title";
14-
public static final String BOOK_ID = "book_id";
15-
public static final String LAST_READ_SPAN_INDEX = "last_read_span_index";
16-
public static final String LAST_READ_CHAPTER_INDEX = "last_read_chapter_index";
17-
public static final String LAST_READ_STATE = "_last_read_state";
1814
public static final int PORT_NUMBER = 8080;
1915
public static final String LOCALHOST = "http://127.0.0.1:" + PORT_NUMBER + "/";
2016
public static final String SELECTED_WORD = "selected_word";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.folioreader.model;
2+
3+
/**
4+
* Created by Hrishikesh Kadam on 20/04/2018.
5+
*/
6+
public interface ReadPosition {
7+
8+
String getBookId();
9+
10+
int getChapterIndex();
11+
12+
String getChapterHref();
13+
14+
boolean isUsingId();
15+
16+
String getValue();
17+
18+
String toJson();
19+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.folioreader.model;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
import android.util.Log;
6+
7+
import com.fasterxml.jackson.core.JsonProcessingException;
8+
import com.fasterxml.jackson.databind.ObjectWriter;
9+
import com.folioreader.util.ObjectMapperSingleton;
10+
11+
/**
12+
* Created by Hrishikesh Kadam on 20/04/2018.
13+
*/
14+
public class ReadPositionImpl implements ReadPosition, Parcelable {
15+
16+
public static final Creator<ReadPositionImpl> CREATOR = new Creator<ReadPositionImpl>() {
17+
@Override
18+
public ReadPositionImpl createFromParcel(Parcel in) {
19+
return new ReadPositionImpl(in);
20+
}
21+
22+
@Override
23+
public ReadPositionImpl[] newArray(int size) {
24+
return new ReadPositionImpl[size];
25+
}
26+
};
27+
28+
private static final String LOG_TAG = ReadPositionImpl.class.getSimpleName();
29+
private String bookId;
30+
private int chapterIndex = -1;
31+
private String chapterHref;
32+
private boolean usingId;
33+
private String value;
34+
35+
public ReadPositionImpl() {
36+
}
37+
38+
public ReadPositionImpl(String bookId, int chapterIndex, String chapterHref, boolean usingId, String value) {
39+
this.bookId = bookId;
40+
this.chapterIndex = chapterIndex;
41+
this.chapterHref = chapterHref;
42+
this.usingId = usingId;
43+
this.value = value;
44+
}
45+
46+
protected ReadPositionImpl(Parcel in) {
47+
bookId = in.readString();
48+
chapterIndex = in.readInt();
49+
chapterHref = in.readString();
50+
usingId = in.readByte() != 0;
51+
value = in.readString();
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "ReadPositionImpl{" +
57+
"bookId='" + bookId + '\'' +
58+
", chapterIndex=" + chapterIndex +
59+
", chapterHref='" + chapterHref + '\'' +
60+
", usingId=" + usingId +
61+
", value='" + value + '\'' +
62+
'}';
63+
}
64+
65+
@Override
66+
public String getBookId() {
67+
return bookId;
68+
}
69+
70+
public void setBookId(String bookId) {
71+
this.bookId = bookId;
72+
}
73+
74+
@Override
75+
public int getChapterIndex() {
76+
return chapterIndex;
77+
}
78+
79+
public void setChapterIndex(int chapterIndex) {
80+
this.chapterIndex = chapterIndex;
81+
}
82+
83+
@Override
84+
public String getChapterHref() {
85+
return chapterHref;
86+
}
87+
88+
public void setChapterHref(String chapterHref) {
89+
this.chapterHref = chapterHref;
90+
}
91+
92+
@Override
93+
public boolean isUsingId() {
94+
return usingId;
95+
}
96+
97+
public void setUsingId(boolean usingId) {
98+
this.usingId = usingId;
99+
}
100+
101+
@Override
102+
public String getValue() {
103+
return value;
104+
}
105+
106+
public void setValue(String value) {
107+
this.value = value;
108+
}
109+
110+
@Override
111+
public String toJson() {
112+
try {
113+
ObjectWriter objectWriter = ObjectMapperSingleton.getObjectMapper().writer();
114+
return objectWriter.writeValueAsString(this);
115+
} catch (JsonProcessingException e) {
116+
Log.e(LOG_TAG, "-> " + e);
117+
return null;
118+
}
119+
}
120+
121+
@Override
122+
public int describeContents() {
123+
return 0;
124+
}
125+
126+
@Override
127+
public void writeToParcel(Parcel dest, int flags) {
128+
129+
dest.writeString(bookId);
130+
dest.writeInt(chapterIndex);
131+
dest.writeString(chapterHref);
132+
dest.writeByte((byte) (usingId ? 1 : 0));
133+
dest.writeString(value);
134+
}
135+
}

folioreader/src/main/java/com/folioreader/ui/folio/activity/FolioActivity.java

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import android.support.annotation.NonNull;
2626
import android.support.v4.app.ActivityCompat;
2727
import android.support.v4.content.ContextCompat;
28-
import android.support.v4.content.LocalBroadcastManager;
2928
import android.support.v7.app.AppCompatActivity;
3029
import android.support.v7.widget.Toolbar;
3130
import android.text.Html;
32-
import android.text.TextUtils;
3331
import android.util.Log;
3432
import android.view.View;
3533
import android.view.animation.AccelerateInterpolator;
@@ -46,6 +44,7 @@
4644
import com.folioreader.Constants;
4745
import com.folioreader.R;
4846
import com.folioreader.model.HighlightImpl;
47+
import com.folioreader.model.ReadPosition;
4948
import com.folioreader.model.event.AnchorIdEvent;
5049
import com.folioreader.model.event.MediaOverlayHighlightStyleEvent;
5150
import com.folioreader.model.event.MediaOverlayPlayPauseEvent;
@@ -93,9 +92,7 @@ public class FolioActivity
9392
public static final String INTENT_EPUB_SOURCE_PATH = "com.folioreader.epub_asset_path";
9493
public static final String INTENT_EPUB_SOURCE_TYPE = "epub_source_type";
9594
public static final String INTENT_HIGHLIGHTS_LIST = "highlight_list";
96-
public static final String EXTRA_LAST_READ_CHAPTER_INDEX = "com.folioreader.extra_last_read_chapter_position";
97-
public static final String EXTRA_LAST_READ_SPAN_INDEX = "com.folioreader.extra_last_read_span_index";
98-
public static final String ACTION_SAVE_LAST_READ_STATE = "save_last_read_state";
95+
public static final String EXTRA_READ_POSITION = "com.folioreader.extra.READ_POSITION";
9996

10097
public enum EpubSourceType {
10198
RAW,
@@ -115,7 +112,7 @@ public enum EpubSourceType {
115112

116113
private int mChapterPosition;
117114
private FolioPageFragmentAdapter mFolioPageFragmentAdapter;
118-
private String lastReadSpanIndex;
115+
private ReadPosition entryReadPosition;
119116
private ConfigBottomSheetDialogFragment mConfigBottomSheetDialogFragment;
120117
private TextView title;
121118

@@ -284,17 +281,28 @@ public void onPageScrollStateChanged(int state) {
284281
if (mSpineReferenceList != null) {
285282
mFolioPageFragmentAdapter = new FolioPageFragmentAdapter(getSupportFragmentManager(), mSpineReferenceList, bookFileName, mBookId);
286283
mFolioPageViewPager.setAdapter(mFolioPageFragmentAdapter);
284+
285+
entryReadPosition = getIntent().getParcelableExtra(FolioActivity.EXTRA_READ_POSITION);
286+
if (entryReadPosition == null ||
287+
(entryReadPosition.getChapterIndex() == -1 &&
288+
entryReadPosition.getChapterHref() == null)) {
289+
mFolioPageViewPager.setCurrentItem(0);
290+
} else if (entryReadPosition.getChapterIndex() != -1) {
291+
mFolioPageViewPager.setCurrentItem(entryReadPosition.getChapterIndex());
292+
} else {
293+
mFolioPageViewPager.setCurrentItem(
294+
getChapterIndex(entryReadPosition.getChapterHref()));
295+
}
287296
}
297+
}
288298

289-
int lastReadChapterIndex =
290-
getIntent().getIntExtra(FolioActivity.EXTRA_LAST_READ_CHAPTER_INDEX, 0);
291-
String lastReadSpanIndex =
292-
getIntent().getStringExtra(FolioActivity.EXTRA_LAST_READ_SPAN_INDEX);
293-
if (TextUtils.isEmpty(lastReadSpanIndex))
294-
lastReadSpanIndex = "{\"usingId\":false,\"value\":0}";
295-
mFolioPageViewPager.setCurrentItem(lastReadChapterIndex);
296-
AppUtil.saveLastReadState(
297-
getApplicationContext(), mBookId, lastReadChapterIndex, lastReadSpanIndex);
299+
private int getChapterIndex(String chapterHref) {
300+
301+
for (int i = 0; i < mSpineReferenceList.size(); i++) {
302+
if (mSpineReferenceList.get(i).getHref().equals(chapterHref))
303+
return i;
304+
}
305+
return 0;
298306
}
299307

300308
private void configDrawerLayoutButtons() {
@@ -335,36 +343,14 @@ public void setPagerToPosition(String href) {
335343
}
336344

337345
@Override
338-
public void setLastReadSpanIndex(String json) {
339-
lastReadSpanIndex = json;
340-
}
341-
342-
@Override
343-
protected void onStop() {
344-
super.onStop();
345-
346-
// Activity is waiting to get lastReadSpanIndex stored from
347-
// Bridge -> FolioPageFragment -> FolioActivity to avoid any race condition.
348-
// This delay goes unnoticed as it is onStop() and not in onPause()
349-
try {
350-
Thread.sleep(500);
351-
} catch (InterruptedException e) {
352-
e.printStackTrace();
353-
}
354-
355-
saveLastReadState();
356-
}
357-
358-
private void saveLastReadState() {
359-
360-
if (mSpineReferenceList.size() > 0) {
346+
public ReadPosition getEntryReadPosition() {
361347

362-
Intent intent = new Intent(FolioActivity.ACTION_SAVE_LAST_READ_STATE);
363-
intent.putExtra(FolioActivity.EXTRA_LAST_READ_CHAPTER_INDEX,
364-
mFolioPageViewPager.getCurrentItem());
365-
intent.putExtra(FolioActivity.EXTRA_LAST_READ_SPAN_INDEX, lastReadSpanIndex);
366-
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
348+
if (entryReadPosition != null) {
349+
ReadPosition tempReadPosition = entryReadPosition;
350+
entryReadPosition = null;
351+
return tempReadPosition;
367352
}
353+
return null;
368354
}
369355

370356
@Override

0 commit comments

Comments
 (0)