Android Open Source - sms-backup-plus Header Generator






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;
/*  w  ww  .  j a  v  a  2 s  .  c o  m*/
import android.provider.CallLog;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
import com.zegoggles.smssync.MmsConsts;
import com.zegoggles.smssync.SmsConsts;
import org.jetbrains.annotations.NotNull;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.zegoggles.smssync.utils.Sanitizer.sanitize;

/**
 * Generates headers for SMS, MMS, Call logs and WhatsApp messages.
 */
class HeaderGenerator {
    private static final String REFERENCE_UID_TEMPLATE = "<%s.%s@sms-backup-plus.local>";
    private static final String MSG_ID_TEMPLATE        = "<%s@sms-backup-plus.local>";

    private final String reference;
    private final String version;

    public HeaderGenerator(String reference, String version) {
        this.version = version;
        this.reference = reference;
    }

    public void setHeaders(final Message message,
                           final Map<String, String> msgMap,
                           final DataType dataType,
                           final String address,
                           final @NotNull PersonRecord contact,
                           final Date sentDate,
                           final int status) throws MessagingException {

        // Threading by contact ID, not by thread ID. I think this value is more stable.
        message.setHeader(Headers.REFERENCES, String.format(REFERENCE_UID_TEMPLATE, reference, contact.getId()));
        message.setHeader(Headers.MESSAGE_ID, createMessageId(sentDate, address, status));
        message.setHeader(Headers.ADDRESS,  sanitize(address));
        message.setHeader(Headers.DATATYPE, dataType.toString());
        message.setHeader(Headers.BACKUP_TIME, toGMTString(new Date()));
        message.setHeader(Headers.VERSION, version);
        message.setSentDate(sentDate, false);
        message.setInternalDate(sentDate);
        switch (dataType) {
            case SMS: setSmsHeaders(message, msgMap); break;
            case MMS: setMmsHeaders(message, msgMap); break;
            case CALLLOG: setCallLogHeaders(message, msgMap); break;
            case WHATSAPP: setWhatsAppHeaders(message, sentDate, status); break;
        }
    }

    private void setSmsHeaders(Message message, Map<String,String> msgMap) throws MessagingException {
        message.setHeader(Headers.ID, msgMap.get(SmsConsts.ID));
        message.setHeader(Headers.TYPE, msgMap.get(SmsConsts.TYPE));
        message.setHeader(Headers.DATE, msgMap.get(SmsConsts.DATE));
        message.setHeader(Headers.THREAD_ID, msgMap.get(SmsConsts.THREAD_ID));
        message.setHeader(Headers.READ, msgMap.get(SmsConsts.READ));
        message.setHeader(Headers.STATUS, msgMap.get(SmsConsts.STATUS));
        message.setHeader(Headers.PROTOCOL, msgMap.get(SmsConsts.PROTOCOL));
        message.setHeader(Headers.SERVICE_CENTER, msgMap.get(SmsConsts.SERVICE_CENTER));
    }

    private void setMmsHeaders(Message message, Map<String,String> msgMap) throws MessagingException {
        message.setHeader(Headers.ID, msgMap.get(MmsConsts.ID));
        message.setHeader(Headers.TYPE, msgMap.get(MmsConsts.TYPE));
        message.setHeader(Headers.DATE, msgMap.get(MmsConsts.DATE));
        message.setHeader(Headers.THREAD_ID, msgMap.get(MmsConsts.THREAD_ID));
        message.setHeader(Headers.READ, msgMap.get(MmsConsts.READ));
    }

    private void setCallLogHeaders(Message message, Map<String,String> msgMap) throws MessagingException {
        message.setHeader(Headers.ID, msgMap.get(CallLog.Calls._ID));
        message.setHeader(Headers.TYPE, msgMap.get(CallLog.Calls.TYPE));
        message.setHeader(Headers.DATE, msgMap.get(CallLog.Calls.DATE));
        message.setHeader(Headers.DURATION, msgMap.get(CallLog.Calls.DURATION));
    }

    private void setWhatsAppHeaders(Message message, Date sentDate, int status) throws MessagingException {
        message.setHeader(Headers.DATE, String.valueOf(sentDate.getTime()));
        message.setHeader(Headers.TYPE, String.valueOf(status));
        message.setHeader(Headers.STATUS, String.valueOf(status));
    }

    private static String toGMTString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("d MMM y HH:mm:ss 'GMT'", Locale.US);
        TimeZone gmtZone = TimeZone.getTimeZone("GMT");
        sdf.setTimeZone(gmtZone);
        GregorianCalendar gc = new GregorianCalendar(gmtZone);
        gc.setTimeInMillis(date.getTime());
        return sdf.format(date);
    }

    /**
     * Create a message-id based on message date, phone number and message
     * type.
     *
     * @param sent    email send date
     * @param address the email address
     * @param type    the type
     * @return the message-id
     */
    private static String createMessageId(Date sent, String address, int type) {
        try {
            final MessageDigest digest = MessageDigest.getInstance("MD5");

            digest.update(Long.toString(sent.getTime()).getBytes("UTF-8"));
            if (address != null) {
                digest.update(address.getBytes("UTF-8"));
            }
            digest.update(Integer.toString(type).getBytes("UTF-8"));

            final StringBuilder sb = new StringBuilder();
            for (byte b : digest.digest()) {
                sb.append(String.format(Locale.ENGLISH, "%02x", b));
            }
            return String.format(Locale.ENGLISH, MSG_ID_TEMPLATE, sb.toString());
        } catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (java.security.NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}




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