2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.provider;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2015-01-06 18:13:28 -05:00
|
|
|
|
2015-01-16 11:38:31 -05:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.List;
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
import android.content.ContentProvider;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.MatrixCursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.ParcelFileDescriptor;
|
|
|
|
import android.util.Log;
|
2015-01-06 18:13:28 -05:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.Account;
|
2015-02-16 18:45:36 -05:00
|
|
|
import com.fsck.k9.BuildConfig;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.K9;
|
2010-11-13 16:40:56 -05:00
|
|
|
import com.fsck.k9.Preferences;
|
|
|
|
import com.fsck.k9.mail.MessagingException;
|
2015-01-16 12:18:48 -05:00
|
|
|
import com.fsck.k9.mail.internet.MimeUtility;
|
2014-12-14 10:28:42 -05:00
|
|
|
import com.fsck.k9.mailstore.LocalStore;
|
|
|
|
import com.fsck.k9.mailstore.LocalStore.AttachmentInfo;
|
2015-01-06 20:29:56 -05:00
|
|
|
import org.openintents.openpgp.util.ParcelFileDescriptorUtil;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2015-01-06 18:13:28 -05:00
|
|
|
|
2010-11-13 16:40:56 -05:00
|
|
|
/**
|
2012-01-24 08:49:08 -05:00
|
|
|
* A simple ContentProvider that allows file access to attachments.
|
2008-11-01 17:32:06 -04:00
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public class AttachmentProvider extends ContentProvider {
|
2015-02-16 18:45:36 -05:00
|
|
|
private static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".attachmentprovider";
|
|
|
|
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2012-01-24 08:49:08 -05:00
|
|
|
private static final String[] DEFAULT_PROJECTION = new String[] {
|
2015-01-06 18:13:28 -05:00
|
|
|
AttachmentProviderColumns._ID,
|
|
|
|
AttachmentProviderColumns.DATA,
|
2012-01-24 08:49:08 -05:00
|
|
|
};
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public static class AttachmentProviderColumns {
|
2008-11-01 17:32:06 -04:00
|
|
|
public static final String _ID = "_id";
|
|
|
|
public static final String DATA = "_data";
|
|
|
|
public static final String DISPLAY_NAME = "_display_name";
|
|
|
|
public static final String SIZE = "_size";
|
|
|
|
}
|
|
|
|
|
2012-01-24 08:49:08 -05:00
|
|
|
|
2015-01-15 18:12:47 -05:00
|
|
|
public static Uri getAttachmentUri(String accountUuid, long id) {
|
2014-11-09 18:56:24 -05:00
|
|
|
return CONTENT_URI.buildUpon()
|
2015-01-15 18:12:47 -05:00
|
|
|
.appendPath(accountUuid)
|
2014-11-09 18:56:24 -05:00
|
|
|
.appendPath(Long.toString(id))
|
|
|
|
.build();
|
2011-03-24 18:36:59 -04:00
|
|
|
}
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean onCreate() {
|
2008-11-01 17:32:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getType(Uri uri) {
|
2008-11-01 17:32:06 -04:00
|
|
|
List<String> segments = uri.getPathSegments();
|
2015-01-06 18:16:37 -05:00
|
|
|
String accountUuid = segments.get(0);
|
2008-11-01 17:32:06 -04:00
|
|
|
String id = segments.get(1);
|
2015-01-16 12:18:48 -05:00
|
|
|
String mimeType = (segments.size() < 3) ? null : segments.get(2);
|
2011-03-24 18:36:59 -04:00
|
|
|
|
2015-01-16 12:18:48 -05:00
|
|
|
return getType(accountUuid, id, mimeType);
|
2011-03-24 18:36:59 -04:00
|
|
|
}
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
|
2008-11-01 17:32:06 -04:00
|
|
|
List<String> segments = uri.getPathSegments();
|
2012-01-24 08:34:32 -05:00
|
|
|
String accountUuid = segments.get(0);
|
|
|
|
String attachmentId = segments.get(1);
|
|
|
|
|
2015-01-06 20:29:56 -05:00
|
|
|
return openAttachment(accountUuid, attachmentId);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-06 18:13:28 -05:00
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
|
2012-01-24 08:49:08 -05:00
|
|
|
|
|
|
|
String[] columnNames = (projection == null) ? DEFAULT_PROJECTION : projection;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
List<String> segments = uri.getPathSegments();
|
2015-01-06 18:16:37 -05:00
|
|
|
String accountUuid = segments.get(0);
|
2008-11-01 17:32:06 -04:00
|
|
|
String id = segments.get(1);
|
2010-12-15 21:38:54 -05:00
|
|
|
|
2010-11-13 16:40:56 -05:00
|
|
|
final AttachmentInfo attachmentInfo;
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2015-01-06 18:16:37 -05:00
|
|
|
final Account account = Preferences.getPreferences(getContext()).getAccount(accountUuid);
|
2014-12-12 00:32:24 -05:00
|
|
|
attachmentInfo = LocalStore.getInstance(account, getContext()).getAttachmentInfo(id);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (MessagingException e) {
|
2011-06-14 09:22:45 -04:00
|
|
|
Log.e(K9.LOG_TAG, "Unable to retrieve attachment info from local store for ID: " + id, e);
|
2010-11-13 16:40:56 -05:00
|
|
|
return null;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2013-03-21 18:19:11 -04:00
|
|
|
if (attachmentInfo == null) {
|
|
|
|
if (K9.DEBUG) {
|
|
|
|
Log.d(K9.LOG_TAG, "No attachment info for ID: " + id);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-01-24 08:49:08 -05:00
|
|
|
MatrixCursor ret = new MatrixCursor(columnNames);
|
|
|
|
Object[] values = new Object[columnNames.length];
|
|
|
|
for (int i = 0, count = columnNames.length; i < count; i++) {
|
|
|
|
String column = columnNames[i];
|
2011-02-06 17:09:48 -05:00
|
|
|
if (AttachmentProviderColumns._ID.equals(column)) {
|
2008-11-01 17:32:06 -04:00
|
|
|
values[i] = id;
|
2011-02-06 17:09:48 -05:00
|
|
|
} else if (AttachmentProviderColumns.DATA.equals(column)) {
|
2008-11-01 17:32:06 -04:00
|
|
|
values[i] = uri.toString();
|
2011-02-06 17:09:48 -05:00
|
|
|
} else if (AttachmentProviderColumns.DISPLAY_NAME.equals(column)) {
|
2010-11-13 16:40:56 -05:00
|
|
|
values[i] = attachmentInfo.name;
|
2011-02-06 17:09:48 -05:00
|
|
|
} else if (AttachmentProviderColumns.SIZE.equals(column)) {
|
2010-11-13 16:40:56 -05:00
|
|
|
values[i] = attachmentInfo.size;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.addRow(values);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-24 08:49:08 -05:00
|
|
|
@Override
|
|
|
|
public int delete(Uri uri, String arg1, String[] arg2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-16 12:18:48 -05:00
|
|
|
private String getType(String accountUuid, String id, String mimeType) {
|
2012-01-24 08:49:08 -05:00
|
|
|
String type;
|
2015-01-16 11:38:31 -05:00
|
|
|
final Account account = Preferences.getPreferences(getContext()).getAccount(accountUuid);
|
2012-01-24 08:49:08 -05:00
|
|
|
|
2015-01-16 11:38:31 -05:00
|
|
|
try {
|
|
|
|
final LocalStore localStore = LocalStore.getInstance(account, getContext());
|
2012-01-24 08:49:08 -05:00
|
|
|
|
2015-01-16 11:38:31 -05:00
|
|
|
AttachmentInfo attachmentInfo = localStore.getAttachmentInfo(id);
|
2015-01-16 12:18:48 -05:00
|
|
|
if (mimeType != null) {
|
2015-01-16 11:38:31 -05:00
|
|
|
type = mimeType;
|
|
|
|
} else {
|
|
|
|
type = attachmentInfo.type;
|
2012-01-24 08:49:08 -05:00
|
|
|
}
|
2015-01-16 11:38:31 -05:00
|
|
|
} catch (MessagingException e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Unable to retrieve LocalStore for " + account, e);
|
2015-01-16 12:18:48 -05:00
|
|
|
type = MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE;
|
2012-01-24 08:49:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:29:56 -05:00
|
|
|
private ParcelFileDescriptor openAttachment(String accountUuid, String attachmentId) {
|
|
|
|
try {
|
|
|
|
InputStream inputStream = getAttachmentInputStream(accountUuid, attachmentId);
|
|
|
|
return ParcelFileDescriptorUtil.pipeFrom(inputStream, null);
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Error getting InputStream for attachment", e);
|
|
|
|
return null;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Error creating ParcelFileDescriptor", e);
|
|
|
|
return null;
|
2012-01-24 08:49:08 -05:00
|
|
|
}
|
2015-01-06 20:29:56 -05:00
|
|
|
}
|
2012-01-24 16:14:21 -05:00
|
|
|
|
2015-01-06 20:29:56 -05:00
|
|
|
private InputStream getAttachmentInputStream(String accountUuid, String attachmentId) throws MessagingException {
|
|
|
|
final Account account = Preferences.getPreferences(getContext()).getAccount(accountUuid);
|
|
|
|
LocalStore localStore = LocalStore.getInstance(account, getContext());
|
|
|
|
return localStore.getAttachmentInputStream(attachmentId);
|
2012-01-24 08:49:08 -05:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|