2 * Copyright (c) 2015. The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.launcher3.stats.external;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager;
25 import android.os.Bundle;
26 import android.util.Log;
28 import com.android.launcher3.LauncherApplication;
29 import com.android.launcher3.stats.util.Logger;
30 import com.cyanogen.ambient.analytics.Event;
35 * Utility for interfacing with CyanogenStats
38 public class StatsUtil {
41 private static final String TAG = StatsUtil.class.getSimpleName();
44 private static final String KEY_TRACKING_ID = "tracking_id";
47 * Send an event to CyangenStats
49 * @param context {@link Context} not null
50 * @param trackingBundle {@link Bundle}
51 * @throws IllegalArgumentException
53 public static void sendEvent(Context context, Bundle trackingBundle)
54 throws IllegalArgumentException {
55 if (context == null) {
56 throw new IllegalArgumentException("'context' cannot be null!");
58 if (trackingBundle == null) {
59 throw new IllegalArgumentException("'trackingBundle' cannot be null!");
61 Logger.logd(TAG, "Stats collection: ENABLED!");
63 if (!trackingBundle.containsKey(KEY_TRACKING_ID)) {
64 Logger.logd(TAG, "No tracking id in bundle");
67 if (trackingBundle.containsKey(TrackingBundle.KEY_EVENT_CATEGORY)
68 && trackingBundle.containsKey(TrackingBundle.KEY_EVENT_ACTION)) {
70 final Event.Builder builder = new Event.Builder(
71 trackingBundle.getString(TrackingBundle.KEY_EVENT_CATEGORY),
72 trackingBundle.getString(TrackingBundle.KEY_EVENT_ACTION));
74 if (trackingBundle.containsKey(TrackingBundle.KEY_METADATA_ORIGIN)) {
75 builder.addField(TrackingBundle.KEY_METADATA_ORIGIN,
76 trackingBundle.getString(TrackingBundle.KEY_METADATA_ORIGIN));
78 if (trackingBundle.containsKey(TrackingBundle.KEY_METADATA_PACKAGE)) {
79 builder.addField(TrackingBundle.KEY_METADATA_PACKAGE,
80 trackingBundle.getString(TrackingBundle.KEY_METADATA_PACKAGE));
82 if (trackingBundle.containsKey(TrackingBundle.KEY_METADATA_VALUE)) {
83 builder.addField(TrackingBundle.KEY_METADATA_VALUE,
84 String.valueOf(trackingBundle.get(TrackingBundle.KEY_METADATA_VALUE)));
86 ((LauncherApplication)context.getApplicationContext()).sendEvent(builder.build());
88 Logger.logd(TAG, trackingBundle.toString());
90 Logger.logd(TAG, "Not a valid tracking bundle");