Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download improvements #3197

Merged
merged 3 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 43 additions & 19 deletions app/src/common/shared/org/mozilla/vrbrowser/downloads/Download.java
Original file line number Diff line number Diff line change
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);
}
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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>