Added a file browser to select and attach files from the sdcard. Minor code cleanup and polish

pull/14/head
Sam Bosley 13 years ago
parent 3f97682595
commit a45ffb0c79

@ -506,6 +506,8 @@
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Dialog"/>
<activity android:name="com.todoroo.astrid.files.FileExplore"/>
<!-- locale -->
<activity android:name="com.todoroo.astrid.locale.LocaleEditAlerts"

@ -62,6 +62,7 @@ public class AACRecordingActivity extends Activity implements AACRecorderCallbac
dismiss.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
recorder.setListener(null);
recorder.stopRecording();
finish();
}
@ -101,7 +102,8 @@ public class AACRecordingActivity extends Activity implements AACRecorderCallbac
e.printStackTrace();
Toast.makeText(this, R.string.audio_err_encoding, Toast.LENGTH_LONG);
}
pd.dismiss();
if (pd != null)
pd.dismiss();
}
}

@ -0,0 +1,218 @@
package com.todoroo.astrid.files;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.timsu.astrid.R;
import com.todoroo.andlib.utility.DialogUtilities;
/**
* Based on the Android-File-Explore project by Manish Burman
* https://github.com/mburman/Android-File-Explore
*
*/
public class FileExplore extends Activity {
// Stores names of traversed directories
ArrayList<String> str = new ArrayList<String>();
// Check if the first level of the directory structure is the one showing
private Boolean firstLvl = true;
private static final String TAG = "F_PATH"; //$NON-NLS-1$
public static final String EXTRA_FILE_SELECTED = "fileSelected"; //$NON-NLS-1$
private Item[] fileList;
private File path = new File(Environment.getExternalStorageDirectory().toString());
private String chosenFile;
private static final int DIALOG_LOAD_FILE = 1000;
private String upString;
ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadFileList();
showDialog(DIALOG_LOAD_FILE);
upString = getString(R.string.file_browser_up);
Log.d(TAG, path.getAbsolutePath());
}
private void loadFileList() {
try {
path.mkdirs();
} catch (SecurityException e) {
DialogUtilities.okDialog(this, getString(R.string.file_browser_err_permissions), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
}
// Checks whether path exists
if (path.exists()) {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
File sel = new File(dir, filename);
// Filters based on whether the file is hidden or not
return (sel.isFile() || sel.isDirectory())
&& !sel.isHidden();
}
};
String[] fList = path.list(filter);
fileList = new Item[fList.length];
for (int i = 0; i < fList.length; i++) {
fileList[i] = new Item(fList[i], R.drawable.file_icon);
// Convert into file path
File sel = new File(path, fList[i]);
// Set drawables
if (sel.isDirectory()) {
fileList[i].icon = R.drawable.directory_icon;
}
}
if (!firstLvl) {
Item temp[] = new Item[fileList.length + 1];
for (int i = 0; i < fileList.length; i++) {
temp[i + 1] = fileList[i];
}
temp[0] = new Item(upString, R.drawable.directory_up);
fileList = temp;
}
} else {
Log.e(TAG, "path does not exist"); //$NON-NLS-1$
}
adapter = new ArrayAdapter<Item>(this,
android.R.layout.select_dialog_item, android.R.id.text1,
fileList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// creates view
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view
.findViewById(android.R.id.text1);
// put the image on the text view
textView.setCompoundDrawablesWithIntrinsicBounds(
fileList[position].icon, 0, 0, 0);
// add margin between image and text (support various screen
// densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
textView.setCompoundDrawablePadding(dp5);
return view;
}
};
}
private class Item {
public String file;
public int icon;
public Item(String file, Integer icon) {
this.file = file;
this.icon = icon;
}
@Override
public String toString() {
return file;
}
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
AlertDialog.Builder builder = new Builder(this);
if (fileList == null) {
dialog = builder.create();
return dialog;
}
switch (id) {
case DIALOG_LOAD_FILE:
builder.setTitle(getString(R.string.file_browser_title));
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
chosenFile = fileList[which].file;
File sel = new File(path + File.separator + chosenFile);
if (sel.isDirectory()) {
firstLvl = false;
// Adds chosen directory to list
str.add(chosenFile);
fileList = null;
path = new File(sel.toString());
loadFileList();
removeDialog(DIALOG_LOAD_FILE);
showDialog(DIALOG_LOAD_FILE);
} else if (chosenFile.equals(upString) && !sel.exists()) { // Checks if 'up' was clicked
// present directory removed from list
String s = str.remove(str.size() - 1);
// path modified to exclude present directory
path = new File(path.toString().substring(0,
path.toString().lastIndexOf(s)));
fileList = null;
// if there are no more directories in the list, then
// its the first level
if (str.isEmpty()) {
firstLvl = true;
}
loadFileList();
removeDialog(DIALOG_LOAD_FILE);
showDialog(DIALOG_LOAD_FILE);
} else {
Intent result = new Intent();
result.putExtra(EXTRA_FILE_SELECTED, sel.getAbsolutePath());
setResult(RESULT_OK, result);
removeDialog(DIALOG_LOAD_FILE);
finish();
}
}
});
break;
}
dialog = builder.show();
return dialog;
}
}

