Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7ff6b74c authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change 6736

* changes:
  Refactor VCard-related code.
parents 228b2f3a 7674b81a
Loading
Loading
Loading
Loading
+1244 −0

File added.

Preview size limit exceeded, changes collapsed.

+88 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.pim.vcard;

import android.content.AbstractSyncableContentProvider;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.IContentProvider;
import android.provider.Contacts;
import android.util.Log;

/**
 * EntryHandler implementation which commits the entry to Contacts Provider 
 */
public class EntryCommitter implements EntryHandler {
    public static String LOG_TAG = "vcard.EntryComitter";
    
    private ContentResolver mContentResolver;
    
    // Ideally, this should be ContactsProvider but it seems Class loader cannot find it,
    // even when it is subclass of ContactsProvider...
    private AbstractSyncableContentProvider mProvider;
    private long mMyContactsGroupId;
    
    private long mTimeToCommit;
    
    public EntryCommitter(ContentResolver resolver) {
        mContentResolver = resolver;
        
        tryGetOriginalProvider();
    }
    
    public void onFinal() {
        if (VCardConfig.showPerformanceLog()) {
            Log.d(LOG_TAG,
                    String.format("time to commit entries: %ld ms", mTimeToCommit));
        }
    }
    
    private void tryGetOriginalProvider() {
        final ContentResolver resolver = mContentResolver;
        
        if ((mMyContactsGroupId = Contacts.People.tryGetMyContactsGroupId(resolver)) == 0) {
            Log.e(LOG_TAG, "Could not get group id of MyContact");
            return;
        }

        IContentProvider iProviderForName = resolver.acquireProvider(Contacts.CONTENT_URI);
        ContentProvider contentProvider =
            ContentProvider.coerceToLocalContentProvider(iProviderForName);
        if (contentProvider == null) {
            Log.e(LOG_TAG, "Fail to get ContentProvider object.");
            return;
        }
        
        if (!(contentProvider instanceof AbstractSyncableContentProvider)) {
            Log.e(LOG_TAG,
                    "Acquired ContentProvider object is not AbstractSyncableContentProvider.");
            return;
        }
        
        mProvider = (AbstractSyncableContentProvider)contentProvider; 
    }
    
    public void onEntryCreated(final ContactStruct contactStruct) {
        long start = System.currentTimeMillis();
        if (mProvider != null) {
            contactStruct.pushIntoAbstractSyncableContentProvider(
                    mProvider, mMyContactsGroupId);
        } else {
            contactStruct.pushIntoContentResolver(mContentResolver);
        }
        mTimeToCommit += System.currentTimeMillis() - start;
    }
}
 No newline at end of file
+33 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.pim.vcard;

/**
 * Unlike VCardBuilderBase, this (and VCardDataBuilder) assumes
 * "each VCard entry should be correctly parsed and passed to each EntryHandler object",
 */
public interface EntryHandler {
    /**
     * Able to be use this method for showing performance log, etc.
     * TODO: better name?
     */
    public void onFinal();

    /**
     * The method called when one VCard entry is successfully created
     */
    public void onEntryCreated(final ContactStruct entry);
}
+64 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.pim.vcard;

import java.util.List;

public interface VCardBuilder {
    void start();

    void end();

    /** 
     * BEGIN:VCARD
     */
    void startRecord(String type);

    /** END:VXX */
    void endRecord();

    void startProperty();

    void endProperty();

    /**
     * @param group 
     */
    void propertyGroup(String group);
    
    /**
     * @param name
     *            N <br>
     *            N
     */
    void propertyName(String name);

    /**
     * @param type
     *            LANGUAGE \ ENCODING <br>
     *            ;LANGUage= \ ;ENCODING=
     */
    void propertyParamType(String type);

    /**
     * @param value
     *            FR-EN \ GBK <br>
     *            FR-EN \ GBK
     */
    void propertyParamValue(String value);

    void propertyValues(List<String> values);
}
+99 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.pim.vcard;

import java.util.Collection;
import java.util.List;

public class VCardBuilderCollection implements VCardBuilder {

    private final Collection<VCardBuilder> mVCardBuilderCollection;
    
    public VCardBuilderCollection(Collection<VCardBuilder> vBuilderCollection) {
        mVCardBuilderCollection = vBuilderCollection; 
    }
    
    public Collection<VCardBuilder> getVCardBuilderBaseCollection() {
        return mVCardBuilderCollection;
    }
    
    public void start() {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.start();
        }
    }
    
    public void end() {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.end();
        }
    }

    public void startRecord(String type) {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.startRecord(type);
        }
    }
    
    public void endRecord() {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.endRecord();
        }
    }

    public void startProperty() {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.startProperty();
        }
    }

    
    public void endProperty() {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.endProperty();
        }
    }

    public void propertyGroup(String group) {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.propertyGroup(group);
        }
    }

    public void propertyName(String name) {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.propertyName(name);
        }
    }

    public void propertyParamType(String type) {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.propertyParamType(type);
        }
    }

    public void propertyParamValue(String value) {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.propertyParamValue(value);
        }
    }

    public void propertyValues(List<String> values) {
        for (VCardBuilder builder : mVCardBuilderCollection) {
            builder.propertyValues(values);
        }
    }
}
Loading