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> mMetadataDirtyRawContacts;
38     private HashSet<Long> mBackupIdChangedRawContacts;
39     private HashSet<Long> mDirtyRawContacts;
40     // Set used to track what has been changed and deleted. This is needed so we can update the
41     // contact last touch timestamp.  Dirty set above is only set when sync adapter is false.
42     // {@see android.provider.ContactsContract#CALLER_IS_SYNCADAPTER}. While the set below will
43     // contain all changed contacts.
44     private HashSet<Long> mChangedRawContacts;
45     private HashSet<Long> mStaleSearchIndexRawContacts;
46     private HashSet<Long> mStaleSearchIndexContacts;
47     private HashMap<Long, Object> mUpdatedSyncStates;
48 
TransactionContext(boolean forProfile)49     public TransactionContext(boolean forProfile) {
50         mForProfile = forProfile;
51     }
52 
isForProfile()53     public boolean isForProfile() {
54         return mForProfile;
55     }
56 
rawContactInserted(long rawContactId, long accountId)57     public void rawContactInserted(long rawContactId, long accountId) {
58         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
59         mInsertedRawContactsAccounts.put(rawContactId, accountId);
60 
61         markRawContactChangedOrDeletedOrInserted(rawContactId);
62     }
63 
rawContactUpdated(long rawContactId)64     public void rawContactUpdated(long rawContactId) {
65         if (mUpdatedRawContacts == null) mUpdatedRawContacts = Sets.newHashSet();
66         mUpdatedRawContacts.add(rawContactId);
67     }
68 
markRawContactDirtyAndChanged(long rawContactId, boolean isSyncAdapter)69     public void markRawContactDirtyAndChanged(long rawContactId, boolean isSyncAdapter) {
70         if (!isSyncAdapter) {
71             if (mDirtyRawContacts == null) {
72                 mDirtyRawContacts = Sets.newHashSet();
73             }
74             mDirtyRawContacts.add(rawContactId);
75         }
76 
77         markRawContactChangedOrDeletedOrInserted(rawContactId);
78     }
79 
markRawContactMetadataDirty(long rawContactId, boolean isMetadataSyncAdapter)80     public void markRawContactMetadataDirty(long rawContactId, boolean isMetadataSyncAdapter) {
81         if (!isMetadataSyncAdapter) {
82             if (mMetadataDirtyRawContacts == null) {
83                 mMetadataDirtyRawContacts = Sets.newHashSet();
84             }
85             mMetadataDirtyRawContacts.add(rawContactId);
86         }
87     }
88 
markBackupIdChangedRawContact(long rawContactId)89     public void markBackupIdChangedRawContact(long rawContactId) {
90         if (mBackupIdChangedRawContacts == null) {
91             mBackupIdChangedRawContacts = Sets.newHashSet();
92         }
93         mBackupIdChangedRawContacts.add(rawContactId);
94     }
95 
markRawContactChangedOrDeletedOrInserted(long rawContactId)96     public void markRawContactChangedOrDeletedOrInserted(long rawContactId) {
97         if (mChangedRawContacts == null) {
98             mChangedRawContacts = Sets.newHashSet();
99         }
100         mChangedRawContacts.add(rawContactId);
101     }
102 
syncStateUpdated(long rowId, Object data)103     public void syncStateUpdated(long rowId, Object data) {
104         if (mUpdatedSyncStates == null) mUpdatedSyncStates = Maps.newHashMap();
105         mUpdatedSyncStates.put(rowId, data);
106     }
107 
invalidateSearchIndexForRawContact(long rawContactId)108     public void invalidateSearchIndexForRawContact(long rawContactId) {
109         if (mStaleSearchIndexRawContacts == null) mStaleSearchIndexRawContacts = Sets.newHashSet();
110         mStaleSearchIndexRawContacts.add(rawContactId);
111     }
112 
invalidateSearchIndexForContact(long contactId)113     public void invalidateSearchIndexForContact(long contactId) {
114         if (mStaleSearchIndexContacts == null) mStaleSearchIndexContacts = Sets.newHashSet();
115         mStaleSearchIndexContacts.add(contactId);
116     }
117 
getInsertedRawContactIds()118     public Set<Long> getInsertedRawContactIds() {
119         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
120         return mInsertedRawContactsAccounts.keySet();
121     }
122 
getUpdatedRawContactIds()123     public Set<Long> getUpdatedRawContactIds() {
124         if (mUpdatedRawContacts == null) mUpdatedRawContacts = Sets.newHashSet();
125         return mUpdatedRawContacts;
126     }
127 
getDirtyRawContactIds()128     public Set<Long> getDirtyRawContactIds() {
129         if (mDirtyRawContacts == null) mDirtyRawContacts = Sets.newHashSet();
130         return mDirtyRawContacts;
131     }
132 
getMetadataDirtyRawContactIds()133     public Set<Long> getMetadataDirtyRawContactIds() {
134         if (mMetadataDirtyRawContacts == null) mMetadataDirtyRawContacts = Sets.newHashSet();
135         return mMetadataDirtyRawContacts;
136     }
137 
getBackupIdChangedRawContacts()138     public Set<Long> getBackupIdChangedRawContacts() {
139         if (mBackupIdChangedRawContacts == null) mBackupIdChangedRawContacts = Sets.newHashSet();
140         return mBackupIdChangedRawContacts;
141     }
142 
getChangedRawContactIds()143     public Set<Long> getChangedRawContactIds() {
144         if (mChangedRawContacts == null) mChangedRawContacts = Sets.newHashSet();
145         return mChangedRawContacts;
146     }
147 
getStaleSearchIndexRawContactIds()148     public Set<Long> getStaleSearchIndexRawContactIds() {
149         if (mStaleSearchIndexRawContacts == null) mStaleSearchIndexRawContacts = Sets.newHashSet();
150         return mStaleSearchIndexRawContacts;
151     }
152 
getStaleSearchIndexContactIds()153     public Set<Long> getStaleSearchIndexContactIds() {
154         if (mStaleSearchIndexContacts == null) mStaleSearchIndexContacts = Sets.newHashSet();
155         return mStaleSearchIndexContacts;
156     }
157 
getUpdatedSyncStates()158     public Set<Entry<Long, Object>> getUpdatedSyncStates() {
159         if (mUpdatedSyncStates == null) mUpdatedSyncStates = Maps.newHashMap();
160         return mUpdatedSyncStates.entrySet();
161     }
162 
getAccountIdOrNullForRawContact(long rawContactId)163     public Long getAccountIdOrNullForRawContact(long rawContactId) {
164         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
165         return mInsertedRawContactsAccounts.get(rawContactId);
166     }
167 
isNewRawContact(long rawContactId)168     public boolean isNewRawContact(long rawContactId) {
169         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
170         return mInsertedRawContactsAccounts.containsKey(rawContactId);
171     }
172 
clearExceptSearchIndexUpdates()173     public void clearExceptSearchIndexUpdates() {
174         mInsertedRawContactsAccounts = null;
175         mUpdatedRawContacts = null;
176         mUpdatedSyncStates = null;
177         mDirtyRawContacts = null;
178         mMetadataDirtyRawContacts = null;
179         mChangedRawContacts = null;
180         mBackupIdChangedRawContacts = null;
181     }
182 
clearSearchIndexUpdates()183     public void clearSearchIndexUpdates() {
184         mStaleSearchIndexRawContacts = null;
185         mStaleSearchIndexContacts = null;
186     }
187 
clearAll()188     public void clearAll() {
189         clearExceptSearchIndexUpdates();
190         clearSearchIndexUpdates();
191     }
192 }
193