DownloadManager

DownloadManager running on iPad

DownloadManager running on iPad

Works With:

apple_logo google_android

DownloadManager

Handles the downloading of files from the internet using either HTTP or FTP protocols.


  • Download one or more files
  • Pause and restart download
  • Provides multiple status events
  • Threaded and load balanced
  • Supports Android and iOS

$80$50

About DownloadManager

DownloadManager handles the downloading of one or more files from the internet using either HTTP or FTP protocols. All downloads take place in a background thread so your mobile application's interface remains responsive.

Available Events

DownloadManager provides several events for both the overall batch and for individual files that can be responded to such as:

  • DownloadError
  • DownloadCanceled
  • DowloadFileStarted
  • DownloadFileCompleted
  • FileDownloadProgressPercent
  • OverallDownloadProgressPercent
  • AllDownloadsCompleted

iOS Example

Here is an example of downloading a single file in iOS and showing an alert when it is completed:

using Appracatappra.ActionComponents.DownloadManager;
...

private DownloadManager _downloadManager;
...

public override void ViewDidLoad ()
{
    //Initialize the manager
    _dowloadManager=new DownloadManager();

    //Wireup completion handler
    _downloadManager.AllDownloadsCompleted+= delegate() {
        InvokeOnMainThread(delegate{
            //Display Alert Dialog Box
            using(var alert = new UIAlertView("DownloadManager", "All files have been downloaded", null, "OK", null))
            {
                alert.Show();   
            }
        });
    };

    //Specify the directory to download the file to
    string directory=Environment.GetFolderPath(Environment.SpecialFolder.Personal);

    //Queue up a file to download: Source URL, Directory to download to and optionally renaming the file
    _downloadManager.QueueFile("http://appracatappra.com/wp-content/plugins/download-monitor/download.php?id=4",directory,"NDA.pdf");

    //Start the download process
    _downloadManager.StartDownloading();
}

Android Example

Here is an example of downloading two files in the Android OS:

using Appracatappra.ActionComponents.DownloadManager;
...

[Activity (Label = "DownloadManager", MainLauncher = true)]
public class Activity1 : Activity
{
    #region Constants
    public const int DialogLongMessage = 1;
    #endregion 

    #region Private Variables
    private DownloadManager downloadManager;
    private string dialogMessage = "";
    #endregion  

    #region Overrides
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Create a new download manager
        downloadManager=new DownloadManager(); 

        // Get all of our interface items
        ProgressBar bar = FindViewById<ProgressBar> (Resource.Id.progressBar);
        TextView title = FindViewById<TextView> (Resource.Id.progressText);
        Button startDowload = FindViewById<Button> (Resource.Id.startDownload);
        Button cancel = FindViewById<Button> (Resource.Id.cancel);

        // Create path to hold downloaded files
        string directory = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
        Console.WriteLine("Directory: {0}",directory);

        // Wireup progress bar
        downloadManager.FileDownloadProgressPercent += (percentage) => {

            // Adjust percentage and display
            RunOnUiThread (() => {
                percentage *= 100f;
                bar.Progress = (int)percentage;
            });
        };

        // Wireup completion handler
        downloadManager.AllDownloadsCompleted += () => {

            // Run on UI Thread
            RunOnUiThread (() => {
                // Inform caller
                dialogMessage="All files have been downloaded";
                ShowDialog (DialogLongMessage);

                // Update GUI
                startDowload.Enabled = true;
                title.Visibility = ViewStates.Invisible;
                bar.Visibility = ViewStates.Invisible;
                cancel.Visibility = ViewStates.Invisible;
            });

        };

        // Wireup error handler
        downloadManager.DownloadError += (message) => {

            // Run on UI Thread
            RunOnUiThread (() => {
                // Update GUI
                startDowload.Enabled = true;
                title.Visibility = ViewStates.Invisible;
                bar.Visibility = ViewStates.Invisible;
                cancel.Visibility = ViewStates.Invisible;

                // Inform caller
                dialogMessage=message;
                ShowDialog (DialogLongMessage);
            });
        };

