session-android/src/org/thoughtcrime/securesms/util/StorageUtil.java
Greyson Parrelli 18756aedf6 Remove the ability to save backups to the external SD card.
The directory we were previously saving backups to on the external SD
card is actually deleted upon app uninstall and/or clearing the app's
data. There's also no reliable way to write to the root of an external
SD card (that isn't comically inconvenient), so for now it's safer if we
just move back to getting the regular 'ol standard external storage
directory (which is likely internal storage, despite its name).

Fixes #7845
2018-06-29 14:10:45 -07:00

91 lines
2.4 KiB
Java

package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.Nullable;
import com.annimon.stream.Objects;
import com.annimon.stream.Stream;
import org.thoughtcrime.securesms.database.NoExternalStorageException;
import java.io.File;
public class StorageUtil {
public static File getBackupDirectory() throws NoExternalStorageException {
File storage = Environment.getExternalStorageDirectory();
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;
}
public static File getBackupCacheDirectory(Context context) {
return context.getExternalCacheDir();
}
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();
}
public static File getLegacyBackupDirectory() throws NoExternalStorageException {
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);
}
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;
}
}