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;
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    private HashMap<Long, AccountWithDataSet> mInsertedRawContacts = Maps.newHashMap();
36    private HashSet<Long> mUpdatedRawContacts = Sets.newHashSet();
37    private HashSet<Long> mDirtyRawContacts = Sets.newHashSet();
38    private HashSet<Long> mStaleSearchIndexRawContacts = Sets.newHashSet();
39    private HashSet<Long> mStaleSearchIndexContacts = Sets.newHashSet();
40    private HashMap<Long, Object> mUpdatedSyncStates = Maps.newHashMap();
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, AccountWithDataSet accountWithDataSet) {
51        mInsertedRawContacts.put(rawContactId, accountWithDataSet);
52    }
53
54    public void rawContactUpdated(long rawContactId) {
55        mUpdatedRawContacts.add(rawContactId);
56    }
57
58    public void markRawContactDirty(long rawContactId) {
59        mDirtyRawContacts.add(rawContactId);
60    }
61
62    public void syncStateUpdated(long rowId, Object data) {
63        mUpdatedSyncStates.put(rowId, data);
64    }
65
66    public void invalidateSearchIndexForRawContact(long rawContactId) {
67        mStaleSearchIndexRawContacts.add(rawContactId);
68    }
69
70    public void invalidateSearchIndexForContact(long contactId) {
71        mStaleSearchIndexContacts.add(contactId);
72    }
73
74    public Set<Long> getInsertedRawContactIds() {
75        return mInsertedRawContacts.keySet();
76    }
77
78    public Set<Long> getUpdatedRawContactIds() {
79        return mUpdatedRawContacts;
80    }
81
82    public Set<Long> getDirtyRawContactIds() {
83        return mDirtyRawContacts;
84    }
85
86    public Set<Long> getStaleSearchIndexRawContactIds() {
87        return mStaleSearchIndexRawContacts;
88    }
89
90    public Set<Long> getStaleSearchIndexContactIds() {
91        return mStaleSearchIndexContacts;
92    }
93
94    public Set<Entry<Long, Object>> getUpdatedSyncStates() {
95        return mUpdatedSyncStates.entrySet();
96    }
97
98    public AccountWithDataSet getAccountWithDataSetForRawContact(long rawContactId) {
99        return mInsertedRawContacts.get(rawContactId);
100    }
101
102    public boolean isNewRawContact(long rawContactId) {
103        return mInsertedRawContacts.containsKey(rawContactId);
104    }
105
106    public void clear() {
107        mInsertedRawContacts.clear();
108        mUpdatedRawContacts.clear();
109        mUpdatedSyncStates.clear();
110        mDirtyRawContacts.clear();
111    }
112
113    public void clearSearchIndexUpdates() {
114        mStaleSearchIndexRawContacts.clear();
115        mStaleSearchIndexContacts.clear();
116    }
117}
118