Download file attachments on demand

pull/14/head
Sam Bosley 13 years ago
parent 50d249dd53
commit 46a4fb57cf

@ -1088,7 +1088,10 @@ public final class ActFmSyncService {
} else { } else {
// Create new file attachment // Create new file attachment
Metadata newAttachment = FileMetadata.createNewFileMetadata(model.getId(), null, file.getString("content_type")); Metadata newAttachment = FileMetadata.createNewFileMetadata(model.getId(), null, file.getString("content_type"));
newAttachment.setValue(FileMetadata.URL, file.getString("url")); String url = file.getString("url");
if (url.contains("?"))
url = url.substring(0, url.lastIndexOf('?'));
newAttachment.setValue(FileMetadata.URL, url);
newAttachment.setValue(FileMetadata.REMOTE_ID, id); newAttachment.setValue(FileMetadata.REMOTE_ID, id);
metadataService.save(newAttachment); metadataService.save(newAttachment);
} }

@ -2,7 +2,6 @@ package com.todoroo.astrid.files;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
@ -145,15 +144,20 @@ public class FilesControlSet extends PopupControlSet {
private void setupFileClickListener(View view, final Metadata m) { private void setupFileClickListener(View view, final Metadata m) {
String fileType = m.getValue(FileMetadata.FILE_TYPE); String fileType = m.getValue(FileMetadata.FILE_TYPE);
final String filePath = m.getValue(FileMetadata.FILE_PATH); final String filePath = m.containsNonNullValue(FileMetadata.FILE_PATH) ? m.getValue(FileMetadata.FILE_PATH) : null;
if (TextUtils.isEmpty(filePath)) { if (TextUtils.isEmpty(filePath)) {
DialogUtilities.okCancelDialog(activity, activity.getString(R.string.file_download_title), view.setOnClickListener(new OnClickListener() {
activity.getString(R.string.file_download_body), new DialogInterface.OnClickListener() { @Override
public void onClick(View v) {
DialogUtilities.okCancelDialog(activity, activity.getString(R.string.file_download_title),
activity.getString(R.string.file_download_body), new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface d, int which) { public void onClick(DialogInterface d, int which) {
downloadFile(m); downloadFile(m);
} }
}, null); }, null);
}
});
} else if (FileMetadata.FILE_TYPE_AUDIO.equals(fileType)) { } else if (FileMetadata.FILE_TYPE_AUDIO.equals(fileType)) {
view.setOnClickListener(new OnClickListener() { view.setOnClickListener(new OnClickListener() {
@Override @Override
@ -216,11 +220,13 @@ public class FilesControlSet extends PopupControlSet {
.append(name); .append(name);
File file = new File(filePathBuilder.toString()); File file = new File(filePathBuilder.toString());
try { if (file.exists()) {
file.createNewFile(); activity.runOnUiThread(new Runnable() {
} catch (IOException e) { @Override
pd.dismiss(); public void run() {
Toast.makeText(activity, R.string.file_err_download, Toast.LENGTH_LONG); Toast.makeText(activity, R.string.file_err_download, Toast.LENGTH_LONG);
}
});
return; return;
} }
@ -246,7 +252,7 @@ public class FilesControlSet extends PopupControlSet {
int bufferLength = 0; //used to store a temporary size of the buffer int bufferLength = 0; //used to store a temporary size of the buffer
while ((bufferLength = inputStream.read(buffer)) > 0 ) { while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength); fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength; downloadedSize += bufferLength;
@ -268,7 +274,12 @@ public class FilesControlSet extends PopupControlSet {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
file.delete(); file.delete();
Toast.makeText(activity, R.string.file_err_download, Toast.LENGTH_LONG); activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, R.string.file_err_download, Toast.LENGTH_LONG);
}
});
} finally { } finally {
pd.dismiss(); pd.dismiss();
} }
@ -291,11 +302,16 @@ public class FilesControlSet extends PopupControlSet {
parent.addView(row, lp); parent.addView(row, lp);
} }
@SuppressWarnings("nls")
private String getNameString(Metadata metadata) { private String getNameString(Metadata metadata) {
String path = metadata.getValue(FileMetadata.FILE_PATH); String path = metadata.containsNonNullValue(FileMetadata.FILE_PATH) ? metadata.getValue(FileMetadata.FILE_PATH) : null;
String name; String name;
if (TextUtils.isEmpty(path)) { if (TextUtils.isEmpty(path)) {
name = metadata.getValue(FileMetadata.URL); name = metadata.getValue(FileMetadata.URL);
if (name.endsWith("/"))
name = name.substring(0, name.length() - 1);
if (name.contains("/"))
name = name.substring(name.lastIndexOf('/') + 1);
} else { } else {
File f = new File(path); File f = new File(path);
name = f.getName(); name = f.getName();
@ -314,8 +330,13 @@ public class FilesControlSet extends PopupControlSet {
@SuppressWarnings("nls") @SuppressWarnings("nls")
private String getTypeString(Metadata metadata) { private String getTypeString(Metadata metadata) {
File f = new File(metadata.getValue(FileMetadata.FILE_PATH)); String name;
String name = f.getName(); if (metadata.containsNonNullValue(FileMetadata.FILE_PATH)) {
File f = new File(metadata.getValue(FileMetadata.FILE_PATH));
name = f.getName();
} else {
name = metadata.getValue(FileMetadata.URL);
}
int extension = name.lastIndexOf('.'); int extension = name.lastIndexOf('.');
if (extension < 0 || extension + 1 >= name.length()) if (extension < 0 || extension + 1 >= name.length())

Loading…
Cancel
Save