diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 0d55985..ecdf514 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -7,7 +7,8 @@ - + diff --git a/src/main/java/com/github/hoverruan/libr/mobile/domain/Book.java b/src/main/java/com/github/hoverruan/libr/mobile/domain/Book.java index 19cb701..b17d848 100644 --- a/src/main/java/com/github/hoverruan/libr/mobile/domain/Book.java +++ b/src/main/java/com/github/hoverruan/libr/mobile/domain/Book.java @@ -69,4 +69,14 @@ public class Book { public void setName(String name) { this.name = name; } + + public String buildFilename() { + StringBuilder builder = new StringBuilder(isbn); + int pos = image.lastIndexOf('.'); + if (pos > 0) { + String suffix = image.substring(pos); + builder.append(suffix); + } + return builder.toString(); + } } diff --git a/src/main/java/com/github/hoverruan/libr/mobile/ui/MainActivity.java b/src/main/java/com/github/hoverruan/libr/mobile/ui/MainActivity.java index 5b4be8d..f77ceb5 100644 --- a/src/main/java/com/github/hoverruan/libr/mobile/ui/MainActivity.java +++ b/src/main/java/com/github/hoverruan/libr/mobile/ui/MainActivity.java @@ -21,6 +21,7 @@ import com.github.hoverruan.libr.mobile.domain.BookList; import com.github.hoverruan.libr.mobile.domain.BookParser; import com.github.hoverruan.libr.mobile.util.DownloadImageTask; import com.github.hoverruan.libr.mobile.util.DownloadJsonTask; +import static com.github.hoverruan.libr.mobile.util.FileUtils.newFile; import java.io.File; import java.util.List; @@ -37,7 +38,7 @@ public class MainActivity extends SherlockListActivity { requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); - setSupportProgressBarIndeterminateVisibility(true); +// setSupportProgressBarIndeterminateVisibility(true); new DownloadBooksInfo().execute("http://libr.herokuapp.com/api/books"); } @@ -100,7 +101,7 @@ public class MainActivity extends SherlockListActivity { setListAdapter(new BookListAdapter(MainActivity.this)); } } - setSupportProgressBarIndeterminateVisibility(false); +// setSupportProgressBarIndeterminateVisibility(false); } } @@ -114,15 +115,15 @@ public class MainActivity extends SherlockListActivity { } public void load() { - File imageFile = DownloadImageTask.calculateImageFile(MainActivity.this, "cover", book.getIsbn()); - if (!imageFile.exists()) { - new DownloadImageTask(MainActivity.this, "cover", book.getIsbn()) { + File coverFile = newFile(MainActivity.this.getCacheDir(), "cover", book.buildFilename()); + if (!coverFile.exists()) { + new DownloadImageTask(MainActivity.this, coverFile) { protected void onPostExecute(File downloadedImageFile) { loadImage(downloadedImageFile); } }.execute(book.getImage()); } else { - loadImage(imageFile); + loadImage(coverFile); } } diff --git a/src/main/java/com/github/hoverruan/libr/mobile/util/FileUtils.java b/src/main/java/com/github/hoverruan/libr/mobile/util/FileUtils.java new file mode 100644 index 0000000..f1b311a --- /dev/null +++ b/src/main/java/com/github/hoverruan/libr/mobile/util/FileUtils.java @@ -0,0 +1,18 @@ +package com.github.hoverruan.libr.mobile.util; + +import java.io.File; + +/** + * @author Hover Ruan + */ +public class FileUtils { + public static File newFile(File root, String... paths) { + File targetFile = root; + + for (String path : paths) { + targetFile = new File(targetFile, path); + } + + return targetFile; + } +} diff --git a/src/test/java/com/github/hoverruan/libr/mobile/domain/BookTest.java b/src/test/java/com/github/hoverruan/libr/mobile/domain/BookTest.java new file mode 100644 index 0000000..e0e8fa4 --- /dev/null +++ b/src/test/java/com/github/hoverruan/libr/mobile/domain/BookTest.java @@ -0,0 +1,24 @@ +package com.github.hoverruan.libr.mobile.domain; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import org.junit.Test; + +/** + * @author Hover Ruan + */ +public class BookTest { + @Test + public void should_build_Filename_with_ISBN_and_image_url() { + String suffix = ".jpg"; + String image = "http://img3.douban.com/mpic/s4483293" + suffix; + String isbn = "9787111316657"; + + Book book = new Book(); + book.setImage(image); + book.setIsbn(isbn); + + String filename = book.buildFilename(); + assertThat(filename, is(isbn + suffix)); + } +}