forked from organicmaps/organicmaps
[alohalytics][android] Reliable statistics delivery.
This commit is contained in:
parent
6c03ca3939
commit
04c050dcd1
6 changed files with 69 additions and 15 deletions
|
@ -46,7 +46,10 @@ Built-in logged events
|
|||
- $update
|
||||
- $androidIds
|
||||
- $androidDeviceInfo
|
||||
|
||||
- $startSession
|
||||
- $endSession
|
||||
- $onStart
|
||||
- $onStop
|
||||
|
||||
Other platforms
|
||||
======
|
||||
|
|
|
@ -64,6 +64,20 @@ public class MainActivity extends Activity {
|
|||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
// onStart and onStop are needed for reliable session tracking/uploading.
|
||||
Statistics.onStart(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
// onStart and onStop are needed for reliable session tracking/uploading.
|
||||
Statistics.onStop(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
|
|
@ -50,6 +50,9 @@ public class ConnectivityChangedReceiver extends BroadcastReceiver {
|
|||
}
|
||||
|
||||
public void onNetworkConnected() {
|
||||
org.alohalytics.Statistics.forceUpload();
|
||||
// Do not break active session, it will be sent later, when user close all app's activities.
|
||||
if (!org.alohalytics.Statistics.isSessionActive()) {
|
||||
org.alohalytics.Statistics.forceUpload();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,10 +24,13 @@
|
|||
|
||||
package org.alohalytics;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.util.Pair;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -38,6 +41,8 @@ public class Statistics {
|
|||
|
||||
private static final String TAG = "Alohalytics";
|
||||
private static boolean sDebugModeEnabled = false;
|
||||
private static int sActivitiesCounter = 0;
|
||||
private static long sSessionStartTimeInNanoSeconds;
|
||||
|
||||
public static void setDebugMode(boolean enable) {
|
||||
sDebugModeEnabled = enable;
|
||||
|
@ -51,6 +56,46 @@ public class Statistics {
|
|||
return sDebugModeEnabled;
|
||||
}
|
||||
|
||||
public static boolean isSessionActive() {
|
||||
return sActivitiesCounter > 0;
|
||||
}
|
||||
|
||||
// Should be called from every activity's onStart for
|
||||
// reliable data delivery and session tracking.
|
||||
public static void onStart(Activity activity) {
|
||||
// TODO(AlexZ): Create instance in setup and check that it was called before onStart.
|
||||
if (sActivitiesCounter == 0) {
|
||||
sSessionStartTimeInNanoSeconds = System.nanoTime();
|
||||
logEvent("$startSession");
|
||||
}
|
||||
++sActivitiesCounter;
|
||||
logEvent("$onStart", activity.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
// Should be called from every activity's onStop for
|
||||
// reliable data delivery and session tracking.
|
||||
// If another activity of the same app is started, it's onStart is called
|
||||
// before onStop of the previous activity of the same app.
|
||||
public static void onStop(Activity activity) {
|
||||
if (sActivitiesCounter == 0) {
|
||||
throw new IllegalStateException("onStop() is called before onStart()");
|
||||
}
|
||||
--sActivitiesCounter;
|
||||
logEvent("$onStop", activity.getClass().getSimpleName());
|
||||
if (sActivitiesCounter == 0) {
|
||||
final long currentTimeInNanoSeconds = System.nanoTime();
|
||||
final long sessionLengthInSeconds =
|
||||
(currentTimeInNanoSeconds - sSessionStartTimeInNanoSeconds) / 1000000000;
|
||||
logEvent("$endSession", String.valueOf(sessionLengthInSeconds));
|
||||
// Send data only if connected to any network.
|
||||
final ConnectivityManager manager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
final NetworkInfo info = manager.getActiveNetworkInfo();
|
||||
if (info != null && info.isConnected()) {
|
||||
forceUpload();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Passed serverUrl will be modified to $(serverUrl)/android/packageName/versionCode
|
||||
public static void setup(String serverUrl, final Context context) {
|
||||
final String storagePath = context.getFilesDir().getAbsolutePath() + "/Alohalytics/";
|
||||
|
|
|
@ -62,18 +62,6 @@ public class SystemInfo {
|
|||
public void run() {
|
||||
collectIds(context);
|
||||
collectDeviceDetails(context);
|
||||
// Force statistics uploading because if user immediately uninstalls the app we won't even know about installation.
|
||||
// But do it only if we are already connected to any network.
|
||||
try {
|
||||
final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
|
||||
if (networkInfo != null && networkInfo.isConnected()) {
|
||||
Statistics.forceUpload();
|
||||
}
|
||||
}
|
||||
catch(Exception ex) {
|
||||
handleException(ex);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
|
|
@ -41,13 +41,14 @@ public class BaseMwmFragmentActivity extends AppCompatActivity
|
|||
{
|
||||
super.onStart();
|
||||
Statistics.INSTANCE.startActivity(this);
|
||||
|
||||
MRMyTracker.onStartActivity(this);
|
||||
org.alohalytics.Statistics.onStart(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop()
|
||||
{
|
||||
org.alohalytics.Statistics.onStop(this);
|
||||
Statistics.INSTANCE.stopActivity(this);
|
||||
super.onStop();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue