mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-06 03:50:19 +02:00
186 lines
5.7 KiB
Java
186 lines
5.7 KiB
Java
/*
|
|
* Copyright (C) 2010-2011 Geometer Plus <contact@geometerplus.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
* 02110-1301, USA.
|
|
*/
|
|
|
|
package org.geometerplus.android.fbreader.network;
|
|
|
|
import android.app.Activity;
|
|
import android.view.View;
|
|
import android.view.Menu;
|
|
import android.view.ContextMenu;
|
|
|
|
import org.geometerplus.fbreader.network.INetworkLink;
|
|
import org.geometerplus.fbreader.network.NetworkTree;
|
|
import org.geometerplus.fbreader.network.tree.TopUpTree;
|
|
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
|
|
|
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) {
|
|
return tree instanceof TopUpTree;
|
|
}
|
|
|
|
@Override
|
|
public void buildContextMenu(Activity activity, ContextMenu menu, NetworkTree tree) {
|
|
buildContextMenu(activity, menu, ((TopUpTree)tree).Item.Link);
|
|
}
|
|
|
|
void buildContextMenu(Activity activity, ContextMenu menu, INetworkLink link) {
|
|
menu.setHeaderTitle(getTitleValue("refillTitle"));
|
|
|
|
if (Util.isSmsTopupSupported(activity, link)) {
|
|
addMenuItem(menu, TOPUP_VIA_CREDIT_CARD_ITEM_ID, "refillViaCreditCard");
|
|
}
|
|
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");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getDefaultActionCode(NetworkBaseActivity activity, NetworkTree tree) {
|
|
return getDefaultActionCode(activity, ((TopUpTree)tree).Item.Link);
|
|
}
|
|
private int getDefaultActionCode(Activity activity, INetworkLink 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 (count > 1) {
|
|
return TREE_SHOW_CONTEXT_MENU;
|
|
} else if (sms) {
|
|
return TOPUP_VIA_SMS_ITEM_ID;
|
|
} else if (creditCard) {
|
|
return TOPUP_VIA_CREDIT_CARD_ITEM_ID;
|
|
} else /* if (browser) */ {
|
|
return TOPUP_VIA_BROWSER_ITEM_ID;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String getConfirmText(NetworkTree tree, int actionCode) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public boolean createOptionsMenu(Menu menu, NetworkTree tree) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean prepareOptionsMenu(NetworkBaseActivity activity, Menu menu, NetworkTree tree) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean runAction(NetworkBaseActivity activity, NetworkTree tree, int actionCode) {
|
|
final INetworkLink link = ((TopUpTree)tree).Item.Link;
|
|
return runAction(activity, link, actionCode);
|
|
}
|
|
|
|
static boolean runAction(Activity activity, INetworkLink link, int actionCode) {
|
|
Runnable topupRunnable = null;
|
|
switch (actionCode) {
|
|
case TOPUP_VIA_SMS_ITEM_ID:
|
|
topupRunnable = smsTopupRunnable(activity, link);
|
|
break;
|
|
case TOPUP_VIA_BROWSER_ITEM_ID:
|
|
topupRunnable = browserTopupRunnable(activity, link);
|
|
break;
|
|
case TOPUP_VIA_CREDIT_CARD_ITEM_ID:
|
|
topupRunnable = creditCardTopupRunnable(activity, link);
|
|
break;
|
|
}
|
|
|
|
if (topupRunnable == null) {
|
|
return false;
|
|
}
|
|
doTopup(activity, link, topupRunnable);
|
|
return true;
|
|
}
|
|
|
|
private static Runnable browserTopupRunnable(final Activity activity, final INetworkLink link) {
|
|
return new Runnable() {
|
|
public void run() {
|
|
Util.openInBrowser(
|
|
activity,
|
|
link.authenticationManager().topupLink()
|
|
);
|
|
}
|
|
};
|
|
}
|
|
|
|
private static Runnable smsTopupRunnable(final Activity activity, final INetworkLink link) {
|
|
return new Runnable() {
|
|
public void run() {
|
|
Util.runSmsTopupDialog(activity, link);
|
|
}
|
|
};
|
|
}
|
|
|
|
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)) {
|
|
action.run();
|
|
} else {
|
|
AuthenticationDialog.show(activity, link, new Runnable() {
|
|
public void run() {
|
|
if (mgr.mayBeAuthorised(false)) {
|
|
action.run();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
public void runStandalone(Activity activity, INetworkLink link) {
|
|
final int topupActionCode = getDefaultActionCode(activity, link);
|
|
if (topupActionCode == TREE_SHOW_CONTEXT_MENU) {
|
|
//activity.getListView().showContextMenu();
|
|
View view = null;
|
|
if (activity instanceof NetworkBaseActivity) {
|
|
view = ((NetworkBaseActivity)activity).getListView();
|
|
} else if (activity instanceof NetworkBookInfoActivity) {
|
|
view = ((NetworkBookInfoActivity)activity).getMainView();
|
|
}
|
|
if (view != null) {
|
|
view.showContextMenu();
|
|
}
|
|
} else if (topupActionCode >= 0) {
|
|
runAction(activity, link, topupActionCode);
|
|
}
|
|
}
|
|
}
|