mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-03 09:49:19 +02:00
loader code updated to be android-independent
This commit is contained in:
parent
f506acdfb2
commit
c49eec0e0d
17 changed files with 123 additions and 120 deletions
|
@ -23,6 +23,9 @@ DONE single book activity
|
|||
depends_on_ip (?)
|
||||
|
||||
resources for search actions
|
||||
errorMessage
|
||||
titles/summaries
|
||||
menu items
|
||||
Search actions
|
||||
stopping search
|
||||
проверить все пути регистрации
|
||||
|
|
|
@ -637,10 +637,6 @@
|
|||
<node name="noRequiredInformation" value="Required information is not specified in the catalog" />
|
||||
<node name="cacheDirectoryError" value="Unable to create cache directory" />
|
||||
</node>
|
||||
<node name="emptySearchResults">
|
||||
<node name="title" value="Search results" />
|
||||
<node name="message" value="There are no suitable books found" />
|
||||
</node>
|
||||
<node name="AuthenticationDialog">
|
||||
<node name="title" value="Authentication" />
|
||||
<node name="unencryptedWarning" value="Your password will be sent unencrypted" />
|
||||
|
@ -693,6 +689,7 @@
|
|||
<node name="noFavorites" value="Your favorites list is empty, sorry"/>
|
||||
<node name="emptyCatalog" value="Catalog is empty, sorry" />
|
||||
<node name="emptyBasket" value="Your basket is empty, sorry" />
|
||||
<node name="emptyNetworkSearchResults" value="No books found, sorry" />
|
||||
</node>
|
||||
<node name="external">
|
||||
<node name="browser" value="Browser"/>
|
||||
|
|
|
@ -21,8 +21,6 @@ package org.geometerplus.android.fbreader.network;
|
|||
|
||||
import java.util.Set;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
|
||||
|
||||
import org.geometerplus.fbreader.network.*;
|
||||
|
@ -30,18 +28,12 @@ import org.geometerplus.fbreader.network.tree.NetworkCatalogTree;
|
|||
import org.geometerplus.fbreader.network.tree.NetworkItemsLoader;
|
||||
|
||||
public abstract class ItemsLoader extends NetworkItemsLoader {
|
||||
protected final Activity myActivity;
|
||||
|
||||
private volatile boolean myFinishProcessed;
|
||||
private final Object myFinishMonitor = new Object();
|
||||
|
||||
private volatile boolean myFinished;
|
||||
private volatile Runnable myPostRunnable;
|
||||
private final Object myFinishedLock = new Object();
|
||||
|
||||
public ItemsLoader(Activity activity, NetworkCatalogTree tree) {
|
||||
public ItemsLoader(NetworkCatalogTree tree) {
|
||||
super(tree);
|
||||
myActivity = activity;
|
||||
}
|
||||
|
||||
public final void run() {
|
||||
|
@ -54,7 +46,7 @@ public abstract class ItemsLoader extends NetworkItemsLoader {
|
|||
try {
|
||||
doBefore();
|
||||
} catch (ZLNetworkException e) {
|
||||
finishOnUiThread(e.getMessage(), false);
|
||||
onFinish(e.getMessage(), false);
|
||||
return;
|
||||
}
|
||||
String error = null;
|
||||
|
@ -64,8 +56,7 @@ public abstract class ItemsLoader extends NetworkItemsLoader {
|
|||
error = e.getMessage();
|
||||
}
|
||||
|
||||
finishOnUiThread(error, isLoadingInterrupted());
|
||||
ensureFinishProcessed();
|
||||
onFinish(error, isLoadingInterrupted());
|
||||
} finally {
|
||||
library.removeStoredLoader(getTree());
|
||||
synchronized (myFinishedLock) {
|
||||
|
@ -91,30 +82,6 @@ public abstract class ItemsLoader extends NetworkItemsLoader {
|
|||
}
|
||||
}
|
||||
|
||||
private final void ensureFinishProcessed() {
|
||||
synchronized (myFinishMonitor) {
|
||||
while (!myFinishProcessed) {
|
||||
try {
|
||||
myFinishMonitor.wait();
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final void finishOnUiThread(final String errorMessage, final boolean interrupted) {
|
||||
myActivity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
synchronized (myFinishMonitor) {
|
||||
onFinish(errorMessage, interrupted);
|
||||
myFinishProcessed = true;
|
||||
// wake up process, that waits for finish condition (see ensureFinish() method)
|
||||
myFinishMonitor.notifyAll();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected abstract void onFinish(String errorMessage, boolean interrupted);
|
||||
|
||||
protected abstract void doBefore() throws ZLNetworkException;
|
||||
|
|
|
@ -396,7 +396,7 @@ public class NetworkBookInfoActivity extends Activity implements NetworkLibrary.
|
|||
super.onStop();
|
||||
}
|
||||
|
||||
public void onLibraryChanged(NetworkLibrary.ChangeListener.Code code) {
|
||||
public void onLibraryChanged(NetworkLibrary.ChangeListener.Code code, Object[] params) {
|
||||
if (myBook == null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -38,6 +38,8 @@ import org.geometerplus.fbreader.network.tree.*;
|
|||
import org.geometerplus.android.fbreader.tree.TreeActivity;
|
||||
import org.geometerplus.android.fbreader.network.action.*;
|
||||
|
||||
import org.geometerplus.android.util.UIUtil;
|
||||
|
||||
public class NetworkLibraryActivity extends TreeActivity implements NetworkLibrary.ChangeListener {
|
||||
protected static final int BASIC_AUTHENTICATION_CODE = 1;
|
||||
protected static final int SIGNUP_CODE = 2;
|
||||
|
@ -324,17 +326,32 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra
|
|||
}
|
||||
|
||||
// method from NetworkLibrary.ChangeListener
|
||||
public void onLibraryChanged(NetworkLibrary.ChangeListener.Code code) {
|
||||
public void onLibraryChanged(final NetworkLibrary.ChangeListener.Code code, final Object[] params) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
final NetworkTree tree = getLoadableNetworkTree((NetworkTree)getCurrentTree());
|
||||
final boolean inProgress =
|
||||
tree != null &&
|
||||
NetworkLibrary.Instance().getStoredLoader(tree) != null;
|
||||
setProgressBarIndeterminateVisibility(inProgress);
|
||||
switch (code) {
|
||||
default:
|
||||
{
|
||||
final NetworkTree tree = getLoadableNetworkTree((NetworkTree)getCurrentTree());
|
||||
final boolean inProgress =
|
||||
tree != null &&
|
||||
NetworkLibrary.Instance().getStoredLoader(tree) != null;
|
||||
setProgressBarIndeterminateVisibility(inProgress);
|
||||
|
||||
getListAdapter().replaceAll(getCurrentTree().subTrees());
|
||||
getListView().invalidateViews();
|
||||
getListAdapter().replaceAll(getCurrentTree().subTrees());
|
||||
getListView().invalidateViews();
|
||||
break;
|
||||
}
|
||||
case NotFound:
|
||||
UIUtil.showErrorMessage(NetworkLibraryActivity.this, "emptyNetworkSearchResults");
|
||||
break;
|
||||
case EmptyCatalog:
|
||||
UIUtil.showErrorMessage(NetworkLibraryActivity.this, "emptyCatalog");
|
||||
break;
|
||||
case NetworkError:
|
||||
UIUtil.showMessageText(NetworkLibraryActivity.this, (String)params[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -64,9 +64,10 @@ public class NetworkSearchActivity extends Activity {
|
|||
|
||||
private static class Searcher extends ItemsLoader {
|
||||
private final String myPattern;
|
||||
private volatile boolean myItemFound;
|
||||
|
||||
public Searcher(Activity activity, SearchCatalogTree tree, String pattern) {
|
||||
super(activity, tree);
|
||||
public Searcher(SearchCatalogTree tree, String pattern) {
|
||||
super(tree);
|
||||
myPattern = pattern;
|
||||
}
|
||||
|
||||
|
@ -76,23 +77,24 @@ public class NetworkSearchActivity extends Activity {
|
|||
|
||||
@Override
|
||||
public void doLoading() {
|
||||
try {
|
||||
NetworkLibrary.Instance().simpleSearch(myPattern, this);
|
||||
} catch (ZLNetworkException e) {
|
||||
}
|
||||
//try {
|
||||
//NetworkLibrary.Instance().simpleSearch(myPattern, this);
|
||||
//} catch (ZLNetworkException e) {
|
||||
//}
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
protected void addItem(NetworkItem item) {
|
||||
SearchResult result = getTree().getSearchResult();
|
||||
if (item instanceof NetworkBookItem) {
|
||||
result.addBook((NetworkBookItem)item);
|
||||
getTree().updateSubTrees();
|
||||
NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.SomeCode);
|
||||
public synchronized void onNewItem(final NetworkItem item) {
|
||||
if (!myItemFound) {
|
||||
((SearchCatalogTree)getTree()).setPattern(myPattern);
|
||||
getTree().clearCatalog();
|
||||
NetworkLibrary.Instance().fireModelChangedEvent(
|
||||
NetworkLibrary.ChangeListener.Code.Found, getTree().getUniqueKey()
|
||||
);
|
||||
myItemFound = true;
|
||||
}
|
||||
super.onNewItem(item);
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
protected void onFinish(String errorMessage, boolean interrupted) {
|
||||
|
@ -102,9 +104,12 @@ public class NetworkSearchActivity extends Activity {
|
|||
//getTree().updateSubTrees();
|
||||
//afterUpdateCatalog(errorMessage, getTree().getSearchResult().isEmpty());
|
||||
}
|
||||
NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.SomeCode);
|
||||
if (!myItemFound) {
|
||||
NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.NotFound);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
private void afterUpdateCatalog(String errorMessage, boolean childrenEmpty) {
|
||||
final ZLResource dialogResource = ZLResource.resource("dialog");
|
||||
ZLResource boxResource = null;
|
||||
|
@ -132,20 +137,19 @@ public class NetworkSearchActivity extends Activity {
|
|||
.setPositiveButton(buttonResource.getResource("ok").getValue(), null)
|
||||
.create().show();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
protected void runSearch(SearchCatalogTree tree, String pattern) {
|
||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||
library.NetworkSearchPatternOption.setValue(pattern);
|
||||
|
||||
new Searcher(tree, pattern).start();
|
||||
/*
|
||||
final String summary = NetworkLibrary.resource().getResource("searchResults").getValue().replace("%s", pattern);
|
||||
final SearchResult result = new SearchResult(summary);
|
||||
|
||||
//tree.setSearchResult(result);
|
||||
NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.SomeCode);
|
||||
|
||||
new Searcher(this, tree, pattern).start();
|
||||
Util.openTree(this, tree);
|
||||
*/
|
||||
}
|
||||
|
|
|
@ -21,12 +21,8 @@ package org.geometerplus.android.fbreader.network.action;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
|
||||
|
||||
import org.geometerplus.android.util.UIUtil;
|
||||
|
||||
import org.geometerplus.fbreader.network.INetworkLink;
|
||||
import org.geometerplus.fbreader.network.NetworkLibrary;
|
||||
import org.geometerplus.fbreader.network.NetworkItem;
|
||||
|
@ -40,9 +36,8 @@ class CatalogExpander extends ItemsLoader {
|
|||
private final boolean myCheckAuthentication;
|
||||
private final boolean myResumeNotLoad;
|
||||
|
||||
public CatalogExpander(Activity activity,
|
||||
NetworkCatalogTree tree, boolean checkAuthentication, boolean resumeNotLoad) {
|
||||
super(activity, tree);
|
||||
public CatalogExpander(NetworkCatalogTree tree, boolean checkAuthentication, boolean resumeNotLoad) {
|
||||
super(tree);
|
||||
myCheckAuthentication = checkAuthentication;
|
||||
myResumeNotLoad = resumeNotLoad;
|
||||
}
|
||||
|
@ -80,9 +75,13 @@ class CatalogExpander extends ItemsLoader {
|
|||
getTree().updateLoadedTime();
|
||||
if (!interrupted) {
|
||||
if (errorMessage != null) {
|
||||
UIUtil.showMessageText(myActivity, errorMessage);
|
||||
NetworkLibrary.Instance().fireModelChangedEvent(
|
||||
NetworkLibrary.ChangeListener.Code.NetworkError, errorMessage
|
||||
);
|
||||
} else if (getTree().subTrees().isEmpty()) {
|
||||
UIUtil.showErrorMessage(myActivity, "emptyCatalog");
|
||||
NetworkLibrary.Instance().fireModelChangedEvent(
|
||||
NetworkLibrary.ChangeListener.Code.EmptyCatalog, errorMessage
|
||||
);
|
||||
}
|
||||
}
|
||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||
|
|
|
@ -84,7 +84,7 @@ public class OpenCatalogAction extends CatalogAction {
|
|||
}
|
||||
}
|
||||
|
||||
new CatalogExpander(myActivity, tree, true, resumeNotLoad).start();
|
||||
new CatalogExpander(tree, true, resumeNotLoad).start();
|
||||
processExtraData(tree.Item.extraData(), new Runnable() {
|
||||
public void run() {
|
||||
Util.openTree(myActivity, tree);
|
||||
|
|
|
@ -50,6 +50,6 @@ public class ReloadCatalogAction extends CatalogAction {
|
|||
return;
|
||||
}
|
||||
((NetworkCatalogTree)tree).clearCatalog();
|
||||
new CatalogExpander(myActivity, (NetworkCatalogTree)tree, false, false).start();
|
||||
new CatalogExpander((NetworkCatalogTree)tree, false, false).start();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ public class RunSearchAction extends Action {
|
|||
|
||||
@Override
|
||||
public boolean isEnabled(NetworkTree tree) {
|
||||
return NetworkLibrary.Instance().getStoredLoader(tree) != null;
|
||||
return NetworkLibrary.Instance().getStoredLoader(tree) == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -25,10 +25,15 @@ import org.geometerplus.fbreader.network.tree.NetworkItemsLoader;
|
|||
|
||||
public class AllCatalogsSearchItem extends SearchItem {
|
||||
public AllCatalogsSearchItem() {
|
||||
super(
|
||||
null,
|
||||
getResource().getResource("summaryAllCatalogs").getValue()
|
||||
);
|
||||
super(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSummaryString() {
|
||||
return
|
||||
getPattern() != null
|
||||
? NetworkLibrary.resource().getResource("found").getResource("summary").getValue().replace("%s", getPattern())
|
||||
: NetworkLibrary.resource().getResource("search").getResource("summaryAllCatalogs").getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -38,17 +38,19 @@ import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
|
|||
public class NetworkLibrary {
|
||||
public interface ChangeListener {
|
||||
public enum Code {
|
||||
SomeCode
|
||||
SomeCode,
|
||||
/*
|
||||
ItemAdded,
|
||||
ItemRemoved,
|
||||
StatusChanged,
|
||||
Found,
|
||||
NotFound
|
||||
*/
|
||||
Found,
|
||||
NotFound,
|
||||
EmptyCatalog,
|
||||
NetworkError
|
||||
}
|
||||
|
||||
void onLibraryChanged(Code code);
|
||||
void onLibraryChanged(Code code, Object[] params);
|
||||
}
|
||||
|
||||
private static NetworkLibrary ourInstance;
|
||||
|
@ -174,6 +176,8 @@ public class NetworkLibrary {
|
|||
|
||||
private volatile boolean myIsInitialized;
|
||||
|
||||
private final SearchItem mySearchItem = new AllCatalogsSearchItem();
|
||||
|
||||
private NetworkLibrary() {
|
||||
}
|
||||
|
||||
|
@ -280,18 +284,6 @@ public class NetworkLibrary {
|
|||
myUpdateVisibility = true;
|
||||
}
|
||||
|
||||
// private static boolean linkIsChanged(INetworkLink link) {
|
||||
// return
|
||||
// link instanceof ICustomNetworkLink &&
|
||||
// ((ICustomNetworkLink)link).hasChanges();
|
||||
// }
|
||||
|
||||
// private static void makeValid(INetworkLink link) {
|
||||
// if (link instanceof ICustomNetworkLink) {
|
||||
// ((ICustomNetworkLink)link).resetChanges();
|
||||
// }
|
||||
// }
|
||||
|
||||
private void makeUpToDate() {
|
||||
final SortedSet<INetworkLink> linkSet = new TreeSet<INetworkLink>(activeLinks());
|
||||
|
||||
|
@ -338,7 +330,7 @@ public class NetworkLibrary {
|
|||
new NetworkCatalogRootTree(myRootTree, link, index);
|
||||
}
|
||||
// we do add non-catalog items
|
||||
new SearchCatalogTree(myRootTree, new AllCatalogsSearchItem(), 0);
|
||||
new SearchCatalogTree(myRootTree, mySearchItem, 0);
|
||||
new AddCustomCatalogItemTree(myRootTree);
|
||||
|
||||
fireModelChangedEvent(ChangeListener.Code.SomeCode);
|
||||
|
@ -463,10 +455,10 @@ public class NetworkLibrary {
|
|||
}
|
||||
|
||||
// TODO: change to private
|
||||
/*private*/ public void fireModelChangedEvent(ChangeListener.Code code) {
|
||||
/*private*/ public void fireModelChangedEvent(ChangeListener.Code code, Object ... params) {
|
||||
synchronized (myListeners) {
|
||||
for (ChangeListener l : myListeners) {
|
||||
l.onLibraryChanged(code);
|
||||
l.onLibraryChanged(code, params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,28 +19,34 @@
|
|||
|
||||
package org.geometerplus.fbreader.network;
|
||||
|
||||
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||
|
||||
import org.geometerplus.fbreader.network.urlInfo.*;
|
||||
|
||||
public abstract class SearchItem extends NetworkCatalogItem {
|
||||
protected static ZLResource getResource() {
|
||||
return NetworkLibrary.resource().getResource("search");
|
||||
}
|
||||
|
||||
private String myPattern;
|
||||
|
||||
protected SearchItem(INetworkLink link, String summary) {
|
||||
protected SearchItem(INetworkLink link) {
|
||||
super(
|
||||
link,
|
||||
getResource().getValue(),
|
||||
summary,
|
||||
NetworkLibrary.resource().getResource("search").getValue(),
|
||||
"",
|
||||
new UrlInfoCollection<UrlInfo>(),
|
||||
Accessibility.ALWAYS,
|
||||
FLAGS_DEFAULT
|
||||
);
|
||||
setSummary(getSummaryString());
|
||||
}
|
||||
|
||||
public void setPattern(String pattern) {
|
||||
myPattern = pattern;
|
||||
setSummary(getSummaryString());
|
||||
}
|
||||
|
||||
protected String getPattern() {
|
||||
return myPattern;
|
||||
}
|
||||
|
||||
protected abstract String getSummaryString();
|
||||
|
||||
@Override
|
||||
public String getStringId() {
|
||||
return "@Search";
|
||||
|
|
|
@ -25,10 +25,15 @@ import org.geometerplus.fbreader.network.tree.NetworkItemsLoader;
|
|||
|
||||
public class SingleCatalogSearchItem extends SearchItem {
|
||||
public SingleCatalogSearchItem(INetworkLink link) {
|
||||
super(
|
||||
link,
|
||||
getResource().getResource("summary").getValue().replace("%s", link.getSiteName())
|
||||
);
|
||||
super(link);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSummaryString() {
|
||||
return
|
||||
getPattern() != null
|
||||
? NetworkLibrary.resource().getResource("found").getResource("summary").getValue().replace("%s", getPattern())
|
||||
: NetworkLibrary.resource().getResource("search").getResource("summary").getValue().replace("%s", Link.getSiteName());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -59,13 +59,17 @@ public class NetworkCatalogTree extends NetworkTree {
|
|||
return Item.canBeOpened();
|
||||
}
|
||||
|
||||
private SearchItem mySearchItem;
|
||||
|
||||
private void addSearchTree() {
|
||||
if ((Item.getFlags() & NetworkCatalogItem.FLAG_ADD_SEARCH_ITEM) != 0) {
|
||||
final INetworkLink link = getLink();
|
||||
if (link != null && link.getUrl(UrlInfo.Type.Search) != null) {
|
||||
final SearchItem item = new SingleCatalogSearchItem(link);
|
||||
myChildrenItems.add(item);
|
||||
new SearchCatalogTree(this, item, 0);
|
||||
if (mySearchItem == null) {
|
||||
mySearchItem = new SingleCatalogSearchItem(link);
|
||||
}
|
||||
myChildrenItems.add(mySearchItem);
|
||||
new SearchCatalogTree(this, mySearchItem, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ public abstract class NetworkItemsLoader implements Runnable {
|
|||
}
|
||||
}
|
||||
|
||||
public final void onNewItem(final NetworkItem item) {
|
||||
public void onNewItem(final NetworkItem item) {
|
||||
getTree().addItem(item);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,10 @@ public class SearchCatalogTree extends NetworkCatalogTree {
|
|||
setCover(null);
|
||||
}
|
||||
|
||||
public void setPattern(String pattern) {
|
||||
((SearchItem)Item).setPattern(pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTreeTitle() {
|
||||
return getSummary();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue