diff --git a/src/org/geometerplus/android/fbreader/network/AddCustomCatalogActivity.java b/src/org/geometerplus/android/fbreader/network/AddCustomCatalogActivity.java index 9b03b88d4..7216c7748 100644 --- a/src/org/geometerplus/android/fbreader/network/AddCustomCatalogActivity.java +++ b/src/org/geometerplus/android/fbreader/network/AddCustomCatalogActivity.java @@ -49,10 +49,8 @@ public class AddCustomCatalogActivity extends Activity { private static final String ADD_CATALOG_ID_KEY = "id"; private static final String ADD_CATALOG_URLS_MAP_KEY = "urls"; - static void addLinkToIntent(Intent intent, ICustomNetworkLink link) { - final String textUrl = link.getUrl(UrlInfo.Type.Catalog); - intent.setData(Uri.parse(textUrl)); - intent + public static void addLinkToIntent(Intent intent, ICustomNetworkLink link) { + Util.intentByLink(intent, link) .putExtra(ADD_CATALOG_TITLE_KEY, link.getTitle()) .putExtra(ADD_CATALOG_SUMMARY_KEY, link.getSummary()) .putExtra(ADD_CATALOG_ID_KEY, link.getId()) diff --git a/src/org/geometerplus/android/fbreader/network/AuthenticationActivity.java b/src/org/geometerplus/android/fbreader/network/AuthenticationActivity.java index 39a4232c5..550d9c81e 100644 --- a/src/org/geometerplus/android/fbreader/network/AuthenticationActivity.java +++ b/src/org/geometerplus/android/fbreader/network/AuthenticationActivity.java @@ -21,6 +21,7 @@ package org.geometerplus.android.fbreader.network; import android.app.Activity; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.TextView; @@ -34,6 +35,8 @@ import org.geometerplus.zlibrary.core.resources.ZLResource; import org.geometerplus.zlibrary.core.network.ZLNetworkManager; import org.geometerplus.zlibrary.core.options.ZLStringOption; +import org.geometerplus.fbreader.network.INetworkLink; + import org.geometerplus.zlibrary.ui.android.R; public class AuthenticationActivity extends Activity { @@ -101,15 +104,22 @@ public class AuthenticationActivity extends Activity { } private ZLResource myResource; + private INetworkLink myLink; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this)); - setResult(RESULT_CANCELED); setContentView(R.layout.authentication); final Intent intent = getIntent(); + myLink = Util.linkByIntent(intent); + if (myLink == null) { + finish(); + return; + } + setResult(RESULT_CANCELED, Util.intentByLink(new Intent(), myLink)); + final String host = intent.getStringExtra(HOST_KEY); final String area = intent.getStringExtra(AREA_KEY); final String username = intent.getStringExtra(USERNAME_KEY); @@ -155,7 +165,7 @@ public class AuthenticationActivity extends Activity { signupView.setText(myResource.getResource("register").getValue()); signupView.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { - setResult(RESULT_SIGNUP); + setResult(RESULT_SIGNUP, Util.intentByLink(new Intent(), myLink)); finish(); } }); @@ -169,7 +179,7 @@ public class AuthenticationActivity extends Activity { okButton.setText(buttonResource.getResource("ok").getValue()); okButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { - final Intent data = new Intent(); + final Intent data = Util.intentByLink(new Intent(), myLink); data.putExtra( USERNAME_KEY, usernameView.getText().toString() diff --git a/src/org/geometerplus/android/fbreader/network/ItemsLoadingService.java b/src/org/geometerplus/android/fbreader/network/ItemsLoadingService.java index e284996ac..890a37a5f 100644 --- a/src/org/geometerplus/android/fbreader/network/ItemsLoadingService.java +++ b/src/org/geometerplus/android/fbreader/network/ItemsLoadingService.java @@ -31,7 +31,7 @@ import org.geometerplus.fbreader.network.NetworkTree; public class ItemsLoadingService extends Service { private static final String KEY = "ItemsLoadingRunnable"; - static void start(Context context, NetworkTree tree, ItemsLoader runnable) { + public static void start(Context context, NetworkTree tree, ItemsLoader runnable) { boolean doDownload = false; synchronized (tree) { if (tree.getUserData(KEY) == null) { diff --git a/src/org/geometerplus/android/fbreader/network/NetworkBookActions.java b/src/org/geometerplus/android/fbreader/network/NetworkBookActions.java index 816af748e..8225db4b1 100644 --- a/src/org/geometerplus/android/fbreader/network/NetworkBookActions.java +++ b/src/org/geometerplus/android/fbreader/network/NetworkBookActions.java @@ -66,7 +66,6 @@ class NetworkBookActions extends NetworkTreeActions { book.reference(UrlInfo.Type.Book) == null; } - @Override public boolean canHandleTree(NetworkTree tree) { return tree instanceof NetworkBookTree || tree instanceof NetworkAuthorTree diff --git a/src/org/geometerplus/android/fbreader/network/NetworkBookInfoActivity.java b/src/org/geometerplus/android/fbreader/network/NetworkBookInfoActivity.java index 9fe9f8b15..df110bb80 100644 --- a/src/org/geometerplus/android/fbreader/network/NetworkBookInfoActivity.java +++ b/src/org/geometerplus/android/fbreader/network/NetworkBookInfoActivity.java @@ -390,7 +390,7 @@ public class NetworkBookInfoActivity extends Activity implements NetworkView.Eve protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case NetworkLibraryActivity.CUSTOM_AUTHENTICATION_CODE: - Util.processCustomAuthentication(this, myBook.Link, resultCode, data); + Util.processCustomAuthentication(this, resultCode, data); break; case NetworkLibraryActivity.SIGNUP_CODE: Util.processSignup(myBook.Link, resultCode, data); diff --git a/src/org/geometerplus/android/fbreader/network/NetworkCatalogActions.java b/src/org/geometerplus/android/fbreader/network/NetworkCatalogActions.java index 76a34d2c0..b34116486 100644 --- a/src/org/geometerplus/android/fbreader/network/NetworkCatalogActions.java +++ b/src/org/geometerplus/android/fbreader/network/NetworkCatalogActions.java @@ -22,18 +22,8 @@ package org.geometerplus.android.fbreader.network; import java.util.*; import android.app.Activity; -import android.app.AlertDialog; -import android.content.Intent; -import android.content.DialogInterface; -import android.content.ActivityNotFoundException; -import android.net.Uri; -import android.view.Menu; -import android.view.ContextMenu; -import android.view.MenuItem; -import org.geometerplus.zlibrary.core.util.ZLBoolean3; import org.geometerplus.zlibrary.core.network.ZLNetworkException; -import org.geometerplus.zlibrary.core.resources.ZLResource; import org.geometerplus.android.util.UIUtil; import org.geometerplus.android.util.PackageUtil; @@ -42,173 +32,19 @@ import org.geometerplus.fbreader.network.*; import org.geometerplus.fbreader.network.authentication.*; import org.geometerplus.fbreader.network.tree.NetworkTreeFactory; import org.geometerplus.fbreader.network.tree.NetworkCatalogTree; -import org.geometerplus.fbreader.network.tree.NetworkCatalogRootTree; import org.geometerplus.fbreader.network.opds.BasketItem; -import org.geometerplus.fbreader.network.urlInfo.UrlInfo; import org.geometerplus.android.fbreader.network.action.ActionCode; -public class NetworkCatalogActions extends NetworkTreeActions { - @Override - public boolean canHandleTree(NetworkTree tree) { - return tree instanceof NetworkCatalogTree; - } - - @Override - public void buildContextMenu(NetworkLibraryActivity activity, ContextMenu menu, NetworkTree tree) { - final NetworkCatalogItem item = ((NetworkCatalogTree)tree).Item; - final NetworkURLCatalogItem urlItem = - item instanceof NetworkURLCatalogItem ? (NetworkURLCatalogItem)item : null; - menu.setHeaderTitle(tree.getName()); - - boolean hasItems = false; - - final String catalogUrl = - urlItem != null ? urlItem.getUrl(UrlInfo.Type.Catalog) : null; - if (catalogUrl != null && - (!(item instanceof BasketItem) || item.Link.basket().bookIds().size() > 0)) { - addMenuItem(menu, ActionCode.OPEN_CATALOG, "openCatalog"); - hasItems = true; - } - - if (tree instanceof NetworkCatalogRootTree) { - if (item.getVisibility() == ZLBoolean3.B3_TRUE) { - final NetworkAuthenticationManager mgr = item.Link.authenticationManager(); - if (mgr != null) { - if (mgr.mayBeAuthorised(false)) { - addMenuItem(menu, ActionCode.SIGNOUT, "signOut", mgr.currentUserName()); - if (TopupMenuActivity.isTopupSupported(item.Link)) { - final String account = mgr.currentAccount(); - if (account != null) { - addMenuItem(menu, ActionCode.TOPUP, "topup", account); - } - } - } else { - addMenuItem(menu, ActionCode.SIGNIN, "signIn"); - //if (mgr.passwordRecoverySupported()) { - // registerAction(new PasswordRecoveryAction(mgr), true); - //} - } - } - } - INetworkLink link = item.Link; - if (link instanceof ICustomNetworkLink) { - addMenuItem(menu, ActionCode.CUSTOM_CATALOG_EDIT, "editCustomCatalog"); - addMenuItem(menu, ActionCode.CUSTOM_CATALOG_REMOVE, "removeCustomCatalog"); - } - } else { - if (urlItem != null && urlItem.getUrl(UrlInfo.Type.HtmlPage) != null) { - addMenuItem(menu, ActionCode.OPEN_IN_BROWSER, "openInBrowser"); - hasItems = true; - } - } - - if (item.getVisibility() == ZLBoolean3.B3_UNDEFINED && - !hasItems && item.Link.authenticationManager() != null) { - addMenuItem(menu, ActionCode.SIGNIN, "signIn"); - } - } - - @Override - public int getDefaultActionCode(NetworkLibraryActivity activity, NetworkTree tree) { - final NetworkCatalogItem item = ((NetworkCatalogTree)tree).Item; - if (!(item instanceof NetworkURLCatalogItem)) { - return ActionCode.OPEN_CATALOG; - } - final NetworkURLCatalogItem urlItem = (NetworkURLCatalogItem)item; - if (urlItem.getUrl(UrlInfo.Type.Catalog) != null) { - return ActionCode.OPEN_CATALOG; - } - if (urlItem.getUrl(UrlInfo.Type.HtmlPage) != null) { - return ActionCode.OPEN_IN_BROWSER; - } - if (urlItem.getVisibility() == ZLBoolean3.B3_UNDEFINED && - urlItem.Link.authenticationManager() != null) { - return ActionCode.SIGNIN; - } - return ActionCode.TREE_NO_ACTION; - } - - private boolean consumeByVisibility(final NetworkLibraryActivity activity, final NetworkTree tree, final int actionCode) { - final NetworkCatalogItem item = ((NetworkCatalogTree)tree).Item; - switch (item.getVisibility()) { - case B3_TRUE: - return false; - case B3_UNDEFINED: - Util.runAuthenticationDialog(activity, item.Link, null, new Runnable() { - public void run() { - if (item.getVisibility() != ZLBoolean3.B3_TRUE) { - return; - } - if (actionCode != ActionCode.SIGNIN) { - runAction(activity, tree, actionCode); - } - } - }); - break; - } - return true; - } - - @Override +public class NetworkCatalogActions { public boolean runAction(final NetworkLibraryActivity activity, NetworkTree tree, int actionCode) { final NetworkCatalogTree catalogTree = (NetworkCatalogTree)tree; - if (consumeByVisibility(activity, catalogTree, actionCode)) { - return true; - } final NetworkCatalogItem item = catalogTree.Item; switch (actionCode) { - case ActionCode.OPEN_CATALOG: - if (item instanceof BasketItem && item.Link.basket().bookIds().size() == 0) { - UIUtil.showErrorMessage(activity, "emptyBasket"); - } else { - doExpandCatalog(activity, catalogTree); - } - return true; - case ActionCode.OPEN_IN_BROWSER: - if (item instanceof NetworkURLCatalogItem) { - final ZLResource buttonResource = ZLResource.resource("dialog").getResource("button"); - final String message = NetworkLibrary.resource().getResource("confirmQuestions").getResource("openInBrowser").getValue(); - new AlertDialog.Builder(activity) - .setTitle(catalogTree.getName()) - .setMessage(message) - .setIcon(0) - .setPositiveButton(buttonResource.getResource("yes").getValue(), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - Util.openInBrowser(activity, item.getUrl(UrlInfo.Type.HtmlPage)); - } - }) - .setNegativeButton(buttonResource.getResource("no").getValue(), null) - .create().show(); - } - return true; - case ActionCode.RELOAD_CATALOG: - doReloadCatalog(activity, catalogTree); - return true; case ActionCode.SIGNIN: Util.runAuthenticationDialog(activity, item.Link, null, null); return true; - case ActionCode.SIGNOUT: - doSignOut(activity, catalogTree); - return true; - case ActionCode.TOPUP: - // TODO: replace 112 with required amount - TopupMenuActivity.runMenu(activity, item.Link, "112"); - return true; - case ActionCode.CUSTOM_CATALOG_EDIT: - { - final Intent intent = new Intent(activity, AddCustomCatalogActivity.class); - AddCustomCatalogActivity.addLinkToIntent( - intent, - (ICustomNetworkLink)item.Link - ); - activity.startActivity(intent); - return true; - } - case ActionCode.CUSTOM_CATALOG_REMOVE: - removeCustomLink((ICustomNetworkLink)item.Link); - return true; case ActionCode.BASKET_CLEAR: item.Link.basket().clear(); return true; @@ -218,7 +54,7 @@ public class NetworkCatalogActions extends NetworkTreeActions { return false; } - private static class CatalogExpander extends ItemsLoader { + public static class CatalogExpander extends ItemsLoader { private final NetworkCatalogTree myTree; private final boolean myCheckAuthentication; private final boolean myResumeNotLoad; @@ -347,7 +183,7 @@ public class NetworkCatalogActions extends NetworkTreeActions { }); } - private static void clearTree(Activity activity, final NetworkCatalogTree tree) { + public static void clearTree(Activity activity, final NetworkCatalogTree tree) { activity.runOnUiThread(new Runnable() { public void run() { tree.ChildrenItems.clear(); @@ -357,18 +193,6 @@ public class NetworkCatalogActions extends NetworkTreeActions { }); } - public static void doReloadCatalog(Activity activity, final NetworkCatalogTree tree) { - if (ItemsLoadingService.getRunnable(tree) != null) { - return; - } - clearTree(activity, tree); - ItemsLoadingService.start( - activity, - tree, - new CatalogExpander(activity, tree, false, false) - ); - } - public static void doSignOut(final Activity activity, NetworkCatalogTree tree) { final NetworkAuthenticationManager mgr = tree.Item.Link.authenticationManager(); final Runnable runnable = new Runnable() { @@ -388,11 +212,4 @@ public class NetworkCatalogActions extends NetworkTreeActions { }; UIUtil.wait("signOut", runnable, activity); } - - private void removeCustomLink(ICustomNetworkLink link) { - final NetworkLibrary library = NetworkLibrary.Instance(); - library.removeCustomLink(link); - library.synchronize(); - NetworkView.Instance().fireModelChangedAsync(); - } } diff --git a/src/org/geometerplus/android/fbreader/network/NetworkLibraryActivity.java b/src/org/geometerplus/android/fbreader/network/NetworkLibraryActivity.java index ace5cbc90..1373046a1 100644 --- a/src/org/geometerplus/android/fbreader/network/NetworkLibraryActivity.java +++ b/src/org/geometerplus/android/fbreader/network/NetworkLibraryActivity.java @@ -34,6 +34,7 @@ import org.geometerplus.zlibrary.core.network.ZLNetworkManager; import org.geometerplus.zlibrary.core.network.ZLNetworkException; import org.geometerplus.zlibrary.core.language.ZLLanguageUtil; import org.geometerplus.zlibrary.core.resources.ZLResource; +import org.geometerplus.zlibrary.core.util.ZLBoolean3; import org.geometerplus.zlibrary.ui.android.network.SQLiteCookieDatabase; import org.geometerplus.zlibrary.ui.android.R; @@ -214,8 +215,13 @@ public class NetworkLibraryActivity extends BaseActivity implements NetworkView. private void fillContextMenuList() { myContextMenuActions.add(new OpenCatalogAction(this)); + myContextMenuActions.add(new OpenInBrowserAction(this)); myContextMenuActions.add(new AddCustomCatalogAction(this)); + myContextMenuActions.add(new SignOutAction(this)); myContextMenuActions.add(new TopupAction(this)); + myContextMenuActions.add(new SignInAction(this)); + myContextMenuActions.add(new EditCustomCatalogAction(this)); + myContextMenuActions.add(new RemoveCustomCatalogAction(this)); } @Override @@ -234,10 +240,12 @@ public class NetworkLibraryActivity extends BaseActivity implements NetworkView. } } if (count == 0) { + /* final NetworkTreeActions actions = NetworkView.Instance().getActions(tree); if (actions != null) { actions.buildContextMenu(this, menu, tree); } + */ } else if (count > 1) { menu.setHeaderTitle(tree.getName()); for (Action a : myContextMenuActions) { @@ -249,15 +257,48 @@ public class NetworkLibraryActivity extends BaseActivity implements NetworkView. } } + private void runAction(final Action action, final NetworkTree tree) { + if (tree instanceof NetworkCatalogTree) { + final NetworkCatalogItem item = ((NetworkCatalogTree)tree).Item; + switch (item.getVisibility()) { + case B3_TRUE: + action.run(tree); + break; + case B3_UNDEFINED: + Util.runAuthenticationDialog(this, item.Link, null, new Runnable() { + public void run() { + if (item.getVisibility() != ZLBoolean3.B3_TRUE) { + return; + } + if (action.Code != ActionCode.SIGNIN) { + action.run(tree); + } + } + }); + break; + } + } else { + action.run(tree); + } + } + @Override public boolean onContextItemSelected(MenuItem item) { final int position = ((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).position; final NetworkTree tree = (NetworkTree)getListAdapter().getItem(position); if (tree != null) { + for (Action a : myContextMenuActions) { + if (a.Code == item.getItemId()) { + runAction(a, tree); + return true; + } + } + /* final NetworkTreeActions actions = NetworkView.Instance().getActions(tree); if (actions != null && actions.runAction(this, tree, item.getItemId())) { return true; } + */ } return super.onContextItemSelected(item); } @@ -270,14 +311,13 @@ public class NetworkLibraryActivity extends BaseActivity implements NetworkView. final NetworkTree tree = (NetworkTree)getListAdapter().getItem(position); Action defaultAction = null; - int count = 0; for (Action a : myContextMenuActions) { if (a.isVisible(tree) && a.isEnabled(tree)) { - defaultAction = a; - ++count; + runAction(a, tree); + return; } } - if (count == 0) { + /* final NetworkView networkView = NetworkView.Instance(); final NetworkTreeActions actions = networkView.getActions(tree); if (actions == null) { @@ -292,11 +332,7 @@ public class NetworkLibraryActivity extends BaseActivity implements NetworkView. return; } actions.runAction(this, tree, actionCode); - } else if (count == 1) { - defaultAction.run(tree); - } else { - // TODO: select "default" action - } + */ } private final AuthenticationActivity.CredentialsCreator myCredentialsCreator = @@ -309,9 +345,7 @@ public class NetworkLibraryActivity extends BaseActivity implements NetworkView. myCredentialsCreator.onDataReceived(resultCode, intent); break; case CUSTOM_AUTHENTICATION_CODE: - Util.processCustomAuthentication( - this, ((NetworkCatalogTree)getCurrentTree()).Item.Link, resultCode, intent - ); + Util.processCustomAuthentication(this, resultCode, intent); break; case SIGNUP_CODE: Util.processSignup(((NetworkCatalogTree)getCurrentTree()).Item.Link, resultCode, intent); diff --git a/src/org/geometerplus/android/fbreader/network/NetworkTreeActions.java b/src/org/geometerplus/android/fbreader/network/NetworkTreeActions.java index a54171e0a..af8e0beae 100644 --- a/src/org/geometerplus/android/fbreader/network/NetworkTreeActions.java +++ b/src/org/geometerplus/android/fbreader/network/NetworkTreeActions.java @@ -61,8 +61,6 @@ abstract class NetworkTreeActions { return addMenuItemWithText(menu, id, getTitleValue(key, arg)); } - public abstract boolean canHandleTree(NetworkTree tree); - public abstract int getDefaultActionCode(NetworkLibraryActivity activity, NetworkTree tree); public abstract boolean runAction(NetworkLibraryActivity activity, NetworkTree tree, int actionCode); diff --git a/src/org/geometerplus/android/fbreader/network/NetworkView.java b/src/org/geometerplus/android/fbreader/network/NetworkView.java index 86fda0a28..101a8ee1f 100644 --- a/src/org/geometerplus/android/fbreader/network/NetworkView.java +++ b/src/org/geometerplus/android/fbreader/network/NetworkView.java @@ -43,7 +43,6 @@ public class NetworkView { } private volatile boolean myInitialized; - private final ArrayList myActions = new ArrayList(); private NetworkView() { } @@ -59,11 +58,6 @@ public class NetworkView { library.initialize(); library.synchronize(); - myActions.add(new NetworkBookActions()); - myActions.add(new NetworkCatalogActions()); - myActions.add(new SearchItemActions()); - myActions.trimToSize(); - myInitialized = true; } @@ -80,19 +74,6 @@ public class NetworkView { fireModelChanged(); } - /* - * NetworkItem's actions - */ - - public NetworkTreeActions getActions(NetworkTree tree) { - for (NetworkTreeActions actions : myActions) { - if (actions.canHandleTree(tree)) { - return actions; - } - } - return null; - } - /* * Code for loading network items (running items-loading service and managing items-loading runnables). */ @@ -147,7 +128,7 @@ public class NetworkView { } } - final void fireModelChangedAsync() { + public final void fireModelChangedAsync() { synchronized (myEventListeners) { if (myEventHandler != null) { myEventHandler.sendEmptyMessage(0); diff --git a/src/org/geometerplus/android/fbreader/network/SearchItemActions.java b/src/org/geometerplus/android/fbreader/network/SearchItemActions.java index 73fe641ff..1c60e6328 100644 --- a/src/org/geometerplus/android/fbreader/network/SearchItemActions.java +++ b/src/org/geometerplus/android/fbreader/network/SearchItemActions.java @@ -29,7 +29,6 @@ import org.geometerplus.android.fbreader.network.action.ActionCode; class SearchItemActions extends NetworkTreeActions { private static final int RUN_SEARCH_ITEM_ID = 0; - @Override public boolean canHandleTree(NetworkTree tree) { return tree instanceof SearchItemTree; } diff --git a/src/org/geometerplus/android/fbreader/network/TopupMenuActivity.java b/src/org/geometerplus/android/fbreader/network/TopupMenuActivity.java index c04dc267f..43a394aa9 100644 --- a/src/org/geometerplus/android/fbreader/network/TopupMenuActivity.java +++ b/src/org/geometerplus/android/fbreader/network/TopupMenuActivity.java @@ -45,13 +45,12 @@ public class TopupMenuActivity extends ListActivity implements AdapterView.OnIte public static boolean isTopupSupported(INetworkLink link) { // TODO: more correct check - return link.authenticationManager().topupLink() != null; + return link.getUrlInfo(UrlInfo.Type.TopUp) != null; } public static void runMenu(Context context, INetworkLink link, String amount) { context.startActivity( - new Intent(context, TopupMenuActivity.class) - .setData(Uri.parse(link.getUrlInfo(UrlInfo.Type.Catalog).Url)) + Util.intentByLink(new Intent(context, TopupMenuActivity.class), link) .putExtra(AMOUNT_KEY, amount) ); } @@ -69,7 +68,7 @@ public class TopupMenuActivity extends ListActivity implements AdapterView.OnIte myAmount = getIntent().getStringExtra(AMOUNT_KEY); myInfos = new ArrayList(); - if (myLink.authenticationManager().topupLink() != null) { + if (myLink.getUrlInfo(UrlInfo.Type.TopUp) != null) { myInfos.add(new PluginApi.TopupActionInfo( Uri.parse(url + "/browser"), NetworkLibrary.resource().getResource("topupViaBrowser").getValue(), @@ -128,7 +127,9 @@ public class TopupMenuActivity extends ListActivity implements AdapterView.OnIte final NetworkAuthenticationManager mgr = myLink.authenticationManager(); if (info.getId().toString().endsWith("/browser")) { // TODO: put amount - Util.openInBrowser(TopupMenuActivity.this, mgr.topupLink()); + if (mgr != null) { + Util.openInBrowser(TopupMenuActivity.this, mgr.topupLink()); + } } else { final Intent intent = new Intent(TOPUP_ACTION, info.getId()); if (mgr != null) { diff --git a/src/org/geometerplus/android/fbreader/network/Util.java b/src/org/geometerplus/android/fbreader/network/Util.java index 8c5bc8909..0bea4d81d 100644 --- a/src/org/geometerplus/android/fbreader/network/Util.java +++ b/src/org/geometerplus/android/fbreader/network/Util.java @@ -43,6 +43,14 @@ public abstract class Util implements UserRegistrationConstants { private static final String REGISTRATION_ACTION = "android.fbreader.action.NETWORK_LIBRARY_REGISTER"; + static INetworkLink linkByIntent(Intent intent) { + return NetworkLibrary.Instance().getLinkByUrl(intent.getData().toString()); + } + + static Intent intentByLink(Intent intent, INetworkLink link) { + return intent.setData(Uri.parse(link.getUrl(UrlInfo.Type.Catalog))); + } + private static boolean testService(Activity activity, String action, String url) { return url != null && PackageUtil.canBeStarted(activity, new Intent(action, Uri.parse(url)), true); } @@ -77,7 +85,7 @@ public abstract class Util implements UserRegistrationConstants { public static void runAuthenticationDialog(Activity activity, INetworkLink link, String error, Runnable onSuccess) { final NetworkAuthenticationManager mgr = link.authenticationManager(); - final Intent intent = new Intent(activity, AuthenticationActivity.class); + final Intent intent = intentByLink(new Intent(activity, AuthenticationActivity.class), link); intent.putExtra(AuthenticationActivity.USERNAME_KEY, mgr.UserNameOption.getValue()); if (isRegistrationSupported(activity, link)) { intent.putExtra(AuthenticationActivity.SHOW_SIGNUP_LINK_KEY, true); @@ -90,9 +98,15 @@ public abstract class Util implements UserRegistrationConstants { activity.startActivityForResult(intent, NetworkLibraryActivity.CUSTOM_AUTHENTICATION_CODE); } - static void processCustomAuthentication(final Activity activity, final INetworkLink link, int resultCode, Intent data) { + static void processCustomAuthentication(final Activity activity, int resultCode, Intent data) { + final INetworkLink link = linkByIntent(data); + if (link == null) { + return; + } + final Runnable onSuccess = ourAfterRegisrationMap.get(activity); ourAfterRegisrationMap.remove(activity); + switch (resultCode) { case AuthenticationActivity.RESULT_CANCELED: UIUtil.wait( @@ -187,7 +201,7 @@ public abstract class Util implements UserRegistrationConstants { } } - static void openInBrowser(Context context, String url) { + public static void openInBrowser(Context context, String url) { if (url != null) { url = NetworkLibrary.Instance().rewriteUrl(url, true); context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); diff --git a/src/org/geometerplus/android/fbreader/network/action/EditCustomCatalogAction.java b/src/org/geometerplus/android/fbreader/network/action/EditCustomCatalogAction.java new file mode 100644 index 000000000..38cb7123a --- /dev/null +++ b/src/org/geometerplus/android/fbreader/network/action/EditCustomCatalogAction.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2010-2011 Geometer Plus + * + * 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.network.action; + +import android.app.Activity; +import android.content.Intent; + +import org.geometerplus.fbreader.network.NetworkTree; +import org.geometerplus.fbreader.network.ICustomNetworkLink; +import org.geometerplus.fbreader.network.tree.NetworkCatalogTree; + +import org.geometerplus.android.fbreader.network.AddCustomCatalogActivity; + +public class EditCustomCatalogAction extends CatalogAction { + public EditCustomCatalogAction(Activity activity) { + super(activity, ActionCode.CUSTOM_CATALOG_EDIT, "editCustomCatalog"); + } + + @Override + public boolean isVisible(NetworkTree tree) { + return + super.isVisible(tree) && + ((NetworkCatalogTree)tree).Item.Link instanceof ICustomNetworkLink; + } + + @Override + public void run(NetworkTree tree) { + final Intent intent = new Intent(myActivity, AddCustomCatalogActivity.class); + AddCustomCatalogActivity.addLinkToIntent( + intent, + (ICustomNetworkLink)((NetworkCatalogTree)tree).Item.Link + ); + myActivity.startActivity(intent); + } +} diff --git a/src/org/geometerplus/android/fbreader/network/action/OpenCatalogAction.java b/src/org/geometerplus/android/fbreader/network/action/OpenCatalogAction.java new file mode 100644 index 000000000..49515e040 --- /dev/null +++ b/src/org/geometerplus/android/fbreader/network/action/OpenCatalogAction.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2010-2011 Geometer Plus + * + * 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.network.action; + +import android.app.Activity; + +import org.geometerplus.fbreader.network.NetworkTree; +import org.geometerplus.fbreader.network.NetworkCatalogItem; +import org.geometerplus.fbreader.network.NetworkURLCatalogItem; +import org.geometerplus.fbreader.network.opds.BasketItem; +import org.geometerplus.fbreader.network.tree.NetworkCatalogTree; +import org.geometerplus.fbreader.network.urlInfo.UrlInfo; + +import org.geometerplus.android.fbreader.network.NetworkCatalogActions; + +import org.geometerplus.android.util.UIUtil; + +public class OpenCatalogAction extends CatalogAction { + public OpenCatalogAction(Activity activity) { + super(activity, ActionCode.OPEN_CATALOG, "openCatalog"); + } + + @Override + public boolean isVisible(NetworkTree tree) { + if (!super.isVisible(tree)) { + return false; + } + final NetworkCatalogItem item = ((NetworkCatalogTree)tree).Item; + if (!(item instanceof NetworkURLCatalogItem)) { + return true; + } + final String catalogUrl = ((NetworkURLCatalogItem)item).getUrl(UrlInfo.Type.Catalog); + + return + catalogUrl != null && + (!(item instanceof BasketItem) || item.Link.basket().bookIds().size() > 0); + } + + @Override + public void run(NetworkTree tree) { + final NetworkCatalogItem item = ((NetworkCatalogTree)tree).Item; + if (item instanceof BasketItem && item.Link.basket().bookIds().size() == 0) { + UIUtil.showErrorMessage(myActivity, "emptyBasket"); + } else { + NetworkCatalogActions.doExpandCatalog(myActivity, (NetworkCatalogTree)tree); + } + } +} diff --git a/src/org/geometerplus/android/fbreader/network/action/OpenInBrowserAction.java b/src/org/geometerplus/android/fbreader/network/action/OpenInBrowserAction.java new file mode 100644 index 000000000..83ea10c2d --- /dev/null +++ b/src/org/geometerplus/android/fbreader/network/action/OpenInBrowserAction.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2010-2011 Geometer Plus + * + * 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.network.action; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; + +import org.geometerplus.zlibrary.core.resources.ZLResource; + +import org.geometerplus.fbreader.network.*; +import org.geometerplus.fbreader.network.tree.NetworkCatalogTree; +import org.geometerplus.fbreader.network.urlInfo.UrlInfo; + +import org.geometerplus.android.fbreader.network.Util; + +public class OpenInBrowserAction extends CatalogAction { + public OpenInBrowserAction(Activity activity) { + super(activity, ActionCode.OPEN_IN_BROWSER, "openInBrowser"); + } + + @Override + public boolean isVisible(NetworkTree tree) { + if (!super.isVisible(tree)) { + return false; + } + + final NetworkCatalogItem item = ((NetworkCatalogTree)tree).Item; + if (!(item instanceof NetworkURLCatalogItem)) { + return false; + } + + return ((NetworkURLCatalogItem)item).getUrl(UrlInfo.Type.HtmlPage) != null; + } + + @Override + public void run(NetworkTree tree) { + final String url = + ((NetworkURLCatalogItem)((NetworkCatalogTree)tree).Item).getUrl(UrlInfo.Type.HtmlPage); + + final ZLResource buttonResource = ZLResource.resource("dialog").getResource("button"); + final String message = NetworkLibrary.resource().getResource("confirmQuestions").getResource("openInBrowser").getValue(); + new AlertDialog.Builder(myActivity) + .setTitle(tree.getName()) + .setMessage(message) + .setIcon(0) + .setPositiveButton(buttonResource.getResource("yes").getValue(), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + Util.openInBrowser(myActivity, url); + } + }) + .setNegativeButton(buttonResource.getResource("no").getValue(), null) + .create().show(); + } +} diff --git a/src/org/geometerplus/android/fbreader/network/action/ReloadCatalogAction.java b/src/org/geometerplus/android/fbreader/network/action/ReloadCatalogAction.java new file mode 100644 index 000000000..bdaf0bd01 --- /dev/null +++ b/src/org/geometerplus/android/fbreader/network/action/ReloadCatalogAction.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2010-2011 Geometer Plus + * + * 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.network.action; + +import android.app.Activity; + +import org.geometerplus.fbreader.network.NetworkTree; +import org.geometerplus.fbreader.network.NetworkCatalogItem; +import org.geometerplus.fbreader.network.NetworkURLCatalogItem; +import org.geometerplus.fbreader.network.tree.NetworkCatalogTree; +import org.geometerplus.fbreader.network.urlInfo.UrlInfo; + +import org.geometerplus.android.fbreader.network.NetworkCatalogActions; +import org.geometerplus.android.fbreader.network.ItemsLoadingService; + +public class ReloadCatalogAction extends CatalogAction { + public ReloadCatalogAction(Activity activity) { + super(activity, ActionCode.RELOAD_CATALOG, "reload"); + } + + @Override + public boolean isVisible(NetworkTree tree) { + if (!super.isVisible(tree)) { + return false; + } + final NetworkCatalogItem item = ((NetworkCatalogTree)tree).Item; + if (!(item instanceof NetworkURLCatalogItem)) { + return false; + } + return + ((NetworkURLCatalogItem)item).getUrl(UrlInfo.Type.Catalog) != null && + ItemsLoadingService.getRunnable(tree) == null; + } + + @Override + public void run(NetworkTree tree) { + if (ItemsLoadingService.getRunnable(tree) != null) { + return; + } + NetworkCatalogActions.clearTree(myActivity, (NetworkCatalogTree)tree); + ItemsLoadingService.start( + myActivity, + tree, + new NetworkCatalogActions.CatalogExpander(myActivity, (NetworkCatalogTree)tree, false, false) + ); + } +} diff --git a/src/org/geometerplus/android/fbreader/network/action/RemoveCustomCatalogAction.java b/src/org/geometerplus/android/fbreader/network/action/RemoveCustomCatalogAction.java new file mode 100644 index 000000000..39e262a4f --- /dev/null +++ b/src/org/geometerplus/android/fbreader/network/action/RemoveCustomCatalogAction.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2010-2011 Geometer Plus + * + * 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.network.action; + +import android.app.Activity; +import android.content.Intent; + +import org.geometerplus.fbreader.network.NetworkLibrary; +import org.geometerplus.fbreader.network.NetworkTree; +import org.geometerplus.fbreader.network.ICustomNetworkLink; +import org.geometerplus.fbreader.network.tree.NetworkCatalogTree; + +import org.geometerplus.android.fbreader.network.NetworkView; + +public class RemoveCustomCatalogAction extends CatalogAction { + public RemoveCustomCatalogAction(Activity activity) { + super(activity, ActionCode.CUSTOM_CATALOG_REMOVE, "removeCustomCatalog"); + } + + @Override + public boolean isVisible(NetworkTree tree) { + return + super.isVisible(tree) && + ((NetworkCatalogTree)tree).Item.Link instanceof ICustomNetworkLink; + } + + @Override + public void run(NetworkTree tree) { + final NetworkLibrary library = NetworkLibrary.Instance(); + library.removeCustomLink((ICustomNetworkLink)((NetworkCatalogTree)tree).Item.Link); + library.synchronize(); + NetworkView.Instance().fireModelChangedAsync(); + } +} diff --git a/src/org/geometerplus/android/fbreader/network/action/SignOutAction.java b/src/org/geometerplus/android/fbreader/network/action/SignOutAction.java index a96af6a0e..dfb57f70f 100644 --- a/src/org/geometerplus/android/fbreader/network/action/SignOutAction.java +++ b/src/org/geometerplus/android/fbreader/network/action/SignOutAction.java @@ -23,36 +23,36 @@ import android.app.Activity; import org.geometerplus.fbreader.network.NetworkTree; import org.geometerplus.fbreader.network.NetworkCatalogItem; -import org.geometerplus.fbreader.network.tree.NetworkCatalogTree; +import org.geometerplus.fbreader.network.tree.NetworkCatalogRootTree; import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager; import org.geometerplus.android.fbreader.network.NetworkCatalogActions; -public class SignOutAction extends CatalogAction { +public class SignOutAction extends Action { public SignOutAction(Activity activity) { - super(activity, ActionCode.SIGNOUT, "signOut"); + super(activity, ActionCode.SIGNOUT, "signOut", -1); } @Override public boolean isVisible(NetworkTree tree) { - if (!super.isVisible(tree)) { + if (!(tree instanceof NetworkCatalogRootTree)) { return false; } - final NetworkCatalogItem item = ((NetworkCatalogTree)tree).Item; + final NetworkCatalogItem item = ((NetworkCatalogRootTree)tree).Item; final NetworkAuthenticationManager mgr = item.Link.authenticationManager(); return mgr != null && mgr.mayBeAuthorised(false); } @Override public void run(NetworkTree tree) { - NetworkCatalogActions.doSignOut(myActivity, (NetworkCatalogTree)tree); + NetworkCatalogActions.doSignOut(myActivity, (NetworkCatalogRootTree)tree); } @Override public String getOptionsLabel(NetworkTree tree) { final NetworkAuthenticationManager mgr = - (((NetworkCatalogTree)tree).Item).Link.authenticationManager(); + (((NetworkCatalogRootTree)tree).Item).Link.authenticationManager(); final String userName = mgr != null && mgr.mayBeAuthorised(false) ? mgr.currentUserName() : ""; return super.getOptionsLabel(tree).replace("%s", userName); @@ -61,7 +61,7 @@ public class SignOutAction extends CatalogAction { @Override public String getContextLabel(NetworkTree tree) { final NetworkAuthenticationManager mgr = - (((NetworkCatalogTree)tree).Item).Link.authenticationManager(); + (((NetworkCatalogRootTree)tree).Item).Link.authenticationManager(); final String userName = mgr != null && mgr.mayBeAuthorised(false) ? mgr.currentUserName() : ""; return super.getContextLabel(tree).replace("%s", userName); diff --git a/src/org/geometerplus/android/fbreader/tree/BaseActivity.java b/src/org/geometerplus/android/fbreader/tree/BaseActivity.java index beb54e5f5..0729ded4f 100644 --- a/src/org/geometerplus/android/fbreader/tree/BaseActivity.java +++ b/src/org/geometerplus/android/fbreader/tree/BaseActivity.java @@ -60,9 +60,13 @@ public abstract class BaseActivity extends ListActivity { } @Override - protected void onNewIntent(Intent intent) { + protected void onNewIntent(final Intent intent) { if (OPEN_TREE_ACTION.equals(intent.getAction())) { - init(intent); + runOnUiThread(new Runnable() { + public void run() { + init(intent); + } + }); } else { super.onNewIntent(intent); }