Bug 1253684 - Introduce preference for forwarding OMA downloads to Android's download manager. r=margaret, a=lizzard

MozReview-Commit-ID: u9j3CGex2q
This commit is contained in:
Sebastian Kaspari 2016-03-10 10:51:25 +01:00
parent 35656eeb85
commit 0eda0ce3a3
3 changed files with 89 additions and 0 deletions

View File

@ -6,6 +6,8 @@
package org.mozilla.gecko;
import android.Manifest;
import android.app.DownloadManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import org.json.JSONArray;
import org.mozilla.gecko.adjust.AdjustHelperInterface;
@ -682,6 +684,7 @@ public class BrowserApp extends GeckoApp
EventDispatcher.getInstance().registerGeckoThreadListener((NativeEventListener)this,
"CharEncoding:Data",
"CharEncoding:State",
"Download:AndroidDownloadManager",
"Experiments:GetActive",
"Favicon:CacheLoad",
"Feedback:LastUrl",
@ -1429,6 +1432,7 @@ public class BrowserApp extends GeckoApp
EventDispatcher.getInstance().unregisterGeckoThreadListener((NativeEventListener) this,
"CharEncoding:Data",
"CharEncoding:State",
"Download:AndroidDownloadManager",
"Experiments:GetActive",
"Favicon:CacheLoad",
"Feedback:LastUrl",
@ -1797,6 +1801,38 @@ public class BrowserApp extends GeckoApp
}
} else if ("Updater:Launch".equals(event)) {
handleUpdaterLaunch();
} else if ("Download:AndroidDownloadManager".equals(event)) {
// Downloading via Android's download manager
final String uri = message.getString("uri");
final String filename = message.getString("filename");
final String mimeType = message.getString("mimeType");
final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uri));
request.setMimeType(mimeType);
try {
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, filename);
} catch (IllegalStateException e) {
Log.e(LOGTAG, "Cannot create download directory");
return;
}
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.addRequestHeader("User-Agent", HardwareUtils.isTablet() ?
AppConstants.USER_AGENT_FENNEC_TABLET :
AppConstants.USER_AGENT_FENNEC_MOBILE);
try {
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Log.d(LOGTAG, "Enqueued download (Download Manager)");
} catch (RuntimeException e) {
Log.e(LOGTAG, "Download failed: " + e);
}
} else {
super.handleMessage(event, message, callback);
}

View File

@ -8,7 +8,12 @@
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
const APK_MIME_TYPE = "application/vnd.android.package-archive";
const OMA_DOWNLOAD_DESCRIPTOR_MIME_TYPE = "application/vnd.oma.dd+xml";
const OMA_DRM_MESSAGE_MIME = "application/vnd.oma.drm.message";
const OMA_DRM_CONTENT_MIME = "application/vnd.oma.drm.content";
const OMA_DRM_RIGHTS_MIME = "application/vnd.oma.drm.rights+wbxml";
const PREF_BD_USEDOWNLOADDIR = "browser.download.useDownloadDir";
const URI_GENERIC_ICON_DOWNLOAD = "drawable://alert_download";
@ -20,6 +25,7 @@ Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "RuntimePermissions", "resource://gre/modules/RuntimePermissions.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Messaging", "resource://gre/modules/Messaging.jsm");
// -----------------------------------------------------------------------
// HelperApp Launcher Dialog
@ -102,12 +108,41 @@ HelperAppLauncherDialog.prototype = {
return mimeType != OMA_DOWNLOAD_DESCRIPTOR_MIME_TYPE;
},
/**
* Returns true if `launcher`represents a download that should not be handled by Firefox
* or a third-party app and instead be forwarded to Android's download manager.
*/
_shouldForwardToAndroidDownloadManager: function(aLauncher) {
let forwardDownload = Services.prefs.getBoolPref('browser.download.forward_oma_android_download_manager');
if (!forwardDownload) {
return false;
}
let mimeType = aLauncher.MIMEInfo.MIMEType;
if (!mimeType) {
mimeType = ContentAreaUtils.getMIMETypeForURI(aLauncher.source) || "";
}
return [
OMA_DOWNLOAD_DESCRIPTOR_MIME_TYPE,
OMA_DRM_MESSAGE_MIME,
OMA_DRM_CONTENT_MIME,
OMA_DRM_RIGHTS_MIME
].indexOf(mimeType) != -1;
},
show: function hald_show(aLauncher, aContext, aReason) {
if (!this._canDownload(aLauncher.source)) {
this._refuseDownload(aLauncher);
return;
}
if (this._shouldForwardToAndroidDownloadManager(aLauncher)) {
this._downloadWithAndroidDownloadManager(aLauncher);
aLauncher.cancel(Cr.NS_BINDING_ABORTED);
return;
}
let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
let defaultHandler = new Object();
@ -203,6 +238,20 @@ HelperAppLauncherDialog.prototype = {
win.toast.show(failedText, "long");
},
_downloadWithAndroidDownloadManager(aLauncher) {
let mimeType = aLauncher.MIMEInfo.MIMEType;
if (!mimeType) {
mimeType = ContentAreaUtils.getMIMETypeForURI(aLauncher.source) || "";
}
Messaging.sendRequest({
'type': 'Download:AndroidDownloadManager',
'uri': aLauncher.source.spec,
'mimeType': mimeType,
'filename': aLauncher.suggestedFileName
});
},
_getPrefName: function getPrefName(mimetype) {
return "browser.download.preferred." + mimetype.replace("\\", ".");
},

View File

@ -3760,6 +3760,10 @@ pref("ui.panel.default_level_parent", true);
pref("mousewheel.system_scroll_override_on_root_content.enabled", false);
// Forward downloads with known OMA MIME types to Android's download manager
// instead of downloading them in the browser.
pref("browser.download.forward_oma_android_download_manager", false);
# ANDROID
#endif