Skip to content

Commit 9bfadb8

Browse files
committed
Sync adapter was all static, which is bad style. Refactored to be non-static
1 parent 322adc7 commit 9bfadb8

File tree

10 files changed

+130
-160
lines changed

10 files changed

+130
-160
lines changed

app/src/androidTest/java/org/fashiontec/bodyapps/managers/test/PersonManagerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void setUp() throws Exception {
2020
context = getContext().getApplicationContext();
2121
pm=PersonManager.getInstance(context);
2222
assertNotNull("Person Manager null",pm);
23-
person=new Person("test_mail","test_name",1);
23+
person=new Person("test_mail","test_name",1, 1407640921L);
2424
}
2525

2626
public void testAddPerson() throws Exception {

app/src/androidTest/java/org/fashiontec/bodyapps/models/test/UnitTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
public class UnitTest extends TestCase {
1515

16-
public void testPersorn() throws Exception {
17-
Person person= new Person("test1","test2",1);
16+
public void testPerson() throws Exception {
17+
Person person= new Person("test1","test2",1, 1407640921L);
1818
assertNotNull(person);
1919
assertEquals("test1", person.getEmail());
2020
assertEquals("test2",person.getName());

app/src/androidTest/java/org/fashiontec/bodyapps/sync/test/SyncAdapterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class SyncAdapterTest extends AndroidTestCase {
3232
public void setUp() throws Exception {
3333
context = getContext().getApplicationContext();
3434

35-
Person person=new Person("test_mail","test_name",1);
35+
Person person = new Person("test_mail", "test_name", 1, 103L);
3636
PersonManager.getInstance(context).addPerson(person);
3737
Measurement measurement=new Measurement("test","test2",PersonManager.getInstance(context).getPerson(person),1);
3838
MeasurementManager.getInstance(context).addMeasurement(measurement);

app/src/main/java/org/fashiontec/bodyapps/main/SettingsActivity.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,7 @@ protected void onPostExecute(Bitmap result) {
438438
* Call to syncUser method
439439
*/
440440
public void postUser() {
441-
442-
userID = SyncUser.getUserID(email, personName);
441+
new SyncUser().getUserID(email, personName);
443442
}
444443

445444
/**

app/src/main/java/org/fashiontec/bodyapps/sync/Sync.java

+7-29
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@
3434
public class Sync {
3535

3636
static final String TAG = Sync.class.getName();
37-
public static String baseURL = "http://freelayers.org";
37+
protected String baseURL = "http://freelayers.org";
3838

39-
public static void setBaseURL(String baseURL) {
40-
Sync.baseURL = baseURL;
39+
public void setBaseURL(String baseURL) {
40+
this.baseURL = baseURL;
4141
}
4242

4343
/**
44-
* Method which makes all the POST calls
44+
* Method which makes all the post calls
4545
*
4646
* @param url
4747
* @param json
4848
* @return
4949
*/
50-
public HttpResponse POST(String url, String json, int conTimeOut, int socTimeOut) {
50+
public HttpResponse post(String url, String json, int conTimeOut, int socTimeOut) {
5151
HttpResponse response = null;
5252
try {
5353

@@ -68,28 +68,6 @@ public HttpResponse POST(String url, String json, int conTimeOut, int socTimeOut
6868
return response;
6969
}
7070

71-
/**
72-
* Converts the given input stream to a string.
73-
*
74-
* @param inputStream
75-
* @return
76-
* @throws IOException
77-
*/
78-
public String convertInputStreamToString(InputStream inputStream)
79-
throws IOException {
80-
BufferedReader bufferedReader = new BufferedReader(
81-
new InputStreamReader(inputStream));
82-
String line = "";
83-
String result = "";
84-
while ((line = bufferedReader.readLine()) != null)
85-
result += line;
86-
87-
inputStream.close();
88-
result = result.replaceAll("\"", "");
89-
return result;
90-
91-
}
92-
9371
public int download(String url, String file) {
9472

9573
try {
@@ -117,7 +95,7 @@ public int download(String url, String file) {
11795
return 1;
11896
}
11997

120-
public HttpResponse GET(String url, int conTimeOut, int socTimeOut) {
98+
public HttpResponse get(String url, int conTimeOut, int socTimeOut) {
12199
HttpResponse response = null;
122100
try {
123101
HttpParams httpParameters = new BasicHttpParams();
@@ -134,7 +112,7 @@ public HttpResponse GET(String url, int conTimeOut, int socTimeOut) {
134112

135113
}
136114

137-
public HttpResponse PUT(String url, String json, int conTimeOut, int socTimeOut) {
115+
public HttpResponse put(String url, String json, int conTimeOut, int socTimeOut) {
138116
HttpResponse response = null;
139117
try {
140118
HttpParams httpParameters = new BasicHttpParams();

app/src/main/java/org/fashiontec/bodyapps/sync/SyncAdapter.java

+39-40
Original file line numberDiff line numberDiff line change
@@ -29,94 +29,93 @@
2929
public class SyncAdapter extends AbstractThreadedSyncAdapter {
3030

3131
ContentResolver mContentResolver;
32-
static final String TAG = SyncAdapter.class.getName();
32+
MeasurementManager measurementMgr;
33+
PersonManager personMgr;
34+
UserManager userMgr;
3335

36+
static final String TAG = SyncAdapter.class.getName();
3437

3538
public SyncAdapter(Context context, boolean autoInitialize) {
36-
super(context, autoInitialize);
37-
mContentResolver = context.getContentResolver();
39+
this(context, autoInitialize, false);
3840
}
3941

4042
public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
41-
4243
super(context, autoInitialize, allowParallelSyncs);
4344
mContentResolver = context.getContentResolver();
45+
46+
measurementMgr = MeasurementManager.getInstance(context.getApplicationContext());
47+
personMgr = PersonManager.getInstance(context.getApplicationContext());
48+
userMgr = UserManager.getInstance(context.getApplicationContext());
4449
}
4550

4651
@Override
47-
public void onPerformSync(Account account, Bundle bundle, String s,
52+
public void onPerformSync(Account account,
53+
Bundle bundle,
54+
String s,
4855
ContentProviderClient contentProviderClient,
4956
SyncResult syncResult) {
57+
5058
Log.d(TAG, "sync happened");
51-
Measurement measurement;
52-
String userID = UserManager.getInstance(getContext().getApplicationContext()).getCurrent();
5359

54-
String[] delListServer = SyncMeasurement.getDelList(userID);
60+
SyncMeasurement sync = new SyncMeasurement();
61+
String userID = userMgr.getCurrent();
62+
Context appContext = getContext().getApplicationContext();
63+
64+
String[] delListServer = sync.getDelList(userID);
65+
5566
if (delListServer != null) {
56-
for (int i = 0; i < delListServer.length; i++) {
57-
int personID = MeasurementManager.getInstance(getContext().getApplicationContext())
58-
.getMeasurement(delListServer[i]).getPersonID();
59-
MeasurementManager.getInstance(getContext().getApplicationContext())
60-
.delMeasurement(delListServer[i], personID);
67+
for (String aDelListServer : delListServer) {
68+
int personID = measurementMgr.getMeasurement(aDelListServer).getPersonID();
69+
measurementMgr.delMeasurement(aDelListServer, personID);
6170
}
6271
}
6372

64-
List<String> delList = MeasurementManager.getInstance(getContext()
65-
.getApplicationContext()).getDelList();
73+
List<String> delList = measurementMgr.getDelList();
6674

6775
for (String ID : delList) {
68-
boolean out = SyncMeasurement.delMeasurement(ID, userID);
6976
Log.d(TAG, ID);
77+
boolean out = sync.delMeasurement(ID, userID);
7078
if (out) {
71-
MeasurementManager.getInstance(getContext().getApplicationContext())
72-
.removeDelEntry(ID);
79+
measurementMgr.removeDelEntry(ID);
7380
}
7481
}
7582

76-
String list[] = SyncMeasurement.getSyncList(UserManager.getInstance(
77-
getContext().getApplicationContext()).getLastSync(), userID);
83+
String list[] = sync.getSyncList(userMgr.getLastSync(), userID);
7884
boolean syncOK = true;
7985

8086
if (list != null) {
81-
82-
for (int i = 0; i < list.length; i++) {
83-
String out = SyncMeasurement.getMeasurement(list[i],
84-
getContext().getApplicationContext(), userID);
85-
if (!out.equals(list[i])) {
87+
for (String aList : list) {
88+
String out = sync.getMeasurement(aList, appContext, userID);
89+
if (!out.equals(aList)) {
8690
syncOK = false;
8791
break;
8892
}
8993
}
9094
}
9195

92-
while ((measurement = MeasurementManager.getInstance(
93-
getContext().getApplicationContext()).getMeasurementSync()) != null) {
96+
Measurement measurement;
97+
Person person;
98+
99+
while ((measurement = measurementMgr.getMeasurementSync()) != null) {
94100

95-
Person person = PersonManager.getInstance(getContext().getApplicationContext())
96-
.getPersonbyID(measurement.getPersonID());
97-
boolean syncedOnce = MeasurementManager.getInstance(getContext()
98-
.getApplicationContext())
99-
.isSyncedOnce(measurement.getID());
101+
person = personMgr.getPersonbyID(measurement.getPersonID());
102+
boolean syncedOnce = measurementMgr.isSyncedOnce(measurement.getID());
100103

101-
String out = SyncMeasurement.sendMeasurement(measurement, person, syncedOnce,
104+
String out = sync.sendMeasurement(measurement, person, syncedOnce,
102105
getContext().getApplicationContext());
103106

104107
if (measurement.getID().equals(out)) {
105108
measurement.setSynced(true);
106-
MeasurementManager.getInstance(getContext().getApplicationContext())
107-
.addMeasurement(measurement);
108-
MeasurementManager.getInstance(getContext().getApplicationContext())
109-
.setSyncedOnce(measurement.getID());
109+
measurementMgr.addMeasurement(measurement);
110+
measurementMgr.setSyncedOnce(measurement.getID());
110111
} else {
111112
break;
112113
}
113114
Log.d(TAG, out);
114115
}
115116

116117
if (syncOK) {
117-
UserManager.getInstance(getContext().getApplicationContext()).setLastSync(
118-
new Date().getTime() + 120000);
118+
userMgr.setLastSync(new Date().getTime() + 120000);
119119
}
120-
121120
}
122121
}

0 commit comments

Comments
 (0)