-
Notifications
You must be signed in to change notification settings - Fork 789
Expand file tree
/
Copy pathBookInfoActivity.java
More file actions
359 lines (307 loc) · 11.4 KB
/
Copy pathBookInfoActivity.java
File metadata and controls
359 lines (307 loc) · 11.4 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
/*
* Copyright (C) 2010-2014 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.android.fbreader.library;
import java.io.File;
import java.text.DateFormat;
import java.util.*;
import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.widget.*;
import org.geometerplus.zlibrary.core.filesystem.ZLPhysicalFile;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.image.ZLLoadableImage;
import org.geometerplus.zlibrary.core.language.Language;
import org.geometerplus.zlibrary.core.language.ZLLanguageUtil;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageManager;
import org.geometerplus.fbreader.book.*;
import org.geometerplus.fbreader.network.HtmlUtil;
import org.geometerplus.android.fbreader.*;
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
import org.geometerplus.android.fbreader.preferences.EditBookInfoActivity;
public class BookInfoActivity extends Activity implements IBookCollection.Listener {
private static final boolean ENABLE_EXTENDED_FILE_INFO = false;
public static final String FROM_READING_MODE_KEY = "fbreader.from.reading.mode";
private final ZLResource myResource = ZLResource.resource("bookInfo");
private Book myBook;
private boolean myDontReloadBook;
private final BookCollectionShadow myCollection = new BookCollectionShadow();
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
Thread.setDefaultUncaughtExceptionHandler(
new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this)
);
myDontReloadBook = getIntent().getBooleanExtra(FROM_READING_MODE_KEY, false);
myBook = bookByIntent(getIntent());
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.book_info);
}
@Override
protected void onStart() {
super.onStart();
OrientationUtil.setOrientation(this, getIntent());
if (myBook != null) {
// we do force language & encoding detection
myBook.getEncoding();
setupCover(myBook);
setupBookInfo(myBook);
setupAnnotation(myBook);
setupFileInfo(myBook);
}
setupButton(R.id.book_info_button_open, "openBook", new View.OnClickListener() {
public void onClick(View view) {
if (myDontReloadBook) {
finish();
} else {
FBReader.openBookActivity(BookInfoActivity.this, myBook, null);
}
}
});
setupButton(R.id.book_info_button_edit, "editInfo", new View.OnClickListener() {
public void onClick(View view) {
OrientationUtil.startActivity(
BookInfoActivity.this,
new Intent(getApplicationContext(), EditBookInfoActivity.class)
.putExtra(FBReader.BOOK_KEY, SerializerUtil.serialize(myBook))
);
}
});
setupButton(R.id.book_info_button_reload, "reloadInfo", new View.OnClickListener() {
public void onClick(View view) {
if (myBook != null) {
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... args) {
myBook.reloadInfoFromFile();
return null;
}
protected void onPostExecute(Void result) {
setupBookInfo(myBook);
myDontReloadBook = false;
myCollection.bindToService(BookInfoActivity.this, new Runnable() {
public void run() {
myCollection.saveBook(myBook);
}
});
}
}.execute();
}
}
});
final View root = findViewById(R.id.book_info_root);
root.invalidate();
root.requestLayout();
myCollection.bindToService(this, null);
myCollection.addListener(this);
}
@Override
protected void onNewIntent(Intent intent) {
OrientationUtil.setOrientation(this, intent);
}
@Override
protected void onDestroy() {
myCollection.removeListener(this);
myCollection.unbind();
super.onDestroy();
}
public static Intent intentByBook(Book book) {
return new Intent().putExtra(FBReader.BOOK_KEY, SerializerUtil.serialize(book));
}
public static Book bookByIntent(Intent intent) {
return intent != null ?
SerializerUtil.deserializeBook(intent.getStringExtra(FBReader.BOOK_KEY)) : null;
}
private Button findButton(int buttonId) {
return (Button)findViewById(buttonId);
}
private void setupButton(int buttonId, String resourceKey, View.OnClickListener listener) {
final ZLResource buttonResource = ZLResource.resource("dialog").getResource("button");
final Button button = findButton(buttonId);
button.setText(buttonResource.getResource(resourceKey).getValue());
button.setOnClickListener(listener);
}
private void setupInfoPair(int id, String key, CharSequence value) {
setupInfoPair(id, key, value, 0);
}
private void setupInfoPair(int id, String key, CharSequence value, int param) {
final LinearLayout layout = (LinearLayout)findViewById(id);
if (value == null || value.length() == 0) {
layout.setVisibility(View.GONE);
return;
}
layout.setVisibility(View.VISIBLE);
((TextView)layout.findViewById(R.id.book_info_key)).setText(myResource.getResource(key).getValue(param));
((TextView)layout.findViewById(R.id.book_info_value)).setText(value);
}
private void setupCover(Book book) {
final ImageView coverView = (ImageView)findViewById(R.id.book_cover);
final DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
final int maxHeight = metrics.heightPixels * 2 / 3;
final int maxWidth = maxHeight * 2 / 3;
coverView.setVisibility(View.GONE);
coverView.setImageDrawable(null);
new AsyncTask<Book, Void, Boolean>() {
Bitmap coverBitmap;
protected Boolean doInBackground(Book... args) {
Book book = args[0];
final ZLImage image = BookUtil.getCover(book);
if (image == null) {
return false;
}
if (image instanceof ZLLoadableImage) {
final ZLLoadableImage loadableImage = (ZLLoadableImage)image;
if (!loadableImage.isSynchronized()) {
loadableImage.synchronize();
}
}
final ZLAndroidImageData data =
((ZLAndroidImageManager)ZLAndroidImageManager.Instance()).getImageData(image);
if (data == null) {
return false;
}
coverBitmap = data.getBitmap(2 * maxWidth, 2 * maxHeight);
if (coverBitmap == null) {
return false;
}
return true;
}
protected void onPostExecute(Boolean result) {
if(result) {
coverView.setVisibility(View.VISIBLE);
coverView.getLayoutParams().width = maxWidth;
coverView.getLayoutParams().height = maxHeight;
coverView.setImageBitmap(coverBitmap);
}
}
}.execute(book);
}
private void setupBookInfo(Book book) {
((TextView)findViewById(R.id.book_info_title)).setText(myResource.getResource("bookInfo").getValue());
setupInfoPair(R.id.book_title, "title", book.getTitle());
final StringBuilder buffer = new StringBuilder();
final List<Author> authors = book.authors();
for (Author a : authors) {
if (buffer.length() > 0) {
buffer.append(", ");
}
buffer.append(a.DisplayName);
}
setupInfoPair(R.id.book_authors, "authors", buffer, authors.size());
final SeriesInfo series = book.getSeriesInfo();
setupInfoPair(R.id.book_series, "series", series == null ? null : series.Series.getTitle());
String seriesIndexString = null;
if (series != null && series.Index != null) {
seriesIndexString = series.Index.toPlainString();
}
setupInfoPair(R.id.book_series_index, "indexInSeries", seriesIndexString);
buffer.delete(0, buffer.length());
final HashSet<String> tagNames = new HashSet<String>();
for (Tag tag : book.tags()) {
if (!tagNames.contains(tag.Name)) {
if (buffer.length() > 0) {
buffer.append(", ");
}
buffer.append(tag.Name);
tagNames.add(tag.Name);
}
}
setupInfoPair(R.id.book_tags, "tags", buffer, tagNames.size());
String language = book.getLanguage();
if (!ZLLanguageUtil.languageCodes().contains(language)) {
language = Language.OTHER_CODE;
}
setupInfoPair(R.id.book_language, "language", new Language(language).Name);
}
private void setupAnnotation(Book book) {
final TextView titleView = (TextView)findViewById(R.id.book_info_annotation_title);
final TextView bodyView = (TextView)findViewById(R.id.book_info_annotation_body);
final String annotation = BookUtil.getAnnotation(book);
if (annotation == null) {
titleView.setVisibility(View.GONE);
bodyView.setVisibility(View.GONE);
} else {
titleView.setText(myResource.getResource("annotation").getValue());
bodyView.setText(HtmlUtil.getHtmlText(annotation));
bodyView.setMovementMethod(new LinkMovementMethod());
bodyView.setTextColor(ColorStateList.valueOf(bodyView.getTextColors().getDefaultColor()));
}
}
private void setupFileInfo(Book book) {
((TextView)findViewById(R.id.file_info_title)).setText(myResource.getResource("fileInfo").getValue());
setupInfoPair(R.id.file_name, "name", book.File.getPath());
if (ENABLE_EXTENDED_FILE_INFO) {
setupInfoPair(R.id.file_type, "type", book.File.getExtension());
final ZLPhysicalFile physFile = book.File.getPhysicalFile();
final File file = physFile == null ? null : physFile.javaFile();
if (file != null && file.exists() && file.isFile()) {
setupInfoPair(R.id.file_size, "size", formatSize(file.length()));
setupInfoPair(R.id.file_time, "time", formatDate(file.lastModified()));
} else {
setupInfoPair(R.id.file_size, "size", null);
setupInfoPair(R.id.file_time, "time", null);
}
} else {
setupInfoPair(R.id.file_type, "type", null);
setupInfoPair(R.id.file_size, "size", null);
setupInfoPair(R.id.file_time, "time", null);
}
}
private String formatSize(long size) {
if (size <= 0) {
return null;
}
final int kilo = 1024;
if (size < kilo) { // less than 1 kilobyte
return myResource.getResource("sizeInBytes").getValue((int)size).replaceAll("%s", String.valueOf(size));
}
final String value;
if (size < kilo * kilo) { // less than 1 megabyte
value = String.format("%.2f", ((float)size) / kilo);
} else {
value = String.valueOf(size / kilo);
}
return myResource.getResource("sizeInKiloBytes").getValue().replaceAll("%s", value);
}
private String formatDate(long date) {
if (date == 0) {
return null;
}
return DateFormat.getDateTimeInstance().format(new Date(date));
}
public void onBookEvent(BookEvent event, Book book) {
if (event == BookEvent.Updated && book.equals(myBook)) {
myBook.updateFrom(book);
setupBookInfo(book);
myDontReloadBook = false;
}
}
public void onBuildEvent(IBookCollection.Status status) {
}
}