Added the ability for the user to specify a custom directory for saving task attachments to

pull/14/head
Sam Bosley 12 years ago
parent 498913017d
commit 07e3046979

@ -65,6 +65,7 @@ abstract public class TodorooPreferenceActivity extends PreferenceActivity {
for(int i = 0; i < group.getPreferenceCount(); i++) {
initializePreference(group.getPreference(i));
}
updatePreferences(group, null);
} else {
Object value = null;
if(preference instanceof ListPreference)

@ -43,7 +43,11 @@ public class FileExplore extends Activity {
private static final String TAG = "F_PATH"; //$NON-NLS-1$
public static final String EXTRA_FILE_SELECTED = "fileSelected"; //$NON-NLS-1$
public static final String RESULT_FILE_SELECTED = "fileSelected"; //$NON-NLS-1$
public static final String RESULT_DIR_SELECTED = "dirSelected"; //$NON-NLS-1$
public static final String EXTRA_DIRECTORIES_SELECTABLE = "directoriesSelectable"; //$NON-NLS-1$
private Item[] fileList;
private File path = new File(Environment.getExternalStorageDirectory().toString());
@ -51,7 +55,9 @@ public class FileExplore extends Activity {
private static final int DIALOG_LOAD_FILE = 1000;
private String upString;
ListAdapter adapter;
private boolean directoryMode;
private ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -60,6 +66,8 @@ public class FileExplore extends Activity {
loadFileList();
directoryMode = getIntent().getBooleanExtra(EXTRA_DIRECTORIES_SELECTABLE, false);
showDialog(DIALOG_LOAD_FILE);
upString = getString(R.string.file_browser_up);
Log.d(TAG, path.getAbsolutePath());
@ -169,7 +177,7 @@ public class FileExplore extends Activity {
switch (id) {
case DIALOG_LOAD_FILE:
builder.setTitle(getString(R.string.file_browser_title));
builder.setTitle(getString(directoryMode ? R.string.dir_browser_title : R.string.file_browser_title));
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
@ -207,7 +215,11 @@ public class FileExplore extends Activity {
showDialog(DIALOG_LOAD_FILE);
} else {
Intent result = new Intent();
result.putExtra(EXTRA_FILE_SELECTED, sel.getAbsolutePath());
if (directoryMode) {
result.putExtra(RESULT_DIR_SELECTED, path.getAbsolutePath());
} else {
result.putExtra(RESULT_FILE_SELECTED, sel.getAbsolutePath());
}
setResult(RESULT_OK, result);
removeDialog(DIALOG_LOAD_FILE);
finish();
@ -217,6 +229,29 @@ public class FileExplore extends Activity {
});
break;
}
if (directoryMode) {
builder.setPositiveButton(R.string.file_dir_dialog_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
Intent result = new Intent();
result.putExtra(RESULT_DIR_SELECTED, path.getAbsolutePath());
setResult(RESULT_OK, result);
removeDialog(DIALOG_LOAD_FILE);
finish();
}
});
builder.setNegativeButton(R.string.file_dir_dialog_default, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
Intent result = new Intent();
result.putExtra(RESULT_DIR_SELECTED, ""); //$NON-NLS-1$
setResult(RESULT_OK, result);
removeDialog(DIALOG_LOAD_FILE);
finish();
}
});
}
dialog = builder.show();
dialog.setCancelable(true);
dialog.setOnCancelListener(new OnCancelListener() {

@ -19,7 +19,11 @@ public class FileMetadata {
/** metadata key */
public static final String METADATA_KEY = "file"; //$NON-NLS-1$
public static final String FILES_DIRECTORY = "attachments"; //$NON-NLS-1$
/** default directory for files on external storage */
public static final String FILES_DIRECTORY_DEFAULT = "attachments"; //$NON-NLS-1$
/** preference key for some other download directory */
public static final String FILES_DIRECTORY_PREF = "custom_files_dir"; //$NON-NLS-1$
/** Constants for file types */
public static final String FILE_TYPE_AUDIO = "audio/"; //$NON-NLS-1$

@ -11,9 +11,11 @@ import java.util.Date;
import java.util.concurrent.atomic.AtomicReference;
import android.content.Context;
import android.text.TextUtils;
import com.timsu.astrid.R;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
public class FileUtilities {
@ -50,7 +52,7 @@ public class FileUtilities {
.append(" ") //$NON-NLS-1$
.append(getDateStringForFilename(context, new Date()));
String dir = context.getExternalFilesDir(FileMetadata.FILES_DIRECTORY).toString();
String dir = getAttachmentsDirectory(context).getAbsolutePath();
String name = getNonCollidingFileName(dir, fileNameBuilder.toString(), extension);
@ -64,6 +66,18 @@ public class FileUtilities {
return filePathBuilder.toString();
}
public static File getAttachmentsDirectory(Context context) {
File directory = null;
String customDir = Preferences.getStringValue(FileMetadata.FILES_DIRECTORY_PREF);
if (!TextUtils.isEmpty(customDir))
directory = new File(customDir);
if (directory == null || !directory.exists())
directory = context.getExternalFilesDir(FileMetadata.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);

@ -322,7 +322,15 @@ public class FilesControlSet extends PopupControlSet {
urlString = urlString.replace(" ", "%20");
String name = m.getValue(FileMetadata.NAME);
StringBuilder filePathBuilder = new StringBuilder();
filePathBuilder.append(activity.getExternalFilesDir(FileMetadata.FILES_DIRECTORY).toString())
File directory = FileUtilities.getAttachmentsDirectory(activity);
if (directory == null) {
Toast.makeText(activity, R.string.file_err_no_directory, Toast.LENGTH_LONG).show();
return;
}
filePathBuilder.append(directory.toString())
.append(File.separator)
.append(name);

@ -38,6 +38,7 @@
<string name="file_prefix_voice">Voice</string>
<string name="file_browser_up">Up</string>
<string name="file_browser_title">Choose a file</string>
<string name="dir_browser_title">Choose a directory</string>
<string name="file_browser_err_permissions">Permissions error! Please make sure you have not blocked Astrid from accessing the SD card.</string>
<string name="file_add_picture">Attach a picture</string>
<string name="file_add_sdcard">Attach a file from your SD card</string>
@ -47,5 +48,14 @@
<string name="file_err_memory">Image is too large to fit in memory</string>
<string name="file_err_copy">Error copying file for attachment</string>
<string name="file_err_download">Error downloading file</string>
<string name="file_err_no_directory">Whoops! Looks like the files directory doesn\'t exist. Please choose a directory to save files to in the Astrid Preferences.</string>
<string name="file_err_show">Sorry, the system does not yet support this type of file</string>
<string name="file_dir_dialog_ok">Use this directory</string>
<string name="file_dir_dialog_default">Reset to default</string>
<string name="p_files_dir">Premium Downloads Directory</string>
<!-- Description for file download directory preference. %s -> chosen directory -->
<string name="p_files_dir_desc">Task attachments saved to: %s</string>
<string name="p_files_dir_desc_default">Default directory</string>
</resources>

@ -79,6 +79,8 @@
<PreferenceCategory
android:title="@string/EPr_powerpack_header">
<PreferenceScreen android:title="@string/p_files_dir" android:key="@string/p_files_dir" />
<PreferenceScreen
android:key="@string/p_voicePrefSection"
android:title="@string/EPr_voice_header">

@ -49,6 +49,8 @@ import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.core.LabsPreferences;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.files.FileExplore;
import com.todoroo.astrid.files.FileMetadata;
import com.todoroo.astrid.gtasks.GtasksPreferences;
import com.todoroo.astrid.helper.MetadataHelper;
import com.todoroo.astrid.producteev.ProducteevPreferences;
@ -84,6 +86,7 @@ public class EditPreferences extends TodorooPreferenceActivity {
private static final int REQUEST_CODE_SYNC = 0;
private static final int REQUEST_CODE_PERFORMANCE = 1;
private static final int REQUEST_CODE_FILES_DIR = 2;
public static final int RESULT_CODE_THEME_CHANGED = 1;
public static final int RESULT_CODE_PERFORMANCE_PREF_CHANGED = 3;
@ -172,6 +175,17 @@ public class EditPreferences extends TodorooPreferenceActivity {
}
});
preference = screen.findPreference(getString(R.string.p_files_dir));
preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference p) {
Intent filesDir = new Intent(EditPreferences.this, FileExplore.class);
filesDir.putExtra(FileExplore.EXTRA_DIRECTORIES_SELECTABLE, true);
startActivityForResult(filesDir, REQUEST_CODE_FILES_DIR);
return true;
}
});
addDebugPreferences();
addPreferenceListeners();
@ -424,12 +438,18 @@ public class EditPreferences extends TodorooPreferenceActivity {
}
// pp preferences
else if (r.getString(R.string.p_files_dir).equals(preference.getKey())) {
String dir = Preferences.getStringValue(FileMetadata.FILES_DIRECTORY_PREF);
if (TextUtils.isEmpty(dir)) {
dir = r.getString(R.string.p_files_dir_desc_default);
}
preference.setSummary(r.getString(R.string.p_files_dir_desc, dir));
}
else if (booleanPreference(preference, value, R.string.p_statistics,
R.string.EPr_statistics_desc_disabled, R.string.EPr_statistics_desc_enabled))
;
R.string.EPr_statistics_desc_disabled, R.string.EPr_statistics_desc_enabled));
else if (booleanPreference(preference, value, R.string.p_autoIdea,
R.string.EPr_ideaAuto_desc_disabled, R.string.EPr_ideaAuto_desc_enabled))
;
R.string.EPr_ideaAuto_desc_disabled, R.string.EPr_ideaAuto_desc_enabled));
// voice input and output
@ -477,6 +497,12 @@ public class EditPreferences extends TodorooPreferenceActivity {
} else if (requestCode == REQUEST_CODE_PERFORMANCE && resultCode == LabsPreferences.PERFORMANCE_SETTING_CHANGED) {
setResult(RESULT_CODE_PERFORMANCE_PREF_CHANGED);
return;
} else if (requestCode == REQUEST_CODE_FILES_DIR && resultCode == RESULT_OK) {
if (data != null) {
String dir = data.getStringExtra(FileExplore.RESULT_DIR_SELECTED);
Preferences.setString(FileMetadata.FILES_DIRECTORY_PREF, dir);
}
return;
}
try {
VoiceOutputService.getVoiceOutputInstance().handleActivityResult(requestCode, resultCode, data);

@ -1048,7 +1048,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
return;
}
File dst = new File(getActivity().getExternalFilesDir(FileMetadata.FILES_DIRECTORY) + File.separator + src.getName());
File dst = new File(FileUtilities.getAttachmentsDirectory(getActivity()) + File.separator + src.getName());
try {
AndroidUtilities.copyFile(src, dst);
} catch (Exception e) {
@ -1207,7 +1207,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
String recordedAudioName = data.getStringExtra(AACRecordingActivity.RESULT_FILENAME);
createNewFileAttachment(recordedAudioPath, recordedAudioName, FileMetadata.FILE_TYPE_AUDIO + "m4a"); //$NON-NLS-1$
} else if (requestCode == REQUEST_CODE_ATTACH_FILE && resultCode == Activity.RESULT_OK) {
attachFile(data.getStringExtra(FileExplore.EXTRA_FILE_SELECTED));
attachFile(data.getStringExtra(FileExplore.RESULT_FILE_SELECTED));
}
ActFmCameraModule.activityResult(getActivity(), requestCode, resultCode, data, new CameraResultCallback() {

Loading…
Cancel
Save