package org.adaptlab.chpir.android.survey.Models;

import java.util.Locale;

import org.adaptlab.chpir.android.activerecordcloudsync.SendModel;
import org.adaptlab.chpir.android.survey.AppUtil;
import org.adaptlab.chpir.android.survey.Location.LocationServiceManager;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class DeviceSyncEntry extends SendModel {
    private static final String TAG = "DeviceSyncEntry";
    private LocationServiceManager mLocationServiceManager;
    
    public DeviceSyncEntry() {
        mLocationServiceManager = LocationServiceManager.get(AppUtil.getContext());
        mLocationServiceManager.startLocationUpdates();
    }

    @Override
    public JSONObject toJSON() {
        Log.i(TAG, "Creating JSON for " + TAG);
        JSONObject json = new JSONObject();
        
        try {
            JSONObject jsonObject = new JSONObject();
            
            jsonObject.put("latitude", mLocationServiceManager.getLatitude());
            jsonObject.put("longitude", mLocationServiceManager.getLongitude());
            jsonObject.put("current_version", AppUtil.getVersionCode(AppUtil.getContext()));
            jsonObject.put("num_surveys", Survey.getAll().size());
            jsonObject.put("current_language", Locale.getDefault().getDisplayLanguage());
            jsonObject.put("instrument_versions", instrumentVersions().toString());
            jsonObject.put("device_uuid", AdminSettings.getInstance().getDeviceIdentifier());
            jsonObject.put("api_key", AdminSettings.getInstance().getApiKey());
            
            json.put("device_sync_entry", jsonObject);
        } catch (JSONException je) {
            Log.e(TAG, "JSON exception", je);
        }
        
        return json;
    }

    @Override
    public boolean isSent() { return false; }

    @Override
    public boolean readyToSend() { return true; }

    @Override
    public void setAsSent() { }
    
    @Override
    public boolean isPersistent() { return false; }
    
    private JSONObject instrumentVersions() {
        JSONObject json = new JSONObject();
        
        try {
            JSONObject jsonObject = new JSONObject();
            
            for (Instrument instrument : Instrument.getAll()) {
                jsonObject.put(instrument.getTitle(), instrument.getVersionNumber());
            }
            
            json.put("instrument_versions", jsonObject);
        } catch (JSONException je) {
            Log.e(TAG, "JSON exception", je);
        }
        
        return json;
    }
}

@@ -68,16 +68,43 @@
 .Locale;
+%0Aimport java.util.TimeZone;
 %0A%0Aimport
@@ -1616,24 +1616,142 @@
 tApiKey());%0A
+            jsonObject.put(%22timezone%22, TimeZone.getDefault().getDisplayName() + %22 %22 + TimeZone.getDefault().getID());%0A
             

Add current device timezone to device sync entry model