号簿助手 v4.6.31版本的 MD5 值为:9d7ff98a6bb5cc178eac2df4d1b2f0e0

以下内容为反编译后的 SyncDataManagerImpl.java 源代码,内容仅作参考


package com.chinatelecom.pim.core.manager.impl;

import android.content.ContentValues;
import android.database.Cursor;
import com.chinatelecom.pim.core.CoreManagerFactory;
import com.chinatelecom.pim.core.manager.PreferenceKeyManager;
import com.chinatelecom.pim.core.manager.SqliteChoiceFactory;
import com.chinatelecom.pim.core.manager.SyncDataManager;
import com.chinatelecom.pim.core.schema.Reports;
import com.chinatelecom.pim.core.sync.model.SyncReport;
import com.chinatelecom.pim.core.utils.ProtoContactUtils;
import com.chinatelecom.pim.foundation.common.model.sync.FastAuthParams;
import com.chinatelecom.pim.foundation.common.model.sync.SyncParams;
import com.chinatelecom.pim.foundation.lang.Closure;
import com.chinatelecom.pim.foundation.lang.KeyValuePare;
import com.chinatelecom.pim.foundation.lang.Transformer;
import com.chinatelecom.pim.foundation.lang.model.ContextConfig;
import com.chinatelecom.pim.foundation.lang.model.NameCard;
import com.chinatelecom.pim.foundation.lang.model.contact.Address;
import com.chinatelecom.pim.foundation.lang.model.contact.Contact;
import com.chinatelecom.pim.foundation.lang.model.contact.Email;
import com.chinatelecom.pim.foundation.lang.model.contact.Employed;
import com.chinatelecom.pim.foundation.lang.model.contact.InstantMessage;
import com.chinatelecom.pim.foundation.lang.model.contact.Phone;
import com.chinatelecom.pim.foundation.lang.model.contact.SyncMetadata;
import com.chinatelecom.pim.foundation.lang.model.contact.Website;
import com.chinatelecom.pim.foundation.lang.model.namecard.NameCardWallet;
import com.chinatelecom.pim.foundation.lang.sqlite.CursorTemplate;
import com.chinatelecom.pim.foundation.lang.sqlite.CursorUtils;
import com.chinatelecom.pim.foundation.lang.utils.StringUtils;
import ctuab.proto.BaseTypeProto;
import ctuab.proto.ContactProto;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SyncDataManagerImpl extends BaseManager implements SyncDataManager {
    protected SqliteChoiceFactory sqliteChoiceFactory = CoreManagerFactory.getInstance().getSqliteChoiceFactory();
    private PreferenceKeyManager preferenceKeyManager = CoreManagerFactory.getInstance().getPreferenceKeyManager();
    private NameCardWallet nameCardManager = NameCardWallet.newInstance();

    @Override
    public SyncParams buildSyncParams() {
        SyncParams syncParams = new SyncParams();
        syncParams.setConfigUrl("http://sync.189.cn/UabSyncService/uabconfig.uab");
        KeyValuePare keyValuePare = this.preferenceKeyManager.getAccountSettings().syncAccount().get() == null ? new KeyValuePare(null, null) : this.preferenceKeyManager.getAccountSettings().syncAccount().get();
        syncParams.setAccount(StringUtils.isBlank(keyValuePare.key) ? "" : keyValuePare.key);
        syncParams.setPassword(StringUtils.isBlank(keyValuePare.value) ? "" : keyValuePare.value);
        syncParams.setContextConfig(new ContextConfig());
        return syncParams;
    }

    @Override
    public void createReport(SyncReport report) {
        ContentValues values = new ContentValues();
        Date endTime = new Date();
        values.put("type", report.getSyncType().name());
        values.put(Reports.Report.STATE, report.getState().name());
        values.put(Reports.Report.SYNC_EXCEPTION_CODE, Integer.valueOf(report.getSyncExceptionCode()));
        values.put(Reports.Report.SERVER_RECEIVE, Integer.valueOf(report.getServerReceive()));
        values.put(Reports.Report.SERVER_ADD, Integer.valueOf(report.getServerAdd()));
        values.put(Reports.Report.SERVER_UPDATE, Integer.valueOf(report.getServerUpdate()));
        values.put(Reports.Report.SERVER_DELETE, Integer.valueOf(report.getServerDelete()));
        values.put(Reports.Report.SERVER_ERROR, Integer.valueOf(report.getServerError()));
        values.put(Reports.Report.CLIENT_SENT, Integer.valueOf(report.getClientSent()));
        values.put(Reports.Report.CLIENT_ADD, Integer.valueOf(report.getClientAdd()));
        values.put(Reports.Report.CLIENT_UPDATE, Integer.valueOf(report.getClientUpdate()));
        values.put(Reports.Report.CLIENT_DELETE, Integer.valueOf(report.getClientDelete()));
        values.put(Reports.Report.CLIENT_ERROR, Integer.valueOf(report.getClientError()));
        values.put(Reports.Report.CONFLICT, (Integer) 0);
        values.put(Reports.Report.SYNC_START_TIME, Long.valueOf(report.getSyncStartTime().getTime()));
        values.put(Reports.Report.SYNC_END_TIME, Long.valueOf(endTime.getTime()));
        this.sqliteChoiceFactory.insert(Reports.TABLE_NAME, Reports.Report.CONTENT_URI, values);
        if (countOfSyncReport() > 15) {
            removeBeforeSyncReport();
        }
    }

    private void removeBeforeSyncReport() {
        Cursor cursor = this.sqliteChoiceFactory.query(Reports.TABLE_NAME, Reports.Report.CONTENT_URI, new String[]{"_id"}, null, null, "_id DESC");
        CursorTemplate.each(cursor, new Closure<Cursor>() {
            private int index = 0;

            @Override
            public boolean execute(Cursor c) {
                this.index++;
                if (this.index > 15) {
                    SyncDataManagerImpl.this.sqliteChoiceFactory.delete(Reports.TABLE_NAME, Reports.Report.CONTENT_URI, "_id=?", new String[]{CursorUtils.getString(c, "_id")});
                }
                return true;
            }
        });
    }

    private int countOfSyncReport() {
        Cursor cursor = this.sqliteChoiceFactory.query(Reports.TABLE_NAME, Reports.Report.CONTENT_URI, null, null, null, null);
        int countReport = cursor == null ? 0 : cursor.getCount();
        cursor.close();
        return countReport;
    }

    @Override
    public List<SyncReport> findSyncReport() {
        final List<SyncReport> reports = new ArrayList<>();
        Cursor cursor = this.sqliteChoiceFactory.query(Reports.TABLE_NAME, Reports.Report.CONTENT_URI, null, null, null, "_id DESC");
        CursorTemplate.each(cursor, new Closure<Cursor>() {
            @Override
            public boolean execute(Cursor cursor2) {
                SyncReport report = SyncDataManagerImpl.this.populateReport(cursor2);
                reports.add(report);
                return true;
            }
        });
        return reports;
    }

    public SyncReport populateReport(Cursor cursor) {
        SyncReport report = new SyncReport();
        report.id = CursorUtils.getInt(cursor, "_id");
        report.state = SyncReport.State.valueOf(CursorUtils.getString(cursor, Reports.Report.STATE));
        report.setSyncType(SyncMetadata.SyncType.valueOf(CursorUtils.getString(cursor, "type")));
        report.syncExceptionCode = CursorUtils.getInt(cursor, Reports.Report.SYNC_EXCEPTION_CODE);
        report.serverReceive = CursorUtils.getInt(cursor, Reports.Report.SERVER_RECEIVE);
        report.serverAdd = CursorUtils.getInt(cursor, Reports.Report.SERVER_ADD);
        report.serverUpdate = CursorUtils.getInt(cursor, Reports.Report.SERVER_UPDATE);
        report.serverDelete = CursorUtils.getInt(cursor, Reports.Report.SERVER_DELETE);
        report.serverError = CursorUtils.getInt(cursor, Reports.Report.SERVER_ERROR);
        report.clientSent = CursorUtils.getInt(cursor, Reports.Report.CLIENT_SENT);
        report.clientAdd = CursorUtils.getInt(cursor, Reports.Report.CLIENT_ADD);
        report.clientUpdate = CursorUtils.getInt(cursor, Reports.Report.CLIENT_UPDATE);
        report.clientDelete = CursorUtils.getInt(cursor, Reports.Report.CLIENT_DELETE);
        report.clientError = CursorUtils.getInt(cursor, Reports.Report.CLIENT_ERROR);
        report.conflict = CursorUtils.getInt(cursor, Reports.Report.CONFLICT);
        report.syncStartTime = new Date(CursorUtils.getLong(cursor, Reports.Report.SYNC_START_TIME));
        report.syncEndTime = new Date(CursorUtils.getLong(cursor, Reports.Report.SYNC_END_TIME));
        return report;
    }

    @Override
    public SyncReport findSyncReportById(int id) {
        Cursor cursor = this.sqliteChoiceFactory.query(Reports.TABLE_NAME, Reports.Report.CONTENT_URI, null, "_id=?", new String[]{String.valueOf(id)}, null);
        return (SyncReport) CursorTemplate.one(cursor, new Transformer<Cursor, SyncReport>() {
            @Override
            public SyncReport transform(Cursor cursor2) {
                return SyncDataManagerImpl.this.populateReport(cursor2);
            }
        });
    }

    @Override
    public ContactProto.Contact.Builder getMyNameCard() {
        Contact contact;
        NameCard nameCard = this.nameCardManager.getItemByIndex(0);
        if (nameCard == null || (contact = nameCard.getContact()) == null) {
            return null;
        }
        ContactProto.Contact.Builder builder = ContactProto.Contact.newBuilder();
        builder.setVersion(contact.getVersion());
        if (contact.getBirthday() != null) {
            builder.setBirthday(contact.getBirthday());
        }
        if (!contact.getPhones().isEmpty()) {
            for (Phone phone : contact.getPhones()) {
                if (StringUtils.equals(phone.getCategory().getLabel(), "常用手机") || phone.getCategory().getType() == 2) {
                    builder.setMobilePhone(BaseTypeProto.Phone.newBuilder().setPhoneValue(phone.getNumber()));
                } else if (StringUtils.equals(phone.getCategory().getLabel(), "商务手机") || phone.getCategory().getType() == 17) {
                    builder.setWorkMobilePhone(BaseTypeProto.Phone.newBuilder().setPhoneValue(phone.getNumber()));
                } else if (StringUtils.equals(phone.getCategory().getLabel(), "商务固话") || phone.getCategory().getType() == 3) {
                    builder.setWorkTelephone(BaseTypeProto.Phone.newBuilder().setPhoneValue(phone.getNumber()));
                } else if (StringUtils.equals(phone.getCategory().getLabel(), "家庭固话") || phone.getCategory().getType() == 7) {
                    builder.setHomeTelephone(BaseTypeProto.Phone.newBuilder().setPhoneValue(phone.getNumber()));
                } else if (StringUtils.equals(phone.getCategory().getLabel(), "常用传真") || phone.getCategory().getType() == 13) {
                    builder.setFax(BaseTypeProto.Phone.newBuilder().setPhoneValue(phone.getNumber()));
                } else if (StringUtils.equals(phone.getCategory().getLabel(), "家庭传真") || phone.getCategory().getType() == 5) {
                    builder.setHomeFax(BaseTypeProto.Phone.newBuilder().setPhoneValue(phone.getNumber()));
                }
            }
        }
        if (!contact.getEmails().isEmpty()) {
            for (Email email : contact.getEmails()) {
                if (email != null && StringUtils.isNotEmpty(email.getAddress())) {
                    builder.setEmail(BaseTypeProto.Email.newBuilder().setEmailValue(email.getAddress()));
                }
            }
        }
        if (!contact.getWebsites().isEmpty()) {
            for (Website website : contact.getWebsites()) {
                if (website.getCategory().getType() == 5) {
                    builder.setComPage(BaseTypeProto.Website.newBuilder().setPageValue(website.getUrl()));
                } else if (website.getCategory().getType() == 4) {
                    builder.setPersonPage(BaseTypeProto.Website.newBuilder().setPageValue(website.getUrl()));
                }
            }
        }
        if (contact.getNote() != null) {
            builder.setComment(contact.getNote());
        }
        if (contact.getEmployeds() != null && contact.getEmployeds().size() > 0) {
            Employed employed = contact.getEmployeds().get(0);
            builder.setEmployed(BaseTypeProto.Employed.newBuilder().setEmpCompany(StringUtils.isNotEmpty(employed.getCompany()) ? employed.getCompany() : "").setEmpDept(StringUtils.isNotEmpty(employed.getDepartment()) ? employed.getDepartment() : "").setEmpTitle(StringUtils.isNotEmpty(employed.getTitle()) ? employed.getTitle() : ""));
        }
        if (contact.getGender() != null && contact.getGender().name() != null) {
            builder.setGender(ProtoContactUtils.convertToProtoGender(contact.getGender()));
        }
        if (!contact.getInstantMessages().isEmpty()) {
            for (InstantMessage instantMessage : contact.getInstantMessages()) {
                if (instantMessage.getCategory().getType() == 4) {
                    builder.setQq(BaseTypeProto.InstantMessage.newBuilder().setImValue(instantMessage.getAccount()));
                } else if (instantMessage.getCategory().getType() == 1) {
                    builder.setMsn(BaseTypeProto.InstantMessage.newBuilder().setImValue(instantMessage.getAccount()));
                } else if (instantMessage.getCategory().getType() == 11) {
                    builder.setWeixin(BaseTypeProto.InstantMessage.newBuilder().setImValue(instantMessage.getAccount()));
                } else if (instantMessage.getCategory().getType() == 10) {
                    builder.setYixin(BaseTypeProto.InstantMessage.newBuilder().setImValue(instantMessage.getAccount()));
                }
            }
        }
        if (contact.getDisplayName() != null) {
            builder.setName(builder.getName().toBuilder().setFamilyName(contact.getDisplayName()).build());
        }
        if (contact.getName().getGivenName() != null) {
            builder.setName(builder.getName().toBuilder().setGivenName(contact.getName().getGivenName()).build());
        }
        if (contact.getName().getNickName() != null) {
            builder.setName(builder.getName().toBuilder().setNickName(contact.getName().getNickName()).build());
        }
        if (!contact.getAddresses().isEmpty()) {
            for (Address address : contact.getAddresses()) {
                if (address.getCategory().getType() == 1) {
                    builder.setHomeAddr(BaseTypeProto.Address.newBuilder().setAddrValue(address.getValue()).setAddrPostal(address.getPostalCode() == null ? "" : address.getPostalCode()));
                } else if (address.getCategory().getType() == 2) {
                    builder.setWorkAddr(BaseTypeProto.Address.newBuilder().setAddrValue(address.getValue()).setAddrPostal(address.getPostalCode() == null ? "" : address.getPostalCode()));
                }
            }
            return builder;
        }
        return builder;
    }

    @Override
    public SyncParams buildFastAuthParams() {
        FastAuthParams params = new FastAuthParams().build();
        SyncParams syncParams = new SyncParams();
        syncParams.setConfigUrl("http://sync.189.cn/UabSyncService/uabconfig.uab");
        KeyValuePare keyValuePare = this.preferenceKeyManager.getAccountSettings().syncAccount().get() == null ? new KeyValuePare(null, null) : this.preferenceKeyManager.getAccountSettings().syncAccount().get();
        syncParams.setAccount(StringUtils.isBlank(keyValuePare.key) ? "" : keyValuePare.key);
        syncParams.setPassword(StringUtils.isBlank(keyValuePare.value) ? "" : keyValuePare.value);
        syncParams.setContextConfig(new ContextConfig());
        syncParams.setSeqId(params.getSeqId());
        syncParams.setRandom(params.getRandom());
        return syncParams;
    }
}