@ -13,7 +13,8 @@ public class FileMetadata {
/** Constants for file types */
public static final int FILE_TYPE_AUDIO = 0;
public static final int FILE_TYPE_PDF = 1;
public static final int FILE_TYPE_IMG = 1;
public static final int FILE_TYPE_OTHER = 2;
public static final StringProperty FILE_PATH = new StringProperty(Metadata.TABLE,
Metadata.VALUE1.name);

@ -124,13 +124,10 @@ public class FilesControlSet extends PopupControlSet {
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
File f = new File(m.getValue(FileMetadata.FILE_PATH));
if (f.delete()) {
metadataService.delete(m);
files.remove(m);
refreshDisplayView();
finalList.removeView(fileRow);
}
metadataService.delete(m);
files.remove(m);
refreshDisplayView();
finalList.removeView(fileRow);
}
}, null);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

@ -9,6 +9,10 @@
android:id="@+id/file_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:singleLine="true"
android:ellipsize="end"
android:textAppearance="@style/TextAppearance.EditRowDisplay"/>
<TextView
android:id="@+id/file_type"

@ -16,4 +16,8 @@
<string name="audio_speak_now">Speak Now!</string>
<string name="audio_encoding">Encoding...</string>
<string name="audio_err_encoding">Error encoding audio</string>
<string name="file_browser_up">Up</string>
<string name="file_browser_title">Choose a file</string>
<string name="file_browser_err_permissions">Permissions error! Please make sure you have not blocked Astrid from accessing the SD card.</string>
</resources>

@ -19,6 +19,7 @@
*/
package com.todoroo.astrid.activity;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -72,6 +73,7 @@ import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.files.AACRecordingActivity;
import com.todoroo.astrid.files.FileExplore;
import com.todoroo.astrid.files.FileMetadata;
import com.todoroo.astrid.files.FilesControlSet;
import com.todoroo.astrid.gcal.GCalControlSet;
@ -155,6 +157,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
private static final int REQUEST_VOICE_RECOG = 10;
public static final int REQUEST_CODE_CONTACT = 20;
public static final int REQUEST_CODE_RECORD = 30;
public static final int REQUEST_CODE_ATTACH_FILE = 40;
// --- menu codes
@ -995,9 +998,14 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
Toast.LENGTH_SHORT).show();
}
private void startAttachFile() {
Intent attachFile = new Intent(getActivity(), FileExplore.class);
startActivityForResult(attachFile, REQUEST_CODE_ATTACH_FILE);
}
private void startRecordingAudio() {
Intent recordAudio = new Intent(getActivity(), AACRecordingActivity.class);
recordAudio.putExtra(AACRecordingActivity.EXTRA_TEMP_FILE, getActivity().getFilesDir() + "/" + "audio.aac");
recordAudio.putExtra(AACRecordingActivity.EXTRA_TEMP_FILE, getActivity().getFilesDir() + File.separator + "audio.aac"); //$NON-NLS-1$
recordAudio.putExtra(AACRecordingActivity.EXTRA_TASK_ID, model.getId());
startActivityForResult(recordAudio, REQUEST_CODE_RECORD);
}
@ -1015,7 +1023,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
deleteButtonClick();
return true;
case MENU_ATTACH_ID:
System.err.println("Attach file!"); //$NON-NLS-1$
startAttachFile();
return true;
case MENU_RECORD_ID:
startRecordingAudio();
@ -1112,7 +1120,12 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
notesControlSet.writeToModel(model);
} else if (requestCode == REQUEST_CODE_RECORD && resultCode == Activity.RESULT_OK) {
String recordedAudio = data.getStringExtra(AACRecordingActivity.RESULT_OUTFILE);
Metadata fileMetadata = FileMetadata.createNewFileMetadata(model.getId(), recordedAudio, FileMetadata.FILE_TYPE_AUDIO);
Metadata audioMetadata = FileMetadata.createNewFileMetadata(model.getId(), recordedAudio, FileMetadata.FILE_TYPE_AUDIO);
metadataService.save(audioMetadata);
filesControlSet.refreshMetadata();
} else if (requestCode == REQUEST_CODE_ATTACH_FILE && resultCode == Activity.RESULT_OK) {
String selectedFile = data.getStringExtra(FileExplore.EXTRA_FILE_SELECTED);
Metadata fileMetadata = FileMetadata.createNewFileMetadata(model.getId(), selectedFile, FileMetadata.FILE_TYPE_OTHER);
metadataService.save(fileMetadata);
filesControlSet.refreshMetadata();
}
@ -1155,7 +1168,6 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
* ======================================================================
*/
@SuppressWarnings("nls")
public int getTabForPosition(int position) {
if ((tabStyle == TAB_STYLE_WEB && position == 0) ||
(tabStyle != TAB_STYLE_WEB && position == 1))

Loading…
Cancel
Save