Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 0192ce9

Browse files
keianhzobluemarvin
andauthored
Download improvements (#3197)
* Allow DownloadManager to handle HTTP downloads in Android > 8 * Show download status Co-authored-by: Randall E. Barker <[email protected]>
1 parent 7d1c67b commit 0192ce9

File tree

4 files changed

+62
-24
lines changed

4 files changed

+62
-24
lines changed

app/src/common/shared/org/mozilla/vrbrowser/downloads/Download.java

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import androidx.annotation.IntDef;
88
import androidx.annotation.NonNull;
99

10+
import org.mozilla.vrbrowser.R;
1011
import org.mozilla.vrbrowser.ui.adapters.Language;
1112
import org.mozilla.vrbrowser.utils.LocaleUtils;
1213

@@ -37,6 +38,7 @@ public class Download {
3738
private String mDescription;
3839
private @Status int mStatus;
3940
private long mLastModified;
41+
private String mReason;
4042

4143
public static Download from(Cursor cursor) {
4244
Download download = new Download();
@@ -69,6 +71,7 @@ public static Download from(Cursor cursor) {
6971
download.mSizeBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
7072
download.mDownloadedBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
7173
download.mLastModified = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
74+
download.mReason = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
7275
return download;
7376
}
7477

@@ -140,7 +143,7 @@ public double getProgress() {
140143

141144
public String getFilename() {
142145
try {
143-
File f = new File(new URL(mOutputFile).getPath());
146+
File f = new File(new URL(mUri).getPath());
144147
return f.getName();
145148

146149
} catch (Exception e) {
@@ -153,30 +156,51 @@ public String getFilename() {
153156
}
154157
}
155158

159+
public String getReason() {
160+
return mReason;
161+
}
162+
156163
@NonNull
157164
public static String progressString(@NonNull Context context, @NonNull Download download) {
165+
158166
Language language = LocaleUtils.getDisplayLanguage(context);
159-
if (download.mStatus == RUNNING) {
160-
if (download.mSizeBytes < MEGABYTE) {
161-
return String.format(language.getLocale(), "%.2f/%.2fKb (%d%%)",
162-
((double)download.mDownloadedBytes / (double)KILOBYTE),
163-
((double)download.mSizeBytes / (double)KILOBYTE),
164-
(download.mDownloadedBytes*100)/download.mSizeBytes);
167+
switch (download.mStatus) {
168+
case Download.RUNNING:
169+
if (download.mSizeBytes < MEGABYTE) {
170+
return String.format(language.getLocale(), "%.2f/%.2fKb (%d%%)",
171+
((double)download.mDownloadedBytes / (double)KILOBYTE),
172+
((double)download.mSizeBytes / (double)KILOBYTE),
173+
(download.mDownloadedBytes*100)/download.mSizeBytes);
165174

166-
} else {
167-
return String.format(language.getLocale(), "%.2f/%.2fMB (%d%%)",
168-
((double)download.mDownloadedBytes / (double)MEGABYTE),
169-
((double)download.mSizeBytes / (double)MEGABYTE),
170-
(download.mDownloadedBytes*100)/download.mSizeBytes);
171-
}
175+
} else {
176+
return String.format(language.getLocale(), "%.2f/%.2fMB (%d%%)",
177+
((double)download.mDownloadedBytes / (double)MEGABYTE),
178+
((double)download.mSizeBytes / (double)MEGABYTE),
179+
(download.mDownloadedBytes*100)/download.mSizeBytes);
180+
}
172181

173-
} else {
174-
if (download.mSizeBytes < MEGABYTE) {
175-
return String.format(language.getLocale(), "%.2fKb", ((double)download.mSizeBytes / (double)KILOBYTE));
182+
case Download.SUCCESSFUL:
183+
if (download.mSizeBytes < MEGABYTE) {
184+
return String.format(language.getLocale(), "%.2fKb", ((double)download.mSizeBytes / (double)KILOBYTE));
176185

177-
} else {
178-
return String.format(language.getLocale(), "%.2fMB", ((double)download.mSizeBytes / (double)MEGABYTE));
179-
}
186+
} else {
187+
return String.format(language.getLocale(), "%.2fMB", ((double)download.mSizeBytes / (double)MEGABYTE));
188+
}
189+
190+
case Download.FAILED:
191+
return context.getString(R.string.download_status_failed);
192+
193+
case Download.PAUSED:
194+
return context.getString(R.string.download_status_paused);
195+
196+
case Download.PENDING:
197+
return context.getString(R.string.download_status_pending);
198+
199+
case Download.UNAVAILABLE:
200+
return context.getString(R.string.download_status_unavailable);
201+
202+
default:
203+
return context.getString(R.string.download_status_unknown_error);
180204
}
181205
}
182206
}

app/src/common/shared/org/mozilla/vrbrowser/downloads/DownloadsManager.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ public void run() {
217217
Cursor c = mDownloadManager.query(query);
218218

219219
while (c.moveToNext()) {
220-
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
221-
if (status == DownloadManager.STATUS_RUNNING) {
222-
mMainHandler.post(() -> notifyDownloadsUpdate());
223-
}
220+
mMainHandler.post(() -> notifyDownloadsUpdate());
224221
}
225222
c.close();
226223
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<application
1818
android:name=".VRBrowserApplication"
1919
android:allowBackup="true"
20+
android:usesCleartextTraffic="true"
2021
android:icon="@mipmap/ic_launcher"
2122
android:label="@string/app_name"
2223
android:roundIcon="@mipmap/ic_launcher_round"

app/src/main/res/values/strings.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,5 +1698,21 @@ the Select` button. When clicked it closes all the previously selected tabs -->
16981698
<string name="download_error_body">An error occurred while downloading %1$s. Please try again.</string>
16991699

17001700
<!-- This string is displayed in the button to dismiss the download error dialog. -->
1701-
<string name="download_error_ok">OK</string>
1701+
<string name="download_error_ok">Ok</string>
1702+
1703+
<!-- This string is displayed in the downloads panel, in the download status when the download has failed. -->
1704+
<string name="download_status_failed">Failed</string>
1705+
1706+
<!-- This string is displayed in the downloads panel, in the download status when the download is paused. -->
1707+
<string name="download_status_paused">Paused</string>
1708+
1709+
<!-- This string is displayed in the downloads panel, in the download status when the download is pending. -->
1710+
<string name="download_status_pending">Pending</string>
1711+
1712+
<!-- This string is displayed in the downloads panel, in the download status when the resource is no longer available. -->
1713+
<string name="download_status_unavailable">Unavailable</string>
1714+
1715+
<!-- This string is displayed in the downloads panel, in the download status when an unknown error happened. -->
1716+
<string name="download_status_unknown_error">Unknown error</string>
1717+
17021718
</resources>

0 commit comments

Comments
 (0)