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

see previous comment

This commit is contained in:
Nikolay Pultsin 2011-03-19 13:36:41 +00:00
parent d72b9d5596
commit f6ff196930
12 changed files with 116 additions and 75 deletions

View file

@ -390,7 +390,7 @@ class NetworkBookActions extends NetworkTreeActions {
buttonKey = "refillAccount";
action = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new RefillAccountActions().runStandalone(activity, book.Link);
new TopupActions().runStandalone(activity, book.Link);
}
};
} else {

View file

@ -126,12 +126,12 @@ public class NetworkBookInfoActivity extends Activity implements NetworkView.Eve
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
NetworkView.Instance().getTopUpActions().buildContextMenu(this, menu, myBook.Link);
NetworkView.Instance().getTopupActions().buildContextMenu(this, menu, myBook.Link);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
RefillAccountActions.runAction(this, myBook.Link, item.getItemId());
TopupActions.runAction(this, myBook.Link, item.getItemId());
return true;
}

View file

@ -51,7 +51,7 @@ class NetworkCatalogActions extends NetworkTreeActions {
public static final int SIGNUP_ITEM_ID = 3;
public static final int SIGNIN_ITEM_ID = 4;
public static final int SIGNOUT_ITEM_ID = 5;
public static final int REFILL_ACCOUNT_ITEM_ID = 6;
public static final int TOPUP_ITEM_ID = 6;
public static final int CUSTOM_CATALOG_EDIT = 7;
public static final int CUSTOM_CATALOG_REMOVE = 8;
@ -95,10 +95,10 @@ class NetworkCatalogActions extends NetworkTreeActions {
if (mgr != null) {
if (mgr.mayBeAuthorised(false)) {
addMenuItem(menu, SIGNOUT_ITEM_ID, "signOut", mgr.currentUserName());
if (mgr.refillAccountLink() != null) {
if (Util.isTopupSupported(activity, item.Link)) {
final String account = mgr.currentAccount();
if (account != null) {
addMenuItem(menu, REFILL_ACCOUNT_ITEM_ID, "refillAccount", account);
addMenuItem(menu, TOPUP_ITEM_ID, "refillAccount", account);
}
}
} else {
@ -161,7 +161,7 @@ class NetworkCatalogActions extends NetworkTreeActions {
addOptionsItem(menu, SIGNIN_ITEM_ID, "signIn");
addOptionsItem(menu, SIGNUP_ITEM_ID, "signUp");
addOptionsItem(menu, SIGNOUT_ITEM_ID, "signOut", "");
addOptionsItem(menu, REFILL_ACCOUNT_ITEM_ID, "refillAccount");
addOptionsItem(menu, TOPUP_ITEM_ID, "refillAccount");
if (((NetworkCatalogTree)tree).Item instanceof BasketItem) {
addOptionsItem(menu, BASKET_CLEAR, "clearBasket");
addOptionsItem(menu, BASKET_BUY_ALL_BOOKS, "buyAllBooks");
@ -182,7 +182,7 @@ class NetworkCatalogActions extends NetworkTreeActions {
boolean signIn = false;
boolean signOut = false;
boolean refill = false;
boolean topup = false;
String userName = null;
String account = null;
NetworkAuthenticationManager mgr = item.Link.authenticationManager();
@ -191,8 +191,8 @@ class NetworkCatalogActions extends NetworkTreeActions {
userName = mgr.currentUserName();
signOut = true;
account = mgr.currentAccount();
if (mgr.refillAccountLink() != null && account != null) {
refill = true;
if (account != null && Util.isTopupSupported(activity, item.Link)) {
topup = true;
}
} else {
signIn = true;
@ -204,7 +204,7 @@ class NetworkCatalogActions extends NetworkTreeActions {
prepareOptionsItem(menu, SIGNIN_ITEM_ID, signIn);
prepareOptionsItem(menu, SIGNUP_ITEM_ID, signIn & Util.isRegistrationSupported(activity, item.Link));
prepareOptionsItem(menu, SIGNOUT_ITEM_ID, signOut, "signOut", userName);
prepareOptionsItem(menu, REFILL_ACCOUNT_ITEM_ID, refill);
prepareOptionsItem(menu, TOPUP_ITEM_ID, topup);
return true;
}
@ -267,8 +267,8 @@ class NetworkCatalogActions extends NetworkTreeActions {
case SIGNOUT_ITEM_ID:
doSignOut(activity, catalogTree);
return true;
case REFILL_ACCOUNT_ITEM_ID:
new RefillAccountActions().runStandalone(activity, item.Link);
case TOPUP_ITEM_ID:
new TopupActions().runStandalone(activity, item.Link);
return true;
case CUSTOM_CATALOG_EDIT:
{

View file

@ -66,8 +66,8 @@ public class NetworkCatalogActivity extends NetworkBaseActivity implements UserR
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
if (menuInfo == null && myTree instanceof NetworkCatalogTree) {
final INetworkLink link = ((NetworkCatalogTree)myTree).Item.Link;
if (Util.isAccountRefillingSupported(this, link)) {
final RefillAccountActions actions = NetworkView.Instance().getTopUpActions();
if (Util.isTopupSupported(this, link)) {
final TopupActions actions = NetworkView.Instance().getTopupActions();
if (actions != null) {
actions.buildContextMenu(this, menu, link);
return;
@ -81,8 +81,8 @@ public class NetworkCatalogActivity extends NetworkBaseActivity implements UserR
public boolean onContextItemSelected(MenuItem item) {
if ((item == null || item.getMenuInfo() == null) && myTree instanceof NetworkCatalogTree) {
final INetworkLink link = ((NetworkCatalogTree)myTree).Item.Link;
if (Util.isAccountRefillingSupported(this, link)) {
final RefillAccountActions actions = NetworkView.Instance().getTopUpActions();
if (Util.isTopupSupported(this, link)) {
final TopupActions actions = NetworkView.Instance().getTopupActions();
if (actions != null && actions.runAction(this, link, item.getItemId())) {
return true;
}

View file

@ -47,7 +47,7 @@ class NetworkView {
private volatile boolean myInitialized;
private final ArrayList<NetworkTreeActions> myActions = new ArrayList<NetworkTreeActions>();
private RefillAccountActions myTopUpActions;
private TopupActions myTopupActions;
private NetworkView() {
}
@ -66,8 +66,8 @@ class NetworkView {
myActions.add(new NetworkBookActions());
myActions.add(new NetworkCatalogActions());
myActions.add(new SearchItemActions());
myTopUpActions = new RefillAccountActions();
myActions.add(myTopUpActions);
myTopupActions = new TopupActions();
myActions.add(myTopupActions);
myActions.add(new AddCustomCatalogItemActions());
myActions.trimToSize();
@ -91,8 +91,8 @@ class NetworkView {
* NetworkItem's actions
*/
public RefillAccountActions getTopUpActions() {
return myTopUpActions;
public TopupActions getTopupActions() {
return myTopupActions;
}
public NetworkTreeActions getActions(NetworkTree tree) {

View file

@ -29,10 +29,10 @@ import org.geometerplus.fbreader.network.NetworkTree;
import org.geometerplus.fbreader.network.tree.TopUpTree;
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
class RefillAccountActions extends NetworkTreeActions {
public static final int REFILL_VIA_SMS_ITEM_ID = 0;
public static final int REFILL_VIA_BROWSER_ITEM_ID = 1;
class TopupActions extends NetworkTreeActions {
public static final int TOPUP_VIA_SMS_ITEM_ID = 0;
public static final int TOPUP_VIA_BROWSER_ITEM_ID = 1;
public static final int TOPUP_VIA_CREDIT_CARD_ITEM_ID = 2;
@Override
public boolean canHandleTree(NetworkTree tree) {
@ -47,11 +47,14 @@ class RefillAccountActions extends NetworkTreeActions {
void buildContextMenu(Activity activity, ContextMenu menu, INetworkLink link) {
menu.setHeaderTitle(getTitleValue("refillTitle"));
if (Util.isSmsAccountRefillingSupported(activity, link)) {
addMenuItem(menu, REFILL_VIA_SMS_ITEM_ID, "refillViaSms");
if (Util.isSmsTopupSupported(activity, link)) {
addMenuItem(menu, TOPUP_VIA_CREDIT_CARD_ITEM_ID, "refillViaCreditCard");
}
if (Util.isBrowserAccountRefillingSupported(activity, link)) {
addMenuItem(menu, REFILL_VIA_BROWSER_ITEM_ID, "refillViaBrowser");
if (Util.isSmsTopupSupported(activity, link)) {
addMenuItem(menu, TOPUP_VIA_SMS_ITEM_ID, "refillViaSms");
}
if (Util.isBrowserTopupSupported(activity, link)) {
addMenuItem(menu, TOPUP_VIA_BROWSER_ITEM_ID, "refillViaBrowser");
}
}
@ -60,15 +63,22 @@ class RefillAccountActions extends NetworkTreeActions {
return getDefaultActionCode(activity, ((TopUpTree)tree).Item.Link);
}
private int getDefaultActionCode(Activity activity, INetworkLink link) {
final boolean sms = Util.isSmsAccountRefillingSupported(activity, link);
final boolean browser = Util.isBrowserAccountRefillingSupported(activity, link);
final boolean sms = Util.isSmsTopupSupported(activity, link);
final boolean browser = Util.isBrowserTopupSupported(activity, link);
final boolean creditCard = Util.isCreditCardTopupSupported(activity, link);
final int count =
(sms ? 1 : 0) +
(browser ? 1 : 0) +
(creditCard ? 1 : 0);
if (sms && browser) {
if (count > 1) {
return TREE_SHOW_CONTEXT_MENU;
} else if (sms) {
return REFILL_VIA_SMS_ITEM_ID;
return TOPUP_VIA_SMS_ITEM_ID;
} else if (creditCard) {
return TOPUP_VIA_CREDIT_CARD_ITEM_ID;
} else /* if (browser) */ {
return REFILL_VIA_BROWSER_ITEM_ID;
return TOPUP_VIA_BROWSER_ITEM_ID;
}
}
@ -94,51 +104,62 @@ class RefillAccountActions extends NetworkTreeActions {
}
static boolean runAction(Activity activity, INetworkLink link, int actionCode) {
Runnable refillRunnable = null;
Runnable topupRunnable = null;
switch (actionCode) {
case REFILL_VIA_SMS_ITEM_ID:
refillRunnable = smsRefillRunnable(activity, link);
case TOPUP_VIA_SMS_ITEM_ID:
topupRunnable = smsTopupRunnable(activity, link);
break;
case REFILL_VIA_BROWSER_ITEM_ID:
refillRunnable = browserRefillRunnable(activity, link);
case TOPUP_VIA_BROWSER_ITEM_ID:
topupRunnable = browserTopupRunnable(activity, link);
break;
case TOPUP_VIA_CREDIT_CARD_ITEM_ID:
topupRunnable = creditCardTopupRunnable(activity, link);
break;
}
if (refillRunnable == null) {
if (topupRunnable == null) {
return false;
}
doRefill(activity, link, refillRunnable);
doTopup(activity, link, topupRunnable);
return true;
}
private static Runnable browserRefillRunnable(final Activity activity, final INetworkLink link) {
private static Runnable browserTopupRunnable(final Activity activity, final INetworkLink link) {
return new Runnable() {
public void run() {
Util.openInBrowser(
activity,
link.authenticationManager().refillAccountLink()
link.authenticationManager().topupLink()
);
}
};
}
private static Runnable smsRefillRunnable(final Activity activity, final INetworkLink link) {
private static Runnable smsTopupRunnable(final Activity activity, final INetworkLink link) {
return new Runnable() {
public void run() {
Util.runSmsDialog(activity, link);
Util.runSmsTopupDialog(activity, link);
}
};
}
private static void doRefill(final Activity activity, final INetworkLink link, final Runnable refiller) {
private static Runnable creditCardTopupRunnable(final Activity activity, final INetworkLink link) {
return new Runnable() {
public void run() {
Util.runCreditCardTopupDialog(activity, link);
}
};
}
private static void doTopup(final Activity activity, final INetworkLink link, final Runnable action) {
final NetworkAuthenticationManager mgr = link.authenticationManager();
if (mgr.mayBeAuthorised(false)) {
refiller.run();
action.run();
} else {
AuthenticationDialog.show(activity, link, new Runnable() {
public void run() {
if (mgr.mayBeAuthorised(false)) {
refiller.run();
action.run();
}
}
});
@ -146,8 +167,8 @@ class RefillAccountActions extends NetworkTreeActions {
}
public void runStandalone(Activity activity, INetworkLink link) {
final int refillActionCode = getDefaultActionCode(activity, link);
if (refillActionCode == TREE_SHOW_CONTEXT_MENU) {
final int topupActionCode = getDefaultActionCode(activity, link);
if (topupActionCode == TREE_SHOW_CONTEXT_MENU) {
//activity.getListView().showContextMenu();
View view = null;
if (activity instanceof NetworkBaseActivity) {
@ -158,8 +179,8 @@ class RefillAccountActions extends NetworkTreeActions {
if (view != null) {
view.showContextMenu();
}
} else if (refillActionCode >= 0) {
runAction(activity, link, refillActionCode);
} else if (topupActionCode >= 0) {
runAction(activity, link, topupActionCode);
}
}
}

View file

@ -38,8 +38,10 @@ import org.geometerplus.android.util.PackageUtil;
abstract class Util implements UserRegistrationConstants {
private static final String REGISTRATION_ACTION =
"android.fbreader.action.NETWORK_LIBRARY_REGISTER";
private static final String SMS_REFILLING_ACTION =
private static final String SMS_TOPUP_ACTION =
"android.fbreader.action.NETWORK_LIBRARY_SMS_REFILLING";
private static final String CREDIT_CARD_TOPUP_ACTION =
"android.fbreader.action.NETWORK_LIBRARY_CREDIT_CARD_TOPUP";
private static boolean testService(Activity activity, String action, String url) {
return url != null && PackageUtil.canBeStarted(activity, new Intent(action, Uri.parse(url)), true);
@ -83,29 +85,46 @@ abstract class Util implements UserRegistrationConstants {
}
}
static boolean isAccountRefillingSupported(Activity activity, INetworkLink link) {
static boolean isTopupSupported(Activity activity, INetworkLink link) {
return
isBrowserAccountRefillingSupported(activity, link) ||
isSmsAccountRefillingSupported(activity, link);
isBrowserTopupSupported(activity, link) ||
isCreditCardTopupSupported(activity, link) ||
isSmsTopupSupported(activity, link);
}
static boolean isSmsAccountRefillingSupported(Activity activity, INetworkLink link) {
static boolean isSmsTopupSupported(Activity activity, INetworkLink link) {
return testService(
activity,
SMS_REFILLING_ACTION,
SMS_TOPUP_ACTION,
link.getUrlInfo(INetworkLink.URL_MAIN).URL
);
}
static void runSmsDialog(Activity activity, INetworkLink link) {
static boolean isCreditCardTopupSupported(Activity activity, INetworkLink link) {
return testService(
activity,
CREDIT_CARD_TOPUP_ACTION,
link.getUrlInfo(INetworkLink.URL_MAIN).URL
);
}
static void runSmsTopupDialog(Activity activity, INetworkLink link) {
runTopupDialog(activity, link, SMS_TOPUP_ACTION);
}
static void runCreditCardTopupDialog(Activity activity, INetworkLink link) {
runTopupDialog(activity, link, CREDIT_CARD_TOPUP_ACTION);
}
private static void runTopupDialog(Activity activity, INetworkLink link, String action) {
try {
final Intent intent = new Intent(
SMS_REFILLING_ACTION,
action,
Uri.parse(link.getUrlInfo(INetworkLink.URL_MAIN).URL)
);
final NetworkAuthenticationManager mgr = link.authenticationManager();
if (mgr != null) {
for (Map.Entry<String,String> entry : mgr.getSmsRefillingData().entrySet()) {
for (Map.Entry<String,String> entry : mgr.getTopupData().entrySet()) {
intent.putExtra(entry.getKey(), entry.getValue());
}
}
@ -116,8 +135,8 @@ abstract class Util implements UserRegistrationConstants {
}
}
static boolean isBrowserAccountRefillingSupported(Activity activity, INetworkLink link) {
return link.getUrlInfo(INetworkLink.URL_REFILL_ACCOUNT).URL != null;
static boolean isBrowserTopupSupported(Activity activity, INetworkLink link) {
return link.getUrlInfo(INetworkLink.URL_TOPUP).URL != null;
}
static void openInBrowser(Context context, String url) {

View file

@ -33,7 +33,7 @@ public interface INetworkLink {
String URL_SIGN_IN = "signIn";
String URL_SIGN_OUT = "signOut";
String URL_SIGN_UP = "signUp";
String URL_REFILL_ACCOUNT = "refillAccount";
String URL_TOPUP = "topup";
String URL_RECOVER_PASSWORD = "recoverPassword";
String getSiteName();

View file

@ -63,8 +63,6 @@ public abstract class NetworkAuthenticationManager {
public abstract void authorise(String password) throws ZLNetworkException;
public abstract void logOut();
public abstract BookReference downloadReference(NetworkBookItem book);
public abstract Map<String,String> getSmsRefillingData();
public final boolean mayBeAuthorised(boolean useNetwork) {
try {
@ -107,12 +105,15 @@ public abstract class NetworkAuthenticationManager {
//public abstract ZLNetworkSSLCertificate certificate();
/*
* refill account
* topup account
*/
public String refillAccountLink() {
public String topupLink() {
return null;
}
public Map<String,String> getTopupData() {
return Collections.emptyMap();
}
public abstract void initUser(String userName, String sid) throws ZLNetworkException;

View file

@ -233,7 +233,7 @@ public class LitResAuthenticationManager extends NetworkAuthenticationManager {
}
@Override
public String refillAccountLink() {
public String topupLink() {
final String sid;
synchronized (this) {
sid = mySidOption.getValue();
@ -241,7 +241,7 @@ public class LitResAuthenticationManager extends NetworkAuthenticationManager {
if (sid.length() == 0) {
return null;
}
final String url = Link.getUrlInfo(INetworkLink.URL_REFILL_ACCOUNT).URL;
final String url = Link.getUrlInfo(INetworkLink.URL_TOPUP).URL;
if (url == null) {
return null;
}
@ -411,7 +411,7 @@ public class LitResAuthenticationManager extends NetworkAuthenticationManager {
}
@Override
public Map<String,String> getSmsRefillingData() {
public Map<String,String> getTopupData() {
final HashMap<String,String> map = new HashMap<String,String>();
map.put("litres:userId", myUserIdOption.getValue());
map.put("litres:sid", mySidOption.getValue());

View file

@ -57,7 +57,7 @@ interface OPDSConstants {
String REL_LINK_SIGN_IN = "http://data.fbreader.org/catalog/sign-in";
String REL_LINK_SIGN_OUT = "http://data.fbreader.org/catalog/sign-out";
String REL_LINK_SIGN_UP = "http://data.fbreader.org/catalog/sign-up";
String REL_LINK_REFILL_ACCOUNT = "http://data.fbreader.org/catalog/refill-account";
String REL_LINK_TOPUP = "http://data.fbreader.org/catalog/refill-account";
String REL_LINK_RECOVER_PASSWORD = "http://data.fbreader.org/catalog/recover-password";
// Entry level / OPDS Link Conditions

View file

@ -133,8 +133,8 @@ class OPDSLinkXMLReader extends OPDSXMLReader implements OPDSConstants, MimeType
infos.put(INetworkLink.URL_SIGN_OUT, new UrlInfo(href));
} else if (rel == REL_LINK_SIGN_UP) {
infos.put(INetworkLink.URL_SIGN_UP, new UrlInfo(href));
} else if (rel == REL_LINK_REFILL_ACCOUNT) {
infos.put(INetworkLink.URL_REFILL_ACCOUNT, new UrlInfo(href));
} else if (rel == REL_LINK_TOPUP) {
infos.put(INetworkLink.URL_TOPUP, new UrlInfo(href));
} else if (rel == REL_LINK_RECOVER_PASSWORD) {
infos.put(INetworkLink.URL_RECOVER_PASSWORD, new UrlInfo(href));
} else if (rel == REL_CONDITION_NEVER) {