From 6cb6e63a367d0248d285944149199569d62d29ae Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 10 Apr 2015 13:24:02 -0500 Subject: [PATCH] Move attachment methods into preferences --- .../astrid/actfm/ActFmCameraModule.java | 18 +++-- .../astrid/activity/TaskEditFragment.java | 3 +- .../astrid/files/AACRecordingActivity.java | 2 +- .../todoroo/astrid/files/FileUtilities.java | 65 ------------------- src/main/java/org/tasks/files/FileHelper.java | 29 --------- .../org/tasks/preferences/Preferences.java | 60 +++++++++++++++++ 6 files changed, 75 insertions(+), 102 deletions(-) delete mode 100644 src/main/java/com/todoroo/astrid/files/FileUtilities.java diff --git a/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java b/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java index 068700413..cf3366be5 100644 --- a/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java +++ b/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java @@ -24,6 +24,7 @@ import org.tasks.R; import org.tasks.preferences.Preferences; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -31,9 +32,6 @@ import java.util.concurrent.atomic.AtomicReference; import javax.inject.Inject; -import static com.todoroo.astrid.files.FileUtilities.getNewAttachmentPath; -import static org.tasks.files.FileHelper.copyFile; - public class ActFmCameraModule { private static final Logger log = LoggerFactory.getLogger(ActFmCameraModule.class); @@ -111,7 +109,7 @@ public class ActFmCameraModule { extension = "." + extension; } try { - String path = getNewAttachmentPath(preferences, fragment.getActivity(), extension, nameRef); + String path = preferences.getNewAttachmentPath(extension, nameRef); File file = new File(path); file.getParentFile().mkdirs(); if (!file.createNewFile()) { @@ -125,7 +123,7 @@ public class ActFmCameraModule { } public interface CameraResultCallback { - public void handleCameraResult(Uri uri); + void handleCameraResult(Uri uri); } public boolean activityResult(int requestCode, int resultCode, Intent data, @@ -157,4 +155,14 @@ public class ActFmCameraModule { } 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(); + } } diff --git a/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java b/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java index c7a0e5300..f5482a333 100755 --- a/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java +++ b/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java @@ -51,7 +51,6 @@ import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.TaskAttachment; import com.todoroo.astrid.files.AACRecordingActivity; import com.todoroo.astrid.files.FileExplore; -import com.todoroo.astrid.files.FileUtilities; import com.todoroo.astrid.files.FilesControlSet; import com.todoroo.astrid.gcal.GCalControlSet; import com.todoroo.astrid.gcal.GCalHelper; @@ -776,7 +775,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { return; } - File dst = new File(FileUtilities.getAttachmentsDirectory(preferences, getActivity()) + File.separator + src.getName()); + File dst = new File(preferences.getAttachmentsDirectory() + File.separator + src.getName()); try { AndroidUtilities.copyFile(src, dst); } catch (Exception e) { diff --git a/src/main/java/com/todoroo/astrid/files/AACRecordingActivity.java b/src/main/java/com/todoroo/astrid/files/AACRecordingActivity.java index b82011455..90b0e7c1b 100644 --- a/src/main/java/com/todoroo/astrid/files/AACRecordingActivity.java +++ b/src/main/java/com/todoroo/astrid/files/AACRecordingActivity.java @@ -43,7 +43,7 @@ public class AACRecordingActivity extends InjectingActivity implements AACRecord setupUi(); nameRef = new AtomicReference<>(); - tempFile = FileUtilities.getNewAudioAttachmentPath(preferences, this, nameRef); + tempFile = preferences.getNewAudioAttachmentPath(nameRef); recorder = new AACRecorder(); recorder.setListener(this); diff --git a/src/main/java/com/todoroo/astrid/files/FileUtilities.java b/src/main/java/com/todoroo/astrid/files/FileUtilities.java deleted file mode 100644 index 90ee5d011..000000000 --- a/src/main/java/com/todoroo/astrid/files/FileUtilities.java +++ /dev/null @@ -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 nameReference) { - return getNewAttachmentPath(preferences, context, ".m4a", nameReference); //$NON-NLS-1$ - } - - public static String getNewAttachmentPath(Preferences preferences, Context context, String extension, AtomicReference 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; - } - -} diff --git a/src/main/java/org/tasks/files/FileHelper.java b/src/main/java/org/tasks/files/FileHelper.java index b4a70e7c0..c16119b05 100644 --- a/src/main/java/org/tasks/files/FileHelper.java +++ b/src/main/java/org/tasks/files/FileHelper.java @@ -1,30 +1,11 @@ package org.tasks.files; import android.app.Activity; -import android.content.Context; import android.database.Cursor; import android.net.Uri; -import android.os.Environment; import android.provider.MediaStore; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; - 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) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = activity.managedQuery(uri, projection, null, null, null); @@ -38,14 +19,4 @@ public class FileHelper { 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(); - } } diff --git a/src/main/java/org/tasks/preferences/Preferences.java b/src/main/java/org/tasks/preferences/Preferences.java index d7b7b1760..794bab53b 100644 --- a/src/main/java/org/tasks/preferences/Preferences.java +++ b/src/main/java/org/tasks/preferences/Preferences.java @@ -5,18 +5,24 @@ import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Environment; import android.preference.PreferenceManager; +import android.text.TextUtils; import com.todoroo.astrid.activity.BeastModePreferences; import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.core.SortHelper; import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.data.TaskAttachment; import com.todoroo.astrid.widget.WidgetConfigActivity; +import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.tasks.R; import org.tasks.injection.ForApplication; +import java.io.File; +import java.util.concurrent.atomic.AtomicReference; + import javax.inject.Inject; import ch.qos.logback.classic.Level; @@ -283,4 +289,58 @@ public class Preferences { public boolean useNotificationActions() { 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 nameReference) { + return getNewAttachmentPath(".m4a", nameReference); //$NON-NLS-1$ + } + + public String getNewAttachmentPath(String extension, AtomicReference 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; + } }