Use Closeable interface for DownloadStateCursor to simplify closing.

PiperOrigin-RevId: 240775823
This commit is contained in:
tonihei 2019-03-28 15:12:58 +00:00 committed by Toni
parent e8cf67c0cd
commit 28ea31cd4c
3 changed files with 14 additions and 20 deletions

View File

@ -140,13 +140,11 @@ public class DownloadTracker implements DownloadManager.Listener {
// Internal methods
private void loadDownloads() {
try {
DownloadStateCursor loadedDownloadStates = downloadIndex.getDownloadStates();
try (DownloadStateCursor loadedDownloadStates = downloadIndex.getDownloadStates()) {
while (loadedDownloadStates.moveToNext()) {
DownloadState downloadState = loadedDownloadStates.getDownloadState();
downloadStates.put(downloadState.uri, downloadState);
}
loadedDownloadStates.close();
} catch (IOException e) {
Log.w(TAG, "Failed to query download states", e);
}

View File

@ -601,15 +601,13 @@ public final class DownloadManager {
fileIOHandler.post(
() -> {
DownloadState[] loadedStates;
DownloadStateCursor cursor = null;
try {
cursor =
downloadIndex.getDownloadStates(
STATE_QUEUED,
STATE_STOPPED,
STATE_DOWNLOADING,
STATE_REMOVING,
STATE_RESTARTING);
try (DownloadStateCursor cursor =
downloadIndex.getDownloadStates(
STATE_QUEUED,
STATE_STOPPED,
STATE_DOWNLOADING,
STATE_REMOVING,
STATE_RESTARTING)) {
loadedStates = new DownloadState[cursor.getCount()];
for (int i = 0, length = loadedStates.length; i < length; i++) {
cursor.moveToNext();
@ -619,10 +617,6 @@ public final class DownloadManager {
} catch (Throwable e) {
Log.e(TAG, "Download state loading failed.", e);
loadedStates = new DownloadState[0];
} finally {
if (cursor != null) {
cursor.close();
}
}
final DownloadState[] states = loadedStates;
handler.post(

View File

@ -15,8 +15,10 @@
*/
package com.google.android.exoplayer2.offline;
import java.io.Closeable;
/** Provides random read-write access to the result set returned by a database query. */
public interface DownloadStateCursor {
public interface DownloadStateCursor extends Closeable {
/** Returns the DownloadState at the current position. */
DownloadState getDownloadState();
@ -119,9 +121,9 @@ public interface DownloadStateCursor {
return getPosition() == getCount();
}
/** Closes the Cursor, releasing all of its resources and making it completely invalid. */
void close();
/** Returns whether the cursor is closed */
boolean isClosed();
@Override
void close();
}