public static ArrayList<ContentValues> parsePosts( TagNode aThread, int aThreadId, int unreadIndex, int opId, AwfulPreferences prefs, int startIndex) { ArrayList<ContentValues> result = new ArrayList<ContentValues>(); boolean lastReadFound = false; int index = startIndex; String update_time = new Timestamp(System.currentTimeMillis()).toString(); Log.v(TAG, "Update time: " + update_time); try { if (!Constants.isICS() || !prefs.inlineYoutube) { // skipping youtube support for now, it kinda sucks. aThread = convertVideos(aThread); } TagNode[] postNodes = aThread.getElementsByAttValue("class", "post", true, true); for (TagNode node : postNodes) { // fyad status, to prevent processing postbody twice if we are in fyad ContentValues post = new ContentValues(); post.put(THREAD_ID, aThreadId); // We'll just reuse the array of objects rather than create // a ton of them int id = Integer.parseInt(node.getAttributeByName("id").replaceAll("post", "")); post.put(ID, id); post.put(AwfulProvider.UPDATED_TIMESTAMP, update_time); post.put(POST_INDEX, index); if (index > unreadIndex) { post.put(PREVIOUSLY_READ, 0); lastReadFound = true; } else { post.put(PREVIOUSLY_READ, 1); } index++; post.put(IS_MOD, 0); post.put(IS_ADMIN, 0); TagNode[] postContent = node.getElementsHavingAttribute("class", true); for (TagNode pc : postContent) { if (pc.getAttributeByName("class").contains("author")) { post.put(USERNAME, pc.getText().toString().trim()); } if (pc.getAttributeByName("class").contains("role-mod")) { post.put(IS_MOD, 1); } if (pc.getAttributeByName("class").contains("role-admin")) { post.put(IS_ADMIN, 1); } if (pc.getAttributeByName("class").equalsIgnoreCase("title") && pc.getChildTags().length > 0) { TagNode[] avatar = pc.getElementsByName("img", true); if (avatar.length > 0) { post.put(AVATAR, avatar[0].getAttributeByName("src")); } post.put(AVATAR_TEXT, pc.getText().toString().trim()); } if (pc.getAttributeByName("class").equalsIgnoreCase("postbody") || pc.getAttributeByName("class").contains("complete_shit")) { TagNode[] images = pc.getElementsByName("img", true); for (TagNode img : images) { // don't alter video mock buttons if ((img.hasAttribute("class") && img.getAttributeByName("class").contains("videoPlayButton"))) { continue; } boolean dontLink = false; TagNode parent = img.getParent(); String src = img.getAttributeByName("src"); if ((parent != null && parent.getName().equals("a")) || (img.hasAttribute("class") && img.getAttributeByName("class") .contains("nolink"))) { // image is linked, don't override dontLink = true; } if (src.contains(".gif")) { img.setAttribute( "class", (img.hasAttribute("class") ? img.getAttributeByName("class") + " " : "") + "gif"); } if (img.hasAttribute("title")) { if (!prefs.showSmilies) { // kill all emotes String name = img.getAttributeByName("title"); img.setName("p"); img.addChild(new ContentNode(name)); } } else { if (!lastReadFound && prefs.hideOldImages || !prefs.imagesEnabled) { if (!dontLink) { img.setName("a"); img.setAttribute("href", src); img.addChild(new ContentNode(src)); } else { img.setName("p"); img.addChild(new ContentNode(src)); } } else { if (!dontLink) { img.setName("a"); img.setAttribute("href", src); TagNode newimg = new TagNode("img"); if (!prefs.imgurThumbnails.equals("d") && src.contains("i.imgur.com")) { int lastSlash = src.lastIndexOf('/'); if (src.length() - lastSlash <= 9) { int pos = src.length() - 4; src = src.substring(0, pos) + prefs.imgurThumbnails + src.substring(pos); } } newimg.setAttribute("src", src); img.addChild(newimg); } } } } StringBuffer fixedContent = new StringBuffer(); Matcher fixCharMatch = fixCharacters_regex.matcher(NetworkUtils.getAsString(pc)); while (fixCharMatch.find()) { fixCharMatch.appendReplacement(fixedContent, ""); } fixCharMatch.appendTail(fixedContent); post.put(CONTENT, fixedContent.toString()); } if (pc.getAttributeByName("class").equalsIgnoreCase("postdate")) { post.put( DATE, NetworkUtils.unencodeHtml(pc.getText().toString()) .replaceAll("[^\\w\\s:,]", "") .trim()); } if (pc.getAttributeByName("class").equalsIgnoreCase("profilelinks")) { TagNode[] links = pc.getElementsHavingAttribute("href", true); if (links.length > 0) { String href = links[0].getAttributeByName("href").trim(); String userId = href.substring(href.lastIndexOf("rid=") + 4); post.put(USER_ID, userId); if (Integer.toString(opId).equals(userId)) { // ugh post.put(IS_OP, 1); } else { post.put(IS_OP, 0); } } } if (pc.getAttributeByName("class").equalsIgnoreCase("editedby") && pc.getChildTags().length > 0) { post.put(EDITED, "<i>" + pc.getChildTags()[0].getText().toString() + "</i>"); } } TagNode[] editImgs = node.getElementsByAttValue("alt", "Edit", true, true); if (editImgs.length > 0) { post.put(EDITABLE, 1); } else { post.put(EDITABLE, 0); } result.add(post); } Log.i( TAG, Integer.toString(postNodes.length) + " posts found, " + result.size() + " posts parsed."); } catch (Exception e) { e.printStackTrace(); } return result; }
@Override public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { for (String valueSummaryKey : VALUE_SUMMARY_KEYS_LIST) { if (valueSummaryKey.equals(key)) { if ("selected_theme".equals(key)) { ListPreference p = (ListPreference) findPreference(key); p.setSummary(p.getEntry()); } if (key.equals("themes")) { ListPreference p = (ListPreference) findPreference(key); if (lastTheme.equals("yospos")) { Editor prefEdit = prefs.edit(); prefEdit.putString("preferred_font", "default"); savePreferences(prefEdit); } if (p.getValue().equals("dark")) { if (lastTheme.equals("custom")) { saveCustomTheme(); } Editor prefEdit = prefs.edit(); prefEdit.putInt( "default_post_font_color", getResources().getColor(R.color.dark_default_post_font)); prefEdit.putInt( "secondary_post_font_color", getResources().getColor(R.color.dark_secondary_post_font)); prefEdit.putInt( "default_post_background_color", getResources().getColor(R.color.dark_background)); prefEdit.putInt( "alternative_post_background_color", getResources().getColor(R.color.dark_alt_background)); prefEdit.putInt( "read_post_font_color", getResources().getColor(R.color.dark_secondary_post_font)); prefEdit.putInt( "read_post_background_color", getResources().getColor(R.color.dark_background_read)); prefEdit.putInt( "alternative_read_post_background_color", getResources().getColor(R.color.dark_alt_background_read)); prefEdit.putInt( "post_header_background_color", getResources().getColor(R.color.dark_header_background)); // TODO prefEdit.putInt( "post_divider_color", getResources().getColor(R.color.dark_header_divider)); // TODO prefEdit.putBoolean("post_divider_enabled", false); prefEdit.putInt( "post_header_font_color", getResources().getColor(R.color.dark_header_font)); // TODO prefEdit.putInt("op_post_color", getResources().getColor(R.color.dark_op_post)); prefEdit.putInt("link_quote_color", getResources().getColor(R.color.dark_link_quote)); prefEdit.putString("selected_theme", "dark"); savePreferences(prefEdit); p.setSummary("Dark"); lastTheme = "dark"; this.finish(); return; } else if (p.getValue().equals("default")) { if (lastTheme.equals("custom")) { saveCustomTheme(); } Editor prefEdit = prefs.edit(); prefEdit.putInt( "default_post_font_color", getResources().getColor(R.color.default_post_font)); prefEdit.putInt( "secondary_post_font_color", getResources().getColor(R.color.secondary_post_font)); prefEdit.putInt( "default_post_background_color", getResources().getColor(R.color.background)); prefEdit.putInt( "alternative_post_background_color", getResources().getColor(R.color.alt_background)); prefEdit.putInt( "read_post_font_color", getResources().getColor(R.color.default_post_font)); prefEdit.putInt( "read_post_background_color", getResources().getColor(R.color.background_read)); prefEdit.putInt( "alternative_read_post_background_color", getResources().getColor(R.color.alt_background_read)); prefEdit.putInt( "post_header_background_color", getResources().getColor(R.color.forums_blue)); // TODO prefEdit.putInt( "post_divider_color", getResources().getColor(R.color.background)); // TODO prefEdit.putInt( "post_header_font_color", getResources().getColor(R.color.forums_gray)); // TODO prefEdit.putBoolean("post_divider_enabled", false); prefEdit.putInt("op_post_color", getResources().getColor(R.color.op_post)); prefEdit.putInt("link_quote_color", getResources().getColor(R.color.link_quote)); if (Constants.isWidescreen( getParent())) { // TODO fix this, tablet mode will only be used in landscape on // certain devices. this won't accommodate that. prefEdit.putString("selected_theme", "light"); } else { prefEdit.putString("selected_theme", "dark"); } savePreferences(prefEdit); p.setSummary("Default"); lastTheme = "default"; this.finish(); return; } else if (p.getValue().equals("yospos")) { if (lastTheme.equals("custom")) { saveCustomTheme(); } Editor prefEdit = prefs.edit(); prefEdit.putInt( "default_post_font_color", getResources().getColor(R.color.yospos_default_post_font)); prefEdit.putInt( "secondary_post_font_color", getResources().getColor(R.color.yospos_secondary_post_font)); prefEdit.putInt( "default_post_background_color", getResources().getColor(R.color.yospos_background)); prefEdit.putInt( "alternative_post_background_color", getResources().getColor(R.color.yospos_alt_background)); prefEdit.putInt( "read_post_font_color", getResources().getColor(R.color.yospos_default_post_font)); prefEdit.putInt( "read_post_background_color", getResources().getColor(R.color.yospos_background_read)); prefEdit.putInt( "alternative_read_post_background_color", getResources().getColor(R.color.yospos_alt_background_read)); prefEdit.putInt( "post_header_background_color", getResources().getColor(R.color.yospos_background)); // TODO prefEdit.putInt( "post_divider_color", getResources().getColor(R.color.yospos_default_post_font)); // TODO prefEdit.putInt( "post_header_font_color", getResources().getColor(R.color.yospos_default_post_font)); // TODO prefEdit.putBoolean("post_divider_enabled", true); prefEdit.putString("preferred_font", "fonts/terminus_mono.ttf.mp3"); prefEdit.putInt("op_post_color", getResources().getColor(R.color.yospos_op_post)); prefEdit.putInt("link_quote_color", getResources().getColor(R.color.yospos_link_quote)); prefEdit.putString("selected_theme", "dark"); savePreferences(prefEdit); p.setSummary("yospos, bitch"); lastTheme = "yospos"; this.finish(); return; } else if (p.getValue().equals("darkblue")) { if (lastTheme.equals("custom")) { saveCustomTheme(); } Editor prefEdit = prefs.edit(); prefEdit.putInt( "default_post_font_color", getResources().getColor(R.color.dark_default_post_font)); prefEdit.putInt( "secondary_post_font_color", getResources().getColor(R.color.dark_secondary_post_font)); prefEdit.putInt( "default_post_background_color", getResources().getColor(R.color.dark_background)); prefEdit.putInt( "alternative_post_background_color", getResources().getColor(R.color.dark_background)); prefEdit.putInt( "read_post_font_color", getResources().getColor(R.color.dark_secondary_post_font)); prefEdit.putInt( "read_post_background_color", getResources().getColor(R.color.dark_background)); prefEdit.putInt( "alternative_read_post_background_color", getResources().getColor(R.color.dark_background)); prefEdit.putInt( "post_header_background_color", getResources().getColor(R.color.dark_background)); // TODO prefEdit.putInt( "post_divider_color", getResources().getColor(R.color.dark_blue)); // TODO prefEdit.putBoolean("post_divider_enabled", true); prefEdit.putInt( "post_header_font_color", getResources().getColor(R.color.dark_header_font)); // TODO prefEdit.putInt("op_post_color", getResources().getColor(R.color.dark_op_post)); prefEdit.putInt("link_quote_color", getResources().getColor(R.color.dark_link_quote)); prefEdit.putString("selected_theme", "dark"); savePreferences(prefEdit); p.setSummary("Darkish Blueish"); lastTheme = "darkblue"; this.finish(); return; } else if (!lastTheme.equals("custom")) { Editor prefEdit = prefs.edit(); prefEdit.putInt( "default_post_font_color", mPrefs.getInt( "custom_default_post_font_color", getResources().getColor(R.color.default_post_font))); prefEdit.putInt( "secondary_post_font_color", mPrefs.getInt( "custom_secondary_post_font_color", getResources().getColor(R.color.secondary_post_font))); prefEdit.putInt( "default_post_background_color", mPrefs.getInt( "custom_default_post_background_color", getResources().getColor(R.color.background))); prefEdit.putInt( "alternative_post_background_color", mPrefs.getInt( "custom_alternative_post_background_color", getResources().getColor(R.color.alt_background))); prefEdit.putInt( "read_post_font_color", mPrefs.getInt( "custom_read_post_font_color", getResources().getColor(R.color.font_read))); prefEdit.putInt( "read_post_background_color", mPrefs.getInt( "custom_read_post_background_color", getResources().getColor(R.color.background_read))); prefEdit.putInt( "alternative_read_post_background_color", mPrefs.getInt( "custom_alternative_read_post_background_color", getResources().getColor(R.color.alt_background_read))); prefEdit.putInt( "post_header_background_color", mPrefs.getInt( "custom_default_post_font_color", getResources().getColor(R.color.forums_blue))); // TODO prefEdit.putInt( "post_divider_color", mPrefs.getInt( "custom_default_post_font_color", getResources().getColor(R.color.background))); // TODO prefEdit.putInt( "post_header_font_color", mPrefs.getInt( "custom_default_post_font_color", getResources().getColor(R.color.forums_gray))); // TODO prefEdit.putInt( "op_post_color", mPrefs.getInt("custom_op_post_color", getResources().getColor(R.color.op_post))); prefEdit.putInt( "link_quote_color", mPrefs.getInt( "custom_link_quote_color", getResources().getColor(R.color.link_quote))); savePreferences(prefEdit); p.setSummary("Custom"); lastTheme = "custom"; } } } } }