        // Wireup start button
        startDowload.Click += (sender, e) => {

            // Update GUI
            startDowload.Enabled = false;
            title.Visibility = ViewStates.Visible;
            bar.Visibility = ViewStates.Visible;
            cancel.Visibility = ViewStates.Visible;

            // Queue up files to download
            downloadManager.QueueFile ("http://appracatappra.com/wp-content/uploads/et_temp/ssh-140751_232x117.jpg",directory);
            downloadManager.QueueFile ("http://appracatappra.com/wp-content/uploads/et_temp/4-TD-web-5-662620_960x332.png",directory);

            // Start the download process
            ThreadPool.QueueUserWorkItem((callback) =>{
                downloadManager.StartDownloading();
            });

        };

        // Wireup cancel button
        cancel.Click += (sender, e) => {

            // Stop any running downloads
            downloadManager.AbortDownload ();

            // Update GUI
            startDowload.Enabled = true;
            title.Visibility = ViewStates.Invisible;
            bar.Visibility = ViewStates.Invisible;
            cancel.Visibility = ViewStates.Invisible;

            // Inform user
            dialogMessage="User has aborted all downloads";
            ShowDialog (DialogLongMessage);
        };

    }

    protected override Dialog OnCreateDialog (int id)
    {
        Dialog alert = null;

        base.OnCreateDialog (id);

        // Build requested dialog type
        switch (id){
        case DialogLongMessage:
            var builder = new AlertDialog.Builder(this);
            builder.SetIcon (Android.Resource.Attribute.AlertDialogIcon);
            builder.SetTitle ("DownloadManager");
            builder.SetMessage(dialogMessage);
            builder.SetPositiveButton ("OK", delegate(object sender, DialogClickEventArgs e) {
                // Ignore for now
            });
            alert=builder.Create ();
            break;
        }

        // Return dialog
        return alert;
    }
    #endregion 
}

Trial Version

The trial version of DownloadManager is limited to downloading a batch of no more than five (5) files at a time and then only image files in the .jpg format. The full version removes these restrictions.

Some screenshots created with PlaceIt

DownloadManager Component

Getting Started with DownloadManager

To use an DownloadManager in your mobile application include the ActionPack.iOS or ActionPack.Android component and reference the following using statement in your C# code:

using Appracatappra.ActionComponents.DownloadManager;

Adding Files to the Download Queue

To use DownloadManager add one or more DownloadItems representing files to be downloaded from the internet to your DownloadManager class.

iOS Example:

//Specify the directory to download the file to
string directory=Environment.GetFolderPath(Environment.SpecialFolder.Personal);

//Queue up a file to download: Source URL, Directory to download to and optionally renaming the file
_downloadManager.QueueFile("http://appracatappra.com/wp-content/plugins/download-monitor/download.php?id=4",directory,"NDA.pdf");

Android Example:

// Create path to hold downloaded files
string directory = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
... 

// Queue up files to download
downloadManager.QueueFile ("http://appracatappra.com/wp-content/uploads/et_temp/ssh-140751_232x117.jpg",directory);
downloadManager.QueueFile ("http://appracatappra.com/wp-content/uploads/et_temp/4-TD-web-5-662620_960x332.png",directory);

Working with Files on a FTP Server

While DownloadManager is able to download files from an HTTP server, for the best performance it is suggested to store your files on a FTP server instead. This is especially true of large files like video or animations.

Currently DownloadManager is unable to query the FTP server for the structure of the files and folders on the server. However, you can store a known index file on the server in a format such as JSON or XML, read that file from the server first and then use it to specify all of the other files required.

When dealing with files on an FTP server, you might need to provide a user ID and Password. DownloadManager is capable of handling files in this situation by using the following code:

// Build a path to the file of the FTP server
var uri = new Uri("ftp://username:password@0.0.0.0/folder/.../filename.ext");

// Queue file
downloadManager.QueueFile (uri.ToString(),directory);

Where:

  • username - is your user name
  • password - is your password
  • 0.0.0.0 - is your ftp server's IP address
  • /folder/... - would be any folder structure leading down to the filename
  • filename.ext - would be the file that you want to download

Handling Download Events

