Android Open Source - Wikipedia-Daily-Widget On This Day Handler






From Project

Back to project page Wikipedia-Daily-Widget.

License

The source code is released under:

GNU General Public License

If you think the Android project Wikipedia-Daily-Widget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rmc.dfaw;
/*from w  ww. j a  v a  2  s. c o m*/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OnThisDayHandler extends WikiWidgetHandler {
  final static String WIKI_TODAY_IN_HISTORY_PATH = "https://en.wikipedia.org/w/api.php?action=featuredfeed&feed=onthisday&feedformat=atom";

  final String YEAR_PATTERN = "([0-9]{3,4}[\\s])([])([\\s])";

  public OnThisDayHandler() {
    super(WIKI_TODAY_IN_HISTORY_PATH);

  }

  public String formatTodayInHistoryText(String strSummary) {
    LinkedList<String> yearToItem = insertNewlines(strSummary);

    String[] s = strSummary.split(YEAR_PATTERN);

    strSummary = "";
    for (int j = 0; j < s.length; j++) {
      if (j > 0) {
        strSummary += yearToItem.get(j - 1) + "- " + s[j];
        //only add a newline if it doesn't already have one.
        if(!s[j].contains("\n")){
          strSummary += "\n";
        }
            } else {
                strSummary = s[j].trim() + ".\n";
      }
    }
    return strSummary;
  }

  private LinkedList<String> insertNewlines(String strSummary) {

    LinkedList<String> yearToItem = new LinkedList<String>();
    Pattern years = Pattern.compile(YEAR_PATTERN);
    Matcher m = years.matcher(strSummary);
    while (m.find()) {
      yearToItem.add(m.group(1));
    }

    return yearToItem;
  }

  public String cleanupSummary(String strSummary) {
    strSummary = super.cleanupSummary(strSummary);
    String temp[] = strSummary.split("More anniversaries:");
    strSummary = temp[0] + "\nTap to see more..";
    return formatTodayInHistoryText(strSummary);

  }

  public String generateClickURL(String strSummary) {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat month = new SimpleDateFormat("MMMMMMMMM");
    String monthName = month.format(calendar.getTime());
    SimpleDateFormat day = new SimpleDateFormat("dd");
    String dayName = day.format(calendar.getTime());

    return WIKI_SLASH_WIKI_URL + monthName + "_" + dayName;
  }

}




Java Source Code List

com.rmc.dfaw.FeaturedArticleHandler.java
com.rmc.dfaw.OnThisDayHandler.java
com.rmc.dfaw.UpdateStoryService.java
com.rmc.dfaw.WikiWidgetActivity.java
com.rmc.dfaw.WikiWidgetHandler.java
com.rmc.dfaw.WikiWidgetLauncherScreen.java
com.rmc.dfaw.WikiWidgetProvider4By2.java
com.rmc.dfaw.WikiWidgetProvider4By3.java
com.rmc.dfaw.WikiWidgetProvider4By4.java
com.rmc.dfaw.WikiWidgetProviderBase.java