1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 19:42:17 +02:00
FBReaderJ/src/org/geometerplus/android/fbreader/preferences/EditBookInfoActivity.java
Nikolay Pultsin 911c99fce8 repackaging
2011-07-17 12:28:55 +01:00

120 lines
3.5 KiB
Java

/*
* Copyright (C) 2009-2011 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.preferences;
import java.util.TreeSet;
import android.content.Context;
import android.content.Intent;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.language.ZLLanguageUtil;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.text.hyphenation.ZLTextHyphenator;
import org.geometerplus.fbreader.library.Book;
import org.geometerplus.android.fbreader.library.BookInfoActivity;
import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase;
class BookTitlePreference extends ZLStringPreference {
private final Book myBook;
BookTitlePreference(Context context, ZLResource rootResource, String resourceKey, Book book) {
super(context, rootResource, resourceKey);
myBook = book;
setValue(book.getTitle());
}
public void onAccept() {
myBook.setTitle(getValue());
}
}
class LanguagePreference extends ZLStringListPreference {
private final Book myBook;
LanguagePreference(Context context, ZLResource rootResource, String resourceKey, Book book) {
super(context, rootResource, resourceKey);
myBook = book;
final TreeSet<String> set = new TreeSet<String>(new ZLLanguageUtil.CodeComparator());
set.addAll(ZLTextHyphenator.Instance().languageCodes());
set.add(ZLLanguageUtil.OTHER_LANGUAGE_CODE);
final int size = set.size();
String[] codes = new String[size];
String[] names = new String[size];
int index = 0;
for (String code : set) {
codes[index] = code;
names[index] = ZLLanguageUtil.languageName(code);
++index;
}
setLists(codes, names);
String language = myBook.getLanguage();
if (language == null) {
language = ZLLanguageUtil.OTHER_LANGUAGE_CODE;
}
if (!setInitialValue(language)) {
setInitialValue(ZLLanguageUtil.OTHER_LANGUAGE_CODE);
}
}
public void onAccept() {
final String value = getValue();
myBook.setLanguage((value.length() != 0) ? value : null);
}
}
public class EditBookInfoActivity extends ZLPreferenceActivity {
private Book myBook;
public EditBookInfoActivity() {
super("BookInfo");
}
@Override
protected void init(Intent intent) {
if (SQLiteBooksDatabase.Instance() == null) {
new SQLiteBooksDatabase(this, "LIBRARY");
}
final String path = intent.getStringExtra(BookInfoActivity.CURRENT_BOOK_PATH_KEY);
final ZLFile file = ZLFile.createFileByPath(path);
myBook = Book.getByFile(file);
if (myBook == null) {
finish();
return;
}
addPreference(new BookTitlePreference(this, Resource, "title", myBook));
addPreference(new LanguagePreference(this, Resource, "language", myBook));
}
@Override
protected void onPause() {
super.onPause();
if (myBook != null) {
myBook.save();
}
}
}