-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#55 Upload ApplicationOpenedEvents to server
- Loading branch information
Jo Grimstad
committed
Jun 7, 2017
1 parent
617f963
commit 47051f0
Showing
8 changed files
with
195 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 82 additions & 8 deletions
90
app/src/main/java/org/literacyapp/analytics/service/ServerSynchronizationJobService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,90 @@ | ||
package org.literacyapp.analytics.service; | ||
|
||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.os.IBinder; | ||
import android.app.job.JobParameters; | ||
import android.app.job.JobService; | ||
import android.os.Environment; | ||
import android.util.Log; | ||
|
||
public class ServerSynchronizationJobService extends Service { | ||
public ServerSynchronizationJobService() { | ||
import org.literacyapp.analytics.util.EnvironmentSettings; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
/** | ||
* Service responsible for uploading event files to server. | ||
* | ||
* This service is triggered in the @{link {@link org.literacyapp.analytics.receiver.BootReceiver}} | ||
*/ | ||
public class ServerSynchronizationJobService extends JobService { | ||
|
||
@Override | ||
public boolean onStartJob(JobParameters jobParameters) { | ||
Log.i(getClass().getName(), "onStartJob"); | ||
|
||
String logsPath = Environment.getExternalStorageDirectory() + "/.literacyapp-analytics/events"; | ||
File eventsDir = new File(logsPath); | ||
Log.i(getClass().getName(), "eventsDir: " + eventsDir); | ||
Log.i(getClass().getName(), "eventsDir.exists(): " + eventsDir.exists()); | ||
if (eventsDir.exists()) { | ||
File[] deviceDirs = eventsDir.listFiles(); | ||
for (int i = 0; i < deviceDirs.length; i++) { | ||
File deviceDir = deviceDirs[i]; | ||
Log.i(getClass().getName(), "deviceDir: " + deviceDir); | ||
|
||
File[] eventFiles = deviceDir.listFiles(); | ||
for (int j = 0; j < eventFiles.length; j++) { | ||
File eventFile = eventFiles[j]; | ||
Log.i(getClass().getName(), "eventFile: " + eventFile); | ||
// Expected filename: "application_opened_events_yyyy-MM-dd.log" | ||
|
||
if (eventFile.getName().startsWith("application_opened_events_")) { | ||
// Skip files generated more than 7 days ago | ||
String dateAsString = eventFile.getName().replace("application_opened_events_", "").replace(".log", ""); | ||
Log.i(getClass().getName(), "dateAsString: " + dateAsString); | ||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); | ||
try { | ||
Date date = simpleDateFormat.parse(dateAsString); | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTime(date); | ||
Log.i(getClass().getName(), "calendar.getTime(): " + calendar.getTime()); | ||
|
||
Calendar calendar7DaysAgo = Calendar.getInstance(); | ||
calendar7DaysAgo.setTime(calendar.getTime()); | ||
calendar7DaysAgo.add(Calendar.DAY_OF_MONTH, -7); | ||
Log.i(getClass().getName(), "calendar7DaysAgo.getTime(): " + calendar7DaysAgo.getTime()); | ||
|
||
if (!calendar.before(calendar7DaysAgo)) { | ||
Log.i(getClass().getName(), "Uploading to web server: " + eventFile); | ||
|
||
new UploadApplicationOpenedEventsAsyncTask().execute(eventFile); | ||
} | ||
} catch (ParseException e) { | ||
Log.e(getClass().getName(), null, e); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
boolean asynchronousProcessing = false; | ||
return asynchronousProcessing; | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
// TODO: Return the communication channel to the service. | ||
throw new UnsupportedOperationException("Not yet implemented"); | ||
public boolean onStopJob(JobParameters jobParameters) { | ||
Log.i(getClass().getName(), "onStopJob"); | ||
|
||
boolean restartJob = false; | ||
return restartJob; | ||
} | ||
} |
Oops, something went wrong.