Android Open Source - tazDownloader Taz File Factory






From Project

Back to project page tazDownloader.

License

The source code is released under:

GNU General Public License

If you think the Android project tazDownloader 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

/*******************************************************************************
 * Copyright (c) 2012 Olaf Sebelin./*  w  w  w.j  a  va2 s .  c o  m*/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package read.taz;

import java.io.File;
import java.io.FileFilter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import read.taz.TazFile.FileType;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class TazFileFactory {

  private static String TAG = TazFileFactory.class.getSimpleName();
  private Context context;
  private SimpleDateFormat fileDateFormat = new SimpleDateFormat(
      "yyyy'_'MM'_'dd");

  public TazFileFactory(Context context) {
    super();
    this.context = context;
  }

  public TazFile getNewspaper(TazFile.FileType type) {
    return getNewspaper(type, Calendar.getInstance());

  }

  public TazFile getNewspaper(TazFile.FileType type, Calendar date) {

    if (externalStorageAvailable()) {
      return fromExternal(type, date);
    }
    Log.e(TAG, "Internal storage not supported");
    return null;

  }

  private boolean externalStorageAvailable() {
    return Environment.MEDIA_MOUNTED.equals(Environment
        .getExternalStorageState());
  }

  private TazFile fromExternal(TazFile.FileType type, Calendar date) {
    File storage = context.getExternalFilesDir(null);
    return new TazFile(type, date, new File(storage, getFilename(type, date)));
  }

  private String getFilename(TazFile.FileType type, Calendar date) {
    StringBuilder buf = new StringBuilder();
    buf.append(type.filenamePrefix);
    // FIXME use SimpleDateFormat
    buf.append(date.get(Calendar.YEAR));
    buf.append("_");
    int month = date.get(Calendar.MONTH) + 1;
    if (month < 10) {
      buf.append("0");
    }
    buf.append(month);
    buf.append("_");
    int day = date.get(Calendar.DAY_OF_MONTH);
    if (day < 10) {
      buf.append("0");
    }
    buf.append(day);
    buf.append(type.filenameSuffix);
    return buf.toString();
  }

  private static class TazFileFilter implements FileFilter {
    static final TazFileFilter INSTANCE = new TazFileFilter();

    @Override
    public boolean accept(File file) {
      for (FileType ft : FileType.values()) {
        String filename = file.getName();
        if (filename.startsWith(ft.filenamePrefix)
            && filename.endsWith(ft.filenameSuffix)) {
          return true;
        }
      }

      return false;
    }
  }

  private TazFile fromFile(File tazFile) {
    for (FileType ft : FileType.values()) {
      String filename = tazFile.getName();
      if (ft.matches(filename)) {
        String rawDate = ft.stripFilename(filename);
        try {
          Calendar cal = Calendar.getInstance();
          cal.setTime(fileDateFormat.parse(rawDate));
          return new TazFile(ft, cal, tazFile);
        } catch (ParseException e) {
          Log.e(TAG, "Cannot parse file date", e);
          return null;
        }
      }
    }
    return null;
  }

  public void cleanupFilesOlderThan(Date that) {
    if (!externalStorageAvailable()) {
      Log.i(TAG, "External storage not available. No cleanup");
      return;
    }
    File storage = context.getExternalFilesDir(null);
    for (File f : storage.listFiles(TazFileFilter.INSTANCE)) {
      TazFile tf = fromFile(f);
      if (tf != null) {
        tf.deleteIfOlderThan(that);
      }
    }
  }
}




Java Source Code List

read.taz.AbstractTazReaderMainActivity.java
read.taz.CleanupService.java
read.taz.DownloadActivity.java
read.taz.SettingsActivity.java
read.taz.ShowOnStartup.java
read.taz.TazDownloadListener.java
read.taz.TazDownloadService.java
read.taz.TazDownloader.java
read.taz.TazFileFactory.java
read.taz.TazFile.java
read.taz.TazReaderActivity.java