am 126c9216: Block oversize attachments from being sent

Merge commit '126c9216b13d915b24a057b5b50bb8ea9826ba7e' into gingerbread

* commit '126c9216b13d915b24a057b5b50bb8ea9826ba7e':
  Block oversize attachments from being sent
This commit is contained in:
Andrew Stadler 2010-08-26 15:24:16 -07:00 committed by Android Git Automerger
commit b47d1a8a1a

View File

@ -60,9 +60,9 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
@ -72,6 +72,7 @@ import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
@ -103,10 +104,15 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus
private static final int ACTIVITY_REQUEST_PICK_ATTACHMENT = 1;
private static final String[] ATTACHMENT_META_COLUMNS = {
OpenableColumns.DISPLAY_NAME,
private static final String[] ATTACHMENT_META_NAME_PROJECTION = {
OpenableColumns.DISPLAY_NAME
};
private static final int ATTACHMENT_META_NAME_COLUMN_DISPLAY_NAME = 0;
private static final String[] ATTACHMENT_META_SIZE_PROJECTION = {
OpenableColumns.SIZE
};
private static final int ATTACHMENT_META_SIZE_COLUMN_SIZE = 0;
// Is set while the draft is saved by a background thread.
// Is static in order to be shared between the two activity instances
@ -1008,24 +1014,55 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus
}
private Attachment loadAttachmentInfo(Uri uri) {
int size = -1;
long size = -1;
String name = null;
ContentResolver contentResolver = getContentResolver();
Cursor metadataCursor = contentResolver.query(uri,
ATTACHMENT_META_COLUMNS, null, null, null);
// Load name & size independently, because not all providers support both
Cursor metadataCursor = contentResolver.query(uri, ATTACHMENT_META_NAME_PROJECTION,
null, null, null);
if (metadataCursor != null) {
try {
if (metadataCursor.moveToFirst()) {
name = metadataCursor.getString(0);
size = metadataCursor.getInt(1);
name = metadataCursor.getString(ATTACHMENT_META_NAME_COLUMN_DISPLAY_NAME);
}
} finally {
metadataCursor.close();
}
}
metadataCursor = contentResolver.query(uri, ATTACHMENT_META_SIZE_PROJECTION,
null, null, null);
if (metadataCursor != null) {
try {
if (metadataCursor.moveToFirst()) {
size = metadataCursor.getLong(ATTACHMENT_META_SIZE_COLUMN_SIZE);
}
} finally {
metadataCursor.close();
}
}
// When the name or size are not provided, we need to generate them locally.
if (name == null) {
name = uri.getLastPathSegment();
}
if (size < 0) {
// if the URI is a file: URI, ask file system for its size
if ("file".equalsIgnoreCase(uri.getScheme())) {
String path = uri.getPath();
if (path != null) {
File file = new File(path);
size = file.length(); // Returns 0 for file not found
}
}
if (size <= 0) {
// The size was not measurable; This attachment is not safe to use.
// Quick hack to force a relevant error into the UI
// TODO: A proper announcement of the problem
size = Email.MAX_ATTACHMENT_UPLOAD_SIZE + 1;
}
}
String contentType = contentResolver.getType(uri);
if (contentType == null) {