Skip to content

Commit

Permalink
Rename DownloadAction to DownloadRequest
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 243806888
  • Loading branch information
erdemguven authored and ojw28 committed Apr 16, 2019
1 parent 9fc3ea7 commit 5856e75
Show file tree
Hide file tree
Showing 27 changed files with 650 additions and 649 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ protected void onDownloadChanged(Download download) {
notificationHelper.buildDownloadCompletedNotification(
R.drawable.ic_download_done,
/* contentIntent= */ null,
Util.fromUtf8Bytes(download.action.data));
Util.fromUtf8Bytes(download.request.data));
} else if (download.state == Download.STATE_FAILED) {
notification =
notificationHelper.buildDownloadFailedNotification(
R.drawable.ic_download_done,
/* contentIntent= */ null,
Util.fromUtf8Bytes(download.action.data));
Util.fromUtf8Bytes(download.request.data));
} else {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.RenderersFactory;
import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.DownloadAction;
import com.google.android.exoplayer2.offline.DownloadCursor;
import com.google.android.exoplayer2.offline.DownloadHelper;
import com.google.android.exoplayer2.offline.DownloadIndex;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadRequest;
import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo;
Expand Down Expand Up @@ -89,7 +89,7 @@ public boolean isDownloaded(Uri uri) {
public List<StreamKey> getOfflineStreamKeys(Uri uri) {
Download download = downloads.get(uri);
return download != null && download.state != Download.STATE_FAILED
? download.action.streamKeys
? download.request.streamKeys
: Collections.emptyList();
}

Expand All @@ -102,7 +102,7 @@ public void toggleDownload(
Download download = downloads.get(uri);
if (download != null) {
DownloadService.startWithRemoveDownload(
context, DemoDownloadService.class, download.action.id, /* foreground= */ false);
context, DemoDownloadService.class, download.request.id, /* foreground= */ false);
} else {
if (startDownloadDialogHelper != null) {
startDownloadDialogHelper.release();
Expand All @@ -117,18 +117,13 @@ private void loadDownloads() {
try (DownloadCursor loadedDownloads = downloadIndex.getDownloads()) {
while (loadedDownloads.moveToNext()) {
Download download = loadedDownloads.getDownload();
downloads.put(download.action.uri, download);
downloads.put(download.request.uri, download);
}
} catch (IOException e) {
Log.w(TAG, "Failed to query downloads", e);
}
}

private void startServiceWithAction(DownloadAction action) {
DownloadService.startWithAction(
context, DemoDownloadService.class, action, /* foreground= */ false);
}

private DownloadHelper getDownloadHelper(
Uri uri, String extension, RenderersFactory renderersFactory) {
int type = Util.inferContentType(uri, extension);
Expand All @@ -150,15 +145,15 @@ private class DownloadManagerListener implements DownloadManager.Listener {

@Override
public void onDownloadChanged(DownloadManager downloadManager, Download download) {
downloads.put(download.action.uri, download);
downloads.put(download.request.uri, download);
for (Listener listener : listeners) {
listener.onDownloadsChanged();
}
}

@Override
public void onDownloadRemoved(DownloadManager downloadManager, Download download) {
downloads.remove(download.action.uri);
downloads.remove(download.request.uri);
for (Listener listener : listeners) {
listener.onDownloadsChanged();
}
Expand Down Expand Up @@ -259,8 +254,9 @@ public void onDismiss(DialogInterface dialogInterface) {
// Internal methods.

private void startDownload() {
DownloadAction downloadAction = downloadHelper.getDownloadAction(Util.getUtf8Bytes(name));
startServiceWithAction(downloadAction);
DownloadRequest downloadRequest = downloadHelper.getDownloadRequest(Util.getUtf8Bytes(name));
DownloadService.startWithNewDownload(
context, DemoDownloadService.class, downloadRequest, /* foreground= */ false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import android.net.Uri;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.offline.DownloadAction.UnsupportedActionException;
import com.google.android.exoplayer2.offline.DownloadRequest.UnsupportedRequestException;
import com.google.android.exoplayer2.util.AtomicFile;
import com.google.android.exoplayer2.util.Util;
import java.io.DataInputStream;
Expand All @@ -28,7 +28,7 @@
import java.util.List;

/**
* Loads {@link DownloadAction DownloadActions} from legacy action files.
* Loads {@link DownloadRequest DownloadRequests} from legacy action files.
*
* @deprecated Legacy action files should be merged into download indices using {@link
* ActionFileUpgradeUtil}.
Expand All @@ -41,7 +41,7 @@
private final AtomicFile atomicFile;

/**
* @param actionFile The file from which {@link DownloadAction DownloadActions} will be loaded.
* @param actionFile The file from which {@link DownloadRequest DownloadRequests} will be loaded.
*/
public ActionFile(File actionFile) {
atomicFile = new AtomicFile(actionFile);
Expand All @@ -58,15 +58,15 @@ public void delete() {
}

/**
* Loads {@link DownloadAction DownloadActions} from the file.
* Loads {@link DownloadRequest DownloadRequests} from the file.
*
* @return The loaded {@link DownloadAction DownloadActions}, or an empty array if the file does
* @return The loaded {@link DownloadRequest DownloadRequests}, or an empty array if the file does
* not exist.
* @throws IOException If there is an error reading the file.
*/
public DownloadAction[] load() throws IOException {
public DownloadRequest[] load() throws IOException {
if (!exists()) {
return new DownloadAction[0];
return new DownloadRequest[0];
}
InputStream inputStream = null;
try {
Expand All @@ -77,21 +77,21 @@ public DownloadAction[] load() throws IOException {
throw new IOException("Unsupported action file version: " + version);
}
int actionCount = dataInputStream.readInt();
ArrayList<DownloadAction> actions = new ArrayList<>();
ArrayList<DownloadRequest> actions = new ArrayList<>();
for (int i = 0; i < actionCount; i++) {
try {
actions.add(readDownloadAction(dataInputStream));
} catch (UnsupportedActionException e) {
// remove DownloadAction is not supported. Ignore the exception and continue loading rest.
actions.add(readDownloadRequest(dataInputStream));
} catch (UnsupportedRequestException e) {
// remove DownloadRequest is not supported. Ignore and continue loading rest.
}
}
return actions.toArray(new DownloadAction[0]);
return actions.toArray(new DownloadRequest[0]);
} finally {
Util.closeQuietly(inputStream);
}
}

private static DownloadAction readDownloadAction(DataInputStream input) throws IOException {
private static DownloadRequest readDownloadRequest(DataInputStream input) throws IOException {
String type = input.readUTF();
int version = input.readInt();

Expand All @@ -108,7 +108,7 @@ private static DownloadAction readDownloadAction(DataInputStream input) throws I
}

// Serialized version 0 progressive actions did not contain keys.
boolean isLegacyProgressive = version == 0 && DownloadAction.TYPE_PROGRESSIVE.equals(type);
boolean isLegacyProgressive = version == 0 && DownloadRequest.TYPE_PROGRESSIVE.equals(type);
List<StreamKey> keys = new ArrayList<>();
if (!isLegacyProgressive) {
int keyCount = input.readInt();
Expand All @@ -120,22 +120,22 @@ private static DownloadAction readDownloadAction(DataInputStream input) throws I
// Serialized version 0 and 1 DASH/HLS/SS actions did not contain a custom cache key.
boolean isLegacySegmented =
version < 2
&& (DownloadAction.TYPE_DASH.equals(type)
|| DownloadAction.TYPE_HLS.equals(type)
|| DownloadAction.TYPE_SS.equals(type));
&& (DownloadRequest.TYPE_DASH.equals(type)
|| DownloadRequest.TYPE_HLS.equals(type)
|| DownloadRequest.TYPE_SS.equals(type));
String customCacheKey = null;
if (!isLegacySegmented) {
customCacheKey = input.readBoolean() ? input.readUTF() : null;
}

// Serialized version 0, 1 and 2 did not contain an id. We need to generate one.
String id = version < 3 ? generateDownloadActionId(uri, customCacheKey) : input.readUTF();
String id = version < 3 ? generateDownloadId(uri, customCacheKey) : input.readUTF();

if (isRemoveAction) {
// Remove actions are not supported anymore.
throw new UnsupportedActionException();
throw new UnsupportedRequestException();
}
return new DownloadAction(id, type, uri, keys, customCacheKey, data);
return new DownloadRequest(id, type, uri, keys, customCacheKey, data);
}

private static StreamKey readKey(String type, int version, DataInputStream input)
Expand All @@ -145,7 +145,7 @@ private static StreamKey readKey(String type, int version, DataInputStream input
int trackIndex;

// Serialized version 0 HLS/SS actions did not contain a period index.
if ((DownloadAction.TYPE_HLS.equals(type) || DownloadAction.TYPE_SS.equals(type))
if ((DownloadRequest.TYPE_HLS.equals(type) || DownloadRequest.TYPE_SS.equals(type))
&& version == 0) {
periodIndex = 0;
groupIndex = input.readInt();
Expand All @@ -158,7 +158,7 @@ private static StreamKey readKey(String type, int version, DataInputStream input
return new StreamKey(periodIndex, groupIndex, trackIndex);
}

private static String generateDownloadActionId(Uri uri, @Nullable String customCacheKey) {
private static String generateDownloadId(Uri uri, @Nullable String customCacheKey) {
return customCacheKey != null ? customCacheKey : uri.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public final class ActionFileUpgradeUtil {
public interface DownloadIdProvider {

/**
* Returns a download id for given action.
* Returns a download id for given request.
*
* @param downloadAction The action for which an ID is required.
* @param downloadRequest The request for which an ID is required.
* @return A corresponding download ID.
*/
String getId(DownloadAction downloadAction);
String getId(DownloadRequest downloadRequest);
}

private ActionFileUpgradeUtil() {}

/**
* Merges {@link DownloadAction DownloadActions} contained in a legacy action file into a {@link
* Merges {@link DownloadRequest DownloadRequests} contained in a legacy action file into a {@link
* DefaultDownloadIndex}, deleting the action file if the merge is successful or if {@code
* deleteOnFailure} is {@code true}.
*
Expand All @@ -49,9 +49,9 @@ private ActionFileUpgradeUtil() {}
* @param actionFilePath The action file path.
* @param downloadIdProvider A download ID provider, or {@code null}. If {@code null} then ID of
* each download will be its custom cache key if one is specified, or else its URL.
* @param downloadIndex The index into which the action will be merged.
* @param downloadIndex The index into which the requests will be merged.
* @param deleteOnFailure Whether to delete the action file if the merge fails.
* @throws IOException If an error occurs loading or merging the actions.
* @throws IOException If an error occurs loading or merging the requests.
*/
@SuppressWarnings("deprecation")
public static void upgradeAndDelete(
Expand All @@ -64,11 +64,11 @@ public static void upgradeAndDelete(
if (actionFile.exists()) {
boolean success = false;
try {
for (DownloadAction action : actionFile.load()) {
for (DownloadRequest request : actionFile.load()) {
if (downloadIdProvider != null) {
action = action.copyWithId(downloadIdProvider.getId(action));
request = request.copyWithId(downloadIdProvider.getId(request));
}
mergeAction(action, downloadIndex);
mergeRequest(request, downloadIndex);
}
success = true;
} finally {
Expand All @@ -80,22 +80,22 @@ public static void upgradeAndDelete(
}

/**
* Merges a {@link DownloadAction} into a {@link DefaultDownloadIndex}.
* Merges a {@link DownloadRequest} into a {@link DefaultDownloadIndex}.
*
* @param action The action to be merged.
* @param downloadIndex The index into which the action will be merged.
* @throws IOException If an error occurs merging the action.
* @param request The request to be merged.
* @param downloadIndex The index into which the request will be merged.
* @throws IOException If an error occurs merging the request.
*/
/* package */ static void mergeAction(DownloadAction action, DefaultDownloadIndex downloadIndex)
throws IOException {
Download download = downloadIndex.getDownload(action.id);
/* package */ static void mergeRequest(
DownloadRequest request, DefaultDownloadIndex downloadIndex) throws IOException {
Download download = downloadIndex.getDownload(request.id);
if (download != null) {
download = DownloadManager.mergeAction(download, action, download.manualStopReason);
download = DownloadManager.mergeRequest(download, request, download.manualStopReason);
} else {
long nowMs = System.currentTimeMillis();
download =
new Download(
action,
request,
STATE_QUEUED,
Download.FAILURE_REASON_NONE,
Download.MANUAL_STOP_REASON_NONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ public DownloadCursor getDownloads(@Download.State int... states) throws Databas
public void putDownload(Download download) throws DatabaseIOException {
ensureInitialized();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, download.action.id);
values.put(COLUMN_TYPE, download.action.type);
values.put(COLUMN_URI, download.action.uri.toString());
values.put(COLUMN_STREAM_KEYS, encodeStreamKeys(download.action.streamKeys));
values.put(COLUMN_CUSTOM_CACHE_KEY, download.action.customCacheKey);
values.put(COLUMN_DATA, download.action.data);
values.put(COLUMN_ID, download.request.id);
values.put(COLUMN_TYPE, download.request.type);
values.put(COLUMN_URI, download.request.uri.toString());
values.put(COLUMN_STREAM_KEYS, encodeStreamKeys(download.request.streamKeys));
values.put(COLUMN_CUSTOM_CACHE_KEY, download.request.customCacheKey);
values.put(COLUMN_DATA, download.request.data);
values.put(COLUMN_STATE, download.state);
values.put(COLUMN_DOWNLOAD_PERCENTAGE, download.getDownloadPercentage());
values.put(COLUMN_DOWNLOADED_BYTES, download.getDownloadedBytes());
Expand Down Expand Up @@ -342,8 +342,8 @@ private static String getStateQuery(@Download.State int... states) {
}

private static Download getDownloadForCurrentRow(Cursor cursor) {
DownloadAction action =
new DownloadAction(
DownloadRequest request =
new DownloadRequest(
cursor.getString(COLUMN_INDEX_ID),
cursor.getString(COLUMN_INDEX_TYPE),
Uri.parse(cursor.getString(COLUMN_INDEX_URI)),
Expand All @@ -355,7 +355,7 @@ private static Download getDownloadForCurrentRow(Cursor cursor) {
cachingCounters.contentLength = cursor.getLong(COLUMN_INDEX_TOTAL_BYTES);
cachingCounters.percentage = cursor.getFloat(COLUMN_INDEX_DOWNLOAD_PERCENTAGE);
return new Download(
action,
request,
cursor.getInt(COLUMN_INDEX_STATE),
cursor.getInt(COLUMN_INDEX_FAILURE_REASON),
cursor.getInt(COLUMN_INDEX_MANUAL_STOP_REASON),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,32 @@ public DefaultDownloaderFactory(DownloaderConstructorHelper downloaderConstructo
}

@Override
public Downloader createDownloader(DownloadAction action) {
switch (action.type) {
case DownloadAction.TYPE_PROGRESSIVE:
public Downloader createDownloader(DownloadRequest request) {
switch (request.type) {
case DownloadRequest.TYPE_PROGRESSIVE:
return new ProgressiveDownloader(
action.uri, action.customCacheKey, downloaderConstructorHelper);
case DownloadAction.TYPE_DASH:
return createDownloader(action, DASH_DOWNLOADER_CONSTRUCTOR);
case DownloadAction.TYPE_HLS:
return createDownloader(action, HLS_DOWNLOADER_CONSTRUCTOR);
case DownloadAction.TYPE_SS:
return createDownloader(action, SS_DOWNLOADER_CONSTRUCTOR);
request.uri, request.customCacheKey, downloaderConstructorHelper);
case DownloadRequest.TYPE_DASH:
return createDownloader(request, DASH_DOWNLOADER_CONSTRUCTOR);
case DownloadRequest.TYPE_HLS:
return createDownloader(request, HLS_DOWNLOADER_CONSTRUCTOR);
case DownloadRequest.TYPE_SS:
return createDownloader(request, SS_DOWNLOADER_CONSTRUCTOR);
default:
throw new IllegalArgumentException("Unsupported type: " + action.type);
throw new IllegalArgumentException("Unsupported type: " + request.type);
}
}

private Downloader createDownloader(
DownloadAction action, @Nullable Constructor<? extends Downloader> constructor) {
DownloadRequest request, @Nullable Constructor<? extends Downloader> constructor) {
if (constructor == null) {
throw new IllegalStateException("Module missing for: " + action.type);
throw new IllegalStateException("Module missing for: " + request.type);
}
try {
// TODO: Support customCacheKey in DASH/HLS/SS, for completeness.
return constructor.newInstance(action.uri, action.streamKeys, downloaderConstructorHelper);
return constructor.newInstance(request.uri, request.streamKeys, downloaderConstructorHelper);
} catch (Exception e) {
throw new RuntimeException("Failed to instantiate downloader for: " + action.type, e);
throw new RuntimeException("Failed to instantiate downloader for: " + request.type, e);
}
}

Expand Down
Loading

0 comments on commit 5856e75

Please sign in to comment.