Android Open Source - tazDownloader Download Activity






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 ww.j  a  v a 2s.  c om*/
 * 
 * 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.Activity;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class DownloadActivity extends Activity {
  protected String TAG = getClass().getSimpleName();

  private ProgressBar downloadProgress;
  private TextView downloadInfo;
  private boolean receiverRegistered = false;
  private TazAvailableReceiver tazAvailableReceiver = new TazAvailableReceiver();
  private DownloadErrorReceiver downloadErrorReceiver = new DownloadErrorReceiver();

  protected class TazAvailableReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context ctx, Intent intent) {
      Log.d("TazAvailableReceiver", "onReceive " + ctx.getClass().getName()
          + " -> " + intent.getData());
      Intent displayIntent = new Intent(Intent.ACTION_VIEW);
      displayIntent.setDataAndType(intent.getData(), intent.getType());
      // displayIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      displayIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      displayIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

      try {
        startActivity(displayIntent);
      } catch (ActivityNotFoundException e) {
        showErrorMessage(ctx, getString(R.string.errNoViewer));
        finish();
        Log.e(getClass().getSimpleName(),
            "No viewer for MIME type " + intent.getType(), e);
      } finally {
        finish();
      }

    }
  }

  protected class DownloadErrorReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
      String msg = getString(R.string.errorDownloadUnspecified);
      if (intent.hasExtra(TazDownloadService.DOWNLOAD_ERROR_MSG)) {
        msg = intent.getStringExtra(TazDownloadService.DOWNLOAD_ERROR_MSG);
      }
      showErrorMessage(context, msg);
      finish();
    }

  }

  private void showErrorMessage(Context context, String msg) {
    Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
    toast.show();
  }

  @Override
  protected void onStart() {
    super.onStart();
    Log.d("DownloadActivity", "DownloadActivity started");
    registerReceiver();
    downloadAndDisplay();
  }

  @Override
  protected void onPause() {
    super.onPause();
    unregisterReceiver();
  }

  @Override
  protected void onResume() {
    super.onResume();
    registerReceiver();
  }

  private void registerReceiver() {
    if (receiverRegistered) {
      Log.d(TAG, "Receiver already registered for "
          + TazDownloadService.ACTION_DISPLAY_TAZ);
      return;
    }
    Log.d(TAG, "registering receiver for "
        + TazDownloadService.ACTION_DISPLAY_TAZ);
    IntentFilter intentFilter = new IntentFilter(
        TazDownloadService.ACTION_DISPLAY_TAZ);
    for (FileType t : FileType.values()) {
      try {
        intentFilter.addDataType(t.mimeType);
      } catch (MalformedMimeTypeException e) {
        throw new IllegalStateException(e);
      }
    }

    registerReceiver(downloadErrorReceiver, new IntentFilter(
        TazDownloadService.DOWNLOAD_ERROR));
    registerReceiver(tazAvailableReceiver, intentFilter);
    receiverRegistered = true;
  }

  private void unregisterReceiver() {
    unregisterReceiver(tazAvailableReceiver);
    unregisterReceiver(downloadErrorReceiver);
    receiverRegistered = false;
  }

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.download);
    downloadProgress = (ProgressBar) findViewById(R.id.downloadProgress);
    downloadInfo = (TextView) findViewById(R.id.downloadStatus);
    downloadInfo.setText("Starte download");
  }

  protected Calendar getDownloadDate() {
    Intent intent = getIntent();
    return (Calendar) intent
        .getSerializableExtra(TazDownloadService.EXTRA_TAZ_DATE);

  }

  protected void downloadAndDisplay() {
    Log.d(getClass().getSimpleName(),
        "Starting service for download and display");
    Calendar cal = getDownloadDate();
    String formattedDate = DateFormat.getDateFormat(this).format(cal.getTime());
    downloadInfo.setText(getString(R.string.startDLMsg).replace("${0}",
        formattedDate));
    Intent intent = new Intent(this, TazDownloadService.class);
    intent.putExtra(TazDownloadService.EXTRA_TAZ_DATE, cal);
    startService(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