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 android.content.cts;
18 
19 import android.accounts.Account;
20 import android.content.ContentResolver;
21 import android.content.ISyncAdapter;
22 import android.content.ISyncAdapterUnsyncableAccountCallback;
23 import android.content.ISyncContext;
24 import android.os.Bundle;
25 import android.os.RemoteException;
26 
27 import java.util.ArrayList;
28 import java.util.concurrent.CountDownLatch;
29 
30 public class MockSyncAdapter extends ISyncAdapter.Stub {
31 
32     private static MockSyncAdapter sSyncAdapter = null;
33 
34     private volatile ArrayList<Account> mAccounts = new ArrayList<Account>();
35     private volatile String mAuthority;
36     private volatile Bundle mExtras;
37     private volatile boolean mInitialized;
38     private volatile boolean mStartSync;
39     private volatile boolean mCancelSync;
40     private volatile CountDownLatch mLatch;
41 
getAccounts()42     public ArrayList<Account> getAccounts() {
43         return mAccounts;
44     }
45 
getAuthority()46     public String getAuthority() {
47         return mAuthority;
48     }
49 
getExtras()50     public Bundle getExtras() {
51         return mExtras;
52     }
53 
isInitialized()54     public boolean isInitialized() {
55         return mInitialized;
56     }
57 
isStartSync()58     public boolean isStartSync() {
59         return mStartSync;
60     }
61 
isCancelSync()62     public boolean isCancelSync() {
63         return mCancelSync;
64     }
65 
clearData()66     public void clearData() {
67         mAccounts.clear();
68         mAuthority = null;
69         mExtras = null;
70         mInitialized = false;
71         mStartSync = false;
72         mCancelSync = false;
73         mLatch = null;
74     }
75 
setLatch(CountDownLatch mLatch)76     public void setLatch(CountDownLatch mLatch) {
77         this.mLatch = mLatch;
78     }
79 
80     @Override
onUnsyncableAccount(ISyncAdapterUnsyncableAccountCallback cb)81     public void onUnsyncableAccount(ISyncAdapterUnsyncableAccountCallback cb)
82             throws RemoteException {
83         cb.onUnsyncableAccountDone(true);
84     }
85 
startSync(ISyncContext syncContext, String authority, Account account, Bundle extras)86     public void startSync(ISyncContext syncContext, String authority, Account account,
87             Bundle extras) throws RemoteException {
88 
89         mAccounts.add(account);
90         mAuthority = authority;
91         mExtras = extras;
92 
93         if (null != extras && extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE)) {
94             mInitialized = true;
95             mStartSync = false;
96             mCancelSync = false;
97         } else {
98             mInitialized = false;
99             mStartSync = true;
100             mCancelSync = false;
101         }
102 
103         countDownLatch();
104     }
105 
cancelSync(ISyncContext syncContext)106     public void cancelSync(ISyncContext syncContext) throws RemoteException {
107         mAccounts.clear();
108         mAuthority = null;
109         mExtras = null;
110 
111         mInitialized = false;
112         mStartSync = false;
113         mCancelSync = true;
114 
115         countDownLatch();
116     }
117 
initialize(android.accounts.Account account, java.lang.String authority)118     public void initialize(android.accounts.Account account, java.lang.String authority)
119             throws android.os.RemoteException {
120 
121         mAccounts.add(account);
122         mAuthority = authority;
123 
124         mInitialized = true;
125         mStartSync = false;
126         mCancelSync = false;
127 
128         countDownLatch();
129     }
130 
countDownLatch()131     private void countDownLatch() {
132         final CountDownLatch latch = mLatch;
133         if (latch != null) {
134             latch.countDown();
135         }
136     }
137 
getMockSyncAdapter()138     public static MockSyncAdapter getMockSyncAdapter() {
139         if (null == sSyncAdapter) {
140             sSyncAdapter = new MockSyncAdapter();
141         }
142         return sSyncAdapter;
143     }
144 }
145