Further improve DownloadService action names & methods

- We had buildAddRequest and sendNewDownload. Converged to
  buildAddDownload and sendAddDownload.
- Also fixed a few more inconsistencies, and brought the
  action constants into line as well.

PiperOrigin-RevId: 244274041
This commit is contained in:
olly 2019-04-18 23:44:58 +01:00 committed by Oliver Woodman
parent bab7b9e9a1
commit 748a29e8f5
3 changed files with 35 additions and 29 deletions

View File

@ -263,7 +263,7 @@ public class DownloadTracker {
} }
private void startDownload(DownloadRequest downloadRequest) { private void startDownload(DownloadRequest downloadRequest) {
DownloadService.sendNewDownload( DownloadService.sendAddDownload(
context, DemoDownloadService.class, downloadRequest, /* foreground= */ false); context, DemoDownloadService.class, downloadRequest, /* foreground= */ false);
} }

View File

@ -63,7 +63,8 @@ public abstract class DownloadService extends Service {
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}. * <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul> * </ul>
*/ */
public static final String ACTION_ADD = "com.google.android.exoplayer.downloadService.action.ADD"; public static final String ACTION_ADD_DOWNLOAD =
"com.google.android.exoplayer.downloadService.action.ADD_DOWNLOAD";
/** /**
* Resumes all downloads except those that have a non-zero {@link Download#stopReason}. Extras: * Resumes all downloads except those that have a non-zero {@link Download#stopReason}. Extras:
@ -72,8 +73,8 @@ public abstract class DownloadService extends Service {
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}. * <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul> * </ul>
*/ */
public static final String ACTION_RESUME = public static final String ACTION_RESUME_DOWNLOADS =
"com.google.android.exoplayer.downloadService.action.RESUME"; "com.google.android.exoplayer.downloadService.action.RESUME_DOWNLOADS";
/** /**
* Pauses all downloads. Extras: * Pauses all downloads. Extras:
@ -82,8 +83,8 @@ public abstract class DownloadService extends Service {
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}. * <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul> * </ul>
*/ */
public static final String ACTION_PAUSE = public static final String ACTION_PAUSE_DOWNLOADS =
"com.google.android.exoplayer.downloadService.action.PAUSE"; "com.google.android.exoplayer.downloadService.action.PAUSE_DOWNLOADS";
/** /**
* Sets the stop reason for one or all downloads. To clear the stop reason, pass {@link * Sets the stop reason for one or all downloads. To clear the stop reason, pass {@link
@ -98,7 +99,7 @@ public abstract class DownloadService extends Service {
* </ul> * </ul>
*/ */
public static final String ACTION_SET_STOP_REASON = public static final String ACTION_SET_STOP_REASON =
"com.google.android.exoplayer.downloadService.action.SET_MANUAL_STOP_REASON"; "com.google.android.exoplayer.downloadService.action.SET_STOP_REASON";
/** /**
* Removes a download. Extras: * Removes a download. Extras:
@ -108,18 +109,22 @@ public abstract class DownloadService extends Service {
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}. * <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul> * </ul>
*/ */
public static final String ACTION_REMOVE = public static final String ACTION_REMOVE_DOWNLOAD =
"com.google.android.exoplayer.downloadService.action.REMOVE"; "com.google.android.exoplayer.downloadService.action.REMOVE_DOWNLOAD";
/** Key for the {@link DownloadRequest} in {@link #ACTION_ADD} intents. */ /** Key for the {@link DownloadRequest} in {@link #ACTION_ADD_DOWNLOAD} intents. */
public static final String KEY_DOWNLOAD_REQUEST = "download_request"; public static final String KEY_DOWNLOAD_REQUEST = "download_request";
/** /**
* Key for the content id in {@link #ACTION_SET_STOP_REASON} and {@link #ACTION_REMOVE} intents. * Key for the content id in {@link #ACTION_SET_STOP_REASON} and {@link #ACTION_REMOVE_DOWNLOAD}
* intents.
*/ */
public static final String KEY_CONTENT_ID = "content_id"; public static final String KEY_CONTENT_ID = "content_id";
/** Key for the stop reason in {@link #ACTION_SET_STOP_REASON} and {@link #ACTION_ADD} intents. */ /**
* Key for the stop reason in {@link #ACTION_SET_STOP_REASON} and {@link #ACTION_ADD_DOWNLOAD}
* intents.
*/
public static final String KEY_STOP_REASON = "manual_stop_reason"; public static final String KEY_STOP_REASON = "manual_stop_reason";
/** /**
@ -233,12 +238,12 @@ public abstract class DownloadService extends Service {
* @param foreground Whether this intent will be used to start the service in the foreground. * @param foreground Whether this intent will be used to start the service in the foreground.
* @return Created Intent. * @return Created Intent.
*/ */
public static Intent buildAddRequestIntent( public static Intent buildAddDownloadIntent(
Context context, Context context,
Class<? extends DownloadService> clazz, Class<? extends DownloadService> clazz,
DownloadRequest downloadRequest, DownloadRequest downloadRequest,
boolean foreground) { boolean foreground) {
return buildAddRequestIntent(context, clazz, downloadRequest, STOP_REASON_NONE, foreground); return buildAddDownloadIntent(context, clazz, downloadRequest, STOP_REASON_NONE, foreground);
} }
/** /**
@ -252,13 +257,13 @@ public abstract class DownloadService extends Service {
* @param foreground Whether this intent will be used to start the service in the foreground. * @param foreground Whether this intent will be used to start the service in the foreground.
* @return Created Intent. * @return Created Intent.
*/ */
public static Intent buildAddRequestIntent( public static Intent buildAddDownloadIntent(
Context context, Context context,
Class<? extends DownloadService> clazz, Class<? extends DownloadService> clazz,
DownloadRequest downloadRequest, DownloadRequest downloadRequest,
int stopReason, int stopReason,
boolean foreground) { boolean foreground) {
return getIntent(context, clazz, ACTION_ADD, foreground) return getIntent(context, clazz, ACTION_ADD_DOWNLOAD, foreground)
.putExtra(KEY_DOWNLOAD_REQUEST, downloadRequest) .putExtra(KEY_DOWNLOAD_REQUEST, downloadRequest)
.putExtra(KEY_STOP_REASON, stopReason); .putExtra(KEY_STOP_REASON, stopReason);
} }
@ -274,7 +279,8 @@ public abstract class DownloadService extends Service {
*/ */
public static Intent buildRemoveDownloadIntent( public static Intent buildRemoveDownloadIntent(
Context context, Class<? extends DownloadService> clazz, String id, boolean foreground) { Context context, Class<? extends DownloadService> clazz, String id, boolean foreground) {
return getIntent(context, clazz, ACTION_REMOVE, foreground).putExtra(KEY_CONTENT_ID, id); return getIntent(context, clazz, ACTION_REMOVE_DOWNLOAD, foreground)
.putExtra(KEY_CONTENT_ID, id);
} }
/** /**
@ -287,7 +293,7 @@ public abstract class DownloadService extends Service {
*/ */
public static Intent buildResumeDownloadsIntent( public static Intent buildResumeDownloadsIntent(
Context context, Class<? extends DownloadService> clazz, boolean foreground) { Context context, Class<? extends DownloadService> clazz, boolean foreground) {
return getIntent(context, clazz, ACTION_RESUME, foreground); return getIntent(context, clazz, ACTION_RESUME_DOWNLOADS, foreground);
} }
/** /**
@ -300,7 +306,7 @@ public abstract class DownloadService extends Service {
*/ */
public static Intent buildPauseDownloadsIntent( public static Intent buildPauseDownloadsIntent(
Context context, Class<? extends DownloadService> clazz, boolean foreground) { Context context, Class<? extends DownloadService> clazz, boolean foreground) {
return getIntent(context, clazz, ACTION_PAUSE, foreground); return getIntent(context, clazz, ACTION_PAUSE_DOWNLOADS, foreground);
} }
/** /**
@ -333,12 +339,12 @@ public abstract class DownloadService extends Service {
* @param downloadRequest The request to be executed. * @param downloadRequest The request to be executed.
* @param foreground Whether the service is started in the foreground. * @param foreground Whether the service is started in the foreground.
*/ */
public static void sendNewDownload( public static void sendAddDownload(
Context context, Context context,
Class<? extends DownloadService> clazz, Class<? extends DownloadService> clazz,
DownloadRequest downloadRequest, DownloadRequest downloadRequest,
boolean foreground) { boolean foreground) {
Intent intent = buildAddRequestIntent(context, clazz, downloadRequest, foreground); Intent intent = buildAddDownloadIntent(context, clazz, downloadRequest, foreground);
startService(context, intent, foreground); startService(context, intent, foreground);
} }
@ -352,13 +358,13 @@ public abstract class DownloadService extends Service {
* if the download should be started. * if the download should be started.
* @param foreground Whether the service is started in the foreground. * @param foreground Whether the service is started in the foreground.
*/ */
public static void sendNewDownload( public static void sendAddDownload(
Context context, Context context,
Class<? extends DownloadService> clazz, Class<? extends DownloadService> clazz,
DownloadRequest downloadRequest, DownloadRequest downloadRequest,
int stopReason, int stopReason,
boolean foreground) { boolean foreground) {
Intent intent = buildAddRequestIntent(context, clazz, downloadRequest, stopReason, foreground); Intent intent = buildAddDownloadIntent(context, clazz, downloadRequest, stopReason, foreground);
startService(context, intent, foreground); startService(context, intent, foreground);
} }
@ -412,7 +418,7 @@ public abstract class DownloadService extends Service {
* @param stopReason An application defined stop reason. * @param stopReason An application defined stop reason.
* @param foreground Whether the service is started in the foreground. * @param foreground Whether the service is started in the foreground.
*/ */
public static void sendStopReason( public static void sendSetStopReason(
Context context, Context context,
Class<? extends DownloadService> clazz, Class<? extends DownloadService> clazz,
@Nullable String id, @Nullable String id,
@ -488,7 +494,7 @@ public abstract class DownloadService extends Service {
case ACTION_RESTART: case ACTION_RESTART:
// Do nothing. // Do nothing.
break; break;
case ACTION_ADD: case ACTION_ADD_DOWNLOAD:
DownloadRequest downloadRequest = intent.getParcelableExtra(KEY_DOWNLOAD_REQUEST); DownloadRequest downloadRequest = intent.getParcelableExtra(KEY_DOWNLOAD_REQUEST);
if (downloadRequest == null) { if (downloadRequest == null) {
Log.e(TAG, "Ignored ADD: Missing " + KEY_DOWNLOAD_REQUEST + " extra"); Log.e(TAG, "Ignored ADD: Missing " + KEY_DOWNLOAD_REQUEST + " extra");
@ -497,10 +503,10 @@ public abstract class DownloadService extends Service {
downloadManager.addDownload(downloadRequest, stopReason); downloadManager.addDownload(downloadRequest, stopReason);
} }
break; break;
case ACTION_RESUME: case ACTION_RESUME_DOWNLOADS:
downloadManager.resumeDownloads(); downloadManager.resumeDownloads();
break; break;
case ACTION_PAUSE: case ACTION_PAUSE_DOWNLOADS:
downloadManager.pauseDownloads(); downloadManager.pauseDownloads();
break; break;
case ACTION_SET_STOP_REASON: case ACTION_SET_STOP_REASON:
@ -512,7 +518,7 @@ public abstract class DownloadService extends Service {
downloadManager.setStopReason(contentId, stopReason); downloadManager.setStopReason(contentId, stopReason);
} }
break; break;
case ACTION_REMOVE: case ACTION_REMOVE_DOWNLOAD:
String contentId = intent.getStringExtra(KEY_CONTENT_ID); String contentId = intent.getStringExtra(KEY_CONTENT_ID);
if (contentId == null) { if (contentId == null) {
Log.e(TAG, "Ignored REMOVE: Missing " + KEY_CONTENT_ID + " extra"); Log.e(TAG, "Ignored REMOVE: Missing " + KEY_CONTENT_ID + " extra");

View File

@ -214,7 +214,7 @@ public class DownloadServiceDashTest {
dummyMainThread.runOnMainThread( dummyMainThread.runOnMainThread(
() -> { () -> {
Intent startIntent = Intent startIntent =
DownloadService.buildAddRequestIntent( DownloadService.buildAddDownloadIntent(
context, DownloadService.class, action, /* foreground= */ false); context, DownloadService.class, action, /* foreground= */ false);
dashDownloadService.onStartCommand(startIntent, 0, 0); dashDownloadService.onStartCommand(startIntent, 0, 0);
}); });