Android Open Source - tazDownloader Taz Download Service






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 v  a2 s  . co 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.util.Calendar;

import read.taz.TazFile.FileType;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.Toast;

public class TazDownloadService extends IntentService implements
    TazDownloadListener {

  protected String TAG = getClass().getSimpleName();
  static final String ACTION_DISPLAY_TAZ = "DISPLAY_TAZ";
  static final String DOWNLOAD_ERROR = "TAZ_DOWNLOAD_ERROR";
  static final String DOWNLOAD_ERROR_MSG = "TAZ_DOWNLOAD_ERROR_MSG";
  static final String EXTRA_TAZ_DATE = "read.taz.date";
  private Toast toast;
  private Calendar downloadDate;

  public TazDownloadService() {
    super("TazDownloadService");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    displayStartDownloadMsg();
    startDownload(intent);
    displayFinishedMsg();
  }

  private void displayStartDownloadMsg() {
    Context context = getApplicationContext();
    CharSequence text = "Starte download der taz";
    int duration = Toast.LENGTH_SHORT;
    toast = Toast.makeText(context, text, duration);
    toast.show();
  }

  private void displayFinishedMsg() {
    if (toast != null) {
      toast.cancel();
    }
  }

  private void startDownload(Intent intent) {

    SharedPreferences prefs = PreferenceManager
        .getDefaultSharedPreferences(this);

    FileType fileType = FileType.valueOf(prefs.getString(
        getString(R.string.fileTypeKey), "PDF"));

    downloadDate = (Calendar) intent.getSerializableExtra(EXTRA_TAZ_DATE);

    TazFile taz = new TazFileFactory(this).getNewspaper(fileType, downloadDate);
    if (taz == null) {
      return;
    }

    Log.d(TAG, "User requested taz " + taz);

    if (taz.file.exists()) {
      success(taz);
    } else {
      String uname = prefs.getString(getString(R.string.unameKey), "unset");
      String passwd = prefs.getString(getString(R.string.passwdKey), "unset");
      TazDownloader downloader = new TazDownloader(taz, uname, passwd, this);
      downloader.run();
    }

  }

  @Override
  public void success(TazFile taz) {
    Log.d(getClass().getSimpleName(), "Download OK. taz available at "
        + taz.file);
    Intent intent = new Intent(ACTION_DISPLAY_TAZ);
    intent.setDataAndType(Uri.fromFile(taz.file), taz.type.mimeType);
    Log.d(
        TAG,
        "Sending intent " + intent.getAction() + " for "
            + Uri.fromFile(taz.file) + " (" + taz.type.mimeType + ")");
    sendBroadcast(intent);

  }

  @Override
  public void error(TazFile taz, Throwable cause) {
    Log.w(getClass().getSimpleName(), "Download failed: " + taz.file, cause);
    stopSelf();
    Intent intent = new Intent(DOWNLOAD_ERROR);

    String formattedDate = DateFormat.getDateFormat(this).format(
        taz.date.getTime());

    intent.putExtra(DOWNLOAD_ERROR_MSG, getString(R.string.errorNoTazForDate)
        .replace("${0}", formattedDate));
    sendBroadcast(intent);
  }

}




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