TransactionContext.java revision bd9abbb6b03b4ec1e28ad3fa2fcba5d1eb8609ea
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 android.accounts.Account;
23
24import java.util.HashMap;
25import java.util.HashSet;
26import java.util.Map.Entry;
27import java.util.Set;
28
29/**
30 * Accumulates information for an entire transaction. {@link ContactsProvider2} consumes
31 * it at commit time.
32 */
33public class TransactionContext  {
34
35    private HashMap<Long, Account> 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 void rawContactInserted(long rawContactId, Account account) {
43        mInsertedRawContacts.put(rawContactId, account);
44    }
45
46    public void rawContactUpdated(long rawContactId) {
47        mUpdatedRawContacts.add(rawContactId);
48    }
49
50    public void markRawContactDirty(long rawContactId) {
51        mDirtyRawContacts.add(rawContactId);
52    }
53
54    public void syncStateUpdated(long rowId, Object data) {
55        mUpdatedSyncStates.put(rowId, data);
56    }
57
58    public void invalidateSearchIndexForRawContact(long rawContactId) {
59        mStaleSearchIndexRawContacts.add(rawContactId);
60    }
61
62    public void invalidateSearchIndexForContact(long contactId) {
63        mStaleSearchIndexContacts.add(contactId);
64    }
65
66    public Set<Long> getInsertedRawContactIds() {
67        return mInsertedRawContacts.keySet();
68    }
69
70    public Set<Long> getDirtyRawContactIds() {
71        return mDirtyRawContacts;
72    }
73
74    public Set<Long> getUpdatedRawContactIds() {
75        return mUpdatedRawContacts;
76    }
77
78    public Set<Long> getStaleSearchIndexRawContactIds() {
79        return mStaleSearchIndexRawContacts;
80    }
81
82    public Set<Long> getStaleSearchIndexContactIds() {
83        return mStaleSearchIndexContacts;
84    }
85
86    public Set<Entry<Long, Object>> getUpdatedSyncStates() {
87        return mUpdatedSyncStates.entrySet();
88    }
89
90    public Account getAccountForRawContact(long rawContactId) {
91        return mInsertedRawContacts.get(rawContactId);
92    }
93
94    public boolean isNewRawContact(long rawContactId) {
95        return mInsertedRawContacts.containsKey(rawContactId);
96    }
97
98    public void clear() {
99        mInsertedRawContacts.clear();
100        mUpdatedRawContacts.clear();
101        mUpdatedSyncStates.clear();
102        mDirtyRawContacts.clear();
103    }
104
105    public void clearSearchIndexUpdates() {
106        mStaleSearchIndexRawContacts.clear();
107        mStaleSearchIndexContacts.clear();
108    }
109}
110