This commit is contained in:
Mariotaku Lee 2016-03-02 11:56:43 +08:00
parent d9f7ac3a84
commit b9122b7a93
4 changed files with 68 additions and 13 deletions

View File

@ -0,0 +1,24 @@
package org.mariotaku.twidere.task;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by mariotaku on 16/3/2.
*/
public class SaveFileTaskTest {
@Test
public void testGetFileNameWithExtension() throws Exception {
assertEquals("pbs_twimg_com_media_abcdefghijklmn.jpg",
SaveFileTask.getFileNameWithExtension("pbs_twimg_com_media_abcdefghijklmn_jpg",
"jpg", '_', null));
assertEquals("pbs_twimg_com_media_abcdefghijklmn_jpg",
SaveFileTask.getFileNameWithExtension("pbs_twimg_com_media_abcdefghijklmn_jpg",
null, '_', null));
assertEquals("pbs_twimg_com_media_abcdefghijklmn_jpgsuffix",
SaveFileTask.getFileNameWithExtension("pbs_twimg_com_media_abcdefghijklmn_jpg",
null, '_', "suffix"));
}
}

View File

@ -194,9 +194,13 @@ public class CacheProvider extends ContentProvider implements TwidereConstants {
@Nullable
@Override
public String getFilename(@NonNull Uri source) {
final String cacheKey = getCacheKey(source);
String cacheKey = getCacheKey(source);
if (cacheKey == null) return null;
return cacheKey.replaceAll("[^\\w\\d_]", "_");
final int indexOfSsp = cacheKey.indexOf("://");
if (indexOfSsp != -1) {
cacheKey = cacheKey.substring(indexOfSsp + 3);
}
return cacheKey.replaceAll("[^\\w\\d_]", String.valueOf(getSpecialCharacter()));
}
@Override
@ -215,6 +219,11 @@ public class CacheProvider extends ContentProvider implements TwidereConstants {
if (mimeType == null) return null;
return MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
}
@Override
public char getSpecialCharacter() {
return '_';
}
}
@StringDef({Type.IMAGE, Type.VIDEO, Type.JSON})

View File

@ -76,10 +76,13 @@ public abstract class SaveFileTask extends AsyncTask<Object, Object, SaveFileTas
final String mimeType = fileInfoCallback.getMimeType(source);
final String extension = fileInfoCallback.getExtension(mimeType);
if (!destinationDir.isDirectory() && !destinationDir.mkdirs()) return null;
String nameToSave = getFileNameWithExtension(name, extension);
String nameToSave = getFileNameWithExtension(name, extension,
fileInfoCallback.getSpecialCharacter(), null);
File saveFile = new File(destinationDir, nameToSave);
if (saveFile.exists()) {
nameToSave = getFileNameWithExtension(name + System.currentTimeMillis(), extension);
nameToSave = getFileNameWithExtension(name, extension,
fileInfoCallback.getSpecialCharacter(),
String.valueOf(System.currentTimeMillis()));
saveFile = new File(destinationDir, nameToSave);
}
final InputStream in = cr.openInputStream(source);
@ -138,11 +141,30 @@ public abstract class SaveFileTask extends AsyncTask<Object, Object, SaveFileTas
return contextRef.get();
}
private static String getFileNameWithExtension(String name, @Nullable String extension) {
if (extension == null) return name;
int lastDotIdx = name.lastIndexOf('.');
if (lastDotIdx < 0) return name + "." + extension;
return name.substring(0, lastDotIdx) + "." + extension;
@NonNull
static String getFileNameWithExtension(@NonNull String name, @Nullable String extension,
char specialCharacter, @Nullable String suffix) {
StringBuilder sb = new StringBuilder();
int end = name.length();
if (extension != null) {
if (name.endsWith(extension)) {
for (int i = end - extension.length() - 1; i >= 0; i--) {
if (name.charAt(i) != specialCharacter) {
end = i + 1;
break;
}
}
}
}
sb.append(name, 0, end);
if (suffix != null) {
sb.append(suffix);
}
if (extension != null) {
sb.append('.');
sb.append(extension);
}
return sb.toString();
}
public interface FileInfoCallback {
@ -154,6 +176,8 @@ public abstract class SaveFileTask extends AsyncTask<Object, Object, SaveFileTas
@Nullable
String getExtension(@Nullable String mimeType);
char getSpecialCharacter();
}
public static final class SaveFileResult {

View File

@ -514,11 +514,9 @@ public class ThemeUtils implements Constants {
return textAppearance;
}
public static int getUserAccentColor(final Context context) {
if (context == null) return Color.TRANSPARENT;
public static int getUserAccentColor(@NonNull final Context context) {
final SharedPreferencesWrapper pref = getSharedPreferencesWrapper(context);
final Resources res = context.getResources();
final int def = res.getColor(R.color.branding_color);
final int def = ContextCompat.getColor(context, R.color.branding_color);
return pref.getInt(KEY_THEME_COLOR, def);
}