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 // Internal methods
private void loadDownloads() { private void loadDownloads() {
try { try (DownloadStateCursor loadedDownloadStates = downloadIndex.getDownloadStates()) {
DownloadStateCursor loadedDownloadStates = downloadIndex.getDownloadStates();
while (loadedDownloadStates.moveToNext()) { while (loadedDownloadStates.moveToNext()) {
DownloadState downloadState = loadedDownloadStates.getDownloadState(); DownloadState downloadState = loadedDownloadStates.getDownloadState();
downloadStates.put(downloadState.uri, downloadState); downloadStates.put(downloadState.uri, downloadState);
} }
loadedDownloadStates.close();
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, "Failed to query download states", e); Log.w(TAG, "Failed to query download states", e);
} }

View File

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

View File

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