Android Open Source - EBrowser Download Runnable






From Project

Back to project page EBrowser.

License

The source code is released under:

GNU General Public License

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

/*
 * Zirco Browser for Android//ww  w.  j a  v a  2  s. c  o m
 * 
 * Copyright (C) 2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * 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.
 */

package org.zirco.ui.runnables;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.zirco.model.items.DownloadItem;
import org.zirco.utils.IOUtils;

import android.os.Handler;
import android.os.Message;

/**
 * Background downloader.
 */
public class DownloadRunnable implements Runnable {
      
  private static final int BUFFER_SIZE = 4096;
  
  private DownloadItem mParent;
  
  private boolean mAborted;
  
  /**
   * Contructor.
   * @param parent The item to download.
   */
  public DownloadRunnable(DownloadItem parent) {
    mParent = parent;
    mAborted = false;
  }
  
  private Handler mHandler = new Handler() {        
    
    public void handleMessage(Message msg) {
      mParent.onFinished();
    }
  };
  
  /**
   * Compute the file name given the url.
   * @return The file name.
   */
  private String getFileNameFromUrl() {
    return DownloadItem.getFileNameFromUrl(mParent.getUrl());
  }
  
  /**
   * Get a file object representation of the file name, in th right folder of the SD card.
   * @return A file object.
   */
  private File getFile() {
    
    File downloadFolder = IOUtils.getDownloadFolder();
    
    if (downloadFolder != null) {
      
      return new File(downloadFolder, getFileNameFromUrl());
      
    } else {
      mParent.setErrorMessage("Unable to get download folder from SD Card.");      
      return null;
    }        
  }
  
  @Override
  public void run() {
    File downloadFile = getFile();
    
    if (downloadFile != null) {
      
      if (downloadFile.exists()) {
        downloadFile.delete();
      }
      
      BufferedInputStream bis = null;
      BufferedOutputStream bos = null;
      
      try {
        
        mParent.onStart();
        
        URL url = new URL(mParent.getUrl());
        URLConnection conn = url.openConnection();
        
        InputStream is = conn.getInputStream();
              
        int size = conn.getContentLength();
        
        double oldCompleted = 0;
        double completed = 0;
        
        bis = new BufferedInputStream(is);
        bos = new BufferedOutputStream(new FileOutputStream(downloadFile));
        
        boolean downLoading = true;
        byte[] buffer = new byte[BUFFER_SIZE];
        int downloaded = 0;
        int read;
        
        while ((downLoading) &&
            (!mAborted)) {

          if ((size - downloaded < BUFFER_SIZE) &&
              (size - downloaded > 0)) {
            buffer = new byte[size - downloaded];
          }

          read = bis.read(buffer);
          
          if (read > 0) {
            bos.write(buffer, 0, read);
            downloaded += read;
            
            completed = ((downloaded * 100f) / size);
            
          } else {
            downLoading = false;
          }
          
          // Notify each 5% or more.
          if (oldCompleted + 5 < completed) {
            mParent.onProgress((int) completed);
            oldCompleted = completed;
          }
        }

      } catch (MalformedURLException mue) {
        mParent.setErrorMessage(mue.getMessage());
      } catch (IOException ioe) {
        mParent.setErrorMessage(ioe.getMessage());
      } finally {
        
        if (bis != null) {
          try {
            bis.close();
          } catch (IOException ioe) {
            mParent.setErrorMessage(ioe.getMessage());
          }
        }
        if (bos != null) {
          try {
            bos.close();
          } catch (IOException ioe) {
            mParent.setErrorMessage(ioe.getMessage());
          }
        }              
      }
    
      if (mAborted) {
        if (downloadFile.exists()) {
          downloadFile.delete();
        }
      }
      
    } 
    
    mHandler.sendEmptyMessage(0);
  }
  
  /**
   * Abort this download.
   */
  public void abort() {
    mAborted = true;
  }

}




Java Source Code List

