summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/stats/external
diff options
context:
space:
mode:
authorMartin Brabham <optedoblivion@cyngn.com>2015-05-07 10:53:31 -0700
committercretin45 <cretin45@gmail.com>2016-01-04 16:24:03 -0800
commita20b046db71039ee581bb80274cf1fc450b3fd99 (patch)
tree1d3511baec094e8ea28589d5bd57411d4a11a66d /src/com/android/launcher3/stats/external
parent2be1306db542d11c4516fb29f2adce08f4c32e7d (diff)
downloadandroid_packages_apps_Trebuchet-a20b046db71039ee581bb80274cf1fc450b3fd99.tar.gz
android_packages_apps_Trebuchet-a20b046db71039ee581bb80274cf1fc450b3fd99.tar.bz2
android_packages_apps_Trebuchet-a20b046db71039ee581bb80274cf1fc450b3fd99.zip
Trebuchet Statistics:
- Add plumbing for sending events. - Add aggregation service that runs on some interval - Hook send events into respective UX interactions Change-Id: I77ba0ab75daf845621d800e0adf3e658096926af Trebuchet: send metrics values as strings - Also refer to the declared intent Change-Id: I3c99cc071aa2f4241ea8d3e6d2e8683f1f748e7d Signed-off-by: Roman Birg <roman@cyngn.com> Stop unnecessary exception posting to logcat. Change-Id: I4f741246dc2ee77b24c3bb94075e12535fcd7ead
Diffstat (limited to 'src/com/android/launcher3/stats/external')
-rw-r--r--src/com/android/launcher3/stats/external/StatsUtil.java117
-rw-r--r--src/com/android/launcher3/stats/external/TrackingBundle.java67
2 files changed, 184 insertions, 0 deletions
diff --git a/src/com/android/launcher3/stats/external/StatsUtil.java b/src/com/android/launcher3/stats/external/StatsUtil.java
new file mode 100644
index 000000000..697df542c
--- /dev/null
+++ b/src/com/android/launcher3/stats/external/StatsUtil.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2015. The CyanogenMod Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.launcher3.stats.external;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.os.Bundle;
+import android.util.Log;
+import com.android.launcher3.stats.util.Logger;
+
+/**
+ * StatsUtil
+ * <pre>
+ * Utility for interfacing with CyanogenStats
+ * </pre>
+ */
+public class StatsUtil {
+
+ // Tag and logging
+ private static final String TAG = StatsUtil.class.getSimpleName();
+
+ // Constants
+ private static final String KEY_TRACKING_ID = "tracking_id";
+ private static final String ANALYTIC_INTENT = "com.cyngn.stats.action.SEND_ANALYTIC_EVENT";
+ private static final String STATS_PACKAGE = "com.cyngn.stats";
+
+ /**
+ * Checks if stats collection is enabled
+ *
+ * @param context {@link android.content.Context}
+ * @return {@link java.lang.Boolean}
+ * @throws IllegalArgumentException {@link IllegalArgumentException}
+ */
+ public static boolean isStatsCollectionEnabled(Context context)
+ throws IllegalArgumentException {
+ return isStatsPackageInstalledAndSystemApp(context);
+ }
+
+ /**
+ * Checks if the stats package is installed
+ *
+ * @param context {@link android.content.Context}
+ * @return {@link Boolean {@link Boolean {@link Boolean {@link Boolean}}}}
+ */
+ private static boolean isStatsPackageInstalledAndSystemApp(Context context)
+ throws IllegalArgumentException {
+ if (context == null) {
+ throw new IllegalArgumentException("'context' cannot be null!");
+ }
+ try {
+ PackageInfo pi = context.getPackageManager().getPackageInfo(STATS_PACKAGE, 0);
+ boolean isSystemApp = (pi.applicationInfo.flags &
+ (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
+ return pi.applicationInfo.enabled && isSystemApp;
+ } catch (PackageManager.NameNotFoundException e) {
+ Log.e(TAG, "stats not found!");
+ return false;
+ }
+ }
+
+ /**
+ * Send an event to CyangenStats
+ *
+ * @param context {@link Context} not null
+ * @param trackingBundle {@link Bundle}
+ * @throws IllegalArgumentException
+ */
+ public static void sendEvent(Context context, Bundle trackingBundle)
+ throws IllegalArgumentException {
+ if (context == null) {
+ throw new IllegalArgumentException("'context' cannot be null!");
+ }
+ if (trackingBundle == null) {
+ throw new IllegalArgumentException("'trackingBundle' cannot be null!");
+ }
+ if (!isStatsCollectionEnabled(context)) {
+ Logger.logd(TAG, "Stats collection: DISABLED!");
+ return;
+ }
+ Logger.logd(TAG, "Stats collection: ENABLED!");
+
+ Intent newIntent = new Intent(ANALYTIC_INTENT);
+
+ if (!trackingBundle.containsKey(KEY_TRACKING_ID)) {
+ Logger.logd(TAG, "No tracking id in bundle");
+ return;
+ } else {
+ if (trackingBundle.containsKey(TrackingBundle.KEY_EVENT_CATEGORY)
+ && trackingBundle.containsKey(TrackingBundle.KEY_EVENT_ACTION)) {
+ Logger.logd(TAG, trackingBundle.toString());
+ newIntent.putExtras(trackingBundle);
+ context.sendBroadcast(newIntent);
+ } else {
+ Logger.logd(TAG, "Not a valid tracking bundle");
+ }
+ }
+ }
+
+}
diff --git a/src/com/android/launcher3/stats/external/TrackingBundle.java b/src/com/android/launcher3/stats/external/TrackingBundle.java
new file mode 100644
index 000000000..6ca5d971e
--- /dev/null
+++ b/src/com/android/launcher3/stats/external/TrackingBundle.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2015. The CyanogenMod Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.launcher3.stats.external;
+
+import android.os.Bundle;
+import android.text.TextUtils;
+
+/**
+ * <pre>
+ * Extension of a {@link Bundle} to provider streamline interfaces for
+ * the specific task of sending events
+ * </pre>
+ *
+ * @see {@link Bundle}
+ */
+public class TrackingBundle {
+
+ // Constants
+ public static final String KEY_TRACKING_ID = "tracking_id";
+ public static final String KEY_EVENT_CATEGORY = "category";
+ public static final String KEY_EVENT_ACTION = "action";
+ public static final String KEY_METADATA_VALUE = "value";
+ public static final String KEY_METADATA_ORIGIN = "origin";
+ public static final String KEY_METADATA_PACKAGE = "package";
+
+
+ /**
+ * Constructor
+ *
+ * @param trackingId {@link String}
+ * @param category {@link String}
+ * @param action {@link String}
+ * @throws IllegalArgumentException {@link IllegalArgumentException}
+ */
+ public static Bundle createTrackingBundle(String trackingId, String category, String action)
+ throws IllegalArgumentException {
+ if (TextUtils.isEmpty(trackingId)) {
+ throw new IllegalArgumentException("'trackingId' cannot be null or empty!");
+ }
+ if (TextUtils.isEmpty(category)) {
+ throw new IllegalArgumentException("'category' cannot be null or empty!");
+ }
+ if (TextUtils.isEmpty(action)) {
+ throw new IllegalArgumentException("'action' cannot be null or empty!");
+ }
+ Bundle bundle = new Bundle();
+ bundle.putString(KEY_EVENT_CATEGORY, category);
+ bundle.putString(KEY_EVENT_ACTION, action);
+ bundle.putString(KEY_TRACKING_ID, trackingId);
+ return bundle;
+ }
+
+} \ No newline at end of file