Simplification

pull/214/head
M66B 11 months ago
parent 8482338f2b
commit 125a47efd8

@ -566,7 +566,7 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
if (TextUtils.isEmpty(fname)) if (TextUtils.isEmpty(fname))
return uri; return uri;
File dir = Helper.ensureExists(new File(getFilesDir(), "shared")); File dir = Helper.ensureExists(getFilesDir(), "shared");
File file = new File(dir, fname); File file = new File(dir, fname);
Log.i("Copying shared file to " + file); Log.i("Copying shared file to " + file);

@ -183,7 +183,7 @@ public class ActivityEML extends ActivityBase {
if (uri == null) if (uri == null)
throw new FileNotFoundException(); throw new FileNotFoundException();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "shared")); File dir = Helper.ensureExists(context.getFilesDir(), "shared");
File file = new File(dir, "email.eml"); File file = new File(dir, "email.eml");
Helper.copy(context, uri, file); Helper.copy(context, uri, file);

@ -132,7 +132,7 @@ public class AdapterCertificate extends RecyclerView.Adapter<AdapterCertificate.
if (certificate == null) if (certificate == null)
return null; return null;
File dir = Helper.ensureExists(new File(context.getFilesDir(), "shared")); File dir = Helper.ensureExists(context.getFilesDir(), "shared");
String name = Helper.sanitizeFilename(certificate.email); String name = Helper.sanitizeFilename(certificate.email);
File file = new File(dir, name + ".pem"); File file = new File(dir, name + ".pem");
Helper.writeText(file, certificate.getPem()); Helper.writeText(file, certificate.getPem());

@ -4105,7 +4105,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
response.setMethod(Method.REPLY); response.setMethod(Method.REPLY);
response.addEvent(ev); response.addEvent(ev);
File dir = Helper.ensureExists(new File(context.getFilesDir(), "calendar")); File dir = Helper.ensureExists(context.getFilesDir(), "calendar");
File ics = new File(dir, message.id + ".ics"); File ics = new File(dir, message.id + ".ics");
response.write(ics); response.write(ics);
@ -6389,7 +6389,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
File source = EntityMessage.getFile(context, id); File source = EntityMessage.getFile(context, id);
File dir = Helper.ensureExists(new File(context.getFilesDir(), "shared")); File dir = Helper.ensureExists(context.getFilesDir(), "shared");
File target = new File(dir, id + ".html"); File target = new File(dir, id + ".html");
Helper.copy(source, target); Helper.copy(source, target);

