Skip to content

Instantly share code, notes, and snippets.

@GrakovNe
Created June 30, 2018 15:12
Show Gist options
  • Select an option

  • Save GrakovNe/aedf3519a45ca09b3aa66877007250e3 to your computer and use it in GitHub Desktop.

Select an option

Save GrakovNe/aedf3519a45ca09b3aa66877007250e3 to your computer and use it in GitHub Desktop.
package org.grakovne.mds.android.service;
import android.content.Intent;
import android.util.Log;
import org.grakovne.mds.android.common.PayloadNames;
import org.grakovne.mds.android.common.Receiver;
import org.grakovne.mds.android.network.domain.responses.Story;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import static org.apache.commons.io.FileUtils.copyURLToFile;
import static org.grakovne.mds.android.MdsApplication.APP_TAG;
import static org.grakovne.mds.android.common.ActionNames.Cache;
import static org.grakovne.mds.android.manager.BroadcastManager.getPayloadObject;
import static org.grakovne.mds.android.manager.BroadcastManager.send;
import static org.grakovne.mds.android.utils.CommonUtils.ifDebugEnabled;
import static org.grakovne.mds.android.utils.NetworkUtils.findStreamUrl;
public class CacheService extends MdsService {
private Receiver receiver;
private File fileCache;
@Override
protected void subscribe() {
super.subscribe();
fileCache = getCacheDir();
receiver = new Receiver(this,
Cache.CACHE_REMOVED_STREAM_INTENT,
Cache.STREAM_IS_CACHED_INTENT,
Cache.CACHED_STREAM_INTENT,
Cache.FOUND_STREAM_INTENT) {
@Override
public void onAction(Intent intent, String action) {
switch (action) {
case Cache.CACHED_STREAM_INTENT:
createStream(getPayloadObject(intent, PayloadNames.STORY, Story.class));
break;
case Cache.CACHE_REMOVED_STREAM_INTENT:
removeStream(getPayloadObject(intent, PayloadNames.STORY, Story.class));
break;
case Cache.FOUND_STREAM_INTENT:
findStream(getPayloadObject(intent, PayloadNames.STORY, Story.class));
break;
case Cache.STREAM_IS_CACHED_INTENT:
isStreamCached(getPayloadObject(intent, PayloadNames.STORY, Story.class));
}
}
};
}
private void findStream(Story story) {
if (isStreamCached(story) && isStreamValid(story)) {
send(this, Cache.FOUND_STREAM_EVENT, buildStreamFile(story));
}
}
private void createStream(Story story) {
new Thread(() -> {
try {
copyURLToFile(new URL(findStreamUrl(story)), buildStreamFile(story));
send(this, Cache.CACHED_STREAM_EVENT, story);
} catch (IOException e) {
ifDebugEnabled(() -> Log.d(APP_TAG, "Can't fetch story stream"));
}
}).start();
}
private void removeStream(Story story) {
if (buildStreamFile(story).delete()) {
send(this, Cache.CACHE_REMOVED_STREAM_EVENT);
}
}
private File buildStreamFile(Story story) {
return new File(fileCache, story.getId() + ".mp3");
}
private Boolean isStreamCached(Story story) {
return isStreamExists(story) && isStreamValid(story);
}
private Boolean isStreamExists(Story story) {
return buildStreamFile(story).exists();
}
private Boolean isStreamValid(Story story) {
return story.getFileSize().equals(new File(fileCache, story.getId() + ".mp3").length());
}
@Override
protected void unsubscribe() {
receiver.unsubscribe();
super.unsubscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment