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

Commit e13f749

Browse files
committed
Null check cursors and DownloadManager instance
1 parent f9edcd6 commit e13f749

1 file changed

Lines changed: 41 additions & 22 deletions

File tree

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

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public void init() {
5757
mContext.registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
5858
List<Download> downloads = getDownloads();
5959
downloads.forEach(download -> {
60-
if (!new File(UrlUtils.stripProtocol(download.getOutputFile())).exists()) {
60+
if (mDownloadManager != null &&
61+
!new File(UrlUtils.stripProtocol(download.getOutputFile())).exists()) {
6162
mDownloadManager.remove(download.getId());
6263
}
6364
});
@@ -121,8 +122,10 @@ public void startDownload(@NonNull DownloadJob job) {
121122
request.setDestinationUri(Uri.parse(outputPath));
122123
}
123124

124-
mDownloadManager.enqueue(request);
125-
scheduleUpdates();
125+
if (mDownloadManager != null) {
126+
mDownloadManager.enqueue(request);
127+
scheduleUpdates();
128+
}
126129
}
127130

128131
@Nullable
@@ -144,15 +147,21 @@ public void removeDownload(long downloadId, boolean deleteFiles) {
144147
if (file.exists()) {
145148
File newFile = new File(UrlUtils.stripProtocol(download.getOutputFile().concat(".bak")));
146149
file.renameTo(newFile);
147-
mDownloadManager.remove(downloadId);
150+
if (mDownloadManager != null) {
151+
mDownloadManager.remove(downloadId);
152+
}
148153
newFile.renameTo(file);
149154

150155
} else {
151-
mDownloadManager.remove(downloadId);
156+
if (mDownloadManager != null) {
157+
mDownloadManager.remove(downloadId);
158+
}
152159
}
153160

154161
} else {
155-
mDownloadManager.remove(downloadId);
162+
if (mDownloadManager != null) {
163+
mDownloadManager.remove(downloadId);
164+
}
156165
}
157166
}
158167
notifyDownloadsUpdate();
@@ -166,26 +175,34 @@ public void removeAllDownloads(boolean deleteFiles) {
166175
public Download getDownload(long downloadId) {
167176
Download download = null;
168177

169-
DownloadManager.Query query = new DownloadManager.Query();
170-
query.setFilterById(downloadId);
171-
Cursor c = mDownloadManager.query(query);
172-
if (c.moveToFirst()) {
173-
download = Download.from(c);
178+
if (mDownloadManager != null) {
179+
DownloadManager.Query query = new DownloadManager.Query();
180+
query.setFilterById(downloadId);
181+
Cursor c = mDownloadManager.query(query);
182+
if (c != null) {
183+
if (c.moveToFirst()) {
184+
download = Download.from(c);
185+
}
186+
c.close();
187+
}
174188
}
175-
c.close();
176189

177190
return download;
178191
}
179192

180193
public List<Download> getDownloads() {
181194
List<Download> downloads = new ArrayList<>();
182195

183-
DownloadManager.Query query = new DownloadManager.Query();
184-
Cursor c = mDownloadManager.query(query);
185-
while (c.moveToNext()) {
186-
downloads.add(Download.from(c));
196+
if (mDownloadManager != null) {
197+
DownloadManager.Query query = new DownloadManager.Query();
198+
Cursor c = mDownloadManager.query(query);
199+
if (c != null) {
200+
while (c.moveToNext()) {
201+
downloads.add(Download.from(c));
202+
}
203+
c.close();
204+
}
187205
}
188-
c.close();
189206

190207
return downloads;
191208
}
@@ -196,15 +213,17 @@ public void onReceive(Context context, Intent intent) {
196213
String action = intent.getAction();
197214
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
198215

199-
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
216+
if (mDownloadManager != null && DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
200217
DownloadManager.Query query = new DownloadManager.Query();
201218
query.setFilterById(downloadId);
202219
Cursor c = mDownloadManager.query(query);
203-
if (c.moveToFirst()) {
204-
notifyDownloadsUpdate();
205-
notifyDownloadCompleted(Download.from(c));
220+
if (c != null) {
221+
if (c.moveToFirst()) {
222+
notifyDownloadsUpdate();
223+
notifyDownloadCompleted(Download.from(c));
224+
}
225+
c.close();
206226
}
207-
c.close();
208227
}
209228
}
210229
};

0 commit comments

Comments
 (0)