diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java index d7ab4201a5..fc1518e5c3 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java @@ -38,7 +38,7 @@ import java.util.List; *

Database access may take a long time, do not call methods of this class from * the application main thread. */ -public final class DefaultDownloadIndex implements DownloadIndex { +public final class DefaultDownloadIndex implements WritableDownloadIndex { private static final String TABLE_NAME = DatabaseProvider.TABLE_PREFIX + "Downloads"; @@ -185,12 +185,7 @@ public final class DefaultDownloadIndex implements DownloadIndex { return new DownloadCursorImpl(cursor); } - /** - * Adds or replaces a {@link Download}. - * - * @param download The {@link Download} to be added. - * @throws DatabaseIOException If an error occurs setting the state. - */ + @Override public void putDownload(Download download) throws DatabaseIOException { ensureInitialized(); ContentValues values = new ContentValues(); @@ -218,12 +213,7 @@ public final class DefaultDownloadIndex implements DownloadIndex { } } - /** - * Removes the {@link Download} with the given {@code id}. - * - * @param id ID of a {@link Download}. - * @throws DatabaseIOException If an error occurs removing the state. - */ + @Override public void removeDownload(String id) throws DatabaseIOException { ensureInitialized(); try { @@ -233,13 +223,7 @@ public final class DefaultDownloadIndex implements DownloadIndex { } } - /** - * Sets the manual stop reason of the downloads in a terminal state ({@link - * Download#STATE_COMPLETED}, {@link Download#STATE_FAILED}). - * - * @param manualStopReason The manual stop reason. - * @throws DatabaseIOException If an error occurs updating the state. - */ + @Override public void setManualStopReason(int manualStopReason) throws DatabaseIOException { ensureInitialized(); try { @@ -252,17 +236,7 @@ public final class DefaultDownloadIndex implements DownloadIndex { } } - /** - * Sets the manual stop reason of the download with the given {@code id} in a terminal state - * ({@link Download#STATE_COMPLETED}, {@link Download#STATE_FAILED}). - * - *

If there's no {@link Download} with the given {@code id} or it isn't in a terminal state, - * then nothing happens. - * - * @param id ID of a {@link Download}. - * @param manualStopReason The manual stop reason. - * @throws DatabaseIOException If an error occurs updating the state. - */ + @Override public void setManualStopReason(String id, int manualStopReason) throws DatabaseIOException { ensureInitialized(); try { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadIndex.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadIndex.java index 90d0fa1b51..3de1b7b212 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadIndex.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadIndex.java @@ -18,7 +18,7 @@ package com.google.android.exoplayer2.offline; import androidx.annotation.Nullable; import java.io.IOException; -/** Persists {@link Download}s. */ +/** An index of {@link Download Downloads}. */ public interface DownloadIndex { /** diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java index 03c33b6aad..fdb3ca1840 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java @@ -34,7 +34,6 @@ import android.os.Message; import androidx.annotation.IntDef; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; -import com.google.android.exoplayer2.database.DatabaseIOException; import com.google.android.exoplayer2.database.DatabaseProvider; import com.google.android.exoplayer2.scheduler.Requirements; import com.google.android.exoplayer2.scheduler.RequirementsWatcher; @@ -155,7 +154,7 @@ public final class DownloadManager { private final int maxSimultaneousDownloads; private final int minRetryCount; private final Context context; - private final DefaultDownloadIndex downloadIndex; + private final WritableDownloadIndex downloadIndex; private final DownloaderFactory downloaderFactory; private final Handler mainHandler; private final HandlerThread internalThread; @@ -231,7 +230,7 @@ public final class DownloadManager { * Constructs a {@link DownloadManager}. * * @param context Any context. - * @param downloadIndex The {@link DefaultDownloadIndex} that holds the downloads. + * @param downloadIndex The download index used to hold the download information. * @param downloaderFactory A factory for creating {@link Downloader}s. * @param maxSimultaneousDownloads The maximum number of simultaneous downloads. * @param minRetryCount The minimum number of times a download must be retried before failing. @@ -239,7 +238,7 @@ public final class DownloadManager { */ public DownloadManager( Context context, - DefaultDownloadIndex downloadIndex, + WritableDownloadIndex downloadIndex, DownloaderFactory downloaderFactory, int maxSimultaneousDownloads, int minRetryCount, @@ -651,7 +650,7 @@ public final class DownloadManager { } else { downloadIndex.setManualStopReason(manualStopReason); } - } catch (DatabaseIOException e) { + } catch (IOException e) { Log.e(TAG, "setManualStopReason failed", e); } } @@ -734,7 +733,7 @@ public final class DownloadManager { logd("Download state is changed", downloadInternal); try { downloadIndex.putDownload(download); - } catch (DatabaseIOException e) { + } catch (IOException e) { Log.e(TAG, "Failed to update index", e); } if (downloadInternal.state == STATE_COMPLETED || downloadInternal.state == STATE_FAILED) { @@ -747,7 +746,7 @@ public final class DownloadManager { logd("Download is removed", downloadInternal); try { downloadIndex.removeDownload(download.request.id); - } catch (DatabaseIOException e) { + } catch (IOException e) { Log.e(TAG, "Failed to remove from index", e); } downloadInternals.remove(downloadInternal); @@ -805,7 +804,7 @@ public final class DownloadManager { private Download loadDownload(String id) { try { return downloadIndex.getDownload(id); - } catch (DatabaseIOException e) { + } catch (IOException e) { Log.e(TAG, "loadDownload failed", e); } return null; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/WritableDownloadIndex.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/WritableDownloadIndex.java new file mode 100644 index 0000000000..24f4421bc4 --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/WritableDownloadIndex.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.offline; + +import java.io.IOException; + +/** An writable index of {@link Download Downloads}. */ +public interface WritableDownloadIndex extends DownloadIndex { + + /** + * Adds or replaces a {@link Download}. + * + * @param download The {@link Download} to be added. + * @throws throws IOException If an error occurs setting the state. + */ + void putDownload(Download download) throws IOException; + + /** + * Removes the {@link Download} with the given {@code id}. + * + * @param id ID of a {@link Download}. + * @throws throws IOException If an error occurs removing the state. + */ + void removeDownload(String id) throws IOException; + /** + * Sets the manual stop reason of the downloads in a terminal state ({@link + * Download#STATE_COMPLETED}, {@link Download#STATE_FAILED}). + * + * @param manualStopReason The manual stop reason. + * @throws throws IOException If an error occurs updating the state. + */ + void setManualStopReason(int manualStopReason) throws IOException; + + /** + * Sets the manual stop reason of the download with the given {@code id} in a terminal state + * ({@link Download#STATE_COMPLETED}, {@link Download#STATE_FAILED}). + * + *

If there's no {@link Download} with the given {@code id} or it isn't in a terminal state, + * then nothing happens. + * + * @param id ID of a {@link Download}. + * @param manualStopReason The manual stop reason. + * @throws throws IOException If an error occurs updating the state. + */ + void setManualStopReason(String id, int manualStopReason) throws IOException; +}