From eb81117c85c9229437a7bef26b83d09e97756482 Mon Sep 17 00:00:00 2001 From: "C. Albert" Date: Thu, 31 Jan 2013 18:17:25 -0800 Subject: [PATCH] Added line to fix issue #67. https://github.com/applications4android/ComicReader/issues/67. Comics that fail finding url now do not add null to the list of comics. --- .../comicreader/core/Comic.java | 266 +++++++++++------- 1 file changed, 167 insertions(+), 99 deletions(-) diff --git a/ComicReader/src/com/blogspot/applications4android/comicreader/core/Comic.java b/ComicReader/src/com/blogspot/applications4android/comicreader/core/Comic.java index 03efd6a..dfa3395 100755 --- a/ComicReader/src/com/blogspot/applications4android/comicreader/core/Comic.java +++ b/ComicReader/src/com/blogspot/applications4android/comicreader/core/Comic.java @@ -23,7 +23,6 @@ import android.util.Log; import com.blogspot.applications4android.comicreader.exceptions.ComicException; import com.blogspot.applications4android.comicreader.exceptions.ComicSDCardFull; - /** * Base class for all comic series */ @@ -96,7 +95,6 @@ public abstract class Comic extends ComicParser { /** background caching enabled */ private boolean mCacheEnabled; - /** * Constructor */ @@ -117,9 +115,11 @@ public abstract class Comic extends ComicParser { } /** - * Tell to this comic whether caching is enabled or not. - * This'll be used to decide whether to forcefully download the latest comic or not. - * @param cache true if it is, else false + * Tell to this comic whether caching is enabled or not. This'll be used to + * decide whether to forcefully download the latest comic or not. + * + * @param cache + * true if it is, else false */ public void setCacheEnabled(boolean cache) { mCacheEnabled = cache; @@ -127,7 +127,9 @@ public abstract class Comic extends ComicParser { /** * Set the launch type for this comic - * @param type launch type + * + * @param type + * launch type */ public void setLaunchType(int type) { mType = type; @@ -135,6 +137,7 @@ public abstract class Comic extends ComicParser { /** * get the launch type + * * @return launch type */ public int getLaunchType() { @@ -143,6 +146,7 @@ public abstract class Comic extends ComicParser { /** * Get the comic name + * * @return name */ public String getName() { @@ -151,6 +155,7 @@ public abstract class Comic extends ComicParser { /** * Gets the current strip + * * @return current strip */ public Strip getCurrentStrip() { @@ -159,6 +164,7 @@ public abstract class Comic extends ComicParser { /** * Get the english name for this comic + * * @return name */ public String getComicName() { @@ -167,7 +173,9 @@ public abstract class Comic extends ComicParser { /** * Set the english name for this comic - * @param name name to be set + * + * @param name + * name to be set */ public void setComicName(String name) { mComicName = name; @@ -175,6 +183,7 @@ public abstract class Comic extends ComicParser { /** * Reads unread strips count for the current comic + * * @return unread strips count * @throws FileNotFoundException * @throws IOException @@ -182,11 +191,11 @@ public abstract class Comic extends ComicParser { */ public int readOnlyUnread() throws FileNotFoundException, IOException, JSONException { File json = _getJsonFile(); - if(!json.exists()) { + if (!json.exists()) { return 0; } JSONObject root = JsonUtils.jsonRoot(new FileInputStream(json)); - if(root.has("mUnread")) { + if (root.has("mUnread")) { return root.getInt("mUnread"); } return 0; @@ -194,38 +203,39 @@ public abstract class Comic extends ComicParser { /** * Helper function to read the properties of this comic from its json file + * * @throws FileNotFoundException * @throws IOException * @throws JSONException */ public void readProperties() throws FileNotFoundException, IOException, JSONException { File json = _getJsonFile(); - if(!json.exists()) { + if (!json.exists()) { _readOldFavs(); _readOldPrevSession(); return; } Log.d(TAG, "Reading comic properties from " + json.getPath()); JSONObject root = JsonUtils.jsonRoot(new FileInputStream(json)); - if(root.has("mLatestUid")) { + if (root.has("mLatestUid")) { mLatestUid = root.getString("mLatestUid"); } - if(root.has("mFirstUid")) { + if (root.has("mFirstUid")) { mFirstUid = root.getString("mFirstUid"); } - if(root.has("mPrevSessionUid")) { + if (root.has("mPrevSessionUid")) { mPrevSessionUid = root.getString("mPrevSessionUid"); } - if(root.has("mDefZoom")) { - mDefZoom = (float)root.getDouble("mDefZoom"); + if (root.has("mDefZoom")) { + mDefZoom = (float) root.getDouble("mDefZoom"); } - if(root.has("mStrips")) { + if (root.has("mStrips")) { JSONArray arr = root.getJSONArray("mStrips"); int len = arr.length(); - for(int i=0;i> itr1 = mStrips.entrySet().iterator(); - while(itr1.hasNext()) { + while (itr1.hasNext()) { Map.Entry e = itr1.next(); - if(!e.getValue().isRead()) { + if (!e.getValue().isRead()) { unread++; } } sb.append("\"mUnread\":" + unread + ",\n"); - if(mLatestUid != null) { + if (mLatestUid != null) { sb.append("\"mLatestUid\":\"" + mLatestUid + "\",\n"); } - if(mFirstUid != null) { + if (mFirstUid != null) { sb.append("\"mFirstUid\":\"" + mFirstUid + "\",\n"); } - if(mPrevSessionUid != null) { + if (mPrevSessionUid != null) { sb.append("\"mPrevSessionUid\":\"" + mPrevSessionUid + "\",\n"); } sb.append("\"mDefZoom\":\"" + mDefZoom + "\",\n"); sb.append("\"mStrips\": [\n"); Iterator> itr = mStrips.entrySet().iterator(); - while(itr.hasNext()) { + while (itr.hasNext()) { Map.Entry e = itr.next(); e.getValue().toJsonString(sb); - if(itr.hasNext()) { + if (itr.hasNext()) { sb.append(","); } sb.append("\n"); @@ -326,6 +339,7 @@ public abstract class Comic extends ComicParser { /** * Returns the default value of zoom + * * @return zoom value */ public float getDefaultZoom() { @@ -334,7 +348,9 @@ public abstract class Comic extends ComicParser { /** * Set the default value of zoom - * @param zoom zoom value + * + * @param zoom + * zoom value */ public void setDefaultZoom(float zoom) { Log.d(TAG, "Setting default zoom to " + zoom); @@ -343,6 +359,7 @@ public abstract class Comic extends ComicParser { /** * Tells whether the comic has any previous sessions or not + * * @return true if it has, else false */ public boolean hasPreviousSession() { @@ -351,15 +368,16 @@ public abstract class Comic extends ComicParser { /** * Sets the current strip as favorite or not - * @param val true if it is to be set as favorite + * + * @param val + * true if it is to be set as favorite */ public void setCurrentAsFavorite(boolean val) { - if(mCurrent != null) { + if (mCurrent != null) { mCurrent.setAsFavorite(val); - if(val) { + if (val) { mFavs.add(mCurrent.uid()); - } - else { + } else { mFavs.remove(mCurrent.uid()); } } @@ -367,10 +385,11 @@ public abstract class Comic extends ComicParser { /** * Whether the current comic is favorite or not + * * @return true if it is */ public boolean isCurrentFavorite() { - if(mCurrent == null) { + if (mCurrent == null) { return false; } return mCurrent.isFavorite(); @@ -378,10 +397,11 @@ public abstract class Comic extends ComicParser { /** * Whether the current comic has image text or not + * * @return true if it is */ public boolean currentHasImageText() { - if(mCurrent == null) { + if (mCurrent == null) { return false; } return mCurrent.hasText(); @@ -389,10 +409,11 @@ public abstract class Comic extends ComicParser { /** * Creates a valid file name out of the strip's title + * * @return valid file name */ public String currentTitleAsValidFilename() { - if(mCurrent == null) { + if (mCurrent == null) { return null; } return mCurrent.currentTitleAsValidFilename(); @@ -413,18 +434,20 @@ public abstract class Comic extends ComicParser { mLatestUid = null; } - - ////// Strip navigation ////// + // //// Strip navigation ////// /** - * Helper function to provide unified interface for navigation among the strips - * @param type navigation type + * Helper function to provide unified interface for navigation among the + * strips + * + * @param type + * navigation type * @return strip * @throws ComicException */ public Strip navigateStrip(int type) throws ComicException { mCache.makeSpace(); - if(mType == TYPE_FAVORITE) { - switch(type) { + if (mType == TYPE_FAVORITE) { + switch (type) { case NAV_LATEST: mFavIdx = mFavs.size() - 1; return _getFavoriteStrip(); @@ -433,30 +456,39 @@ public abstract class Comic extends ComicParser { return _getFavoriteStrip(); case NAV_NEXT: ++mFavIdx; - mFavIdx = (mFavIdx >= mFavs.size())? 0 : mFavIdx; + mFavIdx = (mFavIdx >= mFavs.size()) ? 0 : mFavIdx; return _getFavoriteStrip(); case NAV_PREVIOUS: --mFavIdx; - mFavIdx = (mFavIdx < 0)? mFavs.size() - 1 : mFavIdx; + mFavIdx = (mFavIdx < 0) ? mFavs.size() - 1 : mFavIdx; return _getFavoriteStrip(); case NAV_RANDOM: mFavIdx = RandUtils.getPositiveInt(mFavs.size(), 0); return _getFavoriteStrip(); - case NAV_CURRENT: return mCurrent; + case NAV_CURRENT: + return mCurrent; default: ComicException ce = new ComicException("Bad navigation-type passed: " + type); throw ce; } } - switch(type) { - case NAV_LATEST: return getLatestStrip(); - case NAV_LATEST_FORCE: return getLatestStripForcefully(); - case NAV_FIRST: return getFirstStrip(); - case NAV_NEXT: return getNextStrip(); - case NAV_PREVIOUS: return getPreviousStrip(); - case NAV_RANDOM: return getRandomStrip(); - case NAV_CURRENT: return mCurrent; - case NAV_PREV_SESSION: return getPreviousSessionStrip(); + switch (type) { + case NAV_LATEST: + return getLatestStrip(); + case NAV_LATEST_FORCE: + return getLatestStripForcefully(); + case NAV_FIRST: + return getFirstStrip(); + case NAV_NEXT: + return getNextStrip(); + case NAV_PREVIOUS: + return getPreviousStrip(); + case NAV_RANDOM: + return getRandomStrip(); + case NAV_CURRENT: + return mCurrent; + case NAV_PREV_SESSION: + return getPreviousSessionStrip(); default: ComicException ce = new ComicException("Bad navigation-type passed: " + type); throw ce; @@ -465,10 +497,11 @@ public abstract class Comic extends ComicParser { /** * Gets latest strip + * * @return latest strip */ public Strip getLatestStrip() { - if((mLatestUid == null) || !mCacheEnabled) { + if ((mLatestUid == null) || !mCacheEnabled) { Log.d(TAG, "Latest uid is null (or bg-caching is disabled) , calling getlatestStripUrl..."); mLatestUid = getLatestStripUrl(); } @@ -477,6 +510,7 @@ public abstract class Comic extends ComicParser { /** * Gets latest strip forcefully (used during background caching) + * * @return latest strip */ public Strip getLatestStripForcefully() { @@ -486,6 +520,7 @@ public abstract class Comic extends ComicParser { /** * Gets the first strip + * * @return first strip */ public Strip getFirstStrip() { @@ -495,6 +530,7 @@ public abstract class Comic extends ComicParser { /** * Gets the random strip + * * @return random strip */ public Strip getRandomStrip() { @@ -504,13 +540,14 @@ public abstract class Comic extends ComicParser { /** * Gets the next strip + * * @return next strip */ public Strip getNextStrip() { - if(isCurrentLatestStrip()) { + if (isCurrentLatestStrip()) { return mCurrent; } - if(!mCurrent.hasNext()) { + if (!mCurrent.hasNext()) { mCurrent.setNext(getNextStripUrl()); } String uid = mCurrent.uid(); @@ -521,13 +558,14 @@ public abstract class Comic extends ComicParser { /** * Gets the previous strip + * * @return previous strip */ public Strip getPreviousStrip() { - if(isCurrentFirstStrip()) { + if (isCurrentFirstStrip()) { return mCurrent; } - if(!mCurrent.hasPrevious()) { + if (!mCurrent.hasPrevious()) { mCurrent.setPrevious(getPreviousStripUrl()); } String uid = mCurrent.uid(); @@ -538,10 +576,11 @@ public abstract class Comic extends ComicParser { /** * Gets the previous session strip + * * @return previous session strip */ public Strip getPreviousSessionStrip() { - if(mPrevSessionUid == null) { + if (mPrevSessionUid == null) { return null; } return _querySetCurrentUid(mPrevSessionUid); @@ -549,12 +588,14 @@ public abstract class Comic extends ComicParser { /** * Gets the strip from the given url - * @param url url + * + * @param url + * url * @return strip - * @throws ComicSDCardFull - * @throws IOException - * @throws URISyntaxException - * @throws ClientProtocolException + * @throws ComicSDCardFull + * @throws IOException + * @throws URISyntaxException + * @throws ClientProtocolException */ public Strip getStripFromUrl(String url) throws ClientProtocolException, URISyntaxException, IOException, ComicSDCardFull { Strip s = _querySetCurrentUid(url); @@ -564,84 +605,96 @@ public abstract class Comic extends ComicParser { /** * Download the current strip + * * @throws ClientProtocolException * @throws URISyntaxException * @throws IOException * @throws ComicSDCardFull */ public void downloadCurrentStrip() throws ClientProtocolException, URISyntaxException, IOException, ComicSDCardFull { - if(mCurrent == null) { + if (mCurrent == null) { return; } mCurrent.downloadImage(this); } - ////// Strip navigation ////// + // //// Strip navigation ////// - ////// List of all abstract methods of this class ////// + // //// List of all abstract methods of this class ////// /** * Get the main page url for the current comic series + * * @return desired url */ public abstract String getComicWebPageUrl(); /** * Returns the bound for the current comic + * * @return bound */ public abstract Bound getBound(); /** * Type of dialog to be used for choosing comics + * * @return int */ public abstract int dialogType(); /** * Gets the url for the latest strip + * * @return url */ protected abstract String getLatestStripUrl(); /** * Gets the url for the first strip + * * @return url */ protected abstract String getFirstStripUrl(); /** * Gets the url for the next strip + * * @return url */ protected abstract String getNextStripUrl(); /** * Gets the url for the previous strip + * * @return url */ protected abstract String getPreviousStripUrl(); /** * Gets the url for the previous strip + * * @return url */ protected abstract String getRandomStripUrl(); /** - * Gives out the list of urls (can be regexes) which are not supposed to be cached using 'mStrips' + * Gives out the list of urls (can be regexes) which are not supposed to be + * cached using 'mStrips' + * * @return list */ protected abstract String[] urlsNotForCaching(); - ////// List of all abstract methods of this class ////// + // //// List of all abstract methods of this class ////// - ////// protected methods ////// + // //// protected methods ////// /** * Checks whether the current strip is latest strip or not + * * @return true if it is */ protected boolean isCurrentLatestStrip() { - if(mLatestUid == null) { + if (mLatestUid == null) { mLatestUid = getLatestStripUrl(); } return mLatestUid.equals(mCurrent.uid()); @@ -649,10 +702,11 @@ public abstract class Comic extends ComicParser { /** * Checks whether the current strip is first strip or not + * * @return true if it is */ protected boolean isCurrentFirstStrip() { - if(mFirstUid == null) { + if (mFirstUid == null) { mFirstUid = getFirstStripUrl(); } return mFirstUid.equals(mCurrent.uid()); @@ -660,7 +714,9 @@ public abstract class Comic extends ComicParser { /** * Query whether the uid already exists in the history - * @param uid uid + * + * @param uid + * uid * @return true if it is, else false */ protected boolean hasUid(String uid) { @@ -669,42 +725,51 @@ public abstract class Comic extends ComicParser { /** * Helper function to query for a strip and create if it doesn't exist - * @param uid key to be queried + * + * @param uid + * key to be queried * @return strip */ protected Strip queryUid(String uid) { - if(mStrips.containsKey(uid)) { + if (mStrips.containsKey(uid)) { return mStrips.get(uid); + } + if (uid == null) { + } Strip s = new Strip(uid, mCache.cachePath()); String[] urls = urlsNotForCaching(); - if(urls != null) { - for(String url : urls) { - if(uid.matches(url) || uid.equals(url)) { - Log.d(TAG, "UID="+uid+" matches with the regex/url="+url+". So not caching this strip"); + if (urls != null) { + for (String url : urls) { + if (uid.matches(url) || uid.equals(url)) { + Log.d(TAG, "UID=" + uid + " matches with the regex/url=" + url + ". So not caching this strip"); File f = new File(s.getImagePath()); - if(f.exists()) { + if (f.exists()) { f.delete(); } return s; } } } - mStrips.put(uid, s); + if (uid != null) { + mStrips.put(uid, s); + } return s; } - ////// protected methods ////// + // //// protected methods ////// - ////// private methods ////// + // //// private methods ////// /** * Helper function to query for a strip and set it as current - * @param uid key to be queried + * + * @param uid + * key to be queried * @return strip */ private Strip _querySetCurrentUid(String uid) { mCurrent = queryUid(uid); - if(mType != TYPE_CACHING) { + if (mType != TYPE_CACHING) { mPrevSessionUid = uid; } Log.d(TAG, "Current UID = " + uid); @@ -713,6 +778,7 @@ public abstract class Comic extends ComicParser { /** * Returns the current favorite strip + * * @return strip */ private Strip _getFavoriteStrip() { @@ -721,14 +787,16 @@ public abstract class Comic extends ComicParser { } /** - * Helper function to return the properties json file associated with this comic + * Helper function to return the properties json file associated with this + * comic + * * @return json file */ private File _getJsonFile() { File f = new File(FileUtils.getComicRoot(), PROPS); f.mkdirs(); - return new File(f, mName+".json"); + return new File(f, mName + ".json"); } - ////// private methods ////// + // //// private methods ////// }