1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-06 03:50:19 +02:00

more code is moved to BaseActivity; fb2.zip will be processed as a book in file manager

This commit is contained in:
Nikolay Pultsin 2010-12-17 00:48:10 +00:00
parent cdbd561bcc
commit 17d7a7b585
4 changed files with 157 additions and 118 deletions

View file

@ -5,11 +5,10 @@ VR:
DONE отнаследовать FManagerAdapter от BaseAdapter (вместо ArrayAdapter)
DONE (ReturnRes перестал существовать как класс - причина: измения в FManagerAdaper) убрать ReturnRes внутрь SmartFilter
* По long-tap на книжке -- меню как в LibraryTreeActivity
DONE По long-tap на книжке -- меню как в LibraryTreeActivity
DONE По short-tap на книжке -- делать то же, что в LibraryTreeActivity (пока -- открывать книгу)
DONE Bug: при нажатии на epub-file открывается архив; должна открываться книга
* Bug: При удалении кгини из архива, удаляется весь архив.
NP: просто у книг из архива, при условии, что в архиве больше одного файла (не важно, книги остальные, или нет) не должно быть возможности удаления
DONE Bug: При удалении кгини из архива, удаляется весь архив.
* Bug: Удаление текущей книги, все хорошо -файл удаляется физически, однако книга все равно подгружается при повторном включении программы
NP: при повторном включении? или все-таки при выходе из библиотеки? (fbreader-то продолжает работать, правда?
* Bug: y *.epub FormatPlugin не выдает катринку.

View file

@ -49,21 +49,26 @@ import org.geometerplus.android.fbreader.tree.ZLAndroidTree;
abstract class BaseActivity extends ListActivity {
public static final String SELECTED_BOOK_PATH_KEY = "SelectedBookPath";
/*private*/ static final int OPEN_BOOK_ITEM_ID = 0;
/*private*/ static final int ADD_TO_FAVORITES_ITEM_ID = 1;
/*private*/ static final int REMOVE_FROM_FAVORITES_ITEM_ID = 2;
/*private*/ static final int DELETE_BOOK_ITEM_ID = 3;
private static final int OPEN_BOOK_ITEM_ID = 0;
private static final int ADD_TO_FAVORITES_ITEM_ID = 1;
private static final int REMOVE_FROM_FAVORITES_ITEM_ID = 2;
private static final int DELETE_BOOK_ITEM_ID = 3;
protected static final int CHILD_LIST_REQUEST = 0;
protected static final int RESULT_DONT_INVALIDATE_VIEWS = 0;
protected static final int RESULT_DO_INVALIDATE_VIEWS = 1;
static Library LibraryInstance;
protected final ZLResource myResource = ZLResource.resource("libraryView");
/*private*/ String mySelectedBookPath;
protected String mySelectedBookPath;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
mySelectedBookPath = getIntent().getStringExtra(SELECTED_BOOK_PATH_KEY);
setResult(RESULT_DONT_INVALIDATE_VIEWS);
}
protected void openBook(Book book) {
@ -88,6 +93,15 @@ abstract class BaseActivity extends ListActivity {
}
}
protected View createView(View convertView, ViewGroup parent, String name, String summary) {
final View view = (convertView != null) ? convertView :
LayoutInflater.from(parent.getContext()).inflate(R.layout.library_tree_item, parent, false);
((TextView)view.findViewById(R.id.library_tree_item_name)).setText(name);
((TextView)view.findViewById(R.id.library_tree_item_childrenlist)).setText(summary);
return view;
}
private int myCoverWidth = -1;
private int myCoverHeight = -1;
private final Runnable myInvalidateViewsRunnable = new Runnable() {
@ -131,4 +145,55 @@ abstract class BaseActivity extends ListActivity {
}
return data != null ? data.getBitmap(2 * myCoverWidth, 2 * myCoverHeight) : null;
}
private class BookDeleter implements DialogInterface.OnClickListener {
private final Book myBook;
private final int myMode;
BookDeleter(Book book, int removeMode) {
myBook = book;
myMode = removeMode;
}
public void onClick(DialogInterface dialog, int which) {
deleteBook(myBook, myMode);
setResult(RESULT_DO_INVALIDATE_VIEWS);
}
}
private void tryToDeleteBook(Book book) {
final ZLResource dialogResource = ZLResource.resource("dialog");
final ZLResource buttonResource = dialogResource.getResource("button");
final ZLResource boxResource = dialogResource.getResource("deleteBookBox");
new AlertDialog.Builder(this)
.setTitle(book.getTitle())
.setMessage(boxResource.getResource("message").getValue())
.setIcon(0)
.setPositiveButton(buttonResource.getResource("yes").getValue(), new BookDeleter(book, Library.REMOVE_FROM_DISK))
.setNegativeButton(buttonResource.getResource("no").getValue(), null)
.create().show();
}
protected void deleteBook(Book book, int mode) {
LibraryInstance.removeBook(book, mode);
}
protected boolean onContextItemSelected(int itemId, Book book) {
switch (itemId) {
case OPEN_BOOK_ITEM_ID:
openBook(book);
return true;
case ADD_TO_FAVORITES_ITEM_ID:
LibraryInstance.addBookToFavorites(book);
return true;
case REMOVE_FROM_FAVORITES_ITEM_ID:
LibraryInstance.removeBookFromFavorites(book);
getListView().invalidateViews();
return true;
case DELETE_BOOK_ITEM_ID:
tryToDeleteBook(book);
return true;
}
return false;
}
}

