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