Bug 715347 - Add basic Download Manager support [r=mbrubeck]

This commit is contained in:
Mark Finkle 2012-01-06 09:34:34 -05:00
parent 7c3af0bd18
commit 000092c9ba
5 changed files with 47 additions and 3 deletions

View File

@ -437,6 +437,7 @@ abstract public class GeckoApp
MenuItem share = aMenu.findItem(R.id.share);
MenuItem agentMode = aMenu.findItem(R.id.agent_mode);
MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf);
MenuItem downloads = aMenu.findItem(R.id.downloads);
if (tab == null) {
bookmark.setEnabled(false);
@ -473,6 +474,10 @@ abstract public class GeckoApp
saveAsPDF.setEnabled(!(tab.getURL().equals("about:home") ||
tab.getContentType().equals("application/vnd.mozilla.xul+xml")));
// DownloadManager support is tied to level 12 and higher
if (Build.VERSION.SDK_INT < 12)
downloads.setVisible(false);
return true;
}
@ -533,6 +538,10 @@ abstract public class GeckoApp
case R.id.addons:
loadUrlInTab("about:addons");
return true;
case R.id.downloads:
intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(intent);
return true;
case R.id.agent_mode:
Tab selectedTab = Tabs.getInstance().getSelectedTab();
if (selectedTab == null)
@ -975,6 +984,13 @@ abstract public class GeckoApp
String host = message.getString("host");
JSONArray permissions = message.getJSONArray("permissions");
showSiteSettingsDialog(host, permissions);
} else if (event.equals("Downloads:Done")) {
String displayName = message.getString("displayName");
String path = message.getString("path");
String mimeType = message.getString("mimeType");
int size = message.getInt("size");
handleDownloadDone(displayName, path, mimeType, size);
}
} catch (Exception e) {
Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
@ -1302,6 +1318,17 @@ abstract public class GeckoApp
tabs.closeTab(tab);
}
void handleDownloadDone(String displayName, String path, String mimeType, int size) {
// DownloadManager.addCompletedDownload is supported in level 12 and higher
if (Build.VERSION.SDK_INT >= 12) {
DownloadManager dm = (DownloadManager) mAppContext.getSystemService(Context.DOWNLOAD_SERVICE);
dm.addCompletedDownload(displayName, displayName,
false /* do not use media scanner */,
mimeType, path, size,
false /* no notification */);
}
}
void addPluginView(final View view,
final double x, final double y,
final double w, final double h) {
@ -1559,6 +1586,7 @@ abstract public class GeckoApp
GeckoAppShell.registerGeckoEventListener("AgentMode:Changed", GeckoApp.mAppContext);
GeckoAppShell.registerGeckoEventListener("FormAssist:AutoComplete", GeckoApp.mAppContext);
GeckoAppShell.registerGeckoEventListener("Permissions:Data", GeckoApp.mAppContext);
GeckoAppShell.registerGeckoEventListener("Downloads:Done", GeckoApp.mAppContext);
mConnectivityFilter = new IntentFilter();
mConnectivityFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
@ -1794,6 +1822,7 @@ abstract public class GeckoApp
GeckoAppShell.unregisterGeckoEventListener("AgentMode:Changed", GeckoApp.mAppContext);
GeckoAppShell.unregisterGeckoEventListener("FormAssist:AutoComplete", GeckoApp.mAppContext);
GeckoAppShell.unregisterGeckoEventListener("Permissions:Data", GeckoApp.mAppContext);
GeckoAppShell.unregisterGeckoEventListener("Downloads:Done", GeckoApp.mAppContext);
mFavicons.close();

View File

@ -72,6 +72,7 @@
<!ENTITY quit "Quit">
<!ENTITY addons "Add-ons">
<!ENTITY downloads "Downloads">
<!ENTITY share "Share">
<!ENTITY save_as_pdf "Save as PDF">

View File

@ -33,6 +33,9 @@
<item android:id="@+id/addons"
android:title="@string/addons"/>
<item android:id="@+id/downloads"
android:title="@string/downloads"/>
<item android:id="@+id/quit"
android:title="@string/quit" />
</menu>

View File

@ -80,6 +80,7 @@
<string name="forward">&forward;</string>
<string name="new_tab">&new_tab;</string>
<string name="addons">&addons;</string>
<string name="downloads">&downloads;</string>
<string name="site_settings_title">&site_settings_title;</string>
<string name="site_settings_cancel">&site_settings_cancel;</string>

View File

@ -125,6 +125,7 @@ var Downloads = {
},
observe: function dl_observe(aSubject, aTopic, aData) {
let download = aSubject.QueryInterface(Ci.nsIDownload);
let msgKey = "";
if (aTopic == "dl-start") {
msgKey = "alertDownloadsStart";
@ -138,12 +139,21 @@ var Downloads = {
NativeWindow.toast.show(Strings.browser.GetStringFromName("alertDownloadsToast"), "long");
} else if (aTopic == "dl-done") {
msgKey = "alertDownloadsDone";
let message = {
gecko: {
type: "Downloads:Done",
displayName: download.displayName,
path: download.targetFile.path,
size: download.size,
mimeType: download.MIMEInfo ? download.MIMEInfo.type : ""
}
};
sendMessageToJava(message);
}
if (msgKey) {
let download = aSubject.QueryInterface(Ci.nsIDownload);
if (msgKey)
this.showAlert(download, Strings.browser.formatStringFromName(msgKey, [download.displayName], 1));
}
},
QueryInterface: function (aIID) {