OSDN Git Service

Revert "Trebuchet: always set initial wallpaper offsets"
[android-x86/packages-apps-Trebuchet.git] / src / com / android / launcher3 / stats / external / StatsUtil.java
1 /*
2  *  Copyright (c) 2015. The CyanogenMod Project
3  *
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
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.launcher3.stats.external;
18
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;
27
28 import com.android.launcher3.LauncherApplication;
29 import com.android.launcher3.stats.util.Logger;
30 import com.cyanogen.ambient.analytics.Event;
31
32 /**
33  * StatsUtil
34  * <pre>
35  *     Utility for interfacing with CyanogenStats
36  * </pre>
37  */
38 public class StatsUtil {
39
40     // Tag and logging
41     private static final String TAG = StatsUtil.class.getSimpleName();
42
43     // Constants
44     private static final String KEY_TRACKING_ID = "tracking_id";
45
46     /**
47      * Send an event to CyangenStats
48      *
49      * @param context        {@link Context} not null
50      * @param trackingBundle {@link Bundle}
51      * @throws IllegalArgumentException
52      */
53     public static void sendEvent(Context context, Bundle trackingBundle)
54             throws IllegalArgumentException {
55         if (context == null) {
56             throw new IllegalArgumentException("'context' cannot be null!");
57         }
58         if (trackingBundle == null) {
59             throw new IllegalArgumentException("'trackingBundle' cannot be null!");
60         }
61         Logger.logd(TAG, "Stats collection: ENABLED!");
62
63         if (!trackingBundle.containsKey(KEY_TRACKING_ID)) {
64             Logger.logd(TAG, "No tracking id in bundle");
65             return;
66         } else {
67             if (trackingBundle.containsKey(TrackingBundle.KEY_EVENT_CATEGORY)
68                     && trackingBundle.containsKey(TrackingBundle.KEY_EVENT_ACTION)) {
69
70                 final Event.Builder builder = new Event.Builder(
71                         trackingBundle.getString(TrackingBundle.KEY_EVENT_CATEGORY),
72                         trackingBundle.getString(TrackingBundle.KEY_EVENT_ACTION));
73
74                 if (trackingBundle.containsKey(TrackingBundle.KEY_METADATA_ORIGIN)) {
75                     builder.addField(TrackingBundle.KEY_METADATA_ORIGIN,
76                             trackingBundle.getString(TrackingBundle.KEY_METADATA_ORIGIN));
77                 }
78                 if (trackingBundle.containsKey(TrackingBundle.KEY_METADATA_PACKAGE)) {
79                     builder.addField(TrackingBundle.KEY_METADATA_PACKAGE,
80                             trackingBundle.getString(TrackingBundle.KEY_METADATA_PACKAGE));
81                 }
82                 if (trackingBundle.containsKey(TrackingBundle.KEY_METADATA_VALUE)) {
83                     builder.addField(TrackingBundle.KEY_METADATA_VALUE,
84                             String.valueOf(trackingBundle.get(TrackingBundle.KEY_METADATA_VALUE)));
85                 }
86                 ((LauncherApplication)context.getApplicationContext()).sendEvent(builder.build());
87
88                 Logger.logd(TAG, trackingBundle.toString());
89             } else {
90                 Logger.logd(TAG, "Not a valid tracking bundle");
91             }
92         }
93     }
94
95 }