Android Open Source - sms-backup-plus Attachment






From Project

Back to project page sms-backup-plus.

License

The source code is released under:

Apache License

If you think the Android project sms-backup-plus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.zegoggles.smssync.mail;
/*from  w  w  w.j  a  va  2 s  .  c o m*/
import android.content.ContentResolver;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.filter.Base64OutputStream;
import com.fsck.k9.mail.internet.MimeBodyPart;
import com.fsck.k9.mail.internet.MimeHeader;
import com.fsck.k9.mail.internet.TextBody;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import static com.zegoggles.smssync.App.TAG;

class Attachment {
    public static MimeBodyPart createTextPart(String text) throws MessagingException {
        return new MimeBodyPart(new TextBody(text));
    }

    public static MimeBodyPart createPartFromUri(ContentResolver resolver, Uri uri, String filename, String contentType) throws MessagingException {
        return createPart(new ResolverBody(resolver, uri), filename, contentType);
    }

    public static MimeBodyPart createPartFromFile(File file, String contentType) throws MessagingException {
        return createPart(new FileBody(file), file.getName(), contentType);
    }

    private static MimeBodyPart createPart(Body body, final String filename, final String contentType) throws MessagingException {
        MimeBodyPart part = new MimeBodyPart(body, contentType);

        String contentTypeHeader = TextUtils.isEmpty(contentType) ? "application/octet-stream" : contentType;
        String disposition = "attachment";
        if (!TextUtils.isEmpty(filename)) {
            // should set both name and filename parameters
            // http://www.imc.org/ietf-smtp/mail-archive/msg05023.html
            disposition += encodeRFC2231("filename", filename);
            contentTypeHeader += encodeRFC2231("name", filename);
        }
        part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentTypeHeader);
        part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, disposition);
        part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
        return part;
    }

    private static abstract class Base64Body implements Body {
        @Override
        public void writeTo(OutputStream outputStream) throws IOException, MessagingException {
            InputStream in = getInputStream();
            if (in != null)  {
                Base64OutputStream base64Out = new Base64OutputStream(outputStream);
                IOUtils.copy(in, base64Out);
                base64Out.close();
            } else {
                Log.w(TAG, "input stream is null");
            }
        }
    }

    private static class ResolverBody extends Base64Body {
        private ContentResolver mResolver;
        private Uri mUri;

        public ResolverBody(ContentResolver contentResolver, Uri uri) {
            mResolver = contentResolver;
            mUri = uri;
        }

        public InputStream getInputStream() throws MessagingException {
            try {
                return mResolver.openInputStream(mUri);
            } catch (FileNotFoundException fnfe) {
                /*
                 * Since it's completely normal for us to try to serve up attachments that
                 * have been blown away, we just return an empty stream.
                 */
                return new ByteArrayInputStream(new byte[0]);
            }
        }

        @Override
        public void setEncoding(String s) throws MessagingException {
        }
    }

    private static class FileBody extends Base64Body {
        private final File file;

        public FileBody(File file) {
            this.file = file;
        }

        @Override
        public InputStream getInputStream() throws MessagingException {
            try {
                return new FileInputStream(file);
            } catch (FileNotFoundException e) {
                return new ByteArrayInputStream(new byte[0]);
            }
        }

        @Override
        public void setEncoding(String s) throws MessagingException {
        }
    }

    // RFC2231 encoding from geronimo-javamail
    private static final String MIME_SPECIALS = "()<>@,;:\\\"/[]?=" + "\t ";
    private static final String RFC2231_SPECIALS = "*'%" + MIME_SPECIALS;
    private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();

    protected static String encodeRFC2231(String key, String value) {
        StringBuilder buf = new StringBuilder();
        boolean encoded = encodeRFC2231value(value, buf);
        if (encoded) {
            return "; " + key + "*=" + buf.toString();
        } else {
            return "; " + key + "=" + value;
        }
    }

    private static boolean encodeRFC2231value(String value, StringBuilder buf) {
        String charset = "UTF-8";
        buf.append(charset);
        buf.append("''"); // no language
        byte[] bytes;
        try {
            bytes = value.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            return true;
        }
        boolean encoded = false;
        for (byte aByte : bytes) {
            int ch = aByte & 0xff;
            if (ch <= 32 || ch >= 127 || RFC2231_SPECIALS.indexOf(ch) != -1) {
                buf.append('%');
                buf.append(HEX_DIGITS[ch >> 4]);
                buf.append(HEX_DIGITS[ch & 0xf]);
                encoded = true;
            } else {
                buf.append((char) ch);
            }
        }
        return encoded;
    }
}




Java Source Code List

