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

refactoring: int -> enum

This commit is contained in:
Nikolay Pultsin 2011-04-15 19:27:45 +01:00
parent 399845e223
commit 9cb85dc4e0
8 changed files with 85 additions and 82 deletions

View file

@ -115,7 +115,13 @@ public class BookDownloaderService extends Service {
final String url = uri.toString();
final int bookFormat = intent.getIntExtra(BOOK_FORMAT_KEY, BookReference.Format.NONE);
final int referenceType = intent.getIntExtra(REFERENCE_TYPE_KEY, BookReference.Type.UNKNOWN);
final BookReference.Type referenceType =
(BookReference.Type)intent.getSerializableExtra(REFERENCE_TYPE_KEY);
if (referenceType == null) {
doStop();
return;
}
String cleanURL = intent.getStringExtra(CLEAN_URL_KEY);
if (cleanURL == null) {
cleanURL = url;

View file

@ -65,19 +65,19 @@ class NetworkBookActions extends NetworkTreeActions {
public static final int REMOVE_CATALOG_FROM_FAVORITES = 13;
private static boolean useFullReferences(NetworkBookItem book) {
return book.reference(BookReference.Type.DOWNLOAD_FULL) != null ||
book.reference(BookReference.Type.DOWNLOAD_FULL_CONDITIONAL) != null;
return book.reference(BookReference.Type.Book) != null ||
book.reference(BookReference.Type.BookConditional) != null;
}
private static boolean useDemoReferences(NetworkBookItem book) {
return book.reference(BookReference.Type.DOWNLOAD_DEMO) != null &&
return book.reference(BookReference.Type.BookDemo) != null &&
book.localCopyFileName() == null &&
book.reference(BookReference.Type.DOWNLOAD_FULL) == null;
book.reference(BookReference.Type.Book) == null;
}
private static boolean useBuyReferences(NetworkBookItem book) {
return book.localCopyFileName() == null &&
book.reference(BookReference.Type.DOWNLOAD_FULL) == null;
book.reference(BookReference.Type.Book) == null;
}
@Override
@ -128,7 +128,7 @@ class NetworkBookActions extends NetworkTreeActions {
static int getBookStatus(NetworkBookItem book, BookDownloaderServiceConnection connection) {
if (useFullReferences(book)) {
BookReference reference = book.reference(BookReference.Type.DOWNLOAD_FULL);
BookReference reference = book.reference(BookReference.Type.Book);
if (reference != null
&& connection != null && connection.isBeingDownloaded(reference.URL)) {
return R.drawable.ic_list_download;
@ -139,8 +139,8 @@ class NetworkBookActions extends NetworkTreeActions {
}
}
if (useBuyReferences(book)
&& book.reference(BookReference.Type.BUY) != null
|| book.reference(BookReference.Type.BUY_IN_BROWSER) != null) {
&& book.reference(BookReference.Type.BookBuy) != null
|| book.reference(BookReference.Type.BookBuyInBrowser) != null) {
return R.drawable.ic_list_buy;
}
return 0;
@ -149,7 +149,7 @@ class NetworkBookActions extends NetworkTreeActions {
static Set<Action> getContextMenuActions(NetworkBookItem book, BookDownloaderServiceConnection connection) {
LinkedHashSet<Action> actions = new LinkedHashSet<Action>();
if (useFullReferences(book)) {
BookReference reference = book.reference(BookReference.Type.DOWNLOAD_FULL);
BookReference reference = book.reference(BookReference.Type.Book);
if (reference != null
&& connection != null && connection.isBeingDownloaded(reference.URL)) {
actions.add(new Action(TREE_NO_ACTION, "alreadyDownloading"));
@ -161,10 +161,10 @@ class NetworkBookActions extends NetworkTreeActions {
}
}
if (useDemoReferences(book)) {
BookReference reference = book.reference(BookReference.Type.DOWNLOAD_DEMO);
BookReference reference = book.reference(BookReference.Type.BookDemo);
if (connection != null && connection.isBeingDownloaded(reference.URL)) {
actions.add(new Action(TREE_NO_ACTION, "alreadyDownloadingDemo"));
} else if (reference.localCopyFileName(BookReference.Type.DOWNLOAD_DEMO) != null) {
} else if (reference.localCopyFileName(BookReference.Type.BookDemo) != null) {
actions.add(new Action(READ_DEMO_ITEM_ID, "readDemo"));
actions.add(new Action(DELETE_DEMO_ITEM_ID, "deleteDemo"));
} else {
@ -174,11 +174,11 @@ class NetworkBookActions extends NetworkTreeActions {
if (useBuyReferences(book)) {
int id = TREE_NO_ACTION;
BookReference reference = null;
if (book.reference(BookReference.Type.BUY) != null) {
reference = book.reference(BookReference.Type.BUY);
if (book.reference(BookReference.Type.BookBuy) != null) {
reference = book.reference(BookReference.Type.BookBuy);
id = BUY_DIRECTLY_ITEM_ID;
} else if (book.reference(BookReference.Type.BUY_IN_BROWSER) != null) {
reference = book.reference(BookReference.Type.BUY_IN_BROWSER);
} else if (book.reference(BookReference.Type.BookBuyInBrowser) != null) {
reference = book.reference(BookReference.Type.BookBuyInBrowser);
id = BUY_IN_BROWSER_ITEM_ID;
}
if (reference != null) {
@ -283,8 +283,9 @@ class NetworkBookActions extends NetworkTreeActions {
}
private static void doDownloadBook(Activity activity, final NetworkBookItem book, boolean demo) {
int resolvedType = demo ? BookReference.Type.DOWNLOAD_DEMO : BookReference.Type.DOWNLOAD_FULL;
BookReference ref = book.reference(resolvedType);
final BookReference.Type resolvedType =
demo ? BookReference.Type.BookDemo : BookReference.Type.Book;
final BookReference ref = book.reference(resolvedType);
if (ref != null) {
final String sslCertificate;
if (book.Link.authenticationManager() != null) {
@ -309,9 +310,9 @@ class NetworkBookActions extends NetworkTreeActions {
if (!demo) {
local = book.localCopyFileName();
} else {
BookReference reference = book.reference(BookReference.Type.DOWNLOAD_DEMO);
BookReference reference = book.reference(BookReference.Type.BookDemo);
if (reference != null) {
local = reference.localCopyFileName(BookReference.Type.DOWNLOAD_DEMO);
local = reference.localCopyFileName(BookReference.Type.BookDemo);
}
}
if (local != null) {
@ -339,9 +340,9 @@ class NetworkBookActions extends NetworkTreeActions {
if (!demo) {
book.removeLocalFiles();
} else {
final BookReference reference = book.reference(BookReference.Type.DOWNLOAD_DEMO);
final BookReference reference = book.reference(BookReference.Type.BookDemo);
if (reference != null) {
final String fileName = reference.localCopyFileName(BookReference.Type.DOWNLOAD_DEMO);
final String fileName = reference.localCopyFileName(BookReference.Type.BookDemo);
if (fileName != null) {
new File(fileName).delete();
}
@ -466,7 +467,7 @@ class NetworkBookActions extends NetworkTreeActions {
}
private static void doBuyInBrowser(Activity activity, final NetworkBookItem book) {
BookReference reference = book.reference(BookReference.Type.BUY_IN_BROWSER);
BookReference reference = book.reference(BookReference.Type.BookBuyInBrowser);
if (reference != null) {
Util.openInBrowser(activity, reference.URL);
}

View file

@ -25,15 +25,13 @@ import java.net.URI;
import org.geometerplus.fbreader.Paths;
public class BookReference {
public interface Type {
int UNKNOWN = 0; // Unknown reference type
int DOWNLOAD_FULL = 1; // reference for download full version of the book
int DOWNLOAD_FULL_CONDITIONAL = 2; // reference for download full version of the book, useful only when book is bought
int DOWNLOAD_DEMO = 3; // reference for downloading demo version of the book
int DOWNLOAD_FULL_OR_DEMO = 4; // reference for downloading unknown version of the book
int BUY = 5; // reference for buying the book (useful only when authentication is supported)
int BUY_IN_BROWSER = 6; // reference to the site page, when it is possible to buy the book
public static enum Type {
Book,
BookConditional,
BookDemo,
BookFullOrDemo,
BookBuy,
BookBuyInBrowser
}
// resolvedReferenceType -- reference type without any ambiguity (for example, DOWNLOAD_FULL_OR_DEMO is ambiguous)
@ -46,9 +44,9 @@ public class BookReference {
public final String URL;
public final int BookFormat;
public final int ReferenceType;
public final Type ReferenceType;
public BookReference(String url, int format, int type) {
public BookReference(String url, int format, Type type) {
URL = url;
BookFormat = format;
ReferenceType = type;
@ -61,7 +59,7 @@ public class BookReference {
private static final String TOESCAPE = "<>:\"|?*\\";
public static String makeBookFileName(String url, int format, int resolvedReferenceType) {
public static String makeBookFileName(String url, int format, Type resolvedReferenceType) {
URI uri;
try {
uri = new URI(url);
@ -76,7 +74,7 @@ public class BookReference {
path.delete(0, 4);
}
path.insert(0, File.separator);
if (resolvedReferenceType == Type.DOWNLOAD_DEMO) {
if (resolvedReferenceType == Type.BookDemo) {
path.insert(0, "Demos");
path.insert(0, File.separator);
}
@ -154,11 +152,11 @@ public class BookReference {
return path.append(ext).toString();
}
public final String makeBookFileName(int resolvedReferenceType) {
public final String makeBookFileName(Type resolvedReferenceType) {
return makeBookFileName(cleanURL(), BookFormat, resolvedReferenceType);
}
public final String localCopyFileName(int resolvedReferenceType) {
public final String localCopyFileName(Type resolvedReferenceType) {
String fileName = makeBookFileName(resolvedReferenceType);
if (fileName != null && new File(fileName).exists()) {
return fileName;

View file

@ -20,10 +20,9 @@
package org.geometerplus.fbreader.network;
public class BuyBookReference extends BookReference {
public final String Price;
public BuyBookReference(String url, int format, int type, String price) {
public BuyBookReference(String url, int format, Type type, String price) {
super(url, format, type);
Price = price;
}
@ -44,5 +43,4 @@ public class BuyBookReference extends BookReference {
}
return currency + " " + price;
}
}

View file

@ -113,7 +113,7 @@ public final class NetworkBookItem extends NetworkItem {
myReferences = new LinkedList<BookReference>(references);
}
public BookReference reference(int type) {
public BookReference reference(BookReference.Type type) {
BookReference reference = null;
for (BookReference ref: myReferences) {
if (ref.ReferenceType == type &&
@ -122,8 +122,8 @@ public final class NetworkBookItem extends NetworkItem {
}
}
if (reference == null && type == BookReference.Type.DOWNLOAD_FULL) {
reference = this.reference(BookReference.Type.DOWNLOAD_FULL_CONDITIONAL);
if (reference == null && type == BookReference.Type.Book) {
reference = this.reference(BookReference.Type.BookConditional);
if (reference != null) {
NetworkAuthenticationManager authManager = Link.authenticationManager();
if (authManager == null || authManager.needPurchase(this)) {
@ -134,17 +134,17 @@ public final class NetworkBookItem extends NetworkItem {
}
if (reference == null &&
type == BookReference.Type.DOWNLOAD_FULL &&
this.reference(BookReference.Type.BUY) == null &&
this.reference(BookReference.Type.BUY_IN_BROWSER) == null) {
reference = this.reference(BookReference.Type.DOWNLOAD_FULL_OR_DEMO);
type == BookReference.Type.Book &&
this.reference(BookReference.Type.BookBuy) == null &&
this.reference(BookReference.Type.BookBuyInBrowser) == null) {
reference = this.reference(BookReference.Type.BookFullOrDemo);
}
if (reference == null &&
type == BookReference.Type.DOWNLOAD_DEMO &&
(this.reference(BookReference.Type.BUY) != null ||
this.reference(BookReference.Type.BUY_IN_BROWSER) != null)) {
reference = this.reference(BookReference.Type.DOWNLOAD_FULL_OR_DEMO);
type == BookReference.Type.BookDemo &&
(this.reference(BookReference.Type.BookBuy) != null ||
this.reference(BookReference.Type.BookBuyInBrowser) != null)) {
reference = this.reference(BookReference.Type.BookFullOrDemo);
}
return reference;
@ -152,17 +152,17 @@ public final class NetworkBookItem extends NetworkItem {
public String localCopyFileName() {
final boolean hasBuyReference =
this.reference(BookReference.Type.BUY) != null ||
this.reference(BookReference.Type.BUY_IN_BROWSER) != null;
this.reference(BookReference.Type.BookBuy) != null ||
this.reference(BookReference.Type.BookBuyInBrowser) != null;
BookReference reference = null;
String fileName = null;
for (BookReference ref: myReferences) {
final int type = ref.ReferenceType;
if ((type == BookReference.Type.DOWNLOAD_FULL ||
type == BookReference.Type.DOWNLOAD_FULL_CONDITIONAL ||
(!hasBuyReference && type == BookReference.Type.DOWNLOAD_FULL_OR_DEMO)) &&
final BookReference.Type type = ref.ReferenceType;
if ((type == BookReference.Type.Book ||
type == BookReference.Type.BookConditional ||
(!hasBuyReference && type == BookReference.Type.BookFullOrDemo)) &&
(reference == null || ref.BookFormat > reference.BookFormat)) {
String name = ref.localCopyFileName(BookReference.Type.DOWNLOAD_FULL);
String name = ref.localCopyFileName(BookReference.Type.Book);
if (name != null) {
reference = ref;
fileName = name;
@ -174,14 +174,14 @@ public final class NetworkBookItem extends NetworkItem {
public void removeLocalFiles() {
final boolean hasBuyReference =
this.reference(BookReference.Type.BUY) != null ||
this.reference(BookReference.Type.BUY_IN_BROWSER) != null;
this.reference(BookReference.Type.BookBuy) != null ||
this.reference(BookReference.Type.BookBuyInBrowser) != null;
for (BookReference ref: myReferences) {
final int type = ref.ReferenceType;
if (type == BookReference.Type.DOWNLOAD_FULL ||
type == BookReference.Type.DOWNLOAD_FULL_CONDITIONAL ||
(!hasBuyReference && type == BookReference.Type.DOWNLOAD_FULL_OR_DEMO)) {
String fileName = ref.localCopyFileName(BookReference.Type.DOWNLOAD_FULL);
final BookReference.Type type = ref.ReferenceType;
if (type == BookReference.Type.Book ||
type == BookReference.Type.BookConditional ||
(!hasBuyReference && type == BookReference.Type.BookFullOrDemo)) {
String fileName = ref.localCopyFileName(BookReference.Type.Book);
if (fileName != null) {
// TODO: remove a book from the library
// TODO: remove a record from the database

View file

@ -158,7 +158,7 @@ public class LitResAuthenticationManager extends NetworkAuthenticationManager {
if (sid.length() == 0) {
return null;
}
BookReference reference = book.reference(BookReference.Type.DOWNLOAD_FULL_CONDITIONAL);
BookReference reference = book.reference(BookReference.Type.BookConditional);
if (reference == null) {
return null;
}
@ -196,7 +196,7 @@ public class LitResAuthenticationManager extends NetworkAuthenticationManager {
throw new ZLNetworkException(NetworkException.ERROR_AUTHENTICATION_FAILED);
}
BookReference reference = book.reference(BookReference.Type.BUY);
BookReference reference = book.reference(BookReference.Type.BookBuy);
if (reference == null) {
throw new ZLNetworkException(NetworkException.ERROR_BOOK_NOT_PURCHASED); // TODO: more correct error message???
}

View file

@ -125,7 +125,7 @@ class LitResXMLReader extends LitResAuthenticationXMLReader {
myReferences.add(new BookReference(
"https://robot.litres.ru/pages/catalit_download_book/?art=" + myBookId,
BookReference.Format.FB2_ZIP,
BookReference.Type.DOWNLOAD_FULL_CONDITIONAL
BookReference.Type.BookConditional
));
myState = BOOK;
}

View file

@ -110,19 +110,19 @@ class NetworkOPDSFeedReader implements OPDSFeedReader, OPDSConstants, MimeTypes
}
// returns BookReference.Type value for specified String. String MUST BE interned.
private static int typeByRelation(String rel) {
private static BookReference.Type typeByRelation(String rel) {
if (rel == null || REL_ACQUISITION.equals(rel) || REL_ACQUISITION_OPEN.equals(rel)) {
return BookReference.Type.DOWNLOAD_FULL;
return BookReference.Type.Book;
} else if (REL_ACQUISITION_SAMPLE.equals(rel)) {
return BookReference.Type.DOWNLOAD_DEMO;
return BookReference.Type.BookDemo;
} else if (REL_ACQUISITION_CONDITIONAL.equals(rel)) {
return BookReference.Type.DOWNLOAD_FULL_CONDITIONAL;
return BookReference.Type.BookConditional;
} else if (REL_ACQUISITION_SAMPLE_OR_FULL.equals(rel)) {
return BookReference.Type.DOWNLOAD_FULL_OR_DEMO;
return BookReference.Type.BookFullOrDemo;
} else if (REL_ACQUISITION_BUY.equals(rel)) {
return BookReference.Type.BUY;
return BookReference.Type.BookBuy;
} else {
return BookReference.Type.UNKNOWN;
return null;
}
}
@ -244,7 +244,7 @@ class NetworkOPDSFeedReader implements OPDSFeedReader, OPDSConstants, MimeTypes
final String href = ZLNetworkUtil.url(myBaseURL, link.getHref());
final String type = ZLNetworkUtil.filterMimeType(link.getType());
final String rel = opdsNetworkLink.relation(link.getRel(), type);
final int referenceType = typeByRelation(rel);
final BookReference.Type referenceType = typeByRelation(rel);
if (REL_IMAGE_THUMBNAIL.equals(rel) || REL_THUMBNAIL.equals(rel)) {
if (MIME_IMAGE_PNG.equals(type) || MIME_IMAGE_JPEG.equals(type)) {
urls.put(NetworkItem.UrlType.Thumbnail, href);
@ -253,7 +253,7 @@ class NetworkOPDSFeedReader implements OPDSFeedReader, OPDSConstants, MimeTypes
if (MIME_IMAGE_PNG.equals(type) || MIME_IMAGE_JPEG.equals(type)) {
urls.put(NetworkItem.UrlType.Image, href);
}
} else if (BookReference.Type.BUY == referenceType) {
} else if (BookReference.Type.BookBuy == referenceType) {
final OPDSLink opdsLink = (OPDSLink)link;
String price = null;
final OPDSPrice opdsPrice = opdsLink.selectBestPrice();
@ -269,12 +269,12 @@ class NetworkOPDSFeedReader implements OPDSFeedReader, OPDSConstants, MimeTypes
}
if (MIME_TEXT_HTML.equals(type)) {
collectReferences(references, opdsLink, href,
BookReference.Type.BUY_IN_BROWSER, price, true);
BookReference.Type.BookBuyInBrowser, price, true);
} else {
collectReferences(references, opdsLink, href,
BookReference.Type.BUY, price, false);
BookReference.Type.BookBuy, price, false);
}
} else if (referenceType != BookReference.Type.UNKNOWN) {
} else if (referenceType != null) {
final int format = formatByMimeType(type);
if (format != BookReference.Format.NONE) {
references.add(new BookReference(href, format, referenceType));
@ -349,7 +349,7 @@ class NetworkOPDSFeedReader implements OPDSFeedReader, OPDSConstants, MimeTypes
}
private void collectReferences(LinkedList<BookReference> references,
OPDSLink opdsLink, String href, int type, String price, boolean addWithoutFormat) {
OPDSLink opdsLink, String href, BookReference.Type type, String price, boolean addWithoutFormat) {
boolean added = false;
for (String mime: opdsLink.Formats) {
final int format = formatByMimeType(mime);