This repository was archived by the owner on May 24, 2022. It is now read-only.
forked from skullkey/FBReaderJ
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathFBReaderApp.java
More file actions
534 lines (464 loc) · 16.2 KB
/
Copy pathFBReaderApp.java
File metadata and controls
534 lines (464 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* Copyright (C) 2007-2012 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.fbreader;
import android.app.Activity;
import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase;
import org.geometerplus.android.util.UIUtil;
import org.geometerplus.fbreader.bookmodel.BookModel;
import org.geometerplus.fbreader.bookmodel.TOCTree;
import org.geometerplus.fbreader.library.Author;
import org.geometerplus.fbreader.library.Book;
import org.geometerplus.fbreader.library.Bookmark;
import org.geometerplus.fbreader.library.Library;
import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.core.application.ZLKeyBindings;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.options.ZLBooleanOption;
import org.geometerplus.zlibrary.core.options.ZLColorOption;
import org.geometerplus.zlibrary.core.options.ZLEnumOption;
import org.geometerplus.zlibrary.core.options.ZLIntegerRangeOption;
import org.geometerplus.zlibrary.core.options.ZLStringOption;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.util.ZLColor;
import org.geometerplus.zlibrary.text.hyphenation.ZLTextHyphenator;
import org.geometerplus.zlibrary.text.view.ZLTextWordCursor;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public final class FBReaderApp extends ZLApplication {
public final ZLBooleanOption AllowScreenBrightnessAdjustmentOption =
new ZLBooleanOption("LookNFeel", "AllowScreenBrightnessAdjustment", true);
public final ZLStringOption TextSearchPatternOption =
new ZLStringOption("TextSearch", "Pattern", "");
public final ZLBooleanOption UseSeparateBindingsOption =
new ZLBooleanOption("KeysOptions", "UseSeparateBindings", false);
public final ZLBooleanOption EnableDoubleTapOption =
new ZLBooleanOption("Options", "EnableDoubleTap", true);
public final ZLBooleanOption NavigateAllWordsOption =
new ZLBooleanOption("Options", "NavigateAllWords", false);
public static enum WordTappingAction {
doNothing, selectSingleWord, startSelecting, openDictionary
}
public final ZLEnumOption<WordTappingAction> WordTappingActionOption =
new ZLEnumOption<WordTappingAction>("Options", "WordTappingAction", WordTappingAction.startSelecting);
// Subscription feature settings
public static enum AutomaticDownloadType {
downloadAll, downloadMostRecent
}
public final ZLColorOption ImageViewBackgroundOption =
new ZLColorOption("Colors", "ImageViewBackground", new ZLColor(255, 255, 255));
public static enum ImageTappingAction {
doNothing, selectImage, openImageView
}
public final ZLEnumOption<ImageTappingAction> ImageTappingActionOption =
new ZLEnumOption<ImageTappingAction>("Options", "ImageTappingAction", ImageTappingAction.openImageView);
private final int myDpi = ZLibrary.Instance().getDisplayDPI();
public final ZLIntegerRangeOption LeftMarginOption =
new ZLIntegerRangeOption("Options", "LeftMargin", 0, 120, myDpi / 20);
public final ZLIntegerRangeOption RightMarginOption =
new ZLIntegerRangeOption("Options", "RightMargin", 0, 120, myDpi / 20);
public final ZLIntegerRangeOption TopMarginOption =
new ZLIntegerRangeOption("Options", "TopMargin", 0, 120, 0);
public final ZLIntegerRangeOption BottomMarginOption =
new ZLIntegerRangeOption("Options", "BottomMargin", 0, 120, 4);
public final ZLIntegerRangeOption ScrollbarTypeOption =
new ZLIntegerRangeOption("Options", "ScrollbarType", 0, 3, FBView.SCROLLBAR_SHOW_AS_FOOTER);
public final ZLIntegerRangeOption FooterHeightOption =
new ZLIntegerRangeOption("Options", "FooterHeight", 8, 20, 9);
public final ZLBooleanOption FooterShowTOCMarksOption =
new ZLBooleanOption("Options", "FooterShowTOCMarks", true);
public final ZLBooleanOption FooterShowClockOption =
new ZLBooleanOption("Options", "ShowClockInFooter", true);
public final ZLBooleanOption FooterShowBatteryOption =
new ZLBooleanOption("Options", "ShowBatteryInFooter", true);
public final ZLBooleanOption FooterShowProgressOption =
new ZLBooleanOption("Options", "ShowProgressInFooter", true);
public final ZLStringOption FooterFontOption =
new ZLStringOption("Options", "FooterFont", "Droid Sans");
final ZLStringOption ColorProfileOption =
new ZLStringOption("Options", "ColorProfile", ColorProfile.DAY);
public final ZLBooleanOption ShowPreviousBookInCancelMenuOption =
new ZLBooleanOption("CancelMenu", "previousBook", false);
public final ZLBooleanOption ShowPositionsInCancelMenuOption =
new ZLBooleanOption("CancelMenu", "positions", true);
public final ZLBooleanOption NavigateBySentenceOption =
new ZLBooleanOption("Options", "NavigateBySentence", false);
private final ZLKeyBindings myBindings = new ZLKeyBindings("Keys");
public final FBView BookTextView;
public final FBView FootnoteView;
public volatile BookModel Model;
private final String myArg0;
public FBReaderApp(String arg) {
super();
myArg0 = arg;
addAction(ActionCode.INCREASE_FONT, new ChangeFontSizeAction(this, +1));
addAction(ActionCode.DECREASE_FONT, new ChangeFontSizeAction(this, -1));
addAction(ActionCode.FIND_NEXT, new FindNextAction(this));
addAction(ActionCode.FIND_PREVIOUS, new FindPreviousAction(this));
addAction(ActionCode.CLEAR_FIND_RESULTS, new ClearFindResultsAction(this));
addAction(ActionCode.SELECTION_CLEAR, new SelectionClearAction(this));
addAction(ActionCode.TURN_PAGE_FORWARD, new TurnPageAction(this, true));
addAction(ActionCode.TURN_PAGE_BACK, new TurnPageAction(this, false));
addAction(ActionCode.MOVE_CURSOR_UP, new MoveCursorAction(this, FBView.Direction.up));
addAction(ActionCode.MOVE_CURSOR_DOWN, new MoveCursorAction(this, FBView.Direction.down));
addAction(ActionCode.MOVE_CURSOR_LEFT, new MoveCursorAction(this, FBView.Direction.rightToLeft));
addAction(ActionCode.MOVE_CURSOR_RIGHT, new MoveCursorAction(this, FBView.Direction.leftToRight));
addAction(ActionCode.VOLUME_KEY_SCROLL_FORWARD, new VolumeKeyTurnPageAction(this, true));
addAction(ActionCode.VOLUME_KEY_SCROLL_BACK, new VolumeKeyTurnPageAction(this, false));
addAction(ActionCode.SWITCH_TO_DAY_PROFILE, new SwitchProfileAction(this, ColorProfile.DAY));
addAction(ActionCode.SWITCH_TO_NIGHT_PROFILE, new SwitchProfileAction(this, ColorProfile.NIGHT));
addAction(ActionCode.EXIT, new ExitAction(this));
BookTextView = new FBView(this);
FootnoteView = new FBView(this);
setView(BookTextView);
}
public void initWindow() {
super.initWindow();
wait("loadingBook", new Runnable() {
public void run() {
Book book = createBookForFile(ZLFile.createFileByPath(myArg0));
if (book == null) {
book = Library.getRecentBook();
}
if ((book == null) || !book.File.exists()) {
book = Book.getByFile(Library.getHelpFile());
}
openBookInternal(book, null);
}
});
}
public void openBook(final Book book, final Bookmark bookmark) {
if (book == null) {
return;
}
if (Model != null) {
if (bookmark == null & book.File.getPath().equals(Model.Book.File.getPath())) {
return;
}
}
wait("loadingBook", new Runnable() {
public void run() {
openBookInternal(book, bookmark);
}
});
}
public void openBook(final Book book, final Bookmark bookmark, final Activity activity) {
if (book == null) {
return;
}
if (Model != null) {
if (bookmark == null & book.File.getPath().equals(Model.Book.File.getPath())) {
return;
}
}
if (activity != null) {
UIUtil.wait("loadingBook", new Runnable() {
@Override
public void run() {
openBookInternal(book, bookmark);
activity.finish();
}
}, activity);
}
}
private ColorProfile myColorProfile;
public ColorProfile getColorProfile() {
if (myColorProfile == null) {
myColorProfile = ColorProfile.get(getColorProfileName());
}
return myColorProfile;
}
public String getColorProfileName() {
return ColorProfileOption.getValue();
}
public void setColorProfileName(String name) {
ColorProfileOption.setValue(name);
myColorProfile = null;
}
public ZLKeyBindings keyBindings() {
return myBindings;
}
public FBView getTextView() {
return (FBView)getCurrentView();
}
public void tryOpenFootnote(String id) {
if (Model != null) {
BookModel.Label label = Model.getLabel(id);
if (label != null) {
addInvisibleBookmark();
if (label.ModelId == null) {
BookTextView.gotoPosition(label.ParagraphIndex, 0, 0);
} else {
FootnoteView.setModel(Model.getFootnoteModel(label.ModelId));
setView(FootnoteView);
FootnoteView.gotoPosition(label.ParagraphIndex, 0, 0);
}
getViewWidget().repaint();
}
}
}
public void clearTextCaches() {
BookTextView.clearCaches();
FootnoteView.clearCaches();
}
synchronized void openBookInternal(Book book, Bookmark bookmark) {
if (book != null) {
onViewChanged();
if (Model != null) {
Model.Book.storePosition(BookTextView.getStartCursor());
}
BookTextView.setModel(null);
FootnoteView.setModel(null);
clearTextCaches();
Model = null;
System.gc();
System.gc();
Model = BookModel.createModel(book);
if (Model != null) {
ZLTextHyphenator.Instance().load(book.getLanguage());
BookTextView.setModel(Model.BookTextModel);
BookTextView.gotoPosition(book.getStoredPosition());
if (bookmark == null) {
setView(BookTextView);
} else {
gotoBookmark(bookmark);
}
Library.addBookToRecentList(book);
final StringBuilder title = new StringBuilder(book.getTitle());
if (!book.authors().isEmpty()) {
boolean first = true;
for (Author a : book.authors()) {
title.append(first ? " (" : ", ");
title.append(a.DisplayName);
first = false;
}
title.append(")");
}
setTitle(title.toString());
}
}
getViewWidget().repaint();
SQLiteBooksDatabase database = (SQLiteBooksDatabase) SQLiteBooksDatabase.Instance();
try {
database.updateBookStatus(book);
} catch (Exception e) {
e.printStackTrace();
}
}
public void gotoBookmark(Bookmark bookmark) {
addInvisibleBookmark();
final String modelId = bookmark.ModelId;
if (modelId == null) {
BookTextView.gotoPosition(bookmark);
setView(BookTextView);
} else {
FootnoteView.setModel(Model.getFootnoteModel(modelId));
FootnoteView.gotoPosition(bookmark);
setView(FootnoteView);
}
getViewWidget().repaint();
}
public void showBookTextView() {
setView(BookTextView);
}
private Book createBookForFile(ZLFile file) {
if (file == null) {
return null;
}
Book book = Book.getByFile(file);
if (book != null) {
book.insertIntoBookList();
return book;
}
if (file.isArchive()) {
for (ZLFile child : file.children()) {
book = Book.getByFile(child);
if (book != null) {
book.insertIntoBookList();
return book;
}
}
}
return null;
}
@Override
public void openFile(ZLFile file) {
final Book book = createBookForFile(file);
if (book != null) {
openBook(book, null);
}
}
public void onWindowClosing() {
if (Model != null && BookTextView != null) {
Model.Book.storePosition(BookTextView.getStartCursor());
}
}
static enum CancelActionType {
previousBook,
returnTo,
close
}
public static class CancelActionDescription {
final CancelActionType Type;
public final String Title;
public final String Summary;
CancelActionDescription(CancelActionType type, String summary) {
final ZLResource resource = ZLResource.resource("cancelMenu");
Type = type;
Title = resource.getResource(type.toString()).getValue();
Summary = summary;
}
}
private static class BookmarkDescription extends CancelActionDescription {
final Bookmark Bookmark;
BookmarkDescription(Bookmark b) {
super(CancelActionType.returnTo, b.getText());
Bookmark = b;
}
}
private final ArrayList<CancelActionDescription> myCancelActionsList =
new ArrayList<CancelActionDescription>();
public List<CancelActionDescription> getCancelActionsList() {
myCancelActionsList.clear();
if (ShowPreviousBookInCancelMenuOption.getValue()) {
final Book previousBook = Library.getPreviousBook();
if (previousBook != null) {
myCancelActionsList.add(new CancelActionDescription(
CancelActionType.previousBook, previousBook.getTitle()
));
}
}
if (ShowPositionsInCancelMenuOption.getValue()) {
if (Model != null && Model.Book != null) {
for (Bookmark bookmark : Bookmark.invisibleBookmarks(Model.Book)) {
myCancelActionsList.add(new BookmarkDescription(bookmark));
}
}
}
myCancelActionsList.add(new CancelActionDescription(
CancelActionType.close, null
));
return myCancelActionsList;
}
public void runCancelAction(int index) {
if (index < 0 || index >= myCancelActionsList.size()) {
return;
}
final CancelActionDescription description = myCancelActionsList.get(index);
switch (description.Type) {
case previousBook:
openBook(Library.getPreviousBook(), null);
break;
case returnTo:
{
final Bookmark b = ((BookmarkDescription)description).Bookmark;
b.delete();
gotoBookmark(b);
break;
}
case close:
closeWindow();
break;
}
}
private synchronized void updateInvisibleBookmarksList(Bookmark b) {
if (Model != null && Model.Book != null && b != null) {
for (Bookmark bm : Bookmark.invisibleBookmarks(Model.Book)) {
if (b.equals(bm)) {
bm.delete();
}
}
b.save();
final List<Bookmark> bookmarks = Bookmark.invisibleBookmarks(Model.Book);
for (int i = 3; i < bookmarks.size(); ++i) {
bookmarks.get(i).delete();
}
}
}
public void addInvisibleBookmark(ZLTextWordCursor cursor) {
if (cursor != null && Model != null && Model.Book != null && getTextView() == BookTextView) {
updateInvisibleBookmarksList(new Bookmark(
Model.Book,
getTextView().getModel().getId(),
cursor,
6,
false
));
}
}
public void addInvisibleBookmark() {
if (Model.Book != null && getTextView() == BookTextView) {
updateInvisibleBookmarksList(addBookmark(6, false));
}
}
public Bookmark addBookmark(int maxLength, boolean visible) {
final FBView view = getTextView();
final ZLTextWordCursor cursor = view.getStartCursor();
if (cursor.isNull()) {
return null;
}
return new Bookmark(
Model.Book,
view.getModel().getId(),
cursor,
maxLength,
visible
);
}
public TOCTree getCurrentTOCElement() {
final ZLTextWordCursor cursor = BookTextView.getStartCursor();
if (Model == null || cursor == null) {
return null;
}
int index = cursor.getParagraphIndex();
if (cursor.isEndOfParagraph()) {
++index;
}
TOCTree treeToSelect = null;
for (TOCTree tree : Model.TOCTree) {
final TOCTree.Reference reference = tree.getReference();
if (reference == null) {
continue;
}
if (reference.ParagraphIndex > index) {
break;
}
treeToSelect = tree;
}
return treeToSelect;
}
public LinkedHashMap<String, Integer> getDaisyPageMap() {
if (null != Model) {
return Model.getDaisyPageMap();
} else {
return new LinkedHashMap<String, Integer>();
}
}
public String getLastDaisyPage() {
if (null != Model) {
return Model.getLastDaisyPage();
} else {
return "";
}
}
public boolean isAllDaisyPagesIntegers() {
return Model.isAllDaisyPagesIntegers();
}
}