DownloadManager provides several events to provide feedback during the dowloading of files such as FileDownloadProgressPercent, DownloadError and AllDownloadsCompleted. The following are examples of how to use DownloadManager events in your mobile application.

iOS Example:

// Wireup progress bar
_downloadManager.FileDownloadProgressPercent+= (percentage) => {
    // Update GUI
    InvokeOnMainThread(delegate {
        downloadProgress.Progress=percentage;
    });
};

// Wireup completion handler
_downloadManager.AllDownloadsCompleted+= delegate() {
    //Update GUI on main thread
    InvokeOnMainThread(delegate{
        //Display Alert Dialog Box
        using(var alert = new UIAlertView("DownloadManager", "All files have been downloaded", null, "OK", null))
        {
            alert.Show();   
        }
    });
};

// Wireup download error event
_downloadManager.DownloadError += (message) => {
    // Update GUI
    InvokeOnMainThread(delegate{
        // Display Alert Dialog Box
        using(var alert = new UIAlertView("Download Error", message, null, "OK", null))
        {
            alert.Show();   
        }
    });
};

Android Example:

private string dialogMessage = "";
...

// Wireup progress bar
downloadManager.FileDownloadProgressPercent += (percentage) => {

    // Adjust percentage and display
    RunOnUiThread (() => {
        percentage *= 100f;
        bar.Progress = (int)percentage;
    });
};

// Wireup completion handler
downloadManager.AllDownloadsCompleted += () => {

    // Run on UI Thread
    RunOnUiThread (() => {
        // Inform caller
        dialogMessage="All files have been downloaded";
        ShowDialog (DialogLongMessage);
    });

};

// Wireup error handler
downloadManager.DownloadError += (message) => {

    // Run on UI Thread
    RunOnUiThread (() => {
        // Inform caller
        dialogMessage=message;
        ShowDialog (DialogLongMessage);
    });
};

...
// Display dialog to the user as requested
protected override Dialog OnCreateDialog (int id)
{
    Dialog alert = null;

    base.OnCreateDialog (id);

    // Build requested dialog type
    switch (id){
    case DialogLongMessage:
        var builder = new AlertDialog.Builder(this);
        builder.SetIcon (Android.Resource.Attribute.AlertDialogIcon);
        builder.SetTitle ("DownloadManager");
        builder.SetMessage(dialogMessage);
        builder.SetPositiveButton ("OK", delegate(object sender, DialogClickEventArgs e) {
            // Ignore for now
        });
        alert=builder.Create ();
        break;
    }

    // Return dialog
    return alert;
}

Aborting on Errors

If the abortOnError property of the DownloadManager is true, the DownloadManager will stop executing a batch of files if any file generates an error during the download process. This is the default setting.

Starting the Download

After you have set the collection of files to download and wired up the user feedback, use the following code to start your downloads.

iOS Example:

//Start the download process
_downloadManager.StartDownloading();

Android Example:

// Start the download process
ThreadPool.QueueUserWorkItem((callback) =>{
    downloadManager.StartDownloading();
});

Examples

For full examples of using DownloadManager in your mobile application please see the APTest.iOS or APTest.Android example app included with this component. DownloadManager is used in the DownloadView_iPad.xib and DownloadView_iPhone.xib files.

The test apps not only includes this documentation but it also includes the full API documentation for the entire ActionPack component suite.

See the API documentation for DownloadManager for a complete list of features and their usage.

DownloadManager for iOS

You can find the full API documentation for DownloadManager iOS here:

http://appracatappra.com/api/ios/

 

DownloadManager for Android

You can find the full API documentation for DownloadManager Android here:

http://appracatappra.com/api/android/

At Appracatappra we take quality very seriously and always strive to provide the easiest to use and most trouble free Xamarin Studio Components available.

If you are experiencing an issue with one of our components or have a question about it's usage please either visit our support forum or fill out the request form below:

Your Name (required)

Your Email (required)

Subject

Component:

Device:

iPhone/iPod TouchiPadAndroid PhoneAndroid Tablet

OS Version:

Type:

Question/Issue

Verification:

captcha

Please enter the text above: