Move away from deprecated Environment external file access API.
parent
5e86b253a8
commit
8ebdbf2af7
@ -0,0 +1,66 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ExternalStorageUtil {
|
||||
|
||||
public static final String DIRECTORY_BACKUPS = "Backups";
|
||||
|
||||
/** @see Context#getExternalFilesDir(String) */
|
||||
@NonNull
|
||||
public static File getDir(Context context, @Nullable String type) throws NoExternalStorageException {
|
||||
final File dir = context.getExternalFilesDir(type);
|
||||
if (dir == null) {
|
||||
throw new NoExternalStorageException("External storage dir is currently unavailable: " + type);
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static File getBackupDir(Context context) throws NoExternalStorageException {
|
||||
return getDir(context, DIRECTORY_BACKUPS);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static File getVideoDir(Context context) throws NoExternalStorageException {
|
||||
return getDir(context, Environment.DIRECTORY_MOVIES);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static File getAudioDir(Context context) throws NoExternalStorageException {
|
||||
return getDir(context, Environment.DIRECTORY_MUSIC);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static File getImageDir(Context context) throws NoExternalStorageException {
|
||||
return getDir(context, Environment.DIRECTORY_PICTURES);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static File getDownloadDir(Context context) throws NoExternalStorageException {
|
||||
return getDir(context, Environment.DIRECTORY_DOWNLOADS);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getCacheDir(Context context) {
|
||||
return context.getExternalCacheDir();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getCleanFileName(@Nullable String fileName) {
|
||||
if (fileName == null) return null;
|
||||
|
||||
fileName = fileName.replace('\u202D', '\uFFFD');
|
||||
fileName = fileName.replace('\u202E', '\uFFFD');
|
||||
|
||||
return fileName;
|
||||
}
|
||||
}
|
@ -1,18 +1,16 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import androidx.annotation.NonNull;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
import java.util.List;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
public class IntentUtils {
|
||||
|
||||
public static boolean isResolvable(@NonNull Context context, @NonNull Intent intent) {
|
||||
List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(intent, 0);
|
||||
return resolveInfoList != null && resolveInfoList.size() > 1;
|
||||
return context.getPackageManager()
|
||||
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,86 +0,0 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
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 session = new File(storage, "Session");
|
||||
File backups = new File(session, "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 getSessionStorageDir() throws NoExternalStorageException {
|
||||
final File storage = Environment.getExternalStorageDirectory();
|
||||
|
||||
if (!storage.canWrite()) {
|
||||
throw new NoExternalStorageException();
|
||||
}
|
||||
|
||||
return storage;
|
||||
}
|
||||
|
||||
public static boolean canWriteInSessionStorageDir() {
|
||||
File storage;
|
||||
|
||||
try {
|
||||
storage = getSessionStorageDir();
|
||||
} catch (NoExternalStorageException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return storage.canWrite();
|
||||
}
|
||||
|
||||
public static File getLegacyBackupDirectory() throws NoExternalStorageException {
|
||||
return getSessionStorageDir();
|
||||
}
|
||||
|
||||
public static File getVideoDir() throws NoExternalStorageException {
|
||||
return new File(getSessionStorageDir(), Environment.DIRECTORY_MOVIES);
|
||||
}
|
||||
|
||||
public static File getAudioDir() throws NoExternalStorageException {
|
||||
return new File(getSessionStorageDir(), Environment.DIRECTORY_MUSIC);
|
||||
}
|
||||
|
||||
public static File getImageDir() throws NoExternalStorageException {
|
||||
return new File(getSessionStorageDir(), Environment.DIRECTORY_PICTURES);
|
||||
}
|
||||
|
||||
public static File getDownloadDir() throws NoExternalStorageException {
|
||||
return new File(getSessionStorageDir(), 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue