public static int getBookStatus(
     NetworkBookItem book, BookDownloaderServiceConnection connection) {
   if (useFullReferences(book)) {
     final BookUrlInfo reference = book.reference(UrlInfo.Type.Book);
     if (reference != null && connection != null && connection.isBeingDownloaded(reference.Url)) {
       return R.drawable.ic_list_downloading;
     } else if (book.localCopyFileName() != null) {
       return R.drawable.ic_list_flag;
     } else if (reference != null) {
       return R.drawable.ic_list_download;
     }
   }
   if (book.getStatus() == NetworkBookItem.Status.CanBePurchased) {
     return R.drawable.ic_list_buy;
   }
   return 0;
 }
 private static void doReadBook(Activity activity, final NetworkBookItem book, boolean demo) {
   String local = null;
   if (!demo) {
     local = book.localCopyFileName();
   } else {
     final BookUrlInfo reference = book.reference(UrlInfo.Type.BookDemo);
     if (reference != null) {
       local = reference.localCopyFileName(UrlInfo.Type.BookDemo);
     }
   }
   if (local != null) {
     activity.startActivity(
         new Intent(
                 Intent.ACTION_VIEW,
                 Uri.fromFile(new File(local)),
                 activity.getApplicationContext(),
                 FBReader.class)
             .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
   }
 }
 public static List<NBAction> getContextMenuActions(
     Activity activity, NetworkBookTree tree, BookDownloaderServiceConnection connection) {
   final NetworkBookItem book = tree.Book;
   List<NBAction> actions = new LinkedList<NBAction>();
   if (useFullReferences(book)) {
     final BookUrlInfo reference = book.reference(UrlInfo.Type.Book);
     if (reference != null && connection != null && connection.isBeingDownloaded(reference.Url)) {
       actions.add(new NBAction(activity, ActionCode.TREE_NO_ACTION, "alreadyDownloading"));
     } else if (book.localCopyFileName() != null) {
       actions.add(new NBAction(activity, ActionCode.READ_BOOK, "read"));
       actions.add(new NBAction(activity, ActionCode.DELETE_BOOK, "delete"));
     } else if (reference != null) {
       actions.add(new NBAction(activity, ActionCode.DOWNLOAD_BOOK, "download"));
     }
   }
   if (useDemoReferences(book)) {
     final BookUrlInfo reference = book.reference(UrlInfo.Type.BookDemo);
     if (connection != null && connection.isBeingDownloaded(reference.Url)) {
       actions.add(new NBAction(activity, ActionCode.TREE_NO_ACTION, "alreadyDownloadingDemo"));
     } else if (reference.localCopyFileName(UrlInfo.Type.BookDemo) != null) {
       actions.add(new NBAction(activity, ActionCode.READ_DEMO, "readDemo"));
       actions.add(new NBAction(activity, ActionCode.DELETE_DEMO, "deleteDemo"));
     } else {
       actions.add(new NBAction(activity, ActionCode.DOWNLOAD_DEMO, "downloadDemo"));
     }
   }
   if (book.getStatus() == NetworkBookItem.Status.CanBePurchased) {
     final BookBuyUrlInfo reference = book.buyInfo();
     final int id =
         reference.InfoType == UrlInfo.Type.BookBuy
             ? ActionCode.BUY_DIRECTLY
             : ActionCode.BUY_IN_BROWSER;
     final String priceString = reference.Price != null ? reference.Price.toString() : "";
     actions.add(new NBAction(activity, id, "buy", priceString));
     final BasketItem basketItem = book.Link.getBasketItem();
     if (basketItem != null) {
       if (basketItem.contains(book)) {
         if (tree.Parent instanceof BasketCatalogTree
             || activity instanceof NetworkLibraryActivity) {
           actions.add(
               new NBAction(activity, ActionCode.REMOVE_BOOK_FROM_BASKET, "removeFromBasket"));
         } else {
           actions.add(new NBAction(activity, ActionCode.OPEN_BASKET, "openBasket"));
         }
       } else {
         actions.add(new NBAction(activity, ActionCode.ADD_BOOK_TO_BASKET, "addToBasket"));
       }
     }
   }
   return actions;
 }
 private static boolean useDemoReferences(NetworkBookItem book) {
   return book.reference(UrlInfo.Type.BookDemo) != null
       && book.localCopyFileName() == null
       && book.reference(UrlInfo.Type.Book) == null;
 }
 private static boolean useFullReferences(NetworkBookItem book) {
   return book.reference(UrlInfo.Type.Book) != null
       || book.reference(UrlInfo.Type.BookConditional) != null;
 }
 private static void doBuyInBrowser(Activity activity, final NetworkBookItem book) {
   BookUrlInfo reference = book.reference(UrlInfo.Type.BookBuyInBrowser);
   if (reference != null) {
     Util.openInBrowser(activity, reference.Url);
   }
 }