Exoplayer: Enable updating the DownloadManager reference held by DownloadService

Created on 4 Jun 2019  路  9Comments  路  Source: google/ExoPlayer

[REQUIRED] Use case description

I'm trying to implement a way for users to switch from internal storage to external storage (e.g. SD card), and I want to preserve all downloads when doing that. I'm releasing the current cache, copying all the files, and creating a new cache pointing to the new directory (I'm using SimpleCache). I'm also creating a new DownloadManager with the new cache, but in the end that doesn't matter because the DownloadService will still be stuck to the old DownloadManager (pointing to the old cache) thanks to the static downloadManagerListeners that hold the DownloadManagerHelper which holds the DownloadManager.

Proposed solution

Ideally it'd be great if we had an API that would do all the work needed to switch between internal storage to external storage. But if we have a way to simply update the DownloadManager instance held by DownloadService, we'd be able to do the rest ourselves.

Alternatives considered

At first I thought we could simply have a protected method that clears the downloadManagerListeners, but I don't fully understand its purpose yet, so I'm not sure whether that's reasonable or not.

On our end one solution is to simply kill the process and restart the app, but that's definitely overkill and would be far from good to the users.

enhancement

Most helpful comment

Ability to switch between internal-storage and sd-card would be a very welcome enhancement!

All 9 comments

@erdemguven Could you please take a look?

Another alternative is to use reflection to clear the downloadManagerListeners whenever I need to reinitialize things. I've tested it and it works pretty well, so I guess a quick fix for this would be to just expose a method to clear that.

I'm experiencing the same issue, I had to use reflection as suggested by @tfcporciuncula in order to reinitialize it on storage change.

We would like to have the same functionality (be able to toggle internal storage and external storage). Any information about the status? Is this something planned?

Here's my current reflection code for future reference, since it seems this might be useful for other people:

public static void cleanDownloadManagerReference() throws NoSuchFieldException, IllegalAccessException {
  Field field = DownloadService.class.getDeclaredField("downloadManagerListeners");
  field.setAccessible(true);
  ((Map) field.get(null)).clear();
}

Turns out there's a big downside that I missed: if, for some reason, we run that reflection code while the service is running, we'll get a crash when the service is being destroyed:

@Override
public void onDestroy() {
  isDestroyed = true;
  // downloadManagerHelper will be null
  DownloadManagerHelper downloadManagerHelper = downloadManagerListeners.get(getClass());
  boolean unschedule = !downloadManager.isWaitingForRequirements();
  // so this leads to a NullPointerException
  downloadManagerHelper.detachService(this, unschedule);
  if (foregroundNotificationUpdater != null) {
    foregroundNotificationUpdater.stopPeriodicUpdates();
  }
}

This is just the onDestroy() implementation of the DownloadService. We'll get a null from that downloadManagerListeners.get(getClass()) call, and we don't do any null checks, so we get a NPE.

It'd be great if we could add a null check there so our hack could work, but I understand how that request is weird, given that on the long run we don't want to depend on the hack. Still, might be a good quick win for now cc @tonihei @ojw28

Ability to switch between internal-storage and sd-card would be a very welcome enhancement!

Any update on the same? @janeriksamsonsen @tfcporciuncula @ojw28 @marcbaechinger @AquilesCanta @luiscurini

any news about this ? @marcbaechinger

Was this page helpful?
0 / 5 - 0 ratings