@ -186,7 +186,7 @@ public class CloudSync {
private static Long updateSyncdata(Context context) throws IOException, JSONException { private static Long updateSyncdata(Context context) throws IOException, JSONException {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
File dir = Helper.ensureExists(new File(context.getFilesDir(), "syncdata")); File dir = Helper.ensureExists(context.getFilesDir(), "syncdata");
Long last = null; Long last = null;
@ -352,7 +352,7 @@ public class CloudSync {
private static void receiveRemoteData(Context context, String user, String password, long lrevision, long rrevision, JSONObject jstatus) private static void receiveRemoteData(Context context, String user, String password, long lrevision, long rrevision, JSONObject jstatus)
throws JSONException, GeneralSecurityException, IOException, InvalidCipherTextException { throws JSONException, GeneralSecurityException, IOException, InvalidCipherTextException {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
File dir = Helper.ensureExists(new File(context.getFilesDir(), "syncdata")); File dir = Helper.ensureExists(context.getFilesDir(), "syncdata");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean cloud_receive = prefs.getBoolean("cloud_receive", false); boolean cloud_receive = prefs.getBoolean("cloud_receive", false);

@ -357,7 +357,7 @@ public class ContactInfo {
final String domain = d.toLowerCase(Locale.ROOT); final String domain = d.toLowerCase(Locale.ROOT);
File dir = Helper.ensureExists(new File(context.getFilesDir(), "favicons")); File dir = Helper.ensureExists(context.getFilesDir(), "favicons");
try { try {
// check cache // check cache
@ -548,7 +548,7 @@ public class ContactInfo {
String tag = (TextUtils.isEmpty(info.email) ? name : info.email); String tag = (TextUtils.isEmpty(info.email) ? name : info.email);
String etag = (TextUtils.isEmpty(info.email) ? Helper.sanitizeFilename(name + "@name") : ekey); String etag = (TextUtils.isEmpty(info.email) ? Helper.sanitizeFilename(name + "@name") : ekey);
if (info.bitmap == null && generated && !TextUtils.isEmpty(tag)) { if (info.bitmap == null && generated && !TextUtils.isEmpty(tag)) {
File dir = Helper.ensureExists(new File(context.getFilesDir(), "generated")); File dir = Helper.ensureExists(context.getFilesDir(), "generated");
File[] files = dir.listFiles(new FilenameFilter() { File[] files = dir.listFiles(new FilenameFilter() {
@Override @Override
public boolean accept(File file, String name) { public boolean accept(File file, String name) {

@ -172,7 +172,7 @@ public class EntityAttachment {
} }
static File getFile(Context context, long id, String name) { static File getFile(Context context, long id, String name) {
File dir = Helper.ensureExists(new File(context.getFilesDir(), "attachments")); File dir = Helper.ensureExists(context.getFilesDir(), "attachments");
String filename = Long.toString(id); String filename = Long.toString(id);
if (!TextUtils.isEmpty(name)) if (!TextUtils.isEmpty(name))
filename += "." + Helper.sanitizeFilename(name); filename += "." + Helper.sanitizeFilename(name);

@ -619,8 +619,8 @@ public class EntityMessage implements Serializable {
} }
static File getFile(Context context, Long id) { static File getFile(Context context, Long id) {
File root = Helper.ensureExists(new File(context.getFilesDir(), "messages")); File root = Helper.ensureExists(context.getFilesDir(), "messages");
File dir = Helper.ensureExists(new File(root, "D" + (id / 1000))); File dir = Helper.ensureExists(root, "D" + (id / 1000));
return new File(dir, id.toString()); return new File(dir, id.toString());
} }
@ -644,12 +644,12 @@ public class EntityMessage implements Serializable {
} }
File getFile(Context context, int revision) { File getFile(Context context, int revision) {
File dir = Helper.ensureExists(new File(context.getFilesDir(), "revision")); File dir = Helper.ensureExists(context.getFilesDir(), "revision");
return new File(dir, id + "." + revision); return new File(dir, id + "." + revision);
} }
File getRefFile(Context context) { File getRefFile(Context context) {
File dir = Helper.ensureExists(new File(context.getFilesDir(), "references")); File dir = Helper.ensureExists(context.getFilesDir(), "references");
return new File(dir, id.toString()); return new File(dir, id.toString());
} }
@ -658,7 +658,7 @@ public class EntityMessage implements Serializable {
} }
static File getRawFile(Context context, Long id) { static File getRawFile(Context context, Long id) {
File dir = Helper.ensureExists(new File(context.getFilesDir(), "raw")); File dir = Helper.ensureExists(context.getFilesDir(), "raw");
return new File(dir, id + ".eml"); return new File(dir, id + ".eml");
} }

@ -3562,7 +3562,7 @@ public class FragmentCompose extends FragmentBase {
}); });
snackbar.show(); snackbar.show();
} else { } else {
File dir = Helper.ensureExists(new File(context.getFilesDir(), "photo")); File dir = Helper.ensureExists(context.getFilesDir(), "photo");
File file = new File(dir, working + "_" + new Date().getTime() + ".jpg"); File file = new File(dir, working + "_" + new Date().getTime() + ".jpg");
try { try {
photoURI = FileProviderEx.getUri(context, BuildConfig.APPLICATION_ID, file); photoURI = FileProviderEx.getUri(context, BuildConfig.APPLICATION_ID, file);
@ -3874,7 +3874,7 @@ public class FragmentCompose extends FragmentBase {
throw new IllegalArgumentException(context.getString(R.string.title_from_missing)); throw new IllegalArgumentException(context.getString(R.string.title_from_missing));
// Create files // Create files
File tmp = Helper.ensureExists(new File(context.getFilesDir(), "encryption")); File tmp = Helper.ensureExists(context.getFilesDir(), "encryption");
File input = new File(tmp, draft.id + "_" + session + ".pgp_input"); File input = new File(tmp, draft.id + "_" + session + ".pgp_input");
File output = new File(tmp, draft.id + "_" + session + ".pgp_output"); File output = new File(tmp, draft.id + "_" + session + ".pgp_output");
@ -4242,7 +4242,7 @@ public class FragmentCompose extends FragmentBase {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean check_certificate = prefs.getBoolean("check_certificate", true); boolean check_certificate = prefs.getBoolean("check_certificate", true);
File tmp = Helper.ensureExists(new File(context.getFilesDir(), "encryption")); File tmp = Helper.ensureExists(context.getFilesDir(), "encryption");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);

@ -8828,7 +8828,7 @@ public class FragmentMessages extends FragmentBase
OutputStream out = null; OutputStream out = null;
boolean inline = false; boolean inline = false;
File tmp = Helper.ensureExists(new File(context.getFilesDir(), "encryption")); File tmp = Helper.ensureExists(context.getFilesDir(), "encryption");
File plain = new File(tmp, message.id + ".pgp_out"); File plain = new File(tmp, message.id + ".pgp_out");
// Find encrypted data // Find encrypted data

@ -1839,7 +1839,7 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere
.remove("cloud_last_sync") .remove("cloud_last_sync")
.apply(); .apply();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "syncdata")); File dir = Helper.ensureExists(context.getFilesDir(), "syncdata");
File[] files = dir.listFiles(); File[] files = dir.listFiles();
if (files != null) if (files != null)
for (File file : files) { for (File file : files) {

@ -2641,17 +2641,18 @@ public class Helper {
private static final Map<File, Boolean> exists = new HashMap<>(); private static final Map<File, Boolean> exists = new HashMap<>();
static File ensureExists(File dir) { static File ensureExists(File parent, String subdir) {
parent.mkdir();
File dir = new File(parent, subdir);
dir.mkdir();
synchronized (exists) { synchronized (exists) {
if (exists.containsKey(dir)) if (exists.containsKey(dir))
return dir; return dir;
exists.put(dir, true); exists.put(dir, true);
} }
// CASA: External storage as well
if (!dir.exists() && Log.jni_safe_mkdirs(dir))
throw new IllegalArgumentException("Failed to create directory");
return dir; return dir;
} }

@ -791,7 +791,7 @@ class ImageHelper {
@NonNull @NonNull
static File getCacheFile(Context context, long id, String source, String extension) { static File getCacheFile(Context context, long id, String source, String extension) {
File dir = Helper.ensureExists(new File(context.getFilesDir(), "images")); File dir = Helper.ensureExists(context.getFilesDir(), "images");
return new File(dir, id + "_" + Math.abs(source.hashCode()) + extension); return new File(dir, id + "_" + Math.abs(source.hashCode()) + extension);
} }

Loading…
Cancel
Save