Skip to content

Commit 59bc4df

Browse files
committed
Move sync parameters to bundle
1 parent 7448f75 commit 59bc4df

9 files changed

Lines changed: 192 additions & 210 deletions

File tree

demo/src/main/java/pl/selvin/android/listsyncsample/provider/ListProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public ListProvider() {
4141

4242
public static synchronized SyncContentHelper getHelper() {
4343
if (helperInstance == null)
44-
helperInstance = SyncContentHelper.getInstance(Database.class, Constants.AUTHORITY, DATABASE_NAME, DATABASE_VERSION, Constants.SERVICE_URI);
44+
helperInstance = SyncContentHelper.getInstance(Database.class, Constants.AUTHORITY, DATABASE_NAME, DATABASE_VERSION);
4545
return helperInstance;
4646
}
4747

demo/src/main/java/pl/selvin/android/listsyncsample/provider/RequestExecutor.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package pl.selvin.android.listsyncsample.provider;
22

3+
import android.accounts.Account;
4+
import android.accounts.AccountManager;
5+
import android.accounts.AuthenticatorException;
6+
import android.content.Context;
7+
import android.os.Bundle;
8+
39
import androidx.annotation.NonNull;
10+
import androidx.annotation.Nullable;
411

5-
import java.io.BufferedInputStream;
612
import java.io.IOException;
13+
import java.io.InputStream;
714
import java.util.concurrent.TimeUnit;
815

916
import okhttp3.CacheControl;
@@ -17,9 +24,12 @@
1724
import okio.BufferedSink;
1825
import pl.selvin.android.listsyncsample.BuildConfig;
1926
import pl.selvin.android.listsyncsample.Constants;
27+
import pl.selvin.android.listsyncsample.authenticator.NetworkOperations;
2028
import pl.selvin.android.syncframework.content.BaseContentProvider;
2129

2230
public class RequestExecutor implements pl.selvin.android.syncframework.content.RequestExecutor {
31+
public final static String ACCOUNT_PARAMETER = "ACCOUNT_PARAMETER";
32+
2333
private final OkHttpClient client;
2434

2535
public RequestExecutor() {
@@ -38,10 +48,21 @@ public RequestExecutor() {
3848
}
3949

4050
@Override
41-
public Result execute(int requestMethod, String serviceRequestUrl, final BaseContentProvider.ISyncContentProducer syncContentProducer) throws IOException {
51+
@NonNull
52+
public Result execute(@NonNull Context context, @Nullable final BaseContentProvider.ISyncContentProducer syncContentProducer, @NonNull Bundle parameters) throws IOException, AuthenticatorException {
53+
final Account account = parameters.getParcelable(RequestExecutor.ACCOUNT_PARAMETER);
54+
int requestMethod = parameters.getInt(RequestExecutor.REQUEST_METHOD_PARAMETER);
55+
final String scope = parameters.getString(RequestExecutor.SCOPE_PARAMETER);
56+
final String requestType = parameters.getString(RequestExecutor.REQUEST_TYPE_PARAMETER);
57+
final AccountManager accountManager = AccountManager.get(context);
58+
final String userid = accountManager.getUserData(account, NetworkOperations.LoginResponse.USER_ID);
59+
final String serviceRequestUrl = String.format("%s/DefaultScopeSyncService.svc/%s/%s?userid=%s", Constants.SERVICE_URI, scope, requestType, userid);
4260
final Request.Builder requestBuilder = new Request.Builder().url(serviceRequestUrl)
43-
.addHeader("Cache-Control", "no-store,no-cache").addHeader("Pragma", "no-cache").addHeader("Accept", "application/json");
44-
if (requestMethod == HTTP_POST) {
61+
.addHeader("Cache-Control", "no-store,no-cache")
62+
.addHeader("Pragma", "no-cache")
63+
.addHeader("Accept", "application/json");
64+
65+
if (requestMethod == RequestExecutor.POST && syncContentProducer != null) {
4566
requestBuilder.post(new RequestBody() {
4667
@Override
4768
public MediaType contentType() {
@@ -63,12 +84,7 @@ public void writeTo(@NonNull BufferedSink sink) throws IOException {
6384
} catch (Exception ex) {
6485
throw new RuntimeException(ex);
6586
}
66-
return new Result(body.source().inputStream(), response.code(), error) {
67-
@Override
68-
public void close() {
69-
response.close();
70-
}
71-
};
87+
return new OkHttpResult(body.source().inputStream(), response.code(), error, response);
7288
}
7389
response.close();
7490
throw new RuntimeException("Response body is null");
@@ -84,4 +100,19 @@ public void doPing() {
84100
} catch (Exception ignore) {
85101
}
86102
}
103+
104+
private static class OkHttpResult extends Result {
105+
private Response response;
106+
107+
protected OkHttpResult(InputStream inputBuffer, int status, String error, Response response) {
108+
super(inputBuffer, status, error);
109+
this.response = response;
110+
}
111+
112+
@Override
113+
public void close() {
114+
response.close();
115+
response = null;
116+
}
117+
}
87118
}

demo/src/main/java/pl/selvin/android/listsyncsample/syncadapter/SyncService.java

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@
1919
import android.content.Context;
2020
import android.content.Intent;
2121
import android.content.SyncResult;
22-
import android.net.Uri;
2322
import android.os.Bundle;
2423
import android.os.IBinder;
2524
import android.os.RemoteCallbackList;
2625
import android.os.RemoteException;
2726
import android.util.Log;
2827

28+
import androidx.annotation.NonNull;
29+
import androidx.annotation.Nullable;
30+
2931
import pl.selvin.android.listsyncsample.Constants;
3032
import pl.selvin.android.listsyncsample.authenticator.NetworkOperations;
3133
import pl.selvin.android.listsyncsample.provider.Database;
3234
import pl.selvin.android.listsyncsample.provider.ListProvider;
33-
import pl.selvin.android.syncframework.content.Stats;
35+
import pl.selvin.android.listsyncsample.provider.RequestExecutor;
3436

3537
public 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;

demo/src/main/java/pl/selvin/android/listsyncsample/ui/BaseActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void setContentView(int layoutResID) {
118118
protected void onStart() {
119119
super.onStart();
120120
Intent intent = new Intent(this, SyncService.class);
121-
intent.putExtra(SyncService.ISYNCSERVICE_BINDER, true);
121+
intent.putExtra(SyncService.SYNC_SERVICE_BINDER, true);
122122
startService(intent);
123123
bindService(intent, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT);
124124

gradle/libs.versions.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
[versions]
22
androidx-appcompat = "1.7.0"
33
androidx-recyclerview = "1.4.0"
4-
androidx-sqlite = "2.4.0"
5-
androidx-sqlite-framework = "2.4.0"
4+
androidx-sqlite = "2.5.1"
5+
androidx-sqlite-framework = "2.5.1"
66
androidx-annotation = "1.9.1"
77

88
material = "1.12.0"
99
flexbox = "3.0.0"
1010

11-
sqlcipher-android = "4.7.1"
11+
sqlcipher-android = "4.9.0"
1212

1313
#noinspection GradleDependency
1414
jackson = "2.13.5"
1515

1616
okhttp3 = "4.12.0"
1717

18-
android-gradle-plugin = "8.9.1"
18+
android-gradle-plugin = "8.10.1"
1919

20-
com-android-sdk-common = "31.9.1"
20+
com-android-sdk-common = "31.10.1"
2121
org-apache-commons-text = "1.11.0"
2222
jsr305 = "3.0.2"
2323
constructors-constraints-annotations = "0.1.3"

0 commit comments

Comments
 (0)