• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.util.ArrayMap;
20 import android.util.ArraySet;
21 
22 import com.google.android.collect.Maps;
23 import com.google.android.collect.Sets;
24 
25 import java.util.Map.Entry;
26 import java.util.Set;
27 
28 /**
29  * Accumulates information for an entire transaction. {@link ContactsProvider2} consumes
30  * it at commit time.
31  */
32 public 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> mBackupIdChangedRawContacts;
39     private ArraySet<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 ArraySet<Long> mChangedRawContacts;
45     private ArraySet<Long> mStaleSearchIndexRawContacts;
46     private ArraySet<Long> mStaleSearchIndexContacts;
47     private ArrayMap<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 = new ArrayMap<>();
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 = new ArraySet<>();
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 = new ArraySet<>();
73             }
74             mDirtyRawContacts.add(rawContactId);
75         }
76 
77         markRawContactChangedOrDeletedOrInserted(rawContactId);
78     }
79 
markRawContactChangedOrDeletedOrInserted(long rawContactId)80     public void markRawContactChangedOrDeletedOrInserted(long rawContactId) {
81         if (mChangedRawContacts == null) {
82             mChangedRawContacts = new ArraySet<>();
83         }
84         mChangedRawContacts.add(rawContactId);
85     }
86 
syncStateUpdated(long rowId, Object data)87     public void syncStateUpdated(long rowId, Object data) {
88         if (mUpdatedSyncStates == null) mUpdatedSyncStates = new ArrayMap<>();
89         mUpdatedSyncStates.put(rowId, data);
90     }
91 
invalidateSearchIndexForRawContact(long rawContactId)92     public void invalidateSearchIndexForRawContact(long rawContactId) {
93         if (mStaleSearchIndexRawContacts == null) mStaleSearchIndexRawContacts = new ArraySet<>();
94         mStaleSearchIndexRawContacts.add(rawContactId);
95     }
96 
invalidateSearchIndexForContact(long contactId)97     public void invalidateSearchIndexForContact(long contactId) {
98         if (mStaleSearchIndexContacts == null) mStaleSearchIndexContacts = new ArraySet<>();
99         mStaleSearchIndexContacts.add(contactId);
100     }
101 
getInsertedRawContactIds()102     public Set<Long> getInsertedRawContactIds() {
103         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = new ArrayMap<>();
104         return mInsertedRawContactsAccounts.keySet();
105     }
106 
getUpdatedRawContactIds()107     public Set<Long> getUpdatedRawContactIds() {
108         if (mUpdatedRawContacts == null) mUpdatedRawContacts = new ArraySet<>();
109         return mUpdatedRawContacts;
110     }
111 
getDirtyRawContactIds()112     public Set<Long> getDirtyRawContactIds() {
113         if (mDirtyRawContacts == null) mDirtyRawContacts = new ArraySet<>();
114         return mDirtyRawContacts;
115     }
116 
getChangedRawContactIds()117     public Set<Long> getChangedRawContactIds() {
118         if (mChangedRawContacts == null) mChangedRawContacts = new ArraySet<>();
119         return mChangedRawContacts;
120     }
121 
getStaleSearchIndexRawContactIds()122     public Set<Long> getStaleSearchIndexRawContactIds() {
123         if (mStaleSearchIndexRawContacts == null) mStaleSearchIndexRawContacts = new ArraySet<>();
124         return mStaleSearchIndexRawContacts;
125     }
126 
getStaleSearchIndexContactIds()127     public Set<Long> getStaleSearchIndexContactIds() {
128         if (mStaleSearchIndexContacts == null) mStaleSearchIndexContacts = new ArraySet<>();
129         return mStaleSearchIndexContacts;
130     }
131 
getUpdatedSyncStates()132     public Set<Entry<Long, Object>> getUpdatedSyncStates() {
133         if (mUpdatedSyncStates == null) mUpdatedSyncStates = new ArrayMap<>();
134         return mUpdatedSyncStates.entrySet();
135     }
136 
getAccountIdOrNullForRawContact(long rawContactId)137     public Long getAccountIdOrNullForRawContact(long rawContactId) {
138         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = new ArrayMap<>();
139         return mInsertedRawContactsAccounts.get(rawContactId);
140     }
141 
isNewRawContact(long rawContactId)142     public boolean isNewRawContact(long rawContactId) {
143         if (mInsertedRawContactsAccounts == null) mInsertedRawContactsAccounts = new ArrayMap<>();
144         return mInsertedRawContactsAccounts.containsKey(rawContactId);
145     }
146 
clearExceptSearchIndexUpdates()147     public void clearExceptSearchIndexUpdates() {
148         mInsertedRawContactsAccounts = null;
149         mUpdatedRawContacts = null;
150         mUpdatedSyncStates = null;
151         mDirtyRawContacts = null;
152         mChangedRawContacts = null;
153         mBackupIdChangedRawContacts = null;
154     }
155 
clearSearchIndexUpdates()156     public void clearSearchIndexUpdates() {
157         mStaleSearchIndexRawContacts = null;
158         mStaleSearchIndexContacts = null;
159     }
160 
clearAll()161     public void clearAll() {
162         clearExceptSearchIndexUpdates();
163         clearSearchIndexUpdates();
164     }
165 }
166