diff --git a/app/src/main/java/fi/aalto/legroup/achso/app/App.java b/app/src/main/java/fi/aalto/legroup/achso/app/App.java index e33f859b..67e13469 100644 --- a/app/src/main/java/fi/aalto/legroup/achso/app/App.java +++ b/app/src/main/java/fi/aalto/legroup/achso/app/App.java @@ -99,6 +99,17 @@ public void onCreate() { File cacheVideoDirectory = new File(localStorageDirectory, "cache"); cacheVideoDirectory.mkdirs(); + if (!cacheVideoDirectory.isDirectory()) { + // If the cache directory can't be created use internal App storage + // There is a slim chance that the user saves some modified videos to the internal + // storage and then inserts and SD card causing videos not to be uploaded, but it + // doesn't seem worth the trouble. + + File internalDataDirectory = new File(getApplicationInfo().dataDir); + cacheVideoDirectory = new File(internalDataDirectory, "manifestcache"); + cacheVideoDirectory.mkdirs(); + } + CombinedVideoRepository combinedRepository = new CombinedVideoRepository(bus, jsonSerializer, localStorageDirectory, cacheVideoDirectory); diff --git a/app/src/main/java/fi/aalto/legroup/achso/storage/CombinedVideoRepository.java b/app/src/main/java/fi/aalto/legroup/achso/storage/CombinedVideoRepository.java index ac332e39..6e22a6f5 100644 --- a/app/src/main/java/fi/aalto/legroup/achso/storage/CombinedVideoRepository.java +++ b/app/src/main/java/fi/aalto/legroup/achso/storage/CombinedVideoRepository.java @@ -63,6 +63,10 @@ public CombinedVideoRepository(Bus bus, JsonSerializer serializer, File localRoo private List getCacheIds() { String[] entries = cacheRoot.list(); + if (entries == null) { + return Collections.emptyList(); + } + ArrayList results = new ArrayList<>(entries.length); for (String entry : entries) { @@ -127,6 +131,15 @@ protected void updateVideos(List videos, List groups) { bus.post(new VideoRepositoryUpdatedEvent(this)); } + private File[] safeListFiles(File root, FilenameFilter filter) { + File[] files = root.listFiles(filter); + if (files != null) { + return files; + } else { + return new File[0]; + } + } + /** * Add a host to the repository to sync to. */ @@ -170,7 +183,7 @@ public void refreshOffline() { List videos = new ArrayList<>(); // Add the local videos - File[] localFiles = localRoot.listFiles(new ManifestFileFilter()); + File[] localFiles = safeListFiles(localRoot, new ManifestFileFilter()); for (File file : localFiles) { OptimizedVideo video = tryLoadOrReUseVideo(file, getIdFromFile(file)); @@ -246,7 +259,7 @@ public void refreshOnline() { // Add the local videos // TODO: These can be cached - File[] localFiles = localRoot.listFiles(new ManifestFileFilter()); + File[] localFiles = safeListFiles(localRoot, new ManifestFileFilter()); for (File file : localFiles) { OptimizedVideo localVideo = tryLoadOrReUseVideo(file, getIdFromFile(file));