From b6205b6d32c565735dbff609f2ec0b3e21c299c4 Mon Sep 17 00:00:00 2001 From: bqqbarbhg Date: Mon, 2 Nov 2015 10:37:40 +0200 Subject: [PATCH 1/2] Don't crash if no SD card --- .../achso/storage/CombinedVideoRepository.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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)); From 8ba4c8b8ccc33334a2be36a5937c98c69943e918 Mon Sep 17 00:00:00 2001 From: bqqbarbhg Date: Mon, 2 Nov 2015 11:01:56 +0200 Subject: [PATCH 2/2] Online videos work if no SD card Create the cache directory to internal memory --- app/src/main/java/fi/aalto/legroup/achso/app/App.java | 11 +++++++++++ 1 file changed, 11 insertions(+) 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);