diff options
author | Zhao Wei Liew <zhaoweiliew@gmail.com> | 2017-02-19 11:18:09 +0800 |
---|---|---|
committer | Zhao Wei Liew <zhaoweiliew@gmail.com> | 2017-02-19 11:21:09 +0800 |
commit | f2597ce36785fda486fa7a911ec05147adcbbffe (patch) | |
tree | b3dd5c6632cae4c956f0f0910cb6b29d586a4ebe /src/com/cyanogenmod | |
parent | f78767a5c487f0af799cb2f72a29d21286eb1932 (diff) | |
download | android_packages_apps_Messaging-f2597ce36785fda486fa7a911ec05147adcbbffe.tar.gz android_packages_apps_Messaging-f2597ce36785fda486fa7a911ec05147adcbbffe.tar.bz2 android_packages_apps_Messaging-f2597ce36785fda486fa7a911ec05147adcbbffe.zip |
Revert "Adding metrics related to Ridesharing"
Get rid of dependencies on the ambient SDK.
This reverts commit 7619c3c57f0e9cd41a26aa8170ecae05591ac466.
Change-Id: If9f191cd610e30ef991b994c36b0192620b7c212
Diffstat (limited to 'src/com/cyanogenmod')
-rw-r--r-- | src/com/cyanogenmod/messaging/util/MetricsHelper.java | 223 | ||||
-rw-r--r-- | src/com/cyanogenmod/messaging/util/MetricsJob.java | 139 |
2 files changed, 0 insertions, 362 deletions
diff --git a/src/com/cyanogenmod/messaging/util/MetricsHelper.java b/src/com/cyanogenmod/messaging/util/MetricsHelper.java deleted file mode 100644 index aff2e32..0000000 --- a/src/com/cyanogenmod/messaging/util/MetricsHelper.java +++ /dev/null @@ -1,223 +0,0 @@ -/*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====* - * Copyright (c) 2016 Cyanogen Inc. - * All Rights Reserved. - * Cyanogen Confidential and Proprietary. - * =========================================================================*/ - -package com.cyanogenmod.messaging.util; - -import android.content.SharedPreferences; -import android.util.Log; - -//import com.android.internal.annotations.VisibleForTesting; -import com.cyanogen.ambient.analytics.Event; -import android.content.ComponentName; -import android.content.Context; - -import java.util.HashMap; -import java.util.Map; - -public final class MetricsHelper { - - private static final String TAG = "MetricsHelper"; - private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); - - public static final String METRICS_SHARED_PREFERENCES = "messaging_metrics"; - public static final String METRICS_VALIDATION = "messaging_metrics_validation"; - public static final String DELIMIT = ":"; - private static final String CATEGORY_BASE = "messaging.metrics."; - - // Positions in our shared preference keys - private static final int POS_COMPONENT_NAME = 0; - private static final int POS_CATEGORY_VALUE = 1; - private static final int POS_EVENT_VALUE = 2; - private static final int POS_PARAM_VALUE = 3; - - public static final String CATEGORY_MESSAGING_RIDESHARING_INTEGRATION = "category_messaging_ridesharing_integration"; - public static final String EVENT_RIDESHARING_MAP_SHOWN = "event_ridesharing_map_shown"; - public static final String EVENT_UBER_RIDE_REQUESTED = "event_uber_ride_requested"; - public static final String PARAM_COUNT = "param_count"; - public static final String PARAM_PROVIDER = "provider"; - - public static enum MetricEvent { - RIDESHARING_MAP_SHOWN, - UBER_RIDE_REQUESTED; - } - - //@VisibleForTesting - /* package */ static void storeEvent(Context context, ComponentName cn, - HashMap<String, String> data, String category, String event) { - - SharedPreferences sp = context.getSharedPreferences( - METRICS_SHARED_PREFERENCES, Context.MODE_PRIVATE); - - SharedPreferences.Editor editor = sp.edit(); - - for (String param : data.keySet()) { - StringBuilder sb = new StringBuilder(); - sb.append(cn.flattenToShortString()); // Add ComponentName String - sb.append(DELIMIT); - sb.append(category); // Add our category value - sb.append(DELIMIT); - sb.append(event); // Add our event value - sb.append(DELIMIT); - sb.append(param); // add our param value - editor.putString(sb.toString(), data.get(param)); - } - editor.apply(); - } - - /** - * Get the sharedpreferences events and output a hashmap for the event's values. - * - * @param context the current context - * @param componentName ComponentName who created the event - * - * @return HashMap of our params and their values. - */ - /* package*/ static HashMap<String, String> getStoredEventParams(Context context, - ComponentName componentName, String category, String event) { - - SharedPreferences sp = context.getSharedPreferences( - METRICS_SHARED_PREFERENCES, Context.MODE_PRIVATE); - - StringBuilder sb = new StringBuilder(); - sb.append(componentName.flattenToShortString()); // Add ComponentName String - sb.append(DELIMIT); - sb.append(category); // Add our category value - sb.append(DELIMIT); - sb.append(event); // Add our event value - sb.append(DELIMIT); - - HashMap<String, String> eventMap = new HashMap<>(); - Map<String, ?> map = sp.getAll(); - - for(Map.Entry<String,?> entry : map.entrySet()) { - if (entry.getKey().startsWith(sb.toString())) { - String[] keyParts = entry.getKey().split(DELIMIT); - String key = keyParts[POS_PARAM_VALUE]; - eventMap.put(key, String.valueOf(entry.getValue())); - } - } - return eventMap; - } - - /** - * Helper method to increase the count of event metric if the last action was not - * the same as the current action. - * - * @param context - */ - public static void increaseCountOfEventMetricAfterValidate(Context context, ComponentName componentName, - MetricEvent metricEvent) { - - StringBuilder sb = new StringBuilder(); - sb.append(componentName.flattenToShortString()); // Add ComponentName String - sb.append(DELIMIT); - sb.append(CATEGORY_MESSAGING_RIDESHARING_INTEGRATION); // Add our category value - - String validationKey = sb.toString(); - String event; - switch (metricEvent) { - case UBER_RIDE_REQUESTED: - event = EVENT_UBER_RIDE_REQUESTED; - break; - case RIDESHARING_MAP_SHOWN: - default: - event = EVENT_RIDESHARING_MAP_SHOWN; - break; - } - - if (checkLastEvent(context, validationKey, event)) { - HashMap<String, String> metricsData - = getStoredEventParams(context, componentName, CATEGORY_MESSAGING_RIDESHARING_INTEGRATION, - event); - - int count = 1; - if (metricsData.containsKey(PARAM_COUNT)) { - count += Integer.valueOf(metricsData.get(PARAM_COUNT)); - } - - metricsData.put(PARAM_COUNT, String.valueOf(count)); - storeEvent(context, componentName, metricsData, CATEGORY_MESSAGING_RIDESHARING_INTEGRATION, event); - } - } - - /* package */ static boolean checkLastEvent(Context context, String validationKey, String event) { - SharedPreferences preferences = context.getSharedPreferences(METRICS_VALIDATION, - Context.MODE_PRIVATE); - - SharedPreferences.Editor editor = preferences.edit(); - String lastEvent = preferences.getString(validationKey, null); - - if (lastEvent != null && lastEvent.equals(event)) { - return false; - } else { - editor.putString(validationKey, event); - } - - editor.apply(); - return true; - } - - - /** - * Prepares all our metrics for sending. - */ - public static HashMap<String, Event.Builder> getEventsToSend(Context c) { - SharedPreferences sp = c.getSharedPreferences(METRICS_SHARED_PREFERENCES, - Context.MODE_PRIVATE); - - Map<String, ?> map = sp.getAll(); - - HashMap<String, Event.Builder> unBuiltEvents = new HashMap<>(); - - for(Map.Entry<String,?> entry : map.entrySet()){ - String[] keyParts = entry.getKey().split(DELIMIT); - - if (keyParts.length == POS_PARAM_VALUE + 1) { - String componentString = keyParts[POS_COMPONENT_NAME]; - String eventCategory = keyParts[POS_CATEGORY_VALUE]; - String parameter = keyParts[POS_PARAM_VALUE]; - String eventAction = keyParts[POS_EVENT_VALUE]; - - StringBuilder sb = new StringBuilder(); - sb.append(componentString); // Add ComponentName String - sb.append(DELIMIT); - sb.append(eventCategory); // Add our category value - sb.append(DELIMIT); - sb.append(eventAction); // Add our event value - String eventKey = sb.toString(); - - Event.Builder eventBuilder; - if (unBuiltEvents.containsKey(eventKey)) { - eventBuilder = unBuiltEvents.get(eventKey); - } else { - eventBuilder = new Event.Builder(CATEGORY_BASE + eventCategory, eventAction); - eventBuilder.addField(PARAM_PROVIDER, componentString); - } - - eventBuilder.addField(parameter, String.valueOf(entry.getValue())); - unBuiltEvents.put(eventKey, eventBuilder); - } - } - return unBuiltEvents; - } - - public static void clearEventData(Context c, String key) { - SharedPreferences sp = c.getSharedPreferences(METRICS_SHARED_PREFERENCES, - Context.MODE_PRIVATE); - - Map<String, ?> map = sp.getAll(); - SharedPreferences.Editor editor = sp.edit(); - - for(Map.Entry<String,?> entry : map.entrySet()){ - String storedKey = entry.getKey(); - if (storedKey.startsWith(key)) { - editor.remove(storedKey); - } - } - editor.apply(); - } - -} diff --git a/src/com/cyanogenmod/messaging/util/MetricsJob.java b/src/com/cyanogenmod/messaging/util/MetricsJob.java deleted file mode 100644 index 8b623a7..0000000 --- a/src/com/cyanogenmod/messaging/util/MetricsJob.java +++ /dev/null @@ -1,139 +0,0 @@ -/*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====* - * Copyright (c) 2016 Cyanogen Inc. - * All Rights Reserved. - * Cyanogen Confidential and Proprietary. - * =========================================================================*/ - -package com.cyanogenmod.messaging.util; - -import android.app.job.JobParameters; -import android.app.job.JobService; -import android.os.AsyncTask; -import android.util.Log; - -import com.cyanogen.ambient.common.ConnectionResult; -import com.cyanogen.ambient.common.ConnectionResult; -import com.cyanogen.ambient.common.api.AmbientApiClient; -import com.cyanogen.ambient.analytics.AnalyticsServices; -import com.cyanogen.ambient.analytics.Event; -import com.cyanogen.ambient.common.api.Result; - -import java.util.HashMap; -import java.util.concurrent.TimeUnit; - -/** - * MetricsJob is an aggregation and shipping service that is fired - * once every 24 hours to pass Metrics to ModCore's analytics service. - */ -public final class MetricsJob extends JobService { - - private static final String TAG = "MetricsJob"; - private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); - - private static final long TIMEOUT_MILLIS = 1000; - - public static final int METRICS_JOB_ID = 1441; - - private MetricsTask mUploadTask; - - private AmbientApiClient ambientApiClient; - - public MetricsJob() { - super(); - } - - @Override - public boolean onStartJob(JobParameters params) { - if (DEBUG) Log.v(TAG, "sending events"); - - // AmbientClient - ambientApiClient = new AmbientApiClient.Builder(this) - .addApi(AnalyticsServices.API) - .addOnConnectionFailedListener(new AmbientApiClient.OnConnectionFailedListener() { - @Override - public void onConnectionFailed(ConnectionResult result) { - Log.e(TAG, "Failed to connect to Ambient. reason: " + result.getErrorCode()); - } - }).build(); - - // Send stored Specific events - mUploadTask = new MetricsTask(params); - mUploadTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, (Void) null); - - // Running on another thread, return true. - return true; - } - - @Override - public boolean onStopJob(JobParameters params) { - - if (ambientApiClient != null && (ambientApiClient.isConnected() || ambientApiClient.isConnecting())) { - ambientApiClient.disconnect(); - } - - // Cancel our async task - mUploadTask.cancel(true); - - // report that we should try again soon. - return true; - } - - - class MetricsTask extends AsyncTask<Void, Void, Boolean> { - - JobParameters mMetricsJobParams; - - public MetricsTask(JobParameters params) { - this.mMetricsJobParams = params; - } - - @Override - protected Boolean doInBackground(Void... params) { - - HashMap<String, Event.Builder> eventsToSend - = MetricsHelper.getEventsToSend(MetricsJob.this); - - for (String key : eventsToSend.keySet()) { - - Event.Builder eventBuilder = eventsToSend.get(key); - - if (DEBUG) Log.v(TAG, "sending:" + eventBuilder.toString()); - - if (isCancelled()) { - return false; - } - - Result r = AnalyticsServices.AnalyticsApi.sendEvent( - ambientApiClient, - eventBuilder.build()) - .await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); - - // if any of our results were not successful, something is wrong. - // Stop this job for now. - if (!r.getStatus().isSuccess()) { - return false; - } - - // We sent all the data we had for this event to the database. So clear it from our - // SharedPreferences. - MetricsHelper.clearEventData(MetricsJob.this, key); - } - return true; - } - - @Override - protected void onCancelled() { - if (DEBUG) Log.w(TAG, "Messaging Metrics Job Cancelled"); - // do nothing - } - - @Override - protected void onPostExecute(Boolean success) { - if (DEBUG) Log.v(TAG, "was success: " + success); - - // attempt to reschedule if analytics service is unavailable for our events - jobFinished(mMetricsJobParams, !success /* reschedule */); - } - } - -} |