2017-03-28 22:20:35 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2018-04-04 10:59:12 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Build;
|
2017-03-28 22:20:35 +00:00
|
|
|
import android.os.Environment;
|
2018-02-16 19:13:20 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2017-03-28 22:20:35 +00:00
|
|
|
|
2018-04-04 10:59:12 +00:00
|
|
|
import com.annimon.stream.Objects;
|
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
2017-03-28 22:20:35 +00:00
|
|
|
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
2018-05-24 16:57:16 +00:00
|
|
|
public class StorageUtil {
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2018-06-29 00:38:59 +00:00
|
|
|
public static File getBackupDirectory() throws NoExternalStorageException {
|
|
|
|
File storage = Environment.getExternalStorageDirectory();
|
2018-02-26 17:58:18 +00:00
|
|
|
|
|
|
|
if (!storage.canWrite()) {
|
|
|
|
throw new NoExternalStorageException();
|
|
|
|
}
|
|
|
|
|
|
|
|
File signal = new File(storage, "Signal");
|
|
|
|
File backups = new File(signal, "Backups");
|
|
|
|
|
|
|
|
if (!backups.exists()) {
|
|
|
|
if (!backups.mkdirs()) {
|
|
|
|
throw new NoExternalStorageException("Unable to create backup directory...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return backups;
|
|
|
|
}
|
|
|
|
|
2018-04-24 01:39:43 +00:00
|
|
|
public static File getBackupCacheDirectory(Context context) {
|
|
|
|
return context.getExternalCacheDir();
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:20:35 +00:00
|
|
|
private static File getSignalStorageDir() throws NoExternalStorageException {
|
|
|
|
final File storage = Environment.getExternalStorageDirectory();
|
|
|
|
|
|
|
|
if (!storage.canWrite()) {
|
|
|
|
throw new NoExternalStorageException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canWriteInSignalStorageDir() {
|
|
|
|
File storage;
|
|
|
|
|
|
|
|
try {
|
|
|
|
storage = getSignalStorageDir();
|
|
|
|
} catch (NoExternalStorageException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage.canWrite();
|
|
|
|
}
|
|
|
|
|
2018-02-26 17:58:18 +00:00
|
|
|
public static File getLegacyBackupDirectory() throws NoExternalStorageException {
|
2017-03-28 22:20:35 +00:00
|
|
|
return getSignalStorageDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static File getVideoDir() throws NoExternalStorageException {
|
|
|
|
return new File(getSignalStorageDir(), Environment.DIRECTORY_MOVIES);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static File getAudioDir() throws NoExternalStorageException {
|
|
|
|
return new File(getSignalStorageDir(), Environment.DIRECTORY_MUSIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static File getImageDir() throws NoExternalStorageException {
|
|
|
|
return new File(getSignalStorageDir(), Environment.DIRECTORY_PICTURES);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static File getDownloadDir() throws NoExternalStorageException {
|
|
|
|
return new File(getSignalStorageDir(), Environment.DIRECTORY_DOWNLOADS);
|
|
|
|
}
|
2018-02-16 19:13:20 +00:00
|
|
|
|
|
|
|
public static @Nullable String getCleanFileName(@Nullable String fileName) {
|
|
|
|
if (fileName == null) return null;
|
|
|
|
|
|
|
|
fileName = fileName.replace('\u202D', '\uFFFD');
|
|
|
|
fileName = fileName.replace('\u202E', '\uFFFD');
|
|
|
|
|
|
|
|
return fileName;
|
|
|
|
}
|
2017-03-28 22:20:35 +00:00
|
|
|
}
|