Commit e4f9ff5d by eguven Committed by Oliver Woodman

Add ability to update download requirements to DownloadService

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=200689527
parent afc19bf6
...@@ -773,7 +773,7 @@ public final class DownloadManager { ...@@ -773,7 +773,7 @@ public final class DownloadManager {
private void stop() { private void stop() {
if (changeStateAndNotify(STATE_STARTED, STATE_STARTED_STOPPING)) { if (changeStateAndNotify(STATE_STARTED, STATE_STARTED_STOPPING)) {
logd("Stopping", this); logd("Stopping", this);
thread.interrupt(); cancelDownload();
} }
} }
......
...@@ -44,6 +44,10 @@ public abstract class DownloadService extends Service { ...@@ -44,6 +44,10 @@ public abstract class DownloadService extends Service {
/** Starts a download service, adding a new {@link DownloadAction} to be executed. */ /** Starts a download service, adding a new {@link DownloadAction} to be executed. */
public static final String ACTION_ADD = "com.google.android.exoplayer.downloadService.action.ADD"; public static final String ACTION_ADD = "com.google.android.exoplayer.downloadService.action.ADD";
/** Reloads the download requirements. */
public static final String ACTION_RELOAD_REQUIREMENTS =
"com.google.android.exoplayer.downloadService.action.RELOAD_REQUIREMENTS";
/** Like {@link #ACTION_INIT}, but with {@link #KEY_FOREGROUND} implicitly set to true. */ /** Like {@link #ACTION_INIT}, but with {@link #KEY_FOREGROUND} implicitly set to true. */
private static final String ACTION_RESTART = private static final String ACTION_RESTART =
"com.google.android.exoplayer.downloadService.action.RESTART"; "com.google.android.exoplayer.downloadService.action.RESTART";
...@@ -150,8 +154,7 @@ public abstract class DownloadService extends Service { ...@@ -150,8 +154,7 @@ public abstract class DownloadService extends Service {
Class<? extends DownloadService> clazz, Class<? extends DownloadService> clazz,
DownloadAction downloadAction, DownloadAction downloadAction,
boolean foreground) { boolean foreground) {
return new Intent(context, clazz) return getIntent(context, clazz, ACTION_ADD)
.setAction(ACTION_ADD)
.putExtra(KEY_DOWNLOAD_ACTION, downloadAction.toByteArray()) .putExtra(KEY_DOWNLOAD_ACTION, downloadAction.toByteArray())
.putExtra(KEY_FOREGROUND, foreground); .putExtra(KEY_FOREGROUND, foreground);
} }
...@@ -186,7 +189,7 @@ public abstract class DownloadService extends Service { ...@@ -186,7 +189,7 @@ public abstract class DownloadService extends Service {
* @see #startForeground(Context, Class) * @see #startForeground(Context, Class)
*/ */
public static void start(Context context, Class<? extends DownloadService> clazz) { public static void start(Context context, Class<? extends DownloadService> clazz) {
context.startService(new Intent(context, clazz).setAction(ACTION_INIT)); context.startService(getIntent(context, clazz, ACTION_INIT));
} }
/** /**
...@@ -199,8 +202,7 @@ public abstract class DownloadService extends Service { ...@@ -199,8 +202,7 @@ public abstract class DownloadService extends Service {
* @see #start(Context, Class) * @see #start(Context, Class)
*/ */
public static void startForeground(Context context, Class<? extends DownloadService> clazz) { public static void startForeground(Context context, Class<? extends DownloadService> clazz) {
Intent intent = Intent intent = getIntent(context, clazz, ACTION_INIT).putExtra(KEY_FOREGROUND, true);
new Intent(context, clazz).setAction(ACTION_INIT).putExtra(KEY_FOREGROUND, true);
Util.startForegroundService(context, intent); Util.startForegroundService(context, intent);
} }
...@@ -249,6 +251,10 @@ public abstract class DownloadService extends Service { ...@@ -249,6 +251,10 @@ public abstract class DownloadService extends Service {
case ACTION_START_DOWNLOADS: case ACTION_START_DOWNLOADS:
downloadManager.startDownloads(); downloadManager.startDownloads();
break; break;
case ACTION_RELOAD_REQUIREMENTS:
stopWatchingRequirements();
maybeStartWatchingRequirements();
break;
default: default:
Log.e(TAG, "Ignoring unrecognized action: " + intentAction); Log.e(TAG, "Ignoring unrecognized action: " + intentAction);
break; break;
...@@ -340,6 +346,10 @@ public abstract class DownloadService extends Service { ...@@ -340,6 +346,10 @@ public abstract class DownloadService extends Service {
if (downloadManager.getDownloadCount() > 0) { if (downloadManager.getDownloadCount() > 0) {
return; return;
} }
stopWatchingRequirements();
}
private void stopWatchingRequirements() {
RequirementsHelper requirementsHelper = requirementsHelpers.remove(getClass()); RequirementsHelper requirementsHelper = requirementsHelpers.remove(getClass());
if (requirementsHelper != null) { if (requirementsHelper != null) {
requirementsHelper.stop(); requirementsHelper.stop();
...@@ -363,6 +373,11 @@ public abstract class DownloadService extends Service { ...@@ -363,6 +373,11 @@ public abstract class DownloadService extends Service {
} }
} }
private static Intent getIntent(
Context context, Class<? extends DownloadService> clazz, String action) {
return new Intent(context, clazz).setAction(action);
}
private final class DownloadManagerListener implements DownloadManager.Listener { private final class DownloadManagerListener implements DownloadManager.Listener {
@Override @Override
public void onInitialized(DownloadManager downloadManager) { public void onInitialized(DownloadManager downloadManager) {
...@@ -484,8 +499,7 @@ public abstract class DownloadService extends Service { ...@@ -484,8 +499,7 @@ public abstract class DownloadService extends Service {
} }
private void startServiceWithAction(String action) { private void startServiceWithAction(String action) {
Intent intent = Intent intent = getIntent(context, serviceClass, action).putExtra(KEY_FOREGROUND, true);
new Intent(context, serviceClass).setAction(action).putExtra(KEY_FOREGROUND, true);
Util.startForegroundService(context, intent); Util.startForegroundService(context, intent);
} }
} }
......
...@@ -195,9 +195,7 @@ public final class CacheUtil { ...@@ -195,9 +195,7 @@ public final class CacheUtil {
long start = dataSpec.absoluteStreamPosition; long start = dataSpec.absoluteStreamPosition;
long left = dataSpec.length != C.LENGTH_UNSET ? dataSpec.length : cache.getContentLength(key); long left = dataSpec.length != C.LENGTH_UNSET ? dataSpec.length : cache.getContentLength(key);
while (left != 0) { while (left != 0) {
if (isCanceled != null && isCanceled.get()) { throwExceptionIfInterruptedOrCancelled(isCanceled);
throw new InterruptedException();
}
long blockLength = long blockLength =
cache.getCachedLength(key, start, left != C.LENGTH_UNSET ? left : Long.MAX_VALUE); cache.getCachedLength(key, start, left != C.LENGTH_UNSET ? left : Long.MAX_VALUE);
if (blockLength > 0) { if (blockLength > 0) {
...@@ -205,8 +203,17 @@ public final class CacheUtil { ...@@ -205,8 +203,17 @@ public final class CacheUtil {
} else { } else {
// There is a hole in the cache which is at least "-blockLength" long. // There is a hole in the cache which is at least "-blockLength" long.
blockLength = -blockLength; blockLength = -blockLength;
long read = readAndDiscard(dataSpec, start, blockLength, dataSource, buffer, long read =
priorityTaskManager, priority, counters); readAndDiscard(
dataSpec,
start,
blockLength,
dataSource,
buffer,
priorityTaskManager,
priority,
counters,
isCanceled);
if (read < blockLength) { if (read < blockLength) {
// Reached to the end of the data. // Reached to the end of the data.
if (enableEOFException && left != C.LENGTH_UNSET) { if (enableEOFException && left != C.LENGTH_UNSET) {
...@@ -233,21 +240,28 @@ public final class CacheUtil { ...@@ -233,21 +240,28 @@ public final class CacheUtil {
* caching. * caching.
* @param priority The priority of this task. * @param priority The priority of this task.
* @param counters Counters to be set during reading. * @param counters Counters to be set during reading.
* @param isCanceled An optional flag that will interrupt caching if set to true.
* @return Number of read bytes, or 0 if no data is available because the end of the opened range * @return Number of read bytes, or 0 if no data is available because the end of the opened range
* has been reached. * has been reached.
*/ */
private static long readAndDiscard(DataSpec dataSpec, long absoluteStreamPosition, long length, private static long readAndDiscard(
DataSource dataSource, byte[] buffer, PriorityTaskManager priorityTaskManager, int priority, DataSpec dataSpec,
CachingCounters counters) throws IOException, InterruptedException { long absoluteStreamPosition,
long length,
DataSource dataSource,
byte[] buffer,
PriorityTaskManager priorityTaskManager,
int priority,
CachingCounters counters,
AtomicBoolean isCanceled)
throws IOException, InterruptedException {
while (true) { while (true) {
if (priorityTaskManager != null) { if (priorityTaskManager != null) {
// Wait for any other thread with higher priority to finish its job. // Wait for any other thread with higher priority to finish its job.
priorityTaskManager.proceed(priority); priorityTaskManager.proceed(priority);
} }
try { try {
if (Thread.interrupted()) { throwExceptionIfInterruptedOrCancelled(isCanceled);
throw new InterruptedException();
}
// Create a new dataSpec setting length to C.LENGTH_UNSET to prevent getting an error in // Create a new dataSpec setting length to C.LENGTH_UNSET to prevent getting an error in
// case the given length exceeds the end of input. // case the given length exceeds the end of input.
dataSpec = new DataSpec(dataSpec.uri, dataSpec.postBody, absoluteStreamPosition, dataSpec = new DataSpec(dataSpec.uri, dataSpec.postBody, absoluteStreamPosition,
...@@ -260,9 +274,7 @@ public final class CacheUtil { ...@@ -260,9 +274,7 @@ public final class CacheUtil {
} }
long totalRead = 0; long totalRead = 0;
while (totalRead != length) { while (totalRead != length) {
if (Thread.interrupted()) { throwExceptionIfInterruptedOrCancelled(isCanceled);
throw new InterruptedException();
}
int read = dataSource.read(buffer, 0, int read = dataSource.read(buffer, 0,
length != C.LENGTH_UNSET ? (int) Math.min(buffer.length, length - totalRead) length != C.LENGTH_UNSET ? (int) Math.min(buffer.length, length - totalRead)
: buffer.length); : buffer.length);
...@@ -296,6 +308,13 @@ public final class CacheUtil { ...@@ -296,6 +308,13 @@ public final class CacheUtil {
} }
} }
private static void throwExceptionIfInterruptedOrCancelled(AtomicBoolean isCanceled)
throws InterruptedException {
if (Thread.interrupted() || (isCanceled != null && isCanceled.get())) {
throw new InterruptedException();
}
}
private CacheUtil() {} private CacheUtil() {}
} }
...@@ -30,7 +30,6 @@ import com.google.android.exoplayer2.offline.DownloadManager.TaskState; ...@@ -30,7 +30,6 @@ import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadService; import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.scheduler.Scheduler; import com.google.android.exoplayer2.scheduler.Scheduler;
import com.google.android.exoplayer2.testutil.DummyMainThread; import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.FakeDataSet; import com.google.android.exoplayer2.testutil.FakeDataSet;
...@@ -152,12 +151,6 @@ public class DownloadServiceDashTest { ...@@ -152,12 +151,6 @@ public class DownloadServiceDashTest {
protected Scheduler getScheduler() { protected Scheduler getScheduler() {
return null; return null;
} }
@Nullable
@Override
protected Requirements getRequirements() {
return null;
}
}; };
dashDownloadService.onCreate(); dashDownloadService.onCreate();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment