1919import android .content .Context ;
2020import android .content .Intent ;
2121import android .content .SyncResult ;
22- import android .net .Uri ;
2322import android .os .Bundle ;
2423import android .os .IBinder ;
2524import android .os .RemoteCallbackList ;
2625import android .os .RemoteException ;
2726import android .util .Log ;
2827
28+ import androidx .annotation .NonNull ;
29+ import androidx .annotation .Nullable ;
30+
2931import pl .selvin .android .listsyncsample .Constants ;
3032import pl .selvin .android .listsyncsample .authenticator .NetworkOperations ;
3133import pl .selvin .android .listsyncsample .provider .Database ;
3234import pl .selvin .android .listsyncsample .provider .ListProvider ;
33- import pl .selvin .android .syncframework . content . Stats ;
35+ import pl .selvin .android .listsyncsample . provider . RequestExecutor ;
3436
3537public class SyncService extends Service {
36- public static final String ISYNCSERVICE_BINDER = "ISYNCSERVICE_BINDER " ;
38+ public static final String SYNC_SERVICE_BINDER = "SYNC_SERVICE_BINDER " ;
3739 public static final int SYNC_IDLE = 0 ;
3840 public static final int SYNC_ACTIVE = 1 ;
3941 public static final int SYNC_PENDING = 2 ;
@@ -67,42 +69,59 @@ public int getLastStatus() {
6769 };
6870
6971 public static String getUserId (Context context ) {
70- AccountManager am = AccountManager .get (context );
71- Account [] ac = null ;
72- try {
73- ac = am .getAccountsByType (Constants .ACCOUNT_TYPE );
74- } catch (SecurityException e ) {
75- e .printStackTrace ();
76- }
77- if (ac != null && ac .length > 0 ) {
78- return am .getUserData (ac [0 ], NetworkOperations .LoginResponse .USER_ID );
72+ Account account = getAccount (context );
73+ if (account != null ) {
74+ AccountManager accountManager = AccountManager .get (context );
75+ return accountManager .getUserData (account , NetworkOperations .LoginResponse .USER_ID );
7976 }
8077 return null ;
8178 }
8279
8380 public static Account getAccount (Context context ) {
84- AccountManager am = AccountManager .get (context );
85- Account [] ac = null ;
81+ AccountManager accountManager = AccountManager .get (context );
82+ Account [] accounts = null ;
8683 try {
87- ac = am .getAccountsByType (Constants .ACCOUNT_TYPE );
84+ accounts = accountManager .getAccountsByType (Constants .ACCOUNT_TYPE );
8885 } catch (SecurityException e ) {
8986 e .printStackTrace ();
9087 }
91- if (ac != null && ac .length > 0 ) {
92- return ac [0 ];
88+ if (accounts != null && accounts .length > 0 ) {
89+ return accounts [0 ];
9390 }
9491 return null ;
9592 }
9693
94+ static void copySyncResult (@ Nullable SyncResult source , @ NonNull SyncResult destination ) {
95+ if (source != null && source != destination ) {
96+ destination .tooManyDeletions = source .tooManyDeletions ;
97+ destination .tooManyRetries = source .tooManyRetries ;
98+ destination .fullSyncRequested = source .fullSyncRequested ;
99+ destination .partialSyncUnavailable = source .partialSyncUnavailable ;
100+ destination .moreRecordsToGet = source .moreRecordsToGet ;
101+ destination .delayUntil = source .delayUntil ;
102+ destination .stats .numAuthExceptions = source .stats .numAuthExceptions ;
103+ destination .stats .numIoExceptions = source .stats .numIoExceptions ;
104+ destination .stats .numParseExceptions = source .stats .numParseExceptions ;
105+ destination .stats .numConflictDetectedExceptions = source .stats .numConflictDetectedExceptions ;
106+ destination .stats .numInserts = source .stats .numInserts ;
107+ destination .stats .numUpdates = source .stats .numUpdates ;
108+ destination .stats .numDeletes = source .stats .numDeletes ;
109+ destination .stats .numEntries = source .stats .numEntries ;
110+ destination .stats .numSkippedEntries = source .stats .numSkippedEntries ;
111+ }
112+ }
113+
97114 @ Override
98115 public IBinder onBind (Intent intent ) {
99116 synchronized (sSyncAdapterLock ) {
100117 if (sSyncAdapter == null ) {
101- sSyncAdapter = new SyncAdapter (getApplicationContext ());
118+ sSyncAdapter = new SyncAdapter (this );
119+ } else {
120+ sSyncAdapter .setService (this );
102121 }
103122 }
104- sSyncAdapter . setService ( this );
105- if (intent .hasExtra (ISYNCSERVICE_BINDER ))
123+
124+ if (intent .hasExtra (SYNC_SERVICE_BINDER ))
106125 return mBinder ;
107126 return sSyncAdapter .getSyncAdapterBinder ();
108127 }
@@ -127,10 +146,11 @@ public void onDestroy() {
127146
128147 static class SyncAdapter extends AbstractThreadedSyncAdapter {
129148
130- private SyncService mService = null ;
149+ private SyncService mService ;
131150
132- SyncAdapter (Context context ) {
133- super (context , true );
151+ SyncAdapter (SyncService service ) {
152+ super (service .getApplicationContext (), true );
153+ mService = service ;
134154 }
135155
136156 void setService (SyncService service ) {
@@ -141,46 +161,31 @@ void setService(SyncService service) {
141161 synchronized public void onPerformSync (Account account , Bundle extras , String authority , ContentProviderClient provider , SyncResult syncResult ) {
142162 mService .mLastStatus = SYNC_ACTIVE ;
143163 mService .fireStatusChanged ();
144- Stats stats = new Stats ();
164+ extras .putParcelable (RequestExecutor .ACCOUNT_PARAMETER , account );
165+ extras .putParcelable (RequestExecutor .SYNC_RESULT_PARAMETER , syncResult );
166+ extras .putString (RequestExecutor .SCOPE_PARAMETER , Database .DS );
145167
146- String parameters = null ;
147- try {
148- parameters = String .format ("?userid=%s" , getUserId (getContext ()));
149- } catch (Exception ex ) {
150- stats .stats .numIoExceptions ++;
151- }
152- if (parameters != null ) {
153- final ListProvider mtProvider = (ListProvider ) provider .getLocalContentProvider ();
154- if (mtProvider != null ) {
155- try {
156- mtProvider .sync ("DefaultScopeSyncService" , Database .DS , parameters , stats );
157- } catch (Exception ex ) {
158- stats .stats .numIoExceptions ++;
159- ex .printStackTrace ();
160- }
161- } else {
162- try {
163- final Uri uri = ListProvider .getHelper ().getSyncUri ("DefaultScopeSyncService" , "defaultscope" );
164- Bundle syncParams = new Bundle ();
165- syncParams .putParcelable (ListProvider .SYNC_PARAM_IN_SYNC_STATS , stats );
166- syncParams = provider .call (uri .toString (), parameters , syncParams );
167- stats = syncParams .getParcelable (ListProvider .SYNC_PARAM_IN_SYNC_STATS );
168- } catch (RemoteException e ) {
169- stats .stats .numParseExceptions ++;
170- e .printStackTrace ();
168+ final ListProvider listProvider = (ListProvider ) provider .getLocalContentProvider ();
169+ if (listProvider != null ) {
170+ try {
171+ final Bundle results = listProvider .sync (extras );
172+ } catch (Exception ex ) {
173+ syncResult .stats .numIoExceptions ++;
174+ ex .printStackTrace ();
175+ }
176+ } else {
177+ try {
178+ final Bundle results = provider .call (ListProvider .getHelper ().SYNC_URI .toString (), null , extras );
179+ if (results != null ) {
180+ final SyncResult syncResultResult = results .getParcelable (RequestExecutor .SYNC_RESULT_PARAMETER );
181+ copySyncResult (syncResultResult , syncResult );
171182 }
183+ } catch (RemoteException e ) {
184+ syncResult .stats .numIoExceptions ++;
185+ e .printStackTrace ();
172186 }
173187 }
174- syncResult .stats .numAuthExceptions = stats .stats .numAuthExceptions ;
175- syncResult .stats .numConflictDetectedExceptions = stats .stats .numConflictDetectedExceptions ;
176- syncResult .stats .numDeletes = stats .stats .numDeletes ;
177- syncResult .stats .numEntries = stats .stats .numEntries ;
178- syncResult .stats .numInserts = stats .stats .numInserts ;
179- syncResult .stats .numIoExceptions = stats .stats .numIoExceptions ;
180- syncResult .stats .numParseExceptions = stats .stats .numParseExceptions ;
181- syncResult .stats .numSkippedEntries = stats .stats .numSkippedEntries ;
182- syncResult .stats .numUpdates = stats .stats .numUpdates ;
183- Log .v ("SyncStats: " , stats .stats .toString ());
188+ Log .v ("SyncStats: " , syncResult .stats .toString ());
184189 Log .d ("SyncResult: " , syncResult .toString ());
185190
186191 mService .mLastStatus = SYNC_IDLE ;
0 commit comments