View file

@ -31,7 +31,6 @@ import android.widget.*;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.image.ZLLoadableImage;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.ui.android.R;
@ -48,108 +47,122 @@ import org.geometerplus.android.util.UIUtil;
public final class FileManager extends BaseActivity {
public static String FILE_MANAGER_PATH = "FileManagerPath";
private String myPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FManagerAdapter adapter = new FManagerAdapter();
FileListAdapter adapter = new FileListAdapter();
setListAdapter(adapter);
final Bundle extras = getIntent().getExtras();
final String path = extras != null ? extras.getString(FILE_MANAGER_PATH) : null;
myPath = extras != null ? extras.getString(FILE_MANAGER_PATH) : null;
if (path == null) {
if (myPath == null) {
setTitle(myResource.getResource("fileTree").getValue());
addItem(Paths.BooksDirectoryOption().getValue(), "fileTreeLibrary");
addItem("/", "fileTreeRoot");
addItem(Environment.getExternalStorageDirectory().getPath(), "fileTreeCard");
} else {
setTitle(path);
final SmartFilter filter = new SmartFilter(ZLFile.createFileByPath(path));
new Thread(filter).start();
setTitle(myPath);
startUpdate();
}
getListView().setOnCreateContextMenuListener(adapter);
getListView().setTextFilterEnabled(true);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
runItem(((FManagerAdapter)getListAdapter()).getItem(position));
runItem(((FileListAdapter)getListAdapter()).getItem(position));
}
});
}
private void startUpdate() {
new Thread(
new SmartFilter(ZLFile.createFileByPath(myPath))
).start();
}
@Override
protected void onActivityResult(int requestCode, int returnCode, Intent intent) {
if (requestCode == CHILD_LIST_REQUEST && returnCode == RESULT_DO_INVALIDATE_VIEWS) {
if (myPath != null) {
((FileListAdapter)getListAdapter()).clear();
startUpdate();
}
getListView().invalidateViews();
setResult(RESULT_DO_INVALIDATE_VIEWS);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
final int position = ((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).position;
final FManagerAdapter adapter = (FManagerAdapter)getListAdapter();
final FileItem fileItem = adapter.getItem(position);
final FileItem fileItem = ((FileListAdapter)getListAdapter()).getItem(position);
final Book book = fileItem.getBook();
if (book != null) {
switch (item.getItemId()) {
case OPEN_BOOK_ITEM_ID:
openBook(book);
return true;
case ADD_TO_FAVORITES_ITEM_ID:
LibraryInstance.addBookToFavorites(book);
return true;
case REMOVE_FROM_FAVORITES_ITEM_ID:
LibraryInstance.removeBookFromFavorites(book);
getListView().invalidateViews();
return true;
case DELETE_BOOK_ITEM_ID:
// TODO: implemen
// TODO: if book is in favorites list do ((FManagerAdapter)getListAdapter())
adapter.remove(fileItem);
adapter.notifyDataSetChanged();
book.File.getPhysicalFile().delete();
return true;
}
return onContextItemSelected(item.getItemId(), book);
}
return super.onContextItemSelected(item);
}
@Override
protected void deleteBook(Book book, int mode) {
super.deleteBook(book, mode);
((FileListAdapter)getListAdapter()).deleteFile(book.File);
getListView().invalidateViews();
}
private void runItem(FileItem item) {
final ZLFile file = item.getFile();
final Book book = item.getBook();
if (book != null) {
openBook(book);
} else if (file.isDirectory() || file.isArchive()) {
startActivity(
startActivityForResult(
new Intent(this, FileManager.class)
.putExtra(SELECTED_BOOK_PATH_KEY, mySelectedBookPath)
.putExtra(FILE_MANAGER_PATH, file.getPath())
.putExtra(FILE_MANAGER_PATH, file.getPath()),
CHILD_LIST_REQUEST
);
}
}
private void addItem(String path, String resourceKey) {
final ZLResource resource = myResource.getResource(resourceKey);
((FManagerAdapter)getListAdapter()).add(new FileItem(
((FileListAdapter)getListAdapter()).add(new FileItem(
ZLFile.createFileByPath(path),
resource.getValue(),
resource.getResource("summary").getValue()
));
}
private final class FManagerAdapter extends BaseAdapter implements View.OnCreateContextMenuListener {
private List<FileItem> myItems = Collections.synchronizedList(new ArrayList<FileItem>());
private final class FileListAdapter extends BaseAdapter implements View.OnCreateContextMenuListener {
private List<FileItem> myItems = new ArrayList<FileItem>();
public FManagerAdapter() {
public synchronized void clear() {
myItems.clear();
}
public void add(FileItem item){
public synchronized void add(FileItem item){
myItems.add(item);
}
public void remove(FileItem item){
public synchronized void deleteFile(ZLFile file) {
for (FileItem item : myItems) {
if (file.equals(item.getFile())) {
myItems.remove(item);
break;
}
}
}
public int getCount() {
public synchronized int getCount() {
return myItems.size();
}
public FileItem getItem(int position) {
public synchronized FileItem getItem(int position) {
return myItems.get(position);
}
@ -166,16 +179,17 @@ public final class FileManager extends BaseActivity {
}
public View getView(int position, View convertView, ViewGroup parent) {
final FileItem item = myItems.get(position);
final View view = (convertView != null) ? convertView :
LayoutInflater.from(parent.getContext()).inflate(R.layout.library_tree_item, parent, false);
((TextView)view.findViewById(R.id.library_tree_item_name)).setText(item.getName());
((TextView)view.findViewById(R.id.library_tree_item_childrenlist)).setText(item.getSummary());
final FileItem item = getItem(position);
final View view = createView(convertView, parent, item.getName(), item.getSummary());
if (mySelectedBookPath != null &&
mySelectedBookPath.equals(item.getFile().getPath())) {
view.setBackgroundColor(0xff808080);
} else {
view.setBackgroundColor(0);
}
final ImageView coverView = getCoverView(view);
final Bitmap coverBitmap = getCoverBitmap(item.getCover());
if (coverBitmap != null) {
coverView.setImageBitmap(coverBitmap);
} else {
@ -186,7 +200,6 @@ public final class FileManager extends BaseActivity {
}
}
private final class FileItem {
private final ZLFile myFile;
private final String myName;
@ -204,7 +217,22 @@ public final class FileManager extends BaseActivity {
}
public FileItem(ZLFile file) {
this(file, null, null);
if (file.isArchive() && file.getPath().endsWith(".fb2.zip")) {
final List<ZLFile> children = file.children();
if (children.size() == 1) {
final ZLFile child = children.get(0);
if (child.getPath().endsWith(".fb2")) {
final String fileName = file.getName(false);
myFile = child;
myName = fileName.substring(fileName.lastIndexOf('/') + 1);
mySummary = null;
return;
}
}
}
myFile = file;
myName = null;
mySummary = null;
}
public String getName() {
@ -285,7 +313,7 @@ public final class FileManager extends BaseActivity {
if (file.isDirectory() ||
file.isArchive() ||
PluginCollection.Instance().getPlugin(file) != null) {
final FManagerAdapter adapter = (FManagerAdapter)getListAdapter();
final FileListAdapter adapter = (FileListAdapter)getListAdapter();
adapter.add(new FileItem(file));
// adapter.notifyDataSetChanged(); // TODO question!
runOnUiThread(new Runnable() {

View file

@ -56,16 +56,6 @@ abstract class LibraryBaseActivity extends BaseActivity {
static final ZLStringOption BookSearchPatternOption =
new ZLStringOption("BookSearch", "Pattern", "");
private static int CHILD_LIST_REQUEST = 0;
private static int RESULT_DONT_INVALIDATE_VIEWS = 0;
private static int RESULT_DO_INVALIDATE_VIEWS = 1;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setResult(RESULT_DONT_INVALIDATE_VIEWS);
}
@Override
protected void onActivityResult(int requestCode, int returnCode, Intent intent) {
if (requestCode == CHILD_LIST_REQUEST && returnCode == RESULT_DO_INVALIDATE_VIEWS) {
@ -128,9 +118,7 @@ abstract class LibraryBaseActivity extends BaseActivity {
public View getView(int position, View convertView, final ViewGroup parent) {
final FBTree tree = getItem(position);
final View view = (convertView != null) ? convertView :
LayoutInflater.from(parent.getContext()).inflate(R.layout.library_tree_item, parent, false);
final View view = createView(convertView, parent, tree.getName(), tree.getSecondString());
if (tree instanceof BookTree &&
mySelectedBookPath != null &&
mySelectedBookPath.equals(((BookTree)tree).Book.File.getPath())) {
@ -139,9 +127,6 @@ abstract class LibraryBaseActivity extends BaseActivity {
view.setBackgroundColor(0);
}
((TextView)view.findViewById(R.id.library_tree_item_name)).setText(tree.getName());
((TextView)view.findViewById(R.id.library_tree_item_childrenlist)).setText(tree.getSecondString());
final ImageView coverView = getCoverView(view);
if (tree instanceof ZLAndroidTree) {
@ -170,53 +155,15 @@ abstract class LibraryBaseActivity extends BaseActivity {
final int position = ((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).position;
final FBTree tree = ((LibraryAdapter)getListAdapter()).getItem(position);
if (tree instanceof BookTree) {
final Book book = ((BookTree)tree).Book;
switch (item.getItemId()) {
case OPEN_BOOK_ITEM_ID:
openBook(book);
return true;
case ADD_TO_FAVORITES_ITEM_ID:
LibraryInstance.addBookToFavorites(book);
return true;
case REMOVE_FROM_FAVORITES_ITEM_ID:
LibraryInstance.removeBookFromFavorites(book);
getListView().invalidateViews();
return true;
case DELETE_BOOK_ITEM_ID:
tryToDeleteBook(book);
return true;
}
return onContextItemSelected(item.getItemId(), ((BookTree)tree).Book);
}
return super.onContextItemSelected(item);
}
private class BookDeleter implements DialogInterface.OnClickListener {
private final Book myBook;
private final int myMode;
BookDeleter(Book book, int removeMode) {
myBook = book;
myMode = removeMode;
}
public void onClick(DialogInterface dialog, int which) {
LibraryInstance.removeBook(myBook, myMode);
@Override
protected void deleteBook(Book book, int mode) {
super.deleteBook(book, mode);
getListView().invalidateViews();
setResult(RESULT_DO_INVALIDATE_VIEWS);
}
}
private void tryToDeleteBook(Book book) {
final ZLResource dialogResource = ZLResource.resource("dialog");
final ZLResource buttonResource = dialogResource.getResource("button");
final ZLResource boxResource = dialogResource.getResource("deleteBookBox");
new AlertDialog.Builder(this)
.setTitle(book.getTitle())
.setMessage(boxResource.getResource("message").getValue())
.setIcon(0)
.setPositiveButton(buttonResource.getResource("yes").getValue(), new BookDeleter(book, Library.REMOVE_FROM_DISK))
.setNegativeButton(buttonResource.getResource("no").getValue(), null)
.create().show();
}
protected class OpenTreeRunnable implements Runnable {