SyncAdapter.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package de.tudarmstadt.informatik.hostage.sync.android;
  2. import android.accounts.Account;
  3. import android.content.AbstractThreadedSyncAdapter;
  4. import android.content.ContentProviderClient;
  5. import android.content.Context;
  6. import android.content.SharedPreferences;
  7. import android.content.SyncResult;
  8. import android.os.Bundle;
  9. import android.preference.PreferenceManager;
  10. import android.util.Log;
  11. import java.io.StringWriter;
  12. import java.util.ArrayList;
  13. import de.tudarmstadt.informatik.hostage.logging.Record;
  14. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  15. import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
  16. /**
  17. * Created by abrakowski
  18. */
  19. public class SyncAdapter extends AbstractThreadedSyncAdapter {
  20. public static final String TAG = "SyncAdapter";
  21. private final SharedPreferences pref;
  22. private final SharedPreferences.Editor editor;
  23. private final HostageDBOpenHelper dbh;
  24. private Context context;
  25. public SyncAdapter(Context context, boolean autoInitialize) {
  26. this(context, autoInitialize, false);
  27. }
  28. public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
  29. super(context, autoInitialize, allowParallelSyncs);
  30. this.context = context;
  31. pref = PreferenceManager.getDefaultSharedPreferences(context);
  32. editor = pref.edit();
  33. dbh = new HostageDBOpenHelper(context);
  34. }
  35. /**
  36. * Called by the Android system in response to a request to run the sync adapter. The work
  37. * required to read data from the network, parse it, and store it in the content provider is
  38. * done here. Extending AbstractThreadedSyncAdapter ensures that all methods within SyncAdapter
  39. * run on a background thread. For this reason, blocking I/O and other long-running tasks can be
  40. * run <em>in situ</em>, and you don't have to set up a separate thread for them.
  41. .
  42. *
  43. * <p>This is where we actually perform any work required to perform a sync.
  44. * {@link AbstractThreadedSyncAdapter} guarantees that this will be called on a non-UI thread,
  45. * so it is safe to peform blocking I/O here.
  46. *
  47. * <p>The syncResult argument allows you to pass information back to the method that triggered
  48. * the sync.
  49. */
  50. @Override
  51. public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
  52. // We will only do a one-direction sync. That is, only sending data to the tracing server.
  53. // For bidirectional syncing, we will still have to decide how much data should be requested from the server,
  54. // because the server has relatively to a mobile device almost unlimited space.
  55. Log.i(TAG, "Beginning network synchronization");
  56. long lastSyncTime = pref.getLong("LAST_SYNC_TIME", 0);
  57. String serverAddress = pref.getString("pref_upload_server", "https://ssi.cased.de"); //"https://192.168.1.118:9999"
  58. Log.i(TAG, "Connecting to " + serverAddress);
  59. LogFilter filter = new LogFilter();
  60. filter.setAboveTimestamp(lastSyncTime);
  61. ArrayList<Record> records = dbh.getRecordsForFilter(filter);
  62. StringWriter writer = new StringWriter();
  63. int size = records.size();
  64. int offset = 1;
  65. int currOffset = 1;
  66. boolean error = false;
  67. for (Record record : records) {
  68. SyncUtils.appendRecordToStringWriter(record, writer);
  69. if(currOffset == 100 || offset == size){
  70. boolean success = SyncUtils.uploadRecordsToServer(writer.toString(), serverAddress);
  71. if(!success){
  72. syncResult.stats.numIoExceptions++;
  73. error = true;
  74. break;
  75. }
  76. writer.getBuffer().setLength(0);
  77. currOffset = 0;
  78. }
  79. offset++;
  80. currOffset++;
  81. }
  82. if(!error) pref.edit().putLong("LAST_SYNC_TIME", System.currentTimeMillis()).apply();
  83. Log.i(TAG, "Network synchronization complete");
  84. }
  85. }