Example #1
0
  private void bookRename(final File file) {
    final EditText etNewName = new EditText(this);
    etNewName.setText(BookUtil.getFileName(file));
    new AlertDialog.Builder(this)
        .setTitle("重命名")
        .setView(etNewName)
        .setPositiveButton(
            "确定",
            new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog, int which) {
                String oldName = file.getAbsolutePath().toString();
                String newName =
                    oldName.substring(0, oldName.lastIndexOf("/") + 1)
                        + etNewName.getText().toString()
                        + "."
                        + BookUtil.getFileType(file);
                file.renameTo(new File(newName));
                openDb();
                db.execSQL("insert into book_list(bookname)values('" + newName + "');");
                db.delete("book_list", "bookname = ?", new String[] {oldName});
                initBookList();
                bookAdapter.notifyDataSetChanged();
              }
            })
        .setNegativeButton("取消", null)
        .show();
  }
Example #2
0
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
   Context context = getApplicationContext();
   LinearLayout view = new LinearLayout(context);
   ImageView img = new ImageView(context);
   img.setImageResource(R.drawable.book);
   TextView textView = new TextView(context);
   textView.setText(BookUtil.getFileName(bookList.get(position)));
   textView.setGravity(Gravity.CENTER);
   textView.setTextColor(getResources().getColor(R.color.black));
   view.setOrientation(LinearLayout.VERTICAL);
   view.addView(img, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
   view.addView(textView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
   return view;
 }
 public String getBookHash() {
   final UID uid = BookUtil.createSHA256Uid(getReader().Model.Book.File);
   return uid != null ? uid.Id : null;
 }
  private void build() {
    // Step 0: get database books marked as "existing"
    final FileInfoSet fileInfos = new FileInfoSet(myDatabase);
    final Map<Long, Book> savedBooksByFileId = myDatabase.loadBooks(fileInfos, true);
    final Map<Long, Book> savedBooksByBookId = new HashMap<Long, Book>();
    for (Book b : savedBooksByFileId.values()) {
      savedBooksByBookId.put(b.getId(), b);
    }

    // Step 1: check if files corresponding to "existing" books really exists;
    //         add books to library if yes (and reload book info if needed);
    //         remove from recent/favorites list if no;
    //         collect newly "orphaned" books
    final Set<Book> orphanedBooks = new HashSet<Book>();
    final Set<ZLPhysicalFile> physicalFiles = new HashSet<ZLPhysicalFile>();
    int count = 0;
    for (Book book : savedBooksByFileId.values()) {
      final ZLPhysicalFile file = book.File.getPhysicalFile();
      if (file != null) {
        physicalFiles.add(file);
      }
      if (file != book.File && file != null && file.getPath().endsWith(".epub")) {
        continue;
      }
      if (book.File.exists()) {
        boolean doAdd = true;
        if (file == null) {
          continue;
        }
        if (!fileInfos.check(file, true)) {
          try {
            book.readMetaInfo();
            saveBook(book, false);
          } catch (BookReadingException e) {
            doAdd = false;
          }
          file.setCached(false);
        }
        if (doAdd) {
          // loaded from db
          addBook(book, false);
        }
      } else {
        orphanedBooks.add(book);
      }
    }
    myDatabase.setExistingFlag(orphanedBooks, false);

    // Step 2: collect books from physical files; add new, update already added,
    //         unmark orphaned as existing again, collect newly added
    final Map<Long, Book> orphanedBooksByFileId = myDatabase.loadBooks(fileInfos, false);
    final Set<Book> newBooks = new HashSet<Book>();

    final List<ZLPhysicalFile> physicalFilesList = collectPhysicalFiles(BookDirectories);
    for (ZLPhysicalFile file : physicalFilesList) {
      if (physicalFiles.contains(file)) {
        continue;
      }
      collectBooks(
          file,
          fileInfos,
          savedBooksByFileId,
          orphanedBooksByFileId,
          newBooks,
          !fileInfos.check(file, true));
      file.setCached(false);
    }

    // Step 3: add help file
    try {
      final ZLFile helpFile = BookUtil.getHelpFile();
      Book helpBook = savedBooksByFileId.get(fileInfos.getId(helpFile));
      if (helpBook == null) {
        helpBook = new Book(helpFile);
      }
      saveBook(helpBook, false);
      // saved
      addBook(helpBook, false);
    } catch (BookReadingException e) {
      // that's impossible
      e.printStackTrace();
    }

    // Step 4: save changes into database
    fileInfos.save();

    myDatabase.executeAsTransaction(
        new Runnable() {
          public void run() {
            for (Book book : newBooks) {
              saveBook(book, false);
            }
          }
        });
    myDatabase.setExistingFlag(newBooks, true);
  }