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
17package com.android.providers.contacts;
18
19import com.google.android.collect.Maps;
20import com.google.android.collect.Sets;
21
22import java.util.HashMap;
23import java.util.HashSet;
24import java.util.Map.Entry;
25import java.util.Set;
26
27/**
28 * Accumulates information for an entire transaction. {@link ContactsProvider2} consumes
29 * it at commit time.
30 */
31public 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    // Set used to track what has been changed and deleted. This is needed so we can update the
39    // contact last touch timestamp.  Dirty set above is only set when sync adapter is false.
40    // {@see android.provider.ContactsContract#CALLER_IS_SYNCADAPTER}. While the set below will
41    // contain all changed contacts.
42    private HashSet<Long> mChangedRawContacts;
43    private HashSet<Long> mStaleSearchIndexRawContacts;
44    private HashSet<Long> mStaleSearchIndexContacts;
45    private HashMap<Long, Object> mUpdatedSyncStates;
46
47    public TransactionContext(boolean forProfile) {
48        mForProfile = forProfile;
49    }
50
51    public boolean isForProfile() {
52        return mForProfile;
53    }
54
55    public void rawContactInserted(long rawContactId, long accountId) {
56        if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
57        mInsertedRawContactsAccounts.put(rawContactId, accountId);
58
59        markRawContactChangedOrDeletedOrInserted(rawContactId);
60    }
61
62    public void rawContactUpdated(long rawContactId) {
63        if (mUpdatedRawContacts == null) mUpdatedRawContacts = Sets.newHashSet();
64        mUpdatedRawContacts.add(rawContactId);
65    }
66
67    public void markRawContactDirtyAndChanged(long rawContactId, boolean isSyncAdapter) {
68        if (!isSyncAdapter) {
69            if (mDirtyRawContacts == null) {
70                mDirtyRawContacts = Sets.newHashSet();
71            }
72            mDirtyRawContacts.add(rawContactId);
73        }
74
75        markRawContactChangedOrDeletedOrInserted(rawContactId);
76    }
77
78    public void markRawContactChangedOrDeletedOrInserted(long rawContactId) {
79        if (mChangedRawContacts == null) {
80            mChangedRawContacts = Sets.newHashSet();
81        }
82        mChangedRawContacts.add(rawContactId);
83    }
84
85    public void syncStateUpdated(long rowId, Object data) {
86        if (mUpdatedSyncStates == null) mUpdatedSyncStates = Maps.newHashMap();
87        mUpdatedSyncStates.put(rowId, data);
88    }
89
90    public void invalidateSearchIndexForRawContact(long rawContactId) {
91        if (mStaleSearchIndexRawContacts == null) mStaleSearchIndexRawContacts = Sets.newHashSet();
92        mStaleSearchIndexRawContacts.add(rawContactId);
93    }
94
95    public void invalidateSearchIndexForContact(long contactId) {
96        if (mStaleSearchIndexContacts == null) mStaleSearchIndexContacts = Sets.newHashSet();
97        mStaleSearchIndexContacts.add(contactId);
98    }
99
100    public Set<Long> getInsertedRawContactIds() {
101        if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
102        return mInsertedRawContactsAccounts.keySet();
103    }
104
105    public Set<Long> getUpdatedRawContactIds() {
106        if (mUpdatedRawContacts == null) mUpdatedRawContacts = Sets.newHashSet();
107        return mUpdatedRawContacts;
108    }
109
110    public Set<Long> getDirtyRawContactIds() {
111        if (mDirtyRawContacts == null) mDirtyRawContacts = Sets.newHashSet();
112        return mDirtyRawContacts;
113    }
114
115    public Set<Long> getChangedRawContactIds() {
116        if (mChangedRawContacts == null) mChangedRawContacts = Sets.newHashSet();
117        return mChangedRawContacts;
118    }
119
120    public Set<Long> getStaleSearchIndexRawContactIds() {
121        if (mStaleSearchIndexRawContacts == null) mStaleSearchIndexRawContacts = Sets.newHashSet();
122        return mStaleSearchIndexRawContacts;
123    }
124
125    public Set<Long> getStaleSearchIndexContactIds() {
126        if (mStaleSearchIndexContacts == null) mStaleSearchIndexContacts = Sets.newHashSet();
127        return mStaleSearchIndexContacts;
128    }
129
130    public Set<Entry<Long, Object>> getUpdatedSyncStates() {
131        if (mUpdatedSyncStates == null) mUpdatedSyncStates = Maps.newHashMap();
132        return mUpdatedSyncStates.entrySet();
133    }
134
135    public Long getAccountIdOrNullForRawContact(long rawContactId) {
136        if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
137        return mInsertedRawContactsAccounts.get(rawContactId);
138    }
139
140    public boolean isNewRawContact(long rawContactId) {
141        if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = Maps.newHashMap();
142        return mInsertedRawContactsAccounts.containsKey(rawContactId);
143    }
144
145    public void clearExceptSearchIndexUpdates() {
146        mInsertedRawContactsAccounts = null;
147        mUpdatedRawContacts = null;
148        mUpdatedSyncStates = null;
149        mDirtyRawContacts = null;
150        mChangedRawContacts = null;
151    }
152
153    public void clearSearchIndexUpdates() {
154        mStaleSearchIndexRawContacts = null;
155        mStaleSearchIndexContacts = null;
156    }
157
158    public void clearAll() {
159        clearExceptSearchIndexUpdates();
160        clearSearchIndexUpdates();
161    }
162}
163