package com.sjriley.zappit.services; import android.app.IntentService; import android.content.Intent; import android.content.SharedPreferences; import android.util.Log; import com.sjriley.zappit.PreferencesActivity; import com.sjriley.zappit.models.CampaignModel; import com.sjriley.zappit.models.CodeCheckModel; import com.sjriley.zappit.utils.ConnectionUtils; import com.sjriley.zappit.vo.CampaignResponse; import com.sjriley.zappit.vo.CodeCheckResponse; public class CampaignService extends IntentService { public final static String NAME = "locationService"; public final static int FAILED_RETRY_WAIT = 5000; public final static int RE_DOWNLOAD_WAIT = 50000; private static final String TAG = CampaignService.class.getSimpleName(); public CampaignService() { super(NAME); } @Override protected void onHandleIntent(Intent arg0) { Log.d(TAG, "Starting campaign download"); //try downloading every hour while(true) { //try to get code details every 5 seconds while (getCodeDetails() == false) { try { synchronized (this) { wait(FAILED_RETRY_WAIT); } } catch (InterruptedException e) { e.printStackTrace(); return; } } //pointless? //wait an hour before trying again synchronized (this) { Log.d(TAG, "waiting before downloading again"); try { wait(RE_DOWNLOAD_WAIT); } catch (InterruptedException e) { e.printStackTrace(); } getCodeDetails(); } } } public boolean getCodeDetails() { Log.d(TAG, "Get Code details called"); SharedPreferences prefs = getSharedPreferences(PreferencesActivity.DEFAULT_PREFERECES, 0); String userId = prefs.getString(PreferencesActivity.USER_ID, ""); Log.d(TAG, "Get Code details called with user_id" + userId); if (userId == "") { return false; } CampaignModel model = new CampaignModel(this.getApplicationContext()); return model.downloadCampaigns(userId); } }