/** * Helper function to load the comic object * * @param idx location where it is stored in ComicClassList * @return Comic Object * @throws ComicNotFoundException */ private Comic getComicObject(int idx) throws ComicNotFoundException { try { String fname = mPkg + "." + mClasses[idx].mClass; Log.d(TAG, "Trying to load class=" + fname); Class<?> comicClass = Class.forName(fname); Comic com = (Comic) comicClass.newInstance(); com.setComicName(mClasses[idx].mName); Log.d(TAG, "Successfully loaded class=" + fname); return com; } catch (Exception e) { ComicNotFoundException cnf = new ComicNotFoundException("Comic for idx=" + idx + " could not be found!"); throw cnf; } }
/** * Initializer * * @param mgr asset manager * @param path path to json file wrt assets * @param selected file containing the list of selected comics * @throws JSONException * @throws IOException * @throws ComicNotFoundException */ private void _init_(AssetManager mgr, String path, File selected) throws IOException, JSONException, ComicNotFoundException { // selected JSONArray selArr = null; // by somehow if the selected.json was empty, then this can cause parse error if (selected.exists() && (selected.length() <= 0)) { selected.delete(); } if (selected.exists()) { String sel = FileUtils.slurp(new FileInputStream(selected)); JSONObject selRoot = new JSONObject(sel); selArr = selRoot.getJSONArray("selected"); } // parse the file String data = FileUtils.slurp(path, mgr); JSONObject root = new JSONObject(data); JSONArray classes = root.getJSONArray("classes"); int numClasses = classes.length(); ArrayList<ComicClass> com_arr = new ArrayList<ComicClass>(); // set fields mPkg = root.getString("package"); mIdxs = new HashMap<String, Integer>(); mSel = selected; int j = 0; for (int i = 0; i < numClasses; i++) { JSONObject clz = classes.getJSONObject(i); String key = clz.getString("name"); if (!_canAdd(key)) { Log.d(TAG, "Not adding the comic '" + key + "' to the list..."); continue; } ComicClass cl = new ComicClass(); cl.mClass = clz.getString("class"); cl.mName = key; cl.mPref = clz.getString("pref"); if (clz.has("new") && clz.getString("new").equals("1")) { cl.mNew = true; } else { cl.mNew = false; } cl.mSel = false; com_arr.add(cl); mIdxs.put(key, j); j++; } mClasses = new ComicClass[com_arr.size()]; com_arr.toArray(mClasses); if (selArr != null) { int num = selArr.length(); for (int i = 0; i < num; ++i) { String s = selArr.getString(i); setSelected(s, true); } } numClasses = mClasses.length; for (int i = 0; i < numClasses; ++i) { if (!mClasses[i].mSel) { continue; } Comic com = getComicObject(i); mClasses[i].mUnread = com.readOnlyUnread(); } }