org.emergent.android.weave.client.Base32.java
org.emergent.android.weave.client.Base64Encoder.java
org.emergent.android.weave.client.Base64.java
org.emergent.android.weave.client.BulkKeyCouplet.java
org.emergent.android.weave.client.Dbg.java
org.emergent.android.weave.client.HexEncoder.java
org.emergent.android.weave.client.Hex.java
org.emergent.android.weave.client.QueryParams.java
org.emergent.android.weave.client.QueryResult.java
org.emergent.android.weave.client.UserWeave.java
org.emergent.android.weave.client.WeaveAccountInfo.java
org.emergent.android.weave.client.WeaveBasicObject.java
org.emergent.android.weave.client.WeaveConstants.java
org.emergent.android.weave.client.WeaveCryptoUtil.java
org.emergent.android.weave.client.WeaveException.java
org.emergent.android.weave.client.WeaveFactory.java
org.emergent.android.weave.client.WeaveHeader.java
org.emergent.android.weave.client.WeaveResponse.java
org.emergent.android.weave.client.WeaveSSLSocketFactory.java
org.emergent.android.weave.client.WeaveTransport.java
org.emergent.android.weave.client.WeaveUtil.java
org.greendroid.QuickActionGrid.java
org.greendroid.QuickActionWidget.java
org.greendroid.QuickAction.java
org.zirco.controllers.Controller.java
org.zirco.events.EventConstants.java
org.zirco.events.EventController.java
org.zirco.events.IDownloadEventsListener.java
org.zirco.model.DbAdapter.java
org.zirco.model.UrlSuggestionItemComparator.java
org.zirco.model.adapters.BookmarksCursorAdapter.java
org.zirco.model.adapters.DownloadListAdapter.java
org.zirco.model.adapters.HistoryExpandableListAdapter.java
org.zirco.model.adapters.UrlSuggestionCursorAdapter.java
org.zirco.model.adapters.WeaveBookmarksCursorAdapter.java
org.zirco.model.items.BookmarkItem.java
org.zirco.model.items.DownloadItem.java
org.zirco.model.items.HistoryItem.java
org.zirco.model.items.UrlSuggestionItem.java
org.zirco.model.items.WeaveBookmarkItem.java
org.zirco.providers.BookmarksProviderWrapper.java
org.zirco.providers.WeaveColumns.java
org.zirco.providers.WeaveContentProvider.java
org.zirco.providers.ZircoBookmarksContentProvider.java
org.zirco.sync.ISyncListener.java
org.zirco.sync.WeaveSyncTask.java
org.zirco.ui.activities.AboutActivity.java
org.zirco.ui.activities.AdBlockerWhiteListActivity.java
org.zirco.ui.activities.BookmarksHistoryActivity.java
org.zirco.ui.activities.BookmarksListActivity.java
org.zirco.ui.activities.ChangelogActivity.java
org.zirco.ui.activities.DownloadsListActivity.java
org.zirco.ui.activities.EditBookmarkActivity.java
org.zirco.ui.activities.HistoryListActivity.java
org.zirco.ui.activities.IToolbarsContainer.java
org.zirco.ui.activities.MainActivity.java
org.zirco.ui.activities.MobileViewListActivity.java
org.zirco.ui.activities.WeaveBookmarksListActivity.java
org.zirco.ui.activities.preferences.BaseSpinnerCustomPreferenceActivity.java
org.zirco.ui.activities.preferences.HomepagePreferenceActivity.java
org.zirco.ui.activities.preferences.PreferencesActivity.java
org.zirco.ui.activities.preferences.SearchUrlPreferenceActivity.java
org.zirco.ui.activities.preferences.UserAgentPreferenceActivity.java
org.zirco.ui.activities.preferences.WeavePreferencesActivity.java
org.zirco.ui.activities.preferences.WeaveServerPreferenceActivity.java
org.zirco.ui.components.CustomWebViewClient.java
org.zirco.ui.components.CustomWebView.java
org.zirco.ui.runnables.DownloadRunnable.java
org.zirco.ui.runnables.FaviconUpdaterRunnable.java
org.zirco.ui.runnables.HideToolbarsRunnable.java
org.zirco.ui.runnables.HistoryUpdater.java
org.zirco.ui.runnables.XmlHistoryBookmarksExporter.java
org.zirco.ui.runnables.XmlHistoryBookmarksImporter.java
org.zirco.utils.AnimationManager.java
org.zirco.utils.ApplicationUtils.java
org.zirco.utils.Constants.java
org.zirco.utils.DateUtils.java
org.zirco.utils.IOUtils.java
org.zirco.utils.ProxyChangeReceiver.java
org.zirco.utils.ProxySettings.java
org.zirco.utils.UrlUtils.java