PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/app/src/main/java/com/nitw2014/mutual/SyncAdapter.java

https://gitlab.com/kotaprabhakar/mutualapp
Java | 295 lines | 227 code | 59 blank | 9 comment | 35 complexity | 3f3e9a2b5dac44d473cf24d1059c3a7f MD5 | raw file
  1. package com.nitw2014.mutual;
  2. import android.accounts.Account;
  3. import android.content.AbstractThreadedSyncAdapter;
  4. import android.content.ContentProviderClient;
  5. import android.content.ContentResolver;
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.content.SharedPreferences;
  9. import android.content.SyncResult;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16. import java.io.BufferedReader;
  17. import java.io.IOException;
  18. import java.io.InputStreamReader;
  19. import java.io.OutputStreamWriter;
  20. import java.net.HttpURLConnection;
  21. import java.net.MalformedURLException;
  22. import java.net.ProtocolException;
  23. import java.net.URL;
  24. import java.text.ParseException;
  25. import java.text.SimpleDateFormat;
  26. import java.util.Date;
  27. import java.util.HashMap;
  28. /**
  29. * Created by prabhakar on 30/09/16.
  30. */
  31. class SyncAdapter extends AbstractThreadedSyncAdapter {
  32. private static final String URL_DASHBOARD_DATA = "http://www.mutualing.com/uservote/admin_edit/getAssignedTask";
  33. private static final String URL_GET_SHARED_TASK = "http://www.mutualing.com/uservote/admin_edit/getSharedTask";
  34. private static final String URL_GET_ASKED_TASK = "http://www.mutualing.com/uservote/admin_edit/getTask";
  35. private static final String URL_GET_FRIENDS = "http://www.mutualing.com/uservote/admin_edit/getFriendList";
  36. private static final String LATEST_DATE_ASKED = "LatestDateAsked";
  37. private static final String LATEST_DATE_SHARED = "LatestDateShared";
  38. private static final String LATEST_DATE_PERFORMED = "LatestDatePerformed";
  39. private static final String LATEST_DATE_FRIENDS = "LatestDateFriends";
  40. private static final String DEFAULT_STRING = "NaN";
  41. private static final String TIMESTAMP = "timestamp";
  42. private ContentResolver contentResolver;
  43. Context context;
  44. private String storedDateAsked, storedDateShared, storedDatePerformed, storedDateFriends;
  45. private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  46. private SharedPreferences sharedPreferences;
  47. SyncAdapter(Context context, boolean autoInitialize){
  48. super(context, autoInitialize);
  49. this.context = context;
  50. contentResolver = context.getContentResolver();
  51. }
  52. public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs){
  53. super(context, autoInitialize, allowParallelSyncs);
  54. contentResolver = context.getContentResolver();
  55. this.context = context;
  56. }
  57. @Override
  58. public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
  59. sharedPreferences = context.getSharedPreferences(context.getString(R.string.mutual_shared_preferences), Context.MODE_PRIVATE);
  60. storedDateAsked = sharedPreferences.getString(LATEST_DATE_ASKED, DEFAULT_STRING);
  61. storedDateShared = sharedPreferences.getString(LATEST_DATE_SHARED, DEFAULT_STRING);
  62. storedDatePerformed = sharedPreferences.getString(LATEST_DATE_PERFORMED, DEFAULT_STRING);
  63. storedDateFriends = sharedPreferences.getString(LATEST_DATE_FRIENDS, DEFAULT_STRING);
  64. //Dashboard
  65. handleActionQueryDashboardData();
  66. //Shared Tasks
  67. handleActionQueryGetSharedTask();
  68. //Asked Tasks
  69. handleActionQueryGetAskedTasks();
  70. //Friends
  71. handleActionQueryGetFriends();
  72. }
  73. private JSONArray networkQuery(String URL) throws JSONException, IOException {
  74. String temporaryString, inputString = "";
  75. SharedPreferences sharedPreferences = context.getSharedPreferences(context.getString(R.string.mutual_shared_preferences), Context.MODE_PRIVATE);
  76. int userID = sharedPreferences.getInt("UserID", 0);
  77. JSONObject jsonObject = new JSONObject().put("UserID", userID);
  78. URL url = new URL(URL);
  79. HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
  80. urlConnection.setRequestMethod("POST");
  81. urlConnection.setDoInput(true);
  82. urlConnection.setDoOutput(true);
  83. urlConnection.connect();
  84. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream());
  85. outputStreamWriter.write(jsonObject.toString());
  86. outputStreamWriter.flush();
  87. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  88. while((temporaryString = bufferedReader.readLine())!=null)
  89. {
  90. inputString = inputString + temporaryString;
  91. }
  92. return ((new JSONObject(inputString)).getJSONArray("var"));
  93. }
  94. private Uri handleActionQueryGetFriends()
  95. {
  96. Uri newUri = null;
  97. try{
  98. String dataDate_String;
  99. Date dataDate, storedDate = (!storedDateFriends.equals(DEFAULT_STRING))?dateFormat.parse(storedDateFriends): null, tempDate = storedDate;
  100. JSONArray inputArray = networkQuery(URL_GET_FRIENDS);
  101. Log.d("SyncAdapter", "Friends: " + inputArray.toString());
  102. for(int i = 0; i < inputArray.length(); i++){
  103. JSONObject tempJSON = inputArray.getJSONObject(i);
  104. dataDate_String = tempJSON.getString(TIMESTAMP);
  105. dataDate = dateFormat.parse(dataDate_String);
  106. if (storedDateFriends.equals(DEFAULT_STRING) || dataDate.after(storedDate)) {
  107. ContentValues newValues = new ContentValues();
  108. newValues.put(ProviderContractTask.FriendsList.FRIEND, tempJSON.get("first_name").toString() + " " + tempJSON.get("last_name").toString());
  109. newValues.put(ProviderContractTask.FriendsList.FRIEND_ID, tempJSON.get("frndId").toString());
  110. newUri = contentResolver.insert(ProviderContractTask.FriendsList.CONTENT_URI, newValues);
  111. }
  112. if(tempDate == null || dataDate.after(tempDate)) {
  113. tempDate = dataDate;
  114. (sharedPreferences.edit()).putString(LATEST_DATE_FRIENDS, dataDate_String).apply();
  115. Log.d("SyncAdapter", dataDate_String);
  116. }
  117. }
  118. }catch(Exception ex){
  119. Log.d("SyncAdapter","Friends-Got an exception");
  120. if(ex.getMessage() != null)
  121. Log.d("SyncAdapter",ex.getMessage());
  122. }
  123. return newUri;
  124. }
  125. /**
  126. */
  127. private Uri handleActionQueryGetAskedTasks(){
  128. Uri newUri = null;
  129. try{
  130. String dataDate_String;
  131. Date dataDate, storedDate = (!storedDateAsked.equals(DEFAULT_STRING))?dateFormat.parse(storedDateAsked): null, tempDate = storedDate;
  132. Log.d("SyncAdapter", "AskedTask Stored- " + storedDateAsked);
  133. JSONArray inputArray = networkQuery(URL_GET_ASKED_TASK);
  134. Log.d("SyncAdapter", "AskedTask: " + inputArray.toString());
  135. for(int i = 0; i < inputArray.length(); i++){
  136. JSONObject tempJSON = inputArray.getJSONObject(i);
  137. dataDate_String = tempJSON.getString(TIMESTAMP);
  138. dataDate = dateFormat.parse(dataDate_String);
  139. if (storedDateAsked.equals(DEFAULT_STRING) || dataDate.after(storedDate)) {
  140. ContentValues newValues = new ContentValues();
  141. newValues.put(ProviderContractTask.AskedHistory.TASK, tempJSON.get("task_description").toString());
  142. newValues.put(ProviderContractTask.AskedHistory.TASK_ID, tempJSON.get("id").toString());
  143. newValues.putNull(ProviderContractTask.AskedHistory.SUBMITTER);
  144. newValues.putNull(ProviderContractTask.AskedHistory.SUBMITTER_ID);
  145. newUri = contentResolver.insert(ProviderContractTask.AskedHistory.CONTENT_URI, newValues);
  146. }
  147. if(tempDate == null || dataDate.after(tempDate)) {
  148. tempDate = dataDate;
  149. (sharedPreferences.edit()).putString(LATEST_DATE_ASKED, dataDate_String).apply();
  150. Log.d("SyncAdapter", dataDate_String);
  151. }
  152. }
  153. }catch(Exception ex){
  154. Log.d("SyncAdapter","AskedHistory-Got an exception");
  155. if(ex.getMessage() != null)
  156. Log.d("SyncAdapter",ex.getMessage());
  157. }
  158. return newUri;
  159. }
  160. private Uri handleActionQueryGetSharedTask(){
  161. Uri newUri = null;
  162. try{
  163. String dataDate_String;
  164. Date dataDate, storedDate = (!storedDateShared.equals(DEFAULT_STRING))?dateFormat.parse(storedDateShared):null, tempDate = storedDate;
  165. Log.d("SyncAdapter", "SharedTask Stored- " + storedDateShared);
  166. JSONArray inputArray = networkQuery(URL_GET_SHARED_TASK);
  167. Log.d("SyncAdapter", "SharedTask: " + inputArray.toString());
  168. for(int i = 0; i < inputArray.length(); i++){
  169. JSONObject tempJSON = inputArray.getJSONObject(i);
  170. dataDate_String = tempJSON.getString(TIMESTAMP);
  171. dataDate = dateFormat.parse(dataDate_String);
  172. if (storedDateShared.equals(DEFAULT_STRING) || dataDate.after(storedDate)) {
  173. ContentValues newValues = new ContentValues();
  174. newValues.put(ProviderContractTask.SharedHistory.TASK, tempJSON.get("task_description").toString());
  175. newValues.put(ProviderContractTask.SharedHistory.TASK_ID, tempJSON.get("task_id").toString());
  176. newValues.put(ProviderContractTask.SharedHistory.SUBMITTER, tempJSON.get("first_name").toString() + " " + tempJSON.get("last_name").toString());
  177. newValues.putNull(ProviderContractTask.SharedHistory.SUBMITTER_ID);
  178. newUri = contentResolver.insert(ProviderContractTask.SharedHistory.CONTENT_URI, newValues);
  179. }
  180. if(tempDate == null || dataDate.after(tempDate)) {
  181. tempDate = dataDate;
  182. (sharedPreferences.edit()).putString(LATEST_DATE_SHARED, dataDate_String).apply();
  183. Log.d("SyncAdapter", dataDate_String);
  184. }
  185. }
  186. }catch(Exception ex){
  187. Log.d("SyncAdapter","SharedHistory-Got an exception");
  188. if(ex.getMessage() != null)
  189. Log.d("SyncAdapter",ex.getMessage());
  190. }
  191. return newUri;
  192. }
  193. private Uri handleActionQueryDashboardData(){
  194. Uri newUri = null;
  195. try{
  196. String dataDate_String;
  197. Date dataDate, storedDate = (!storedDatePerformed.equals(DEFAULT_STRING))?dateFormat.parse(storedDatePerformed):null, tempDate = storedDate;
  198. Log.d("SyncAdapter", "PerformedTask Stored- " + storedDatePerformed);
  199. JSONArray inputArray = networkQuery(URL_DASHBOARD_DATA);
  200. Log.d("SyncAdapter", "PerformedTask: " + inputArray.toString());
  201. for(int i = 0; i < inputArray.length(); i++){
  202. JSONObject tempJSON = inputArray.getJSONObject(i);
  203. dataDate_String = tempJSON.getString(TIMESTAMP);
  204. dataDate = dateFormat.parse(dataDate_String);
  205. if (storedDatePerformed.equals(DEFAULT_STRING) || dataDate.after(storedDate)) {
  206. if (tempJSON.get("task_answer").toString().equals(null) || tempJSON.get("task_answer").toString().equals("")) {
  207. ContentValues newValues = new ContentValues();
  208. newValues.put(ProviderContractTask.TasksAssigned.TASK, tempJSON.get("task_description").toString());
  209. newValues.putNull(ProviderContractTask.TasksAssigned.TASK_ID);
  210. newValues.put(ProviderContractTask.TasksAssigned.SUBMITTER, tempJSON.get("first_name").toString() + " " + tempJSON.get("last_name").toString());
  211. newValues.putNull(ProviderContractTask.TasksAssigned.SUBMITTER_ID);
  212. newUri = contentResolver.insert(ProviderContractTask.TasksAssigned.CONTENT_URI, newValues);
  213. } else {
  214. ContentValues newValues = new ContentValues();
  215. newValues.put(ProviderContractTask.PerformedHistory.TASK, tempJSON.get("task_description").toString());
  216. newValues.putNull(ProviderContractTask.PerformedHistory.TASK_ID);
  217. newValues.put(ProviderContractTask.PerformedHistory.ANSWER, tempJSON.get("task_answer").toString());
  218. newValues.put(ProviderContractTask.PerformedHistory.SUBMITTER, tempJSON.get("first_name").toString() + " " + tempJSON.get("last_name").toString());
  219. newValues.putNull(ProviderContractTask.PerformedHistory.SUBMITTER_ID);
  220. newUri = contentResolver.insert(ProviderContractTask.PerformedHistory.CONTENT_URI, newValues);
  221. }
  222. }
  223. if(tempDate == null || dataDate.after(tempDate)) {
  224. tempDate = dataDate;
  225. (sharedPreferences.edit()).putString(LATEST_DATE_PERFORMED, dataDate_String).apply();
  226. Log.d("SyncAdapter", dataDate_String);
  227. }
  228. }
  229. }catch(Exception ex){
  230. Log.d("SyncAdapter","PerformedHistory-Got an exception");
  231. if(ex.getMessage() != null)
  232. Log.d("SyncAdapter",ex.getMessage());
  233. }
  234. return newUri;
  235. }
  236. }