Move attachment methods into preferences

pull/281/head
Alex Baker 10 years ago
parent 46d31fe81d
commit 6cb6e63a36

@ -24,6 +24,7 @@ import org.tasks.R;
import org.tasks.preferences.Preferences; import org.tasks.preferences.Preferences;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,9 +32,6 @@ import java.util.concurrent.atomic.AtomicReference;
import javax.inject.Inject; import javax.inject.Inject;
import static com.todoroo.astrid.files.FileUtilities.getNewAttachmentPath;
import static org.tasks.files.FileHelper.copyFile;
public class ActFmCameraModule { public class ActFmCameraModule {
private static final Logger log = LoggerFactory.getLogger(ActFmCameraModule.class); private static final Logger log = LoggerFactory.getLogger(ActFmCameraModule.class);
@ -111,7 +109,7 @@ public class ActFmCameraModule {
extension = "." + extension; extension = "." + extension;
} }
try { try {
String path = getNewAttachmentPath(preferences, fragment.getActivity(), extension, nameRef); String path = preferences.getNewAttachmentPath(extension, nameRef);
File file = new File(path); File file = new File(path);
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
if (!file.createNewFile()) { if (!file.createNewFile()) {
@ -125,7 +123,7 @@ public class ActFmCameraModule {
} }
public interface CameraResultCallback { public interface CameraResultCallback {
public void handleCameraResult(Uri uri); void handleCameraResult(Uri uri);
} }
public boolean activityResult(int requestCode, int resultCode, Intent data, public boolean activityResult(int requestCode, int resultCode, Intent data,
@ -157,4 +155,14 @@ public class ActFmCameraModule {
} }
return false; return false;
} }
private static void copyFile(InputStream inputStream, String to) throws IOException {
FileOutputStream fos = new FileOutputStream(to);
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
}
} }

@ -51,7 +51,6 @@ import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskAttachment; import com.todoroo.astrid.data.TaskAttachment;
import com.todoroo.astrid.files.AACRecordingActivity; import com.todoroo.astrid.files.AACRecordingActivity;
import com.todoroo.astrid.files.FileExplore; import com.todoroo.astrid.files.FileExplore;
import com.todoroo.astrid.files.FileUtilities;
import com.todoroo.astrid.files.FilesControlSet; import com.todoroo.astrid.files.FilesControlSet;
import com.todoroo.astrid.gcal.GCalControlSet; import com.todoroo.astrid.gcal.GCalControlSet;
import com.todoroo.astrid.gcal.GCalHelper; import com.todoroo.astrid.gcal.GCalHelper;
@ -776,7 +775,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
return; return;
} }
File dst = new File(FileUtilities.getAttachmentsDirectory(preferences, getActivity()) + File.separator + src.getName()); File dst = new File(preferences.getAttachmentsDirectory() + File.separator + src.getName());
try { try {
AndroidUtilities.copyFile(src, dst); AndroidUtilities.copyFile(src, dst);
} catch (Exception e) { } catch (Exception e) {

@ -43,7 +43,7 @@ public class AACRecordingActivity extends InjectingActivity implements AACRecord
setupUi(); setupUi();
nameRef = new AtomicReference<>(); nameRef = new AtomicReference<>();
tempFile = FileUtilities.getNewAudioAttachmentPath(preferences, this, nameRef); tempFile = preferences.getNewAudioAttachmentPath(nameRef);
recorder = new AACRecorder(); recorder = new AACRecorder();
recorder.setListener(this); recorder.setListener(this);

@ -1,65 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.files;
import android.content.Context;
import android.text.TextUtils;
import com.todoroo.astrid.data.TaskAttachment;
import org.joda.time.DateTime;
import org.tasks.files.FileHelper;
import org.tasks.preferences.Preferences;
import java.io.File;
import java.util.concurrent.atomic.AtomicReference;
public class FileUtilities {
public static String getNewAudioAttachmentPath(Preferences preferences, Context context, AtomicReference<String> nameReference) {
return getNewAttachmentPath(preferences, context, ".m4a", nameReference); //$NON-NLS-1$
}
public static String getNewAttachmentPath(Preferences preferences, Context context, String extension, AtomicReference<String> nameReference) {
String dir = getAttachmentsDirectory(preferences, context).getAbsolutePath();
String name = getNonCollidingFileName(dir, new DateTime().toString("yyyyMMddHHmm"), extension);
if (nameReference != null) {
nameReference.set(name);
}
return dir + File.separator + name;
}
public static File getAttachmentsDirectory(Preferences preferences, Context context) {
File directory = null;
String customDir = preferences.getStringValue(TaskAttachment.FILES_DIRECTORY_PREF);
if (!TextUtils.isEmpty(customDir)) {
directory = new File(customDir);
}
if (directory == null || !directory.exists()) {
directory = FileHelper.getExternalFilesDir(context, TaskAttachment.FILES_DIRECTORY_DEFAULT);
}
return directory;
}
private static String getNonCollidingFileName(String dir, String baseName, String extension) {
int tries = 1;
File f = new File(dir + File.separator + baseName + extension);
String tempName = baseName;
while (f.exists()) {
tempName = baseName + "-" + tries; //$NON-NLS-1$
f = new File(dir + File.separator + tempName + extension);
tries++;
}
return tempName + extension;
}
}

@ -1,30 +1,11 @@
package org.tasks.files; package org.tasks.files;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore; import android.provider.MediaStore;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileHelper { public class FileHelper {
public static File getExternalFilesDir(Context context, String type) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String directory = String.format("%s/Android/data/%s/files/%s", Environment.getExternalStorageDirectory(), context.getPackageName(), type);
File file = new File(directory);
if (file.isDirectory() || file.mkdirs()) {
return file;
}
}
return null;
}
public static String getPathFromUri(Activity activity, Uri uri) { public static String getPathFromUri(Activity activity, Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA}; String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = activity.managedQuery(uri, projection, null, null, null); Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
@ -38,14 +19,4 @@ public class FileHelper {
return uri.getPath(); return uri.getPath();
} }
} }
public static void copyFile(InputStream inputStream, String to) throws IOException {
FileOutputStream fos = new FileOutputStream(to);
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
}
} }

