mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Create internal methods that will run on internal thread
This is the first of a series of cls that will convert DownloadManager threading model as public methods post actions on an internal thread which then do the work. PiperOrigin-RevId: 240743400
This commit is contained in:
parent
62f719f3ee
commit
b1a13f9651
1 changed files with 103 additions and 85 deletions
|
|
@ -237,23 +237,40 @@ public final class DownloadManager {
|
||||||
logd("Created");
|
logd("Created");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns whether the manager has completed initialization. */
|
||||||
|
public boolean isInitialized() {
|
||||||
|
Assertions.checkState(!released);
|
||||||
|
return initialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns whether there are no active downloads. */
|
||||||
|
public boolean isIdle() {
|
||||||
|
Assertions.checkState(!released);
|
||||||
|
return initialized
|
||||||
|
&& activeDownloads.isEmpty()
|
||||||
|
&& dowloadUpdateQueue.isEmpty()
|
||||||
|
&& !loadingDownload;
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns the used {@link DownloadIndex}. */
|
/** Returns the used {@link DownloadIndex}. */
|
||||||
public DownloadIndex getDownloadIndex() {
|
public DownloadIndex getDownloadIndex() {
|
||||||
return downloadIndex;
|
return downloadIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Returns the number of downloads. */
|
||||||
* Sets the requirements needed to be met to start downloads.
|
public int getDownloadCount() {
|
||||||
*
|
|
||||||
* @param requirements Need to be met to start downloads.
|
|
||||||
*/
|
|
||||||
public void setRequirements(Requirements requirements) {
|
|
||||||
Assertions.checkState(!released);
|
Assertions.checkState(!released);
|
||||||
if (requirements.equals(requirementsWatcher.getRequirements())) {
|
return downloads.size();
|
||||||
return;
|
}
|
||||||
|
|
||||||
|
/** Returns the states of all current downloads. */
|
||||||
|
public DownloadState[] getAllDownloadStates() {
|
||||||
|
Assertions.checkState(!released);
|
||||||
|
DownloadState[] states = new DownloadState[downloads.size()];
|
||||||
|
for (int i = 0; i < states.length; i++) {
|
||||||
|
states[i] = downloads.get(i).getUpdatedDownloadState();
|
||||||
}
|
}
|
||||||
requirementsWatcher.stop();
|
return states;
|
||||||
onRequirementsStateChanged(watchRequirements(requirements));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the requirements needed to be met to start downloads. */
|
/** Returns the requirements needed to be met to start downloads. */
|
||||||
|
|
@ -279,6 +296,16 @@ public final class DownloadManager {
|
||||||
listeners.remove(listener);
|
listeners.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the requirements needed to be met to start downloads.
|
||||||
|
*
|
||||||
|
* @param requirements Need to be met to start downloads.
|
||||||
|
*/
|
||||||
|
public void setRequirements(Requirements requirements) {
|
||||||
|
Assertions.checkState(!released);
|
||||||
|
setRequirementsInternal(requirements);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears manual stop reason of all downloads. Downloads are started if the requirements are met.
|
* Clears manual stop reason of all downloads. Downloads are started if the requirements are met.
|
||||||
*/
|
*/
|
||||||
|
|
@ -348,30 +375,7 @@ public final class DownloadManager {
|
||||||
*/
|
*/
|
||||||
public void addDownload(DownloadAction action) {
|
public void addDownload(DownloadAction action) {
|
||||||
Assertions.checkState(!released);
|
Assertions.checkState(!released);
|
||||||
dowloadUpdateQueue.add(
|
addDownloadInternal(action);
|
||||||
new DownloadUpdater(action.id) {
|
|
||||||
@Override
|
|
||||||
void onExisting(Download download) {
|
|
||||||
download.addAction(action);
|
|
||||||
logd("Action is added to existing download", download);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
DownloadState onLoad(@Nullable DownloadState downloadState) {
|
|
||||||
DownloadState state;
|
|
||||||
if (downloadState == null) {
|
|
||||||
state = new DownloadState(action);
|
|
||||||
logd("Download state is created for " + id);
|
|
||||||
} else {
|
|
||||||
state = downloadState.mergeAction(action);
|
|
||||||
logd("Download state is loaded for " + id);
|
|
||||||
}
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (initialized) {
|
|
||||||
processDownloadUpdateQueue();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -381,57 +385,7 @@ public final class DownloadManager {
|
||||||
*/
|
*/
|
||||||
public void removeDownload(String id) {
|
public void removeDownload(String id) {
|
||||||
Assertions.checkState(!released);
|
Assertions.checkState(!released);
|
||||||
dowloadUpdateQueue.add(
|
removeDownloadInternal(id);
|
||||||
new DownloadUpdater(id) {
|
|
||||||
@Override
|
|
||||||
void onExisting(Download download) {
|
|
||||||
download.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
DownloadState onLoad(@Nullable DownloadState downloadState) {
|
|
||||||
if (downloadState != null) {
|
|
||||||
downloadState = downloadState.setRemoveState();
|
|
||||||
} else {
|
|
||||||
logd("Can't remove download. No download with id: " + id);
|
|
||||||
}
|
|
||||||
return downloadState;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (initialized) {
|
|
||||||
processDownloadUpdateQueue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns the number of downloads. */
|
|
||||||
public int getDownloadCount() {
|
|
||||||
Assertions.checkState(!released);
|
|
||||||
return downloads.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns the states of all current downloads. */
|
|
||||||
public DownloadState[] getAllDownloadStates() {
|
|
||||||
Assertions.checkState(!released);
|
|
||||||
DownloadState[] states = new DownloadState[downloads.size()];
|
|
||||||
for (int i = 0; i < states.length; i++) {
|
|
||||||
states[i] = downloads.get(i).getUpdatedDownloadState();
|
|
||||||
}
|
|
||||||
return states;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns whether the manager has completed initialization. */
|
|
||||||
public boolean isInitialized() {
|
|
||||||
Assertions.checkState(!released);
|
|
||||||
return initialized;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns whether there are no active downloads. */
|
|
||||||
public boolean isIdle() {
|
|
||||||
Assertions.checkState(!released);
|
|
||||||
return initialized
|
|
||||||
&& activeDownloads.isEmpty()
|
|
||||||
&& dowloadUpdateQueue.isEmpty()
|
|
||||||
&& !loadingDownload;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -443,6 +397,12 @@ public final class DownloadManager {
|
||||||
if (released) {
|
if (released) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
releaseInternal();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methods that run on internal thread.
|
||||||
|
|
||||||
|
private void releaseInternal() {
|
||||||
released = true;
|
released = true;
|
||||||
stopAllDownloadThreads();
|
stopAllDownloadThreads();
|
||||||
if (requirementsWatcher != null) {
|
if (requirementsWatcher != null) {
|
||||||
|
|
@ -455,6 +415,14 @@ public final class DownloadManager {
|
||||||
logd("Released");
|
logd("Released");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setRequirementsInternal(Requirements requirements) {
|
||||||
|
if (requirements.equals(requirementsWatcher.getRequirements())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
requirementsWatcher.stop();
|
||||||
|
onRequirementsStateChanged(watchRequirements(requirements));
|
||||||
|
}
|
||||||
|
|
||||||
private void setManualStopReason(@Nullable String id, int manualStopReason) {
|
private void setManualStopReason(@Nullable String id, int manualStopReason) {
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
Download download = getDownload(id);
|
Download download = getDownload(id);
|
||||||
|
|
@ -483,6 +451,56 @@ public final class DownloadManager {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addDownloadInternal(DownloadAction action) {
|
||||||
|
dowloadUpdateQueue.add(
|
||||||
|
new DownloadUpdater(action.id) {
|
||||||
|
@Override
|
||||||
|
void onExisting(Download download) {
|
||||||
|
download.addAction(action);
|
||||||
|
logd("Action is added to existing download", download);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
DownloadState onLoad(@Nullable DownloadState downloadState) {
|
||||||
|
DownloadState state;
|
||||||
|
if (downloadState == null) {
|
||||||
|
state = new DownloadState(action);
|
||||||
|
logd("Download state is created for " + id);
|
||||||
|
} else {
|
||||||
|
state = downloadState.mergeAction(action);
|
||||||
|
logd("Download state is loaded for " + id);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (initialized) {
|
||||||
|
processDownloadUpdateQueue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeDownloadInternal(String id) {
|
||||||
|
dowloadUpdateQueue.add(
|
||||||
|
new DownloadUpdater(id) {
|
||||||
|
@Override
|
||||||
|
void onExisting(Download download) {
|
||||||
|
download.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
DownloadState onLoad(@Nullable DownloadState downloadState) {
|
||||||
|
if (downloadState != null) {
|
||||||
|
downloadState = downloadState.setRemoveState();
|
||||||
|
} else {
|
||||||
|
logd("Can't remove download. No download with id: " + id);
|
||||||
|
}
|
||||||
|
return downloadState;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (initialized) {
|
||||||
|
processDownloadUpdateQueue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void maybeNotifyListenersIdle() {
|
private void maybeNotifyListenersIdle() {
|
||||||
if (!isIdle()) {
|
if (!isIdle()) {
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue