OSDN Git Service

analytics: send anonymous usage information
authorHugo <hugo@jidemail.com>
Wed, 6 Jul 2016 03:28:38 +0000 (11:28 +0800)
committerChih-Wei Huang <cwhuang@linux.org.tw>
Sun, 10 Dec 2017 15:43:48 +0000 (23:43 +0800)
Send usage information to google analytics when applications are started
or throws exceptions.

Android.mk
core/java/android/app/Activity.java
core/java/com/android/internal/os/RuntimeInit.java

index dfcfa2b..e3cc2b5 100644 (file)
@@ -623,6 +623,8 @@ LOCAL_PROTOC_OPTIMIZE_TYPE := stream
 LOCAL_PROTOC_FLAGS := \
     -Iexternal/protobuf/src
 
+LOCAL_STATIC_JAVA_LIBRARIES += analytics-utils
+
 LOCAL_MODULE := framework
 
 LOCAL_DX_FLAGS := --core-library --multi-dex
index 2c04f8f..2436fe8 100644 (file)
@@ -126,6 +126,8 @@ import com.android.internal.app.WindowDecorActionBar;
 import com.android.internal.policy.DecorView;
 import com.android.internal.policy.PhoneWindow;
 
+import org.android_x86.analytics.AnalyticsHelper;
+
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.lang.annotation.Retention;
@@ -759,6 +761,7 @@ public class Activity extends ContextThemeWrapper
     boolean mFinished;
     boolean mStartedActivity;
     private boolean mDestroyed;
+    private boolean mAppsStatistics;
     private boolean mDoReportFullyDrawn = true;
     private boolean mRestoredFromBundle;
 
@@ -1018,6 +1021,7 @@ public class Activity extends ContextThemeWrapper
         }
         mRestoredFromBundle = savedInstanceState != null;
         mCalled = true;
+        mAppsStatistics = SystemProperties.getBoolean("persist.sys.apps_statistics", false);
     }
 
     /**
@@ -1242,6 +1246,12 @@ public class Activity extends ContextThemeWrapper
 
         mFragments.doLoaderStart();
 
+        // region @android-x86-analytics
+        // screen view
+        if (mAppsStatistics) {
+            AnalyticsHelper.hitScreen(this);
+        }
+        // endregion
         getApplication().dispatchActivityStarted(this);
 
         if (mAutoFillResetNeeded) {
index 66475e4..d67c961 100644 (file)
@@ -38,6 +38,8 @@ import java.util.TimeZone;
 import java.util.logging.LogManager;
 import org.apache.harmony.luni.internal.util.TimezoneGetter;
 
+import org.android_x86.analytics.AnalyticsHelper;
+
 /**
  * Main entry point for runtime initialization.  Not for
  * public consumption.
@@ -61,6 +63,12 @@ public class RuntimeInit {
         return Log.printlns(Log.LOG_ID_CRASH, Log.ERROR, tag, msg, tr);
     }
 
+    // region @android-x86-analytics
+    // delay 120 seconds if Analytics failed to capture exception
+    private static final long ANDROID_X86_ANALYTICS_FAILED_DELAY = 120 * 1000;
+    private static long mAnalyticsEnableTime = 0;
+    // endregion
+
     /**
      * Logs a message when a thread encounters an uncaught exception. By
      * default, {@link KillApplicationHandler} will terminate this process later,
@@ -105,6 +113,23 @@ public class RuntimeInit {
                 if (mCrashing) return;
                 mCrashing = true;
 
+                // region @android-x86-analytics
+                if (System.currentTimeMillis() > mAnalyticsEnableTime &&
+                        SystemProperties.getBoolean("persist.sys.apps_statistics", false)) {
+                    try {
+                        AnalyticsHelper.captureException(
+                                ActivityThread.currentActivityThread().getSystemContext(),
+                                e,
+                                t.getName(),
+                                ActivityThread.currentPackageName());
+                    } catch (Throwable ex) {
+                        // delay some time to avoid endless loop exception
+                        mAnalyticsEnableTime =
+                                System.currentTimeMillis() + ANDROID_X86_ANALYTICS_FAILED_DELAY;
+                    }
+                }
+                // endregion
+
                 // Try to end profiling. If a profiler is running at this point, and we kill the
                 // process (below), the in-memory buffer will be lost. So try to stop, which will
                 // flush the buffer. (This makes method trace profiling useful to debug crashes.)