Skip to content

Commit

Permalink
Download improvements (#3197)
Browse files Browse the repository at this point in the history
* Allow DownloadManager to handle HTTP downloads in Android > 8

* Show download status

Co-authored-by: Randall E. Barker <simstorm@mac.com>
  • Loading branch information
keianhzo and bluemarvin committed Apr 20, 2020
1 parent 7d1c67b commit 0192ce9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
62 changes: 43 additions & 19 deletions app/src/common/shared/org/mozilla/vrbrowser/downloads/Download.java
Expand Up @@ -7,6 +7,7 @@
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.ui.adapters.Language;
import org.mozilla.vrbrowser.utils.LocaleUtils;

Expand Down Expand Up @@ -37,6 +38,7 @@ public class Download {
private String mDescription;
private @Status int mStatus;
private long mLastModified;
private String mReason;

public static Download from(Cursor cursor) {
Download download = new Download();
Expand Down Expand Up @@ -69,6 +71,7 @@ public static Download from(Cursor cursor) {
download.mSizeBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
download.mDownloadedBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
download.mLastModified = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
download.mReason = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
return download;
}

Expand Down Expand Up @@ -140,7 +143,7 @@ public double getProgress() {

public String getFilename() {
try {
File f = new File(new URL(mOutputFile).getPath());
File f = new File(new URL(mUri).getPath());
return f.getName();

} catch (Exception e) {
Expand All @@ -153,30 +156,51 @@ public String getFilename() {
}
}

public String getReason() {
return mReason;
}

@NonNull
public static String progressString(@NonNull Context context, @NonNull Download download) {

Language language = LocaleUtils.getDisplayLanguage(context);
if (download.mStatus == RUNNING) {
if (download.mSizeBytes < MEGABYTE) {
return String.format(language.getLocale(), "%.2f/%.2fKb (%d%%)",
((double)download.mDownloadedBytes / (double)KILOBYTE),
((double)download.mSizeBytes / (double)KILOBYTE),
(download.mDownloadedBytes*100)/download.mSizeBytes);
switch (download.mStatus) {
case Download.RUNNING:
if (download.mSizeBytes < MEGABYTE) {
return String.format(language.getLocale(), "%.2f/%.2fKb (%d%%)",
((double)download.mDownloadedBytes / (double)KILOBYTE),
((double)download.mSizeBytes / (double)KILOBYTE),
(download.mDownloadedBytes*100)/download.mSizeBytes);

} else {
return String.format(language.getLocale(), "%.2f/%.2fMB (%d%%)",
((double)download.mDownloadedBytes / (double)MEGABYTE),
((double)download.mSizeBytes / (double)MEGABYTE),
(download.mDownloadedBytes*100)/download.mSizeBytes);
}
} else {
return String.format(language.getLocale(), "%.2f/%.2fMB (%d%%)",
((double)download.mDownloadedBytes / (double)MEGABYTE),
((double)download.mSizeBytes / (double)MEGABYTE),
(download.mDownloadedBytes*100)/download.mSizeBytes);
}

} else {
if (download.mSizeBytes < MEGABYTE) {
return String.format(language.getLocale(), "%.2fKb", ((double)download.mSizeBytes / (double)KILOBYTE));
case Download.SUCCESSFUL:
if (download.mSizeBytes < MEGABYTE) {
return String.format(language.getLocale(), "%.2fKb", ((double)download.mSizeBytes / (double)KILOBYTE));

} else {
return String.format(language.getLocale(), "%.2fMB", ((double)download.mSizeBytes / (double)MEGABYTE));
}
} else {
return String.format(language.getLocale(), "%.2fMB", ((double)download.mSizeBytes / (double)MEGABYTE));
}

case Download.FAILED:
return context.getString(R.string.download_status_failed);

case Download.PAUSED:
return context.getString(R.string.download_status_paused);

case Download.PENDING:
return context.getString(R.string.download_status_pending);

case Download.UNAVAILABLE:
return context.getString(R.string.download_status_unavailable);

default:
return context.getString(R.string.download_status_unknown_error);
}
}
}
Expand Up @@ -217,10 +217,7 @@ public void run() {
Cursor c = mDownloadManager.query(query);

while (c.moveToNext()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_RUNNING) {
mMainHandler.post(() -> notifyDownloadsUpdate());
}
mMainHandler.post(() -> notifyDownloadsUpdate());
}
c.close();
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -17,6 +17,7 @@
<application
android:name=".VRBrowserApplication"
android:allowBackup="true"
android:usesCleartextTraffic="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/res/values/strings.xml
Expand Up @@ -1698,5 +1698,21 @@ the Select` button. When clicked it closes all the previously selected tabs -->
<string name="download_error_body">An error occurred while downloading %1$s. Please try again.</string>

<!-- This string is displayed in the button to dismiss the download error dialog. -->
<string name="download_error_ok">OK</string>
<string name="download_error_ok">Ok</string>

<!-- This string is displayed in the downloads panel, in the download status when the download has failed. -->
<string name="download_status_failed">Failed</string>

<!-- This string is displayed in the downloads panel, in the download status when the download is paused. -->
<string name="download_status_paused">Paused</string>

<!-- This string is displayed in the downloads panel, in the download status when the download is pending. -->
<string name="download_status_pending">Pending</string>

<!-- This string is displayed in the downloads panel, in the download status when the resource is no longer available. -->
<string name="download_status_unavailable">Unavailable</string>

<!-- This string is displayed in the downloads panel, in the download status when an unknown error happened. -->
<string name="download_status_unknown_error">Unknown error</string>

</resources>

0 comments on commit 0192ce9

Please sign in to comment.