mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
BuyBookActivity
This commit is contained in:
parent
ea4a66ab7f
commit
0a9be397a8
15 changed files with 268 additions and 148 deletions
|
@ -150,6 +150,7 @@
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
<activity android:name="org.geometerplus.android.fbreader.network.AuthenticationActivity" android:process=":networkLibrary" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Dialog"/>
|
<activity android:name="org.geometerplus.android.fbreader.network.AuthenticationActivity" android:process=":networkLibrary" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Dialog"/>
|
||||||
|
<activity android:name="org.geometerplus.android.fbreader.network.BuyBookActivity" android:process=":networkLibrary" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Dialog"/>
|
||||||
<activity android:name="org.geometerplus.android.fbreader.network.AddCustomCatalogActivity" android:process=":networkLibrary" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Dialog">
|
<activity android:name="org.geometerplus.android.fbreader.network.AddCustomCatalogActivity" android:process=":networkLibrary" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Dialog">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.VIEW" />
|
<action android:name="android.intent.action.VIEW" />
|
||||||
|
|
|
@ -150,6 +150,7 @@
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
<activity android:name="org.geometerplus.android.fbreader.network.AuthenticationActivity" android:process=":networkLibrary" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Dialog"/>
|
<activity android:name="org.geometerplus.android.fbreader.network.AuthenticationActivity" android:process=":networkLibrary" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Dialog"/>
|
||||||
|
<activity android:name="org.geometerplus.android.fbreader.network.BuyBookActivity" android:process=":networkLibrary" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Dialog"/>
|
||||||
<activity android:name="org.geometerplus.android.fbreader.network.AddCustomCatalogActivity" android:process=":networkLibrary" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Dialog">
|
<activity android:name="org.geometerplus.android.fbreader.network.AddCustomCatalogActivity" android:process=":networkLibrary" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.Dialog">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.VIEW" />
|
<action android:name="android.intent.action.VIEW" />
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
ignore first 5 books when creating recommendations
|
||||||
do we cache cover images?
|
do we cache cover images?
|
||||||
|
|
||||||
states:
|
states:
|
||||||
|
|
|
@ -89,7 +89,7 @@
|
||||||
<node name="signIn" value="Войти"/>
|
<node name="signIn" value="Войти"/>
|
||||||
<node name="signOut" value="Выйти (%s)"/>
|
<node name="signOut" value="Выйти (%s)"/>
|
||||||
<node name="topup" value="Пополнить счёт (баланс: %s)" />
|
<node name="topup" value="Пополнить счёт (баланс: %s)" />
|
||||||
<node name="topupTitle" value="Пополнить счёт"/>
|
<node name="topupTitle" value="Заплатить"/>
|
||||||
<node name="topupViaBrowser" value="В окне браузера"/>
|
<node name="topupViaBrowser" value="В окне браузера"/>
|
||||||
<node name="topupSummary" value="Текущий счёт: %s"/>
|
<node name="topupSummary" value="Текущий счёт: %s"/>
|
||||||
<node name="basket" value="Корзина"/>
|
<node name="basket" value="Корзина"/>
|
||||||
|
|
22
res/layout/buy_book.xml
Normal file
22
res/layout/buy_book.xml
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
>
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingLeft="10dp"
|
||||||
|
android:paddingRight="10dp"
|
||||||
|
android:orientation="vertical"
|
||||||
|
>
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/buy_book_text"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="10dp"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
|
/>
|
||||||
|
<include layout="@layout/ok_cancel_buttons" android:id="@+id/buy_book_buttons" />
|
||||||
|
</LinearLayout>
|
||||||
|
</ScrollView>
|
|
@ -0,0 +1,162 @@
|
||||||
|
/*
|
||||||
|
* 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 java.util.*;
|
||||||
|
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Button;
|
||||||
|
|
||||||
|
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||||
|
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
|
||||||
|
import org.geometerplus.zlibrary.core.money.Money;
|
||||||
|
|
||||||
|
import org.geometerplus.zlibrary.ui.android.R;
|
||||||
|
|
||||||
|
import org.geometerplus.android.util.UIUtil;
|
||||||
|
import org.geometerplus.android.fbreader.FBReader;
|
||||||
|
|
||||||
|
import org.geometerplus.fbreader.network.*;
|
||||||
|
import org.geometerplus.fbreader.network.tree.NetworkBookTree;
|
||||||
|
import org.geometerplus.fbreader.network.urlInfo.*;
|
||||||
|
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||||
|
|
||||||
|
import org.geometerplus.android.fbreader.network.*;
|
||||||
|
|
||||||
|
public class BuyBookActivity extends Activity {
|
||||||
|
public static void run(Activity activity, NetworkBookTree tree) {
|
||||||
|
final Intent intent = new Intent(activity, BuyBookActivity.class);
|
||||||
|
intent.putExtra(NetworkLibraryActivity.TREE_KEY_KEY, tree.getUniqueKey());
|
||||||
|
activity.startActivity(intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle bundle) {
|
||||||
|
super.onCreate(bundle);
|
||||||
|
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
|
||||||
|
setContentView(R.layout.buy_book);
|
||||||
|
|
||||||
|
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||||
|
final NetworkTree tree = library.getTreeByKey(
|
||||||
|
(NetworkTree.Key)getIntent().getSerializableExtra(
|
||||||
|
NetworkLibraryActivity.TREE_KEY_KEY
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
final NetworkBookItem book;
|
||||||
|
if (tree instanceof NetworkBookTree) {
|
||||||
|
book = ((NetworkBookTree)tree).Book;
|
||||||
|
} else {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final NetworkAuthenticationManager mgr = book.Link.authenticationManager();
|
||||||
|
if (mgr == null) {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final ZLResource dialogResource = ZLResource.resource("dialog");
|
||||||
|
final ZLResource buttonResource = dialogResource.getResource("button");
|
||||||
|
|
||||||
|
final TextView textArea = (TextView)findViewById(R.id.buy_book_text);
|
||||||
|
final Button okButton =
|
||||||
|
(Button)findViewById(R.id.buy_book_buttons).findViewById(R.id.ok_button);
|
||||||
|
final Button cancelButton =
|
||||||
|
(Button)findViewById(R.id.buy_book_buttons).findViewById(R.id.cancel_button);
|
||||||
|
|
||||||
|
final Runnable buyRunnable = new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
mgr.purchaseBook(book);
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
Util.doDownloadBook(BuyBookActivity.this, book, false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
finish();
|
||||||
|
} catch (final ZLNetworkException e) {
|
||||||
|
if (NetworkException.ERROR_PURCHASE_NOT_ENOUGH_MONEY.equals(e.getCode())) {
|
||||||
|
final BookBuyUrlInfo info = book.buyInfo();
|
||||||
|
Money price = info != null ? info.Price : null;
|
||||||
|
if (price != null) {
|
||||||
|
final Money account = mgr.currentAccount();
|
||||||
|
if (account != null) {
|
||||||
|
price = price.subtract(account);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TopupMenuActivity.runMenu(BuyBookActivity.this, book.Link, price);
|
||||||
|
finish();
|
||||||
|
} else {
|
||||||
|
final ZLResource boxResource = dialogResource.getResource("networkError");
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
new AlertDialog.Builder(BuyBookActivity.this)
|
||||||
|
.setTitle(boxResource.getResource("title").getValue())
|
||||||
|
.setMessage(e.getMessage())
|
||||||
|
.setIcon(0)
|
||||||
|
.setPositiveButton(buttonResource.getResource("ok").getValue(), null)
|
||||||
|
.create().show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
library.invalidateVisibility();
|
||||||
|
library.synchronize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
okButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
public void onClick(View v) {
|
||||||
|
UIUtil.wait("purchaseBook", buyRunnable, BuyBookActivity.this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
cancelButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
public void onClick(View v) {
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (book.getStatus() == NetworkBookItem.Status.CanBePurchased) {
|
||||||
|
final ZLResource boxResource = dialogResource.getResource("purchaseConfirmBox");
|
||||||
|
setTitle(boxResource.getResource("title").getValue());
|
||||||
|
textArea.setText(
|
||||||
|
boxResource.getResource("message").getValue().replace("%s", book.Title)
|
||||||
|
);
|
||||||
|
okButton.setText(buttonResource.getResource("buy").getValue());
|
||||||
|
cancelButton.setText(buttonResource.getResource("cancel").getValue());
|
||||||
|
} else {
|
||||||
|
final ZLResource boxResource = dialogResource.getResource("alreadyPurchasedBox");
|
||||||
|
setTitle(boxResource.getResource("title").getValue());
|
||||||
|
textArea.setText(boxResource.getResource("message").getValue());
|
||||||
|
cancelButton.setText(buttonResource.getResource("ok").getValue());
|
||||||
|
okButton.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -55,6 +55,7 @@ import org.geometerplus.android.fbreader.network.action.NetworkBookActions;
|
||||||
import org.geometerplus.android.util.UIUtil;
|
import org.geometerplus.android.util.UIUtil;
|
||||||
|
|
||||||
public class NetworkBookInfoActivity extends Activity implements NetworkLibrary.ChangeListener {
|
public class NetworkBookInfoActivity extends Activity implements NetworkLibrary.ChangeListener {
|
||||||
|
private NetworkBookTree myTree;
|
||||||
private NetworkBookItem myBook;
|
private NetworkBookItem myBook;
|
||||||
private View myMainView;
|
private View myMainView;
|
||||||
|
|
||||||
|
@ -76,11 +77,21 @@ public class NetworkBookInfoActivity extends Activity implements NetworkLibrary.
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
UIUtil.wait("loadingBookInfo", myInitializer, this);
|
if (!myInitializerStarted) {
|
||||||
|
UIUtil.wait("loadingNetworkBookInfo", myInitializer, this);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private volatile boolean myInitializerStarted;
|
||||||
|
|
||||||
private final Runnable myInitializer = new Runnable() {
|
private final Runnable myInitializer = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
|
synchronized (this) {
|
||||||
|
if (myInitializerStarted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
myInitializerStarted = true;
|
||||||
|
}
|
||||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||||
if (!library.isInitialized()) {
|
if (!library.isInitialized()) {
|
||||||
if (SQLiteNetworkDatabase.Instance() == null) {
|
if (SQLiteNetworkDatabase.Instance() == null) {
|
||||||
|
@ -96,6 +107,9 @@ public class NetworkBookInfoActivity extends Activity implements NetworkLibrary.
|
||||||
library.getLinkBySiteName("litres.ru"),
|
library.getLinkBySiteName("litres.ru"),
|
||||||
url.toString().replace("litres-book://", "http://")
|
url.toString().replace("litres-book://", "http://")
|
||||||
);
|
);
|
||||||
|
if (myBook != null) {
|
||||||
|
myTree = library.getFakeBookTree(myBook);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
final NetworkTree tree = library.getTreeByKey(
|
final NetworkTree tree = library.getTreeByKey(
|
||||||
(NetworkTree.Key)getIntent().getSerializableExtra(
|
(NetworkTree.Key)getIntent().getSerializableExtra(
|
||||||
|
@ -103,7 +117,8 @@ public class NetworkBookInfoActivity extends Activity implements NetworkLibrary.
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
if (tree instanceof NetworkBookTree) {
|
if (tree instanceof NetworkBookTree) {
|
||||||
myBook = ((NetworkBookTree)tree).Book;
|
myTree = (NetworkBookTree)tree;
|
||||||
|
myBook = myTree.Book;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -355,7 +370,7 @@ public class NetworkBookInfoActivity extends Activity implements NetworkLibrary.
|
||||||
button.setVisibility(View.VISIBLE);
|
button.setVisibility(View.VISIBLE);
|
||||||
button.setOnClickListener(new View.OnClickListener() {
|
button.setOnClickListener(new View.OnClickListener() {
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
a.run(myBook);
|
a.run(myTree);
|
||||||
NetworkBookInfoActivity.this.updateView();
|
NetworkBookInfoActivity.this.updateView();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -399,9 +414,10 @@ public class NetworkBookInfoActivity extends Activity implements NetworkLibrary.
|
||||||
public void onLibraryChanged(NetworkLibrary.ChangeListener.Code code, Object[] params) {
|
public void onLibraryChanged(NetworkLibrary.ChangeListener.Code code, Object[] params) {
|
||||||
if (code == NetworkLibrary.ChangeListener.Code.InitializationFailed) {
|
if (code == NetworkLibrary.ChangeListener.Code.InitializationFailed) {
|
||||||
// TODO: implement
|
// TODO: implement
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (myBook == null) {
|
if (myBook == null || myTree == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ import org.geometerplus.fbreader.network.*;
|
||||||
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||||
import org.geometerplus.fbreader.network.authentication.litres.LitResAuthenticationManager;
|
import org.geometerplus.fbreader.network.authentication.litres.LitResAuthenticationManager;
|
||||||
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
|
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
|
||||||
|
import org.geometerplus.fbreader.network.urlInfo.BookUrlInfo;
|
||||||
|
|
||||||
import org.geometerplus.android.util.UIUtil;
|
import org.geometerplus.android.util.UIUtil;
|
||||||
import org.geometerplus.android.util.PackageUtil;
|
import org.geometerplus.android.util.PackageUtil;
|
||||||
|
@ -159,4 +160,20 @@ public abstract class Util implements UserRegistrationConstants {
|
||||||
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
|
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void doDownloadBook(Activity activity, final NetworkBookItem book, boolean demo) {
|
||||||
|
final UrlInfo.Type resolvedType =
|
||||||
|
demo ? UrlInfo.Type.BookDemo : UrlInfo.Type.Book;
|
||||||
|
final BookUrlInfo ref = book.reference(resolvedType);
|
||||||
|
if (ref != null) {
|
||||||
|
activity.startService(
|
||||||
|
new Intent(Intent.ACTION_VIEW, Uri.parse(ref.Url),
|
||||||
|
activity.getApplicationContext(), BookDownloaderService.class)
|
||||||
|
.putExtra(BookDownloaderService.BOOK_FORMAT_KEY, ref.BookFormat)
|
||||||
|
.putExtra(BookDownloaderService.REFERENCE_TYPE_KEY, resolvedType)
|
||||||
|
.putExtra(BookDownloaderService.CLEAN_URL_KEY, ref.cleanUrl())
|
||||||
|
.putExtra(BookDownloaderService.TITLE_KEY, book.Title)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ import org.geometerplus.android.util.UIUtil;
|
||||||
import org.geometerplus.android.fbreader.FBReader;
|
import org.geometerplus.android.fbreader.FBReader;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.network.*;
|
import org.geometerplus.fbreader.network.*;
|
||||||
|
import org.geometerplus.fbreader.network.tree.NetworkBookTree;
|
||||||
import org.geometerplus.fbreader.network.urlInfo.*;
|
import org.geometerplus.fbreader.network.urlInfo.*;
|
||||||
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||||
|
|
||||||
|
@ -82,11 +83,7 @@ public abstract class NetworkBookActions {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(NetworkTree tree) {
|
public void run(NetworkTree tree) {
|
||||||
run(getBook(tree));
|
runActionStatic(myActivity, (NetworkBookTree)tree, myId);
|
||||||
}
|
|
||||||
|
|
||||||
public void run(NetworkBookItem book) {
|
|
||||||
runActionStatic(myActivity, book, myId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,58 +147,42 @@ public abstract class NetworkBookActions {
|
||||||
return actions;
|
return actions;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean runActionStatic(Activity activity, NetworkBookItem book, int actionCode) {
|
private static boolean runActionStatic(Activity activity, NetworkBookTree tree, int actionCode) {
|
||||||
switch (actionCode) {
|
switch (actionCode) {
|
||||||
case ActionCode.DOWNLOAD_BOOK:
|
case ActionCode.DOWNLOAD_BOOK:
|
||||||
doDownloadBook(activity, book, false);
|
Util.doDownloadBook(activity, tree.Book, false);
|
||||||
return true;
|
return true;
|
||||||
case ActionCode.DOWNLOAD_DEMO:
|
case ActionCode.DOWNLOAD_DEMO:
|
||||||
doDownloadBook(activity, book, true);
|
Util.doDownloadBook(activity, tree.Book, true);
|
||||||
return true;
|
return true;
|
||||||
case ActionCode.READ_BOOK:
|
case ActionCode.READ_BOOK:
|
||||||
doReadBook(activity, book, false);
|
doReadBook(activity, tree.Book, false);
|
||||||
return true;
|
return true;
|
||||||
case ActionCode.READ_DEMO:
|
case ActionCode.READ_DEMO:
|
||||||
doReadBook(activity, book, true);
|
doReadBook(activity, tree.Book, true);
|
||||||
return true;
|
return true;
|
||||||
case ActionCode.DELETE_BOOK:
|
case ActionCode.DELETE_BOOK:
|
||||||
tryToDeleteBook(activity, book, false);
|
tryToDeleteBook(activity, tree.Book, false);
|
||||||
return true;
|
return true;
|
||||||
case ActionCode.DELETE_DEMO:
|
case ActionCode.DELETE_DEMO:
|
||||||
tryToDeleteBook(activity, book, true);
|
tryToDeleteBook(activity, tree.Book, true);
|
||||||
return true;
|
return true;
|
||||||
case ActionCode.BUY_DIRECTLY:
|
case ActionCode.BUY_DIRECTLY:
|
||||||
doBuyDirectly(activity, book);
|
doBuyDirectly(activity, tree);
|
||||||
return true;
|
return true;
|
||||||
case ActionCode.BUY_IN_BROWSER:
|
case ActionCode.BUY_IN_BROWSER:
|
||||||
doBuyInBrowser(activity, book);
|
doBuyInBrowser(activity, tree.Book);
|
||||||
return true;
|
return true;
|
||||||
case ActionCode.ADD_BOOK_TO_BASKET:
|
case ActionCode.ADD_BOOK_TO_BASKET:
|
||||||
book.Link.getBasketItem().add(book);
|
tree.Book.Link.getBasketItem().add(tree.Book);
|
||||||
return true;
|
return true;
|
||||||
case ActionCode.REMOVE_BOOK_FROM_BASKET:
|
case ActionCode.REMOVE_BOOK_FROM_BASKET:
|
||||||
book.Link.getBasketItem().remove(book);
|
tree.Book.Link.getBasketItem().remove(tree.Book);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void doDownloadBook(Activity activity, final NetworkBookItem book, boolean demo) {
|
|
||||||
final UrlInfo.Type resolvedType =
|
|
||||||
demo ? UrlInfo.Type.BookDemo : UrlInfo.Type.Book;
|
|
||||||
final BookUrlInfo ref = book.reference(resolvedType);
|
|
||||||
if (ref != null) {
|
|
||||||
activity.startService(
|
|
||||||
new Intent(Intent.ACTION_VIEW, Uri.parse(ref.Url),
|
|
||||||
activity.getApplicationContext(), BookDownloaderService.class)
|
|
||||||
.putExtra(BookDownloaderService.BOOK_FORMAT_KEY, ref.BookFormat)
|
|
||||||
.putExtra(BookDownloaderService.REFERENCE_TYPE_KEY, resolvedType)
|
|
||||||
.putExtra(BookDownloaderService.CLEAN_URL_KEY, ref.cleanUrl())
|
|
||||||
.putExtra(BookDownloaderService.TITLE_KEY, book.Title)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void doReadBook(Activity activity, final NetworkBookItem book, boolean demo) {
|
private static void doReadBook(Activity activity, final NetworkBookItem book, boolean demo) {
|
||||||
String local = null;
|
String local = null;
|
||||||
if (!demo) {
|
if (!demo) {
|
||||||
|
@ -252,113 +233,17 @@ public abstract class NetworkBookActions {
|
||||||
.create().show();
|
.create().show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void doBuyDirectly(final Activity activity, final NetworkBookItem book) {
|
private static void doBuyDirectly(Activity activity, NetworkBookTree tree) {
|
||||||
final NetworkAuthenticationManager mgr = book.Link.authenticationManager();
|
final NetworkAuthenticationManager mgr = tree.Book.Link.authenticationManager();
|
||||||
if (mgr == null) {
|
if (mgr == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ZLResource dialogResource = ZLResource.resource("dialog");
|
|
||||||
final ZLResource buttonResource = dialogResource.getResource("button");
|
|
||||||
|
|
||||||
final Runnable buyRunnable = new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
mgr.purchaseBook(book);
|
|
||||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
|
||||||
library.invalidateVisibility();
|
|
||||||
library.synchronize();
|
|
||||||
} catch (final ZLNetworkException e) {
|
|
||||||
final String buttonKey;
|
|
||||||
final DialogInterface.OnClickListener action;
|
|
||||||
if (NetworkException.ERROR_PURCHASE_NOT_ENOUGH_MONEY.equals(e.getCode())) {
|
|
||||||
buttonKey = "topup";
|
|
||||||
action = new DialogInterface.OnClickListener() {
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
final BookBuyUrlInfo info = book.buyInfo();
|
|
||||||
Money price = info != null ? info.Price : null;
|
|
||||||
System.err.println("price = " + price);
|
|
||||||
if (price != null) {
|
|
||||||
final Money account = mgr.currentAccount();
|
|
||||||
System.err.println("account = " + account);
|
|
||||||
if (account != null) {
|
|
||||||
price = price.subtract(account);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.err.println("price = " + price);
|
|
||||||
TopupMenuActivity.runMenu(activity, book.Link, price);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
buttonKey = "ok";
|
|
||||||
action = null;
|
|
||||||
}
|
|
||||||
final ZLResource boxResource = dialogResource.getResource("networkError");
|
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
new AlertDialog.Builder(activity)
|
|
||||||
.setTitle(boxResource.getResource("title").getValue())
|
|
||||||
.setMessage(e.getMessage())
|
|
||||||
.setIcon(0)
|
|
||||||
.setPositiveButton(buttonResource.getResource(buttonKey).getValue(), action)
|
|
||||||
.create().show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
|
||||||
library.invalidateVisibility();
|
|
||||||
library.synchronize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
if (which == DialogInterface.BUTTON_NEGATIVE) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (book.getStatus() != NetworkBookItem.Status.CanBePurchased) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
UIUtil.wait("purchaseBook", buyRunnable, activity);
|
|
||||||
final boolean downloadBook = which == DialogInterface.BUTTON_NEUTRAL;
|
|
||||||
if (downloadBook) {
|
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
doDownloadBook(activity, book, false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
final Runnable buyDialogRunnable = new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
if (book.getStatus() != NetworkBookItem.Status.CanBePurchased) {
|
|
||||||
final ZLResource boxResource = dialogResource.getResource("alreadyPurchasedBox");
|
|
||||||
new AlertDialog.Builder(activity)
|
|
||||||
.setTitle(boxResource.getResource("title").getValue())
|
|
||||||
.setMessage(boxResource.getResource("message").getValue())
|
|
||||||
.setIcon(0)
|
|
||||||
.setPositiveButton(buttonResource.getResource("ok").getValue(), null)
|
|
||||||
.create().show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final ZLResource boxResource = dialogResource.getResource("purchaseConfirmBox");
|
|
||||||
new AlertDialog.Builder(activity)
|
|
||||||
.setTitle(boxResource.getResource("title").getValue())
|
|
||||||
.setMessage(boxResource.getResource("message").getValue().replace("%s", book.Title))
|
|
||||||
.setIcon(0)
|
|
||||||
.setPositiveButton(buttonResource.getResource("buy").getValue(), listener)
|
|
||||||
.setNeutralButton(buttonResource.getResource("buyAndDownload").getValue(), listener)
|
|
||||||
.setNegativeButton(buttonResource.getResource("cancel").getValue(), listener)
|
|
||||||
.create().show();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
try {
|
try {
|
||||||
if (mgr.isAuthorised(true)) {
|
if (mgr.isAuthorised(true)) {
|
||||||
buyDialogRunnable.run();
|
BuyBookActivity.run(activity, tree);
|
||||||
} else {
|
} else {
|
||||||
AccountMenuActivity.runMenu(activity, book.Link);
|
AccountMenuActivity.runMenu(activity, tree.Book.Link);
|
||||||
}
|
}
|
||||||
} catch (ZLNetworkException e) {
|
} catch (ZLNetworkException e) {
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ package org.geometerplus.android.fbreader.network.action;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.network.NetworkTree;
|
import org.geometerplus.fbreader.network.NetworkTree;
|
||||||
import org.geometerplus.fbreader.network.tree.NetworkCatalogTree;
|
import org.geometerplus.fbreader.network.tree.NetworkCatalogRootTree;
|
||||||
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||||
|
|
||||||
import org.geometerplus.android.fbreader.network.Util;
|
import org.geometerplus.android.fbreader.network.Util;
|
||||||
|
@ -34,7 +34,7 @@ public class SignInAction extends Action {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isVisible(NetworkTree tree) {
|
public boolean isVisible(NetworkTree tree) {
|
||||||
if (!(tree instanceof NetworkCatalogTree)) {
|
if (!(tree instanceof NetworkCatalogRootTree)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ import android.app.Activity;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.network.NetworkLibrary;
|
import org.geometerplus.fbreader.network.NetworkLibrary;
|
||||||
import org.geometerplus.fbreader.network.NetworkTree;
|
import org.geometerplus.fbreader.network.NetworkTree;
|
||||||
import org.geometerplus.fbreader.network.tree.NetworkCatalogTree;
|
import org.geometerplus.fbreader.network.tree.NetworkCatalogRootTree;
|
||||||
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||||
|
|
||||||
import org.geometerplus.android.util.UIUtil;
|
import org.geometerplus.android.util.UIUtil;
|
||||||
|
@ -35,7 +35,7 @@ public class SignOutAction extends Action {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isVisible(NetworkTree tree) {
|
public boolean isVisible(NetworkTree tree) {
|
||||||
if (!(tree instanceof NetworkCatalogTree)) {
|
if (!(tree instanceof NetworkCatalogRootTree)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ import android.app.Activity;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.network.INetworkLink;
|
import org.geometerplus.fbreader.network.INetworkLink;
|
||||||
import org.geometerplus.fbreader.network.NetworkTree;
|
import org.geometerplus.fbreader.network.NetworkTree;
|
||||||
import org.geometerplus.fbreader.network.tree.NetworkCatalogTree;
|
import org.geometerplus.fbreader.network.tree.NetworkCatalogRootTree;
|
||||||
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||||
|
|
||||||
import org.geometerplus.android.fbreader.network.Util;
|
import org.geometerplus.android.fbreader.network.Util;
|
||||||
|
@ -35,7 +35,7 @@ public class SignUpAction extends Action {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isVisible(NetworkTree tree) {
|
public boolean isVisible(NetworkTree tree) {
|
||||||
if (!(tree instanceof NetworkCatalogTree)) {
|
if (!(tree instanceof NetworkCatalogRootTree)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -238,4 +238,8 @@ public class NetworkBookItem extends NetworkItem {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getStringId() {
|
||||||
|
return "@Book:" + Id + ":" + Title;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -398,12 +398,23 @@ public class NetworkLibrary {
|
||||||
return myRootTree;
|
return myRootTree;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public NetworkBookTree getFakeBookTree(NetworkBookItem book) {
|
||||||
|
final String id = book.getStringId();
|
||||||
|
for (FBTree tree : myFakeRootTree.subTrees()) {
|
||||||
|
if (tree instanceof NetworkBookTree &&
|
||||||
|
id.equals(tree.getUniqueKey().Id)) {
|
||||||
|
return (NetworkBookTree)tree;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new NetworkBookTree(myFakeRootTree, book, true);
|
||||||
|
}
|
||||||
|
|
||||||
public NetworkCatalogTree getFakeCatalogTree(NetworkCatalogItem item) {
|
public NetworkCatalogTree getFakeCatalogTree(NetworkCatalogItem item) {
|
||||||
final String id = item.getStringId();
|
final String id = item.getStringId();
|
||||||
for (FBTree tree : myFakeRootTree.subTrees()) {
|
for (FBTree tree : myFakeRootTree.subTrees()) {
|
||||||
final NetworkCatalogTree ncTree = (NetworkCatalogTree)tree;
|
if (tree instanceof NetworkCatalogTree &&
|
||||||
if (id.equals(ncTree.getUniqueKey().Id)) {
|
id.equals(tree.getUniqueKey().Id)) {
|
||||||
return ncTree;
|
return (NetworkCatalogTree)tree;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new NetworkCatalogTree(myFakeRootTree, item.Link, item, 0);
|
return new NetworkCatalogTree(myFakeRootTree, item.Link, item, 0);
|
||||||
|
|
|
@ -28,7 +28,7 @@ public class NetworkBookTree extends NetworkTree {
|
||||||
|
|
||||||
private final boolean myShowAuthors;
|
private final boolean myShowAuthors;
|
||||||
|
|
||||||
NetworkBookTree(NetworkTree parent, NetworkBookItem book, boolean showAuthors) {
|
public NetworkBookTree(NetworkTree parent, NetworkBookItem book, boolean showAuthors) {
|
||||||
super(parent);
|
super(parent);
|
||||||
Book = book;
|
Book = book;
|
||||||
myShowAuthors = showAuthors;
|
myShowAuthors = showAuthors;
|
||||||
|
@ -68,6 +68,6 @@ public class NetworkBookTree extends NetworkTree {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getStringId() {
|
protected String getStringId() {
|
||||||
return "@Book:" + Book.Id + ":" + Book.Title;
|
return Book.getStringId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue