Fetch commands since last sync

pull/286/head
Alinson S. Xavier 10 years ago
parent 56e1268f85
commit b0040bd83c

@ -39,7 +39,11 @@ dependencies {
compile 'com.github.paolorotolo:appintro:3.4.0' compile 'com.github.paolorotolo:appintro:3.4.0'
compile 'org.apmem.tools:layouts:1.10@aar' compile 'org.apmem.tools:layouts:1.10@aar'
compile 'com.opencsv:opencsv:3.7' compile 'com.opencsv:opencsv:3.7'
compile 'com.github.nkzawa:socket.io-client:0.3.0'
compile ('io.socket:socket.io-client:0.7.0') {
exclude group: 'org.json', module: 'json'
}
compile project(':libs:drag-sort-listview:library') compile project(':libs:drag-sort-listview:library')
compile files('libs/ActiveAndroid.jar') compile files('libs/ActiveAndroid.jar')

@ -19,13 +19,11 @@
package org.isoron.uhabits; package org.isoron.uhabits;
import android.content.SharedPreferences;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v7.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import org.isoron.uhabits.commands.Command; import org.isoron.uhabits.commands.Command;
import org.isoron.uhabits.commands.CommandParser; import org.isoron.uhabits.commands.CommandParser;
import org.isoron.uhabits.helpers.DatabaseHelper; import org.isoron.uhabits.helpers.DatabaseHelper;
@ -35,14 +33,19 @@ import org.json.JSONObject;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.LinkedList; import java.util.LinkedList;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class SyncManager public class SyncManager
{ {
public static final String EXECUTE_COMMAND = "executeCommand"; public static final String EXECUTE_COMMAND = "executeCommand";
public static final String POST_COMMAND = "postCommand"; public static final String POST_COMMAND = "postCommand";
public static final String SYNC_SERVER_URL = "http://10.0.2.2:4000"; public static final String SYNC_SERVER_URL = "http://sync.loophabits.org:4000";
private static String GROUP_KEY = "sEBY3poXHFH7EyB43V2JoQUNEtBjMgdD"; private static String GROUP_KEY;
private static String CLIENT_ID; private static String CLIENT_ID;
private final SharedPreferences prefs;
@NonNull @NonNull
private Socket socket; private Socket socket;
@ -53,6 +56,9 @@ public class SyncManager
{ {
this.activity = activity; this.activity = activity;
outbox = new LinkedList<>(); outbox = new LinkedList<>();
prefs = PreferenceManager.getDefaultSharedPreferences(activity);
GROUP_KEY = prefs.getString("syncKey", DatabaseHelper.getRandomId());
CLIENT_ID = DatabaseHelper.getRandomId(); CLIENT_ID = DatabaseHelper.getRandomId();
try try
@ -91,6 +97,24 @@ public class SyncManager
{ {
JSONObject authMsg = buildAuthMessage(); JSONObject authMsg = buildAuthMessage();
socket.emit("auth", authMsg.toString()); socket.emit("auth", authMsg.toString());
Long lastSync = prefs.getLong("lastSync", 0);
JSONObject fetchMsg = buildFetchMessage(lastSync);
socket.emit("fetchCommands", fetchMsg.toString());
}
private JSONObject buildFetchMessage(Long lastSync)
{
try
{
JSONObject json = new JSONObject();
json.put("since", lastSync);
return json;
}
catch (JSONException e)
{
throw new RuntimeException(e.getMessage());
}
} }
private JSONObject buildAuthMessage() private JSONObject buildAuthMessage()
@ -98,8 +122,8 @@ public class SyncManager
try try
{ {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("group_key", GROUP_KEY); json.put("groupKey", GROUP_KEY);
json.put("client_id", CLIENT_ID); json.put("clientId", CLIENT_ID);
json.put("version", BuildConfig.VERSION_NAME); json.put("version", BuildConfig.VERSION_NAME);
return json; return json;
} }
@ -117,6 +141,8 @@ public class SyncManager
{ {
try try
{ {
JSONObject root = new JSONObject(args[0].toString());
updateLastSync(root.getLong("timestamp"));
executeCommand(args[0]); executeCommand(args[0]);
} }
catch (JSONException e) catch (JSONException e)
@ -147,4 +173,9 @@ public class SyncManager
activity.executeCommand(received, null, false); activity.executeCommand(received, null, false);
Log.d("SyncManager", "Received command executed"); Log.d("SyncManager", "Received command executed");
} }
private void updateLastSync(Long timestamp)
{
prefs.edit().putLong("lastSync", timestamp).apply();
}
} }

@ -49,6 +49,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
setResultOnPreferenceClick("bugReport", MainActivity.RESULT_BUG_REPORT); setResultOnPreferenceClick("bugReport", MainActivity.RESULT_BUG_REPORT);
updateRingtoneDescription(); updateRingtoneDescription();
updateSyncDescription();
if(UIHelper.isLocaleFullyTranslated()) if(UIHelper.isLocaleFullyTranslated())
removePreference("translate", "linksCategory"); removePreference("translate", "linksCategory");
@ -101,9 +102,18 @@ public class SettingsFragment extends PreferenceFragmentCompat
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{ {
if(key.equals("syncKey"))
updateSyncDescription();
BackupManager.dataChanged("org.isoron.uhabits"); BackupManager.dataChanged("org.isoron.uhabits");
} }
private void updateSyncDescription()
{
SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
findPreference("syncKey").setSummary(preferences.getString("syncKey", ""));
}
@Override @Override
public boolean onPreferenceTreeClick(Preference preference) public boolean onPreferenceTreeClick(Preference preference)
{ {

@ -88,6 +88,17 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory
android:key="syncCategory"
android:title="Cloud sync">
<EditTextPreference
android:key="syncKey"
android:title="Key"
android:summary="%s"/>
</PreferenceCategory>
<PreferenceCategory <PreferenceCategory
android:key="pref_key_debug" android:key="pref_key_debug"
android:title="@string/troubleshooting"> android:title="@string/troubleshooting">

Loading…
Cancel
Save