package de.animux.android.andmal.api.anime; import java.io.InputStream; import java.net.URL; import java.util.Collection; import java.util.HashMap; import java.util.Map; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import de.animux.android.andmal.api.MalList; import de.animux.android.andmal.api.MalObject; import de.animux.android.andmal.api.MalObjectStore; import de.animux.android.andmal.api.MalState; import de.animux.android.andmal.api.anime.State; import de.animux.android.andmal.util.SortedLinkedList; public class AnimeList extends MalList implements MalObjectStore { // cache last instance of this class to restore if a new one is created with // the same username and password private static AnimeList cache; private Map> animes; public AnimeList(String username, String password) { super(username, password); if (((cache != null) && cache.getUsername().equals(username)) && cache.getPassword().equals(username)) { animes = cache.getObjects(); } else { cache = this; needsRefresh = true; } } public AnimeList() { super(); if (cache != null && cache.getUsername() != null && cache.getUsername().equals(this.getUsername())) { animes = cache.getObjects(); } else { cache = this; needsRefresh = true; } } @Override public void refresh() { if (this.getUsername() == null) return; animes = new HashMap>(); animes.put(State.WATCHING, new SortedLinkedList()); animes.put(State.COMPLETED, new SortedLinkedList()); animes.put(State.ONHOLD, new SortedLinkedList()); animes.put(State.DROPPED, new SortedLinkedList()); animes.put(State.PLANTOWATCH, new SortedLinkedList()); try { URL url = new URL( "http://myanimelist.net/malappinfo.php?status=all&type=anime&u=" + this.getUsername()); InputStream stream = url.openStream(); SAXParser sax = SAXParserFactory.newInstance().newSAXParser(); sax.parse(stream, new AnimeListParser(this)); needsRefresh = false; } catch (Exception e) { e.printStackTrace(); } } @Override public void add(Anime object) { if (animes.containsKey(State.valueOf(object.getMyStatus()))) { animes.get(State.valueOf(object.getMyStatus())).add(object); } } @Override public Map> getObjects() { return animes; } }