Created FileNameGenerator class

This commit is contained in:
daniel oeh 2012-10-31 16:56:23 +01:00
parent 8c541fb82d
commit 1c0874dabe
3 changed files with 44 additions and 26 deletions

View File

@ -14,7 +14,7 @@ import de.danoeh.antennapod.feed.FeedFile;
import de.danoeh.antennapod.feed.FeedImage;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.service.download.DownloadService;
import de.danoeh.antennapod.util.NumberGenerator;
import de.danoeh.antennapod.util.FileNameGenerator;
import de.danoeh.antennapod.util.URLChecker;
public class DownloadRequester {
@ -94,7 +94,8 @@ public class DownloadRequester {
}
}
public void downloadMedia(Context context, FeedMedia feedmedia) throws DownloadRequestException {
public void downloadMedia(Context context, FeedMedia feedmedia)
throws DownloadRequestException {
if (feedFileValid(feedmedia)) {
download(context, feedmedia,
new File(getMediafilePath(context, feedmedia),
@ -199,7 +200,8 @@ public class DownloadRequester {
}
public String getFeedfileName(Feed feed) {
return "feed-" + NumberGenerator.generateLong(feed.getDownload_url());
return "feed-"
+ FileNameGenerator.generateFileName(feed.getDownload_url());
}
public String getImagefilePath(Context context)
@ -209,7 +211,8 @@ public class DownloadRequester {
}
public String getImagefileName(FeedImage image) {
return "image-" + NumberGenerator.generateLong(image.getDownload_url());
return "image-"
+ FileNameGenerator.generateFileName(image.getDownload_url());
}
public String getMediafilePath(Context context, FeedMedia media)
@ -217,7 +220,7 @@ public class DownloadRequester {
File externalStorage = getExternalFilesDirOrThrowException(
context,
MEDIA_DOWNLOADPATH
+ NumberGenerator.generateLong(media.getItem()
+ FileNameGenerator.generateFileName(media.getItem()
.getFeed().getTitle()) + "/");
return externalStorage.toString();
}

View File

@ -0,0 +1,36 @@
package de.danoeh.antennapod.util;
import java.util.Arrays;
/** Generates valid filenames for a given string. */
public class FileNameGenerator {
private static final char[] ILLEGAL_CHARACTERS = { '/', '\\', '?', '%',
'*', ':', '|', '"', '<', '>' };
static {
Arrays.sort(ILLEGAL_CHARACTERS);
}
private FileNameGenerator() {
}
/**
* This method will return a new string that doesn't contain any illegal
* characters of the given string.
*/
public static String generateFileName(String string) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
if (Arrays.binarySearch(ILLEGAL_CHARACTERS, c) < 0) {
builder.append(c);
}
}
return builder.toString();
}
public static long generateLong(final String str) {
return str.hashCode();
}
}

View File

@ -1,21 +0,0 @@
package de.danoeh.antennapod.util;
/**Utility class for creating numbers.*/
public final class NumberGenerator {
/** Class shall not be instantiated.*/
private NumberGenerator() {
}
/**Logging tag.*/
private static final String TAG = "NumberGenerator";
/** Takes a string and generates a random value out of
* the hash-value of that string.
* @param strSeed The string to take for the return value
* @return The generated random value
* */
public static long generateLong(final String strSeed) {
return strSeed.hashCode();
}
}