com.zegoggles.smssync.App.java
com.zegoggles.smssync.Consts.java
com.zegoggles.smssync.MmsConsts.java
com.zegoggles.smssync.SmsConsts.java
com.zegoggles.smssync.activity.Dialogs.java
com.zegoggles.smssync.activity.MainActivity.java
com.zegoggles.smssync.activity.StatusPreference.java
com.zegoggles.smssync.activity.auth.AccountManagerAuthActivity.java
com.zegoggles.smssync.activity.auth.WebAuthActivity.java
com.zegoggles.smssync.activity.donation.DonationActivity.java
com.zegoggles.smssync.auth.TokenRefreshException.java
com.zegoggles.smssync.auth.TokenRefresher.java
com.zegoggles.smssync.auth.XOAuthConsumer.java
com.zegoggles.smssync.calendar.CalendarAccessorPost40.java
com.zegoggles.smssync.calendar.CalendarAccessorPre40.java
com.zegoggles.smssync.calendar.CalendarAccessor.java
com.zegoggles.smssync.compat.ComposeSmsActivity.java
com.zegoggles.smssync.compat.HeadlessSmsSendService.java
com.zegoggles.smssync.compat.MmsReceiver.java
com.zegoggles.smssync.compat.SmsReceiver.java
com.zegoggles.smssync.compat.package-info.java
com.zegoggles.smssync.contacts.ContactAccessorPost20.java
com.zegoggles.smssync.contacts.ContactAccessorPre20.java
com.zegoggles.smssync.contacts.ContactAccessor.java
com.zegoggles.smssync.contacts.ContactGroupIds.java
com.zegoggles.smssync.contacts.ContactGroup.java
com.zegoggles.smssync.contacts.Group.java
com.zegoggles.smssync.mail.AllTrustedSocketFactory.java
com.zegoggles.smssync.mail.Attachment.java
com.zegoggles.smssync.mail.BackupImapStore.java
com.zegoggles.smssync.mail.BackupStoreConfig.java
com.zegoggles.smssync.mail.CallFormatter.java
com.zegoggles.smssync.mail.ConversionResult.java
com.zegoggles.smssync.mail.DataType.java
com.zegoggles.smssync.mail.HeaderGenerator.java
com.zegoggles.smssync.mail.Headers.java
com.zegoggles.smssync.mail.MessageConverter.java
com.zegoggles.smssync.mail.MessageGenerator.java
com.zegoggles.smssync.mail.MmsSupport.java
com.zegoggles.smssync.mail.PersonLookup.java
com.zegoggles.smssync.mail.PersonRecord.java
com.zegoggles.smssync.preferences.AddressStyle.java
com.zegoggles.smssync.preferences.AuthMode.java
com.zegoggles.smssync.preferences.AuthPreferences.java
com.zegoggles.smssync.preferences.BackupManagerWrapper.java
com.zegoggles.smssync.preferences.CalendarPreferences.java
com.zegoggles.smssync.preferences.CallLogTypes.java
com.zegoggles.smssync.preferences.Defaults.java
com.zegoggles.smssync.preferences.MarkAsReadTypes.java
com.zegoggles.smssync.preferences.PreferenceBackupAgent.java
com.zegoggles.smssync.preferences.Preferences.java
com.zegoggles.smssync.preferences.ServerPreferences.java
com.zegoggles.smssync.receiver.BackupBroadcastReceiver.java
com.zegoggles.smssync.receiver.SmsBroadcastReceiver.java
com.zegoggles.smssync.service.Alarms.java
com.zegoggles.smssync.service.BackupConfig.java
com.zegoggles.smssync.service.BackupCursors.java
com.zegoggles.smssync.service.BackupItemsFetcher.java
com.zegoggles.smssync.service.BackupQueryBuilder.java
com.zegoggles.smssync.service.BackupTask.java
com.zegoggles.smssync.service.BackupType.java
com.zegoggles.smssync.service.BulkFetcher.java
com.zegoggles.smssync.service.CalendarSyncer.java
com.zegoggles.smssync.service.RestoreConfig.java
com.zegoggles.smssync.service.RestoreTask.java
com.zegoggles.smssync.service.ServiceBase.java
com.zegoggles.smssync.service.SmsBackupService.java
com.zegoggles.smssync.service.SmsRestoreService.java
com.zegoggles.smssync.service.UserCanceled.java
com.zegoggles.smssync.service.WhatsAppItemsFetcher.java
com.zegoggles.smssync.service.exception.BackupDisabledException.java
com.zegoggles.smssync.service.exception.ConnectivityException.java
com.zegoggles.smssync.service.exception.LocalizableException.java
com.zegoggles.smssync.service.exception.NoConnectionException.java
com.zegoggles.smssync.service.exception.RequiresBackgroundDataException.java
com.zegoggles.smssync.service.exception.RequiresLoginException.java
com.zegoggles.smssync.service.exception.RequiresWifiException.java
com.zegoggles.smssync.service.exception.SmsProviderNotWritableException.java
com.zegoggles.smssync.service.state.BackupState.java
com.zegoggles.smssync.service.state.RestoreState.java
com.zegoggles.smssync.service.state.SmsSyncState.java
com.zegoggles.smssync.service.state.State.java
com.zegoggles.smssync.tasks.OAuthCallbackTask.java
com.zegoggles.smssync.tasks.RequestTokenTask.java
com.zegoggles.smssync.utils.AppLog.java
com.zegoggles.smssync.utils.ListPreferenceHelper.java
com.zegoggles.smssync.utils.Sanitizer.java
com.zegoggles.smssync.utils.ThreadHelper.java