|
29 | 29 | public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
30 | 30 |
|
31 | 31 | ContentResolver mContentResolver;
|
32 |
| - static final String TAG = SyncAdapter.class.getName(); |
| 32 | + MeasurementManager measurementMgr; |
| 33 | + PersonManager personMgr; |
| 34 | + UserManager userMgr; |
33 | 35 |
|
| 36 | + static final String TAG = SyncAdapter.class.getName(); |
34 | 37 |
|
35 | 38 | public SyncAdapter(Context context, boolean autoInitialize) {
|
36 |
| - super(context, autoInitialize); |
37 |
| - mContentResolver = context.getContentResolver(); |
| 39 | + this(context, autoInitialize, false); |
38 | 40 | }
|
39 | 41 |
|
40 | 42 | public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
|
41 |
| - |
42 | 43 | super(context, autoInitialize, allowParallelSyncs);
|
43 | 44 | mContentResolver = context.getContentResolver();
|
| 45 | + |
| 46 | + measurementMgr = MeasurementManager.getInstance(context.getApplicationContext()); |
| 47 | + personMgr = PersonManager.getInstance(context.getApplicationContext()); |
| 48 | + userMgr = UserManager.getInstance(context.getApplicationContext()); |
44 | 49 | }
|
45 | 50 |
|
46 | 51 | @Override
|
47 |
| - public void onPerformSync(Account account, Bundle bundle, String s, |
| 52 | + public void onPerformSync(Account account, |
| 53 | + Bundle bundle, |
| 54 | + String s, |
48 | 55 | ContentProviderClient contentProviderClient,
|
49 | 56 | SyncResult syncResult) {
|
| 57 | + |
50 | 58 | Log.d(TAG, "sync happened");
|
51 |
| - Measurement measurement; |
52 |
| - String userID = UserManager.getInstance(getContext().getApplicationContext()).getCurrent(); |
53 | 59 |
|
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 | + |
55 | 66 | 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); |
61 | 70 | }
|
62 | 71 | }
|
63 | 72 |
|
64 |
| - List<String> delList = MeasurementManager.getInstance(getContext() |
65 |
| - .getApplicationContext()).getDelList(); |
| 73 | + List<String> delList = measurementMgr.getDelList(); |
66 | 74 |
|
67 | 75 | for (String ID : delList) {
|
68 |
| - boolean out = SyncMeasurement.delMeasurement(ID, userID); |
69 | 76 | Log.d(TAG, ID);
|
| 77 | + boolean out = sync.delMeasurement(ID, userID); |
70 | 78 | if (out) {
|
71 |
| - MeasurementManager.getInstance(getContext().getApplicationContext()) |
72 |
| - .removeDelEntry(ID); |
| 79 | + measurementMgr.removeDelEntry(ID); |
73 | 80 | }
|
74 | 81 | }
|
75 | 82 |
|
76 |
| - String list[] = SyncMeasurement.getSyncList(UserManager.getInstance( |
77 |
| - getContext().getApplicationContext()).getLastSync(), userID); |
| 83 | + String list[] = sync.getSyncList(userMgr.getLastSync(), userID); |
78 | 84 | boolean syncOK = true;
|
79 | 85 |
|
80 | 86 | 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)) { |
86 | 90 | syncOK = false;
|
87 | 91 | break;
|
88 | 92 | }
|
89 | 93 | }
|
90 | 94 | }
|
91 | 95 |
|
92 |
| - while ((measurement = MeasurementManager.getInstance( |
93 |
| - getContext().getApplicationContext()).getMeasurementSync()) != null) { |
| 96 | + Measurement measurement; |
| 97 | + Person person; |
| 98 | + |
| 99 | + while ((measurement = measurementMgr.getMeasurementSync()) != null) { |
94 | 100 |
|
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()); |
100 | 103 |
|
101 |
| - String out = SyncMeasurement.sendMeasurement(measurement, person, syncedOnce, |
| 104 | + String out = sync.sendMeasurement(measurement, person, syncedOnce, |
102 | 105 | getContext().getApplicationContext());
|
103 | 106 |
|
104 | 107 | if (measurement.getID().equals(out)) {
|
105 | 108 | 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()); |
110 | 111 | } else {
|
111 | 112 | break;
|
112 | 113 | }
|
113 | 114 | Log.d(TAG, out);
|
114 | 115 | }
|
115 | 116 |
|
116 | 117 | if (syncOK) {
|
117 |
| - UserManager.getInstance(getContext().getApplicationContext()).setLastSync( |
118 |
| - new Date().getTime() + 120000); |
| 118 | + userMgr.setLastSync(new Date().getTime() + 120000); |
119 | 119 | }
|
120 |
| - |
121 | 120 | }
|
122 | 121 | }
|
0 commit comments