@ -5,18 +5,24 @@ import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Environment; import android.os.Environment;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils;
import com.todoroo.astrid.activity.BeastModePreferences; import com.todoroo.astrid.activity.BeastModePreferences;
import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.core.SortHelper; import com.todoroo.astrid.core.SortHelper;
import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskAttachment;
import com.todoroo.astrid.widget.WidgetConfigActivity; import com.todoroo.astrid.widget.WidgetConfigActivity;
import org.joda.time.DateTime;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.tasks.R; import org.tasks.R;
import org.tasks.injection.ForApplication; import org.tasks.injection.ForApplication;
import java.io.File;
import java.util.concurrent.atomic.AtomicReference;
import javax.inject.Inject; import javax.inject.Inject;
import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Level;
@ -283,4 +289,58 @@ public class Preferences {
public boolean useNotificationActions() { public boolean useNotificationActions() {
return atLeastJellybean() && getBoolean(R.string.p_rmd_notif_actions_enabled, true); return atLeastJellybean() && getBoolean(R.string.p_rmd_notif_actions_enabled, true);
} }
public File getAttachmentsDirectory() {
File directory = null;
String customDir = getStringValue(TaskAttachment.FILES_DIRECTORY_PREF);
if (!TextUtils.isEmpty(customDir)) {
directory = new File(customDir);
}
if (directory == null || !directory.exists()) {
directory = getExternalFilesDir(TaskAttachment.FILES_DIRECTORY_DEFAULT);
}
return directory;
}
private File getExternalFilesDir(String type) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String directory = String.format("%s/Android/data/%s/files/%s", Environment.getExternalStorageDirectory(), context.getPackageName(), type);
File file = new File(directory);
if (file.isDirectory() || file.mkdirs()) {
return file;
}
}
return null;
}
public String getNewAudioAttachmentPath(AtomicReference<String> nameReference) {
return getNewAttachmentPath(".m4a", nameReference); //$NON-NLS-1$
}
public String getNewAttachmentPath(String extension, AtomicReference<String> nameReference) {
String dir = getAttachmentsDirectory().getAbsolutePath();
String name = getNonCollidingFileName(dir, new DateTime().toString("yyyyMMddHHmm"), extension);
if (nameReference != null) {
nameReference.set(name);
}
return dir + File.separator + name;
}
private static String getNonCollidingFileName(String dir, String baseName, String extension) {
int tries = 1;
File f = new File(dir + File.separator + baseName + extension);
String tempName = baseName;
while (f.exists()) {
tempName = baseName + "-" + tries; //$NON-NLS-1$
f = new File(dir + File.separator + tempName + extension);
tries++;
}
return tempName + extension;
}
} }

Loading…
Cancel
Save