Add WritableDownloadIndex interface

One goal we forgot about a little bit was to allow applications
to provide their own index implementation. This requires the
writable side to also be defined by an interface.

PiperOrigin-RevId: 243979660
This commit is contained in:
olly 2019-04-17 13:32:17 +01:00 committed by Andrew Lewis
parent e0c8af5190
commit 2feadc9762
4 changed files with 72 additions and 40 deletions

View File

@ -38,7 +38,7 @@ import java.util.List;
* <p class="caution">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}).
*
* <p>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 {

View File

@ -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 {
/**

View File

@ -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;

View File

@ -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}).
*
* <p>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;
}