Android Open Source - EBrowser Download Item






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// w  w  w. java 2 s .  co 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.model.items;

import java.util.Random;

import com.mrpej.ebrowser.R;
import org.zirco.events.EventConstants;
import org.zirco.events.EventController;
import org.zirco.ui.activities.DownloadsListActivity;
import org.zirco.ui.runnables.DownloadRunnable;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

/**
 * Represent a download item.
 */
public class DownloadItem {
  
  private Context mContext;
  
  private String mUrl;
  private String mFileName;
  
  private int mProgress;
  
  private String mErrorMessage;
  
  private DownloadRunnable mRunnable;
  
  private boolean mIsFinished;
  private boolean mIsAborted;
  
  private NotificationManager mNotificationManager;
  private Notification mNotification;
  private int mNotificationId;
  
  
  /**
   * get filename between '/' and '?'
   */
  public static String getFileNameFromUrl(String url) {
    int i = url.lastIndexOf('/');
    String filename = i!=-1? url.substring(i+1) : url;
    
    i = filename.indexOf('?');
    filename = i!=-1? filename.substring(0, i) : filename;
    
    return filename;
  }
  
  /**
   * Constructor.
   * @param context The current context.
   * @param url The download url.
   */
  public DownloadItem(Context context, String url) {
    mContext = context;
    
    mUrl = url;
    
    mFileName = getFileNameFromUrl(mUrl);
    
    mProgress = 0;
  
    mRunnable = null;
    mErrorMessage = null;
    
    mIsFinished = false;
    mIsAborted = false;
    
    Random r = new Random();
    mNotificationId = r.nextInt();
    mNotification = null;
    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
  }
  
  /**
   * Gets the download url.
   * @return The download url.
   */
  public String getUrl() {
    return mUrl;
  }
  
  /**
   * Gets the filename on disk.
   * @return The filename on disk.
   */
  public String getFileName() {
    return mFileName;
  }
  
  /**
   * Gets the download progress.
   * @return The download progress.
   */
  public int getProgress() {
    return mProgress;
  }
  
  /**
   * Set the current error message for this download.
   * @param errorMessage The error message.
   */
  public void setErrorMessage(String errorMessage) {
    mErrorMessage = errorMessage;
  }
  
  /**
   * Gets the error message for this download.
   * @return The error message.
   */
  public String getErrorMessage() {
    return mErrorMessage;
  }
  
  /**
   * Trigger a start download event.
   */
  public void onStart() {
    createNotification();
    
    EventController.getInstance().fireDownloadEvent(EventConstants.EVT_DOWNLOAD_ON_START, this);
  }
  
  /**
   * Set this item is download finished state. Trigger a finished download event.
   */
  public void onFinished() {
    mProgress = 100;
    mRunnable = null;
    
    mIsFinished = true;
    
    updateNotificationOnEnd();
    
    EventController.getInstance().fireDownloadEvent(EventConstants.EVT_DOWNLOAD_ON_FINISHED, this);
  }
  
  /**
   * Set the current progress. Trigger a progress download event.
   * @param progress The current progress.
   */
  public void onProgress(int progress) {
    mProgress = progress;
    
    EventController.getInstance().fireDownloadEvent(EventConstants.EVT_DOWNLOAD_ON_PROGRESS, this);
  }
  
  /**
   * Start the current download.
   */
  public void startDownload() {
    if (mRunnable != null) {
      mRunnable.abort();
    }
    mRunnable = new DownloadRunnable(this);
    new Thread(mRunnable).start();
  }
  
  /**
   * Abort the current download.
   */
  public void abortDownload() {
    if (mRunnable != null) {
      mRunnable.abort();
    }
    mIsAborted = true;
  }
  
  /**
   * Check if the download is finished.
   * @return True if the download is finished.
   */
  public boolean isFinished() {
    return mIsFinished;
  }
  
  /**
   * Check if the download is aborted.
   * @return True if the download is aborted.
   */
  public boolean isAborted() {
    return mIsAborted;
  }
  
  /**
   * Create the download notification.
   */
  private void createNotification() {
    mNotification = new Notification(R.drawable.download_anim, mContext.getString(R.string.DownloadNotification_DownloadStart), System.currentTimeMillis());    

    Intent notificationIntent = new Intent(mContext.getApplicationContext(), DownloadsListActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(mContext.getApplicationContext(), 0, notificationIntent, 0);

    mNotification.setLatestEventInfo(mContext.getApplicationContext(), mContext.getString(R.string.DownloadNotification_DownloadInProgress), mFileName, contentIntent);

    mNotificationManager.notify(mNotificationId, mNotification);
  }
  
  /**
   * Update the download notification at the end of download.
   */
  private void updateNotificationOnEnd() {
    if (mNotification != null) {
      mNotificationManager.cancel(mNotificationId);      
    }
    
    String message;
    if (mIsAborted) {
      message = mContext.getString(R.string.DownloadNotification_DownloadCanceled);
    } else {
      message = mContext.getString(R.string.DownloadNotification_DownloadComplete);
    }

    mNotification = new Notification(R.drawable.stat_sys_download, mContext.getString(R.string.DownloadNotification_DownloadComplete), System.currentTimeMillis());
    mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    Intent notificationIntent = new Intent(mContext.getApplicationContext(), DownloadsListActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(mContext.getApplicationContext(), 0, notificationIntent, 0);

    mNotification.setLatestEventInfo(mContext.getApplicationContext(), mFileName, message, contentIntent);

    mNotificationManager.notify(mNotificationId, mNotification);    
  }

}




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