Skip to content

Instantly share code, notes, and snippets.

@binho
Last active August 29, 2015 14:02
Show Gist options
  • Save binho/b8037afa7c4ee30fad34 to your computer and use it in GitHub Desktop.
Save binho/b8037afa7c4ee30fad34 to your computer and use it in GitHub Desktop.
Titanium SDK - Android - Show activity indicator android download files
if (Ti.Platform.osname == 'android') {
var filesToDownload = [
{url: "http://site.com/image1.jpg"},
{url: "http://site.com/image2.jpg"},
{url: "http://site.com/image3.jpg"}
];
var totalFiles = filesToDownload.length;
var activityWindow = Ti.UI.createWindow({
backgroundColor: 'white',
fullscreen: true
});
var activityIndicator = Ti.UI.createActivityIndicator({
color: '#555',
font: {fontFamily : 'Helvetica Neue', fontSize : 20},
message: ' Downloading files...',
style: Ti.UI.ActivityIndicatorStyle.DARK,
height: Ti.UI.FILL,
width: Ti.UI.FILL
});
activityWindow.add(activityIndicator);
setTimeout(function() {
activityIndicator.show();
var totalDownloaded = 0;
filesToDownload.forEach(function(fileToDownload, index) {
// Get file name from URL
var fileName = fileToDownload.url.substr(fileToDownload.url.lastIndexOf('/') + 1);
// Start download of file
util.downloadFile(fileName, fileToDownload.url, function() {
totalDownloaded++;
if (totalDownloaded == totalFiles) {
activityIndicator.hide();
activityWindow.close();
}
}, function(progress) {
Ti.API.info("Progress: " + Math.round(progress * 100) + "%");
});
});
}, 1000);
}
win.open();
activityWindow.open();
var util = {};
util.getOrientation = function(o, previousOrientation) {
switch (o) {
case Titanium.UI.PORTRAIT: return 'portrait';
case Titanium.UI.UPSIDE_PORTRAIT: return 'portrait';
case Titanium.UI.LANDSCAPE_LEFT: return 'landscape';
case Titanium.UI.LANDSCAPE_RIGHT: return 'landscape';
default: return (previousOrientation == 'portrait') ? 'portrait' : 'landscape';
}
};
util.downloadFile = function(filename, url, downloadCompleted, downloadProgress) {
var fileData = {file:filename, url:url, path:null};
var directory = Titanium.Filesystem.applicationDataDirectory;
var file = Titanium.Filesystem.getFile(directory, filename);
if (!file.exists()) {
if (Titanium.Network.online) {
var client = Titanium.Network.createHTTPClient();
client.setTimeout(10000);
client.onload = function() {
if (client.status == 200) {
var f = Titanium.Filesystem.getFile(directory, filename);
f.write(this.responseData);
var fcontent = f.read();
fileData.path = directory + Titanium.Filesystem.separator;
} else {
fileData.error = 'File not found at ' + url;
}
downloadCompleted();
};
client.ondatastream = function(e) {
if (downloadProgress) {
downloadProgress(e.progress);
}
};
client.error = function(e) {
fileData.error = e.error;
downloadCompleted();
};
client.open('GET', url, false);
client.send();
} else {
fileData.error = 'No internet available';
downloadCompleted();
}
} else {
downloadCompleted();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment