Home | History | Annotate | Download | only in contacts
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package com.android.providers.contacts;
     18 
     19 import com.google.android.collect.Maps;
     20 import com.google.android.collect.Sets;
     21 
     22 import java.util.HashMap;
     23 import java.util.HashSet;
     24 import java.util.Map.Entry;
     25 import java.util.Set;
     26 
     27 /**
     28  * Accumulates information for an entire transaction. {@link ContactsProvider2} consumes
     29  * it at commit time.
     30  */
     31 public class TransactionContext  {
     32 
     33     private final boolean mForProfile;
     34     /** Map from raw contact id to account Id */
     35     private HashMap<Long, Long> mInsertedRawContactsAccounts;
     36     private HashSet<Long> mUpdatedRawContacts;
     37     private HashSet<Long> mDirtyRawContacts;
     38     private HashSet<Long> mStaleSearchIndexRawContacts;
     39     private HashSet<Long> mStaleSearchIndexContacts;
     40     private HashMap<Long, Object> mUpdatedSyncStates;
     41 
     42     public TransactionContext(boolean forProfile) {
     43         mForProfile = forProfile;
     44     }
     45 
     46     public boolean isForProfile() {
     47         return mForProfile;
     48     }
     49 
     50     public void rawContactInserted(long rawContactId, long accountId) {
     51         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
     52         mInsertedRawContactsAccounts.put(rawContactId, accountId);
     53     }
     54 
     55     public void rawContactUpdated(long rawContactId) {
     56         if (mUpdatedRawContacts == null) mUpdatedRawContacts = Sets.newHashSet();
     57         mUpdatedRawContacts.add(rawContactId);
     58     }
     59 
     60     public void markRawContactDirty(long rawContactId) {
     61         if (mDirtyRawContacts == null) mDirtyRawContacts = Sets.newHashSet();
     62         mDirtyRawContacts.add(rawContactId);
     63     }
     64 
     65     public void syncStateUpdated(long rowId, Object data) {
     66         if (mUpdatedSyncStates == null) mUpdatedSyncStates = Maps.newHashMap();
     67         mUpdatedSyncStates.put(rowId, data);
     68     }
     69 
     70     public void invalidateSearchIndexForRawContact(long rawContactId) {
     71         if (mStaleSearchIndexRawContacts == null) mStaleSearchIndexRawContacts = Sets.newHashSet();
     72         mStaleSearchIndexRawContacts.add(rawContactId);
     73     }
     74 
     75     public void invalidateSearchIndexForContact(long contactId) {
     76         if (mStaleSearchIndexContacts == null) mStaleSearchIndexContacts = Sets.newHashSet();
     77         mStaleSearchIndexContacts.add(contactId);
     78     }
     79 
     80     public Set<Long> getInsertedRawContactIds() {
     81         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
     82         return mInsertedRawContactsAccounts.keySet();
     83     }
     84 
     85     public Set<Long> getUpdatedRawContactIds() {
     86         if (mUpdatedRawContacts == null) mUpdatedRawContacts = Sets.newHashSet();
     87         return mUpdatedRawContacts;
     88     }
     89 
     90     public Set<Long> getDirtyRawContactIds() {
     91         if (mDirtyRawContacts == null) mDirtyRawContacts = Sets.newHashSet();
     92         return mDirtyRawContacts;
     93     }
     94 
     95     public Set<Long> getStaleSearchIndexRawContactIds() {
     96         if (mStaleSearchIndexRawContacts == null) mStaleSearchIndexRawContacts = Sets.newHashSet();
     97         return mStaleSearchIndexRawContacts;
     98     }
     99 
    100     public Set<Long> getStaleSearchIndexContactIds() {
    101         if (mStaleSearchIndexContacts == null) mStaleSearchIndexContacts = Sets.newHashSet();
    102         return mStaleSearchIndexContacts;
    103     }
    104 
    105     public Set<Entry<Long, Object>> getUpdatedSyncStates() {
    106         if (mUpdatedSyncStates == null) mUpdatedSyncStates = Maps.newHashMap();
    107         return mUpdatedSyncStates.entrySet();
    108     }
    109 
    110     public Long getAccountIdOrNullForRawContact(long rawContactId) {
    111         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
    112         return mInsertedRawContactsAccounts.get(rawContactId);
    113     }
    114 
    115     public boolean isNewRawContact(long rawContactId) {
    116         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
    117         return mInsertedRawContactsAccounts.containsKey(rawContactId);
    118     }
    119 
    120     public void clearExceptSearchIndexUpdates() {
    121         mInsertedRawContactsAccounts = null;
    122         mUpdatedRawContacts = null;
    123         mUpdatedSyncStates = null;
    124         mDirtyRawContacts = null;
    125     }
    126 
    127     public void clearSearchIndexUpdates() {
    128         mStaleSearchIndexRawContacts = null;
    129         mStaleSearchIndexContacts = null;
    130     }
    131 
    132     public void clearAll() {
    133         clearExceptSearchIndexUpdates();
    134         clearSearchIndexUpdates();
    135     }
    136 }
    137