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; }
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.Key.BOOK_MIME, ref.Mime.toString()) .putExtra(BookDownloaderService.Key.BOOK_KIND, resolvedType) .putExtra(BookDownloaderService.Key.CLEAN_URL, ref.cleanUrl()) .putExtra(BookDownloaderService.Key.BOOK_TITLE, book.Title)); } }
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)); } }
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)); } }
@Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); doStart(); final Uri uri = intent.getData(); if (uri == null) { doStop(); return; } intent.setData(null); final int notifications = intent.getIntExtra(SHOW_NOTIFICATIONS_KEY, 0); final String url = uri.toString(); final int bookFormat = intent.getIntExtra(BOOK_FORMAT_KEY, BookUrlInfo.Format.NONE); UrlInfo.Type referenceType = (UrlInfo.Type) intent.getSerializableExtra(REFERENCE_TYPE_KEY); if (referenceType == null) { referenceType = UrlInfo.Type.Book; } String cleanURL = intent.getStringExtra(CLEAN_URL_KEY); if (cleanURL == null) { cleanURL = url; } if (myDownloadingURLs.contains(url)) { if ((notifications & Notifications.ALREADY_DOWNLOADING) != 0) { showMessage("alreadyDownloading"); } doStop(); return; } String fileName = BookUrlInfo.makeBookFileName(cleanURL, bookFormat, referenceType); if (fileName == null) { doStop(); return; } int index = fileName.lastIndexOf(File.separator); if (index != -1) { final String dir = fileName.substring(0, index); final File dirFile = new File(dir); if (!dirFile.exists() && !dirFile.mkdirs()) { showMessage("cannotCreateDirectory", dirFile.getPath()); doStop(); return; } if (!dirFile.exists() || !dirFile.isDirectory()) { showMessage("cannotCreateDirectory", dirFile.getPath()); doStop(); return; } } final File fileFile = new File(fileName); if (fileFile.exists()) { if (!fileFile.isFile()) { showMessage("cannotCreateFile", fileFile.getPath()); doStop(); return; } // TODO: question box: redownload? doStop(); startActivity(getFBReaderIntent(fileFile)); return; } String title = intent.getStringExtra(TITLE_KEY); if (title == null || title.length() == 0) { title = fileFile.getName(); } if ((notifications & Notifications.DOWNLOADING_STARTED) != 0) { showMessage("downloadingStarted"); } final String sslCertificate = intent.getStringExtra(SSL_CERTIFICATE_KEY); startFileDownload(url, sslCertificate, fileFile, title); }