DeckManagerImpl.java :  » Log » cogtomp » com » googlecode » cogtomp » banga3_0603 » deck » impl » Android Open Source

Android Open Source » Log » cogtomp 
cogtomp » com » googlecode » cogtomp » banga3_0603 » deck » impl » DeckManagerImpl.java
package com.googlecode.cogtomp.banga3_0603.deck.impl;

import static com.googlecode.cogtomp.banga3_0603.BangaMain.TAG;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import android.util.Log;
import android.os.Environment;

import com.googlecode.cogtomp.banga3_0603.deck.Deck;
import com.googlecode.cogtomp.banga3_0603.deck.DeckBuilder;
import com.googlecode.cogtomp.banga3_0603.deck.DeckManager;

/**
 * Basic implementation of a DeckFactory
 * 
 * @author Brian Freeman (brian dot freeman at colorado dot edu)
 * @since February 12, 2008
 */
public class DeckManagerImpl extends DeckManager {

    /** The default directory to start searching for deck jars files in */
    //private static final String DEFAULT_DIR = "/data/download";
    private static final String DEFAULT_DIR = "/data/data/com.googlecode.cogtomp.banga3_0603/files";

    private static final String BANGA_XML_FILENAME = "banga-deck.xml";

    private static final String BANGA_ZIP_EXT = ".deck";
    /**
     * Protected so it can only be created by other package members
     */
    public DeckManagerImpl() {
        super();
    }

    /*
     * (non-Javadoc)
     * 
     * Reads the list of jar files from the DEFAULT_DIR and parses them
     * returning the set of found decks
     * 
     * @see com.googlecode.cogtomp.banga.deck.DeckManager#getDecks()
     */
    @Override
    public List<Deck> loadDecks() {
        List<Deck> deckList = new ArrayList<Deck>(0);
        
        // start searching for card decks and add them to the set
        Log.d(TAG, "in deck mgr");
        File f = new File(DEFAULT_DIR);
        //File f=Environment.getDataDirectory();
        Log.d(TAG, "dir "+f.toString());
        File[] zipFiles = f.listFiles(new FileFilter() {
            public boolean accept(File fileArg) {
                // if it ends with .jar then add it to the list
              Log.d(TAG, "file"+fileArg.getName());
                return fileArg.getName().endsWith(BANGA_ZIP_EXT);
            }

        });

        //Log.d(TAG, "There are " + zipFiles.length + " Banga deck file(s) in "
        //        + DEFAULT_DIR);
      Log.d(TAG, "There are " + zipFiles.length + " Banga deck file(s) in here"
                + f.toString());

        for (File jf : zipFiles) {
            // see if the jar file is a card deck
            try {
                ZipFile jarFile = new ZipFile(jf);
                ZipEntry xmlZipEntry = jarFile.getEntry(BANGA_XML_FILENAME);

                if (xmlZipEntry != null) {
                    Log.d(TAG, "JarFile contains " + BANGA_XML_FILENAME + ": "
                            + jf);
                    //processJarFile(deckFile, xmlZipEntry);
                    DeckFileReader dfr = new DeckFileReader();
                    deckList.addAll(dfr.processDeckFile(jarFile, xmlZipEntry));
                } else {
                    // invalid jar, skip it
                    Log.d(TAG, "Skipping jar file that didn't contain "
                            + BANGA_XML_FILENAME + ": " + jf);
                }
            } catch (IOException e) {
                Log.d(TAG, "Ignoring IOException reading jar file: " + e, e);
                // just ignore the exception and move on to the next file
            }
        }

        return Collections.unmodifiableList(deckList);
    }

    /*
     * (non-Javadoc)
     * @see com.googlecode.cogtomp.banga.deck.DeckManager#getDeckBuilder()
     */
    public DeckBuilder createDeckBuilder() {
        return new DeckBuilderImpl();
    }
    
    /*
     * (non-Javadoc)
     * @see com.googlecode.cogtomp.banga.deck.DeckManager#saveDeck(com.googlecode.cogtomp.banga.deck.Deck, java.lang.String)
     *
    @Override
    public void saveDeck(Deck deckToSave, String filename) {
        throw new UnsupportedOperationException("This method has not been implemented yet");
    }*/
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.