<commit_before>package com.kickstarter.libs;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.kickstarter.libs.utils.KoalaUtils;
import com.kickstarter.libs.utils.MapUtils;
import com.kickstarter.models.User;
import com.mixpanel.android.mpmetrics.MixpanelAPI;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

public final class KoalaTrackingClient implements TrackingClientType {
  @Inject CurrentUser currentUser;
  @Nullable private User loggedInUser;

  @NonNull private final MixpanelAPI mixpanel;

  public KoalaTrackingClient(@ForApplication @NonNull final Context context, @NonNull final CurrentUser currentUser) {
    this.currentUser = currentUser;

    // Cache the most recent logged in user for default Koala properties.
    this.currentUser.observable().subscribe(u -> loggedInUser = u);

    mixpanel = MixpanelAPI.getInstance(context, "koala");
  }

  @Override
  public void track(@NonNull final String eventName) {
    track(eventName, new HashMap<>());
  }

  @Override
  public void track(@NonNull final String eventName, @NonNull final Map<String, Object> properties) {
    final Map<String, Object> newProperties = new HashMap<>(properties);
    newProperties.putAll(defaultProperties());

    mixpanel.trackMap(eventName, MapUtils.compact(newProperties));
  }

  @NonNull
  @Override
  public Map<String, Object> defaultProperties() {

    return new HashMap<String, Object>() {{
      if (loggedInUser != null) {
        putAll(KoalaUtils.userProperties(loggedInUser));
      }

      put("client_type", "native");
      // TODO: can we detect phone app running on tablets?
      put("device_format", "phone");
      put("client_platform", "android");

      // TODO: any equivalent to iOS's UIDevice.currentDevice.identifierForVendor.UUIDString?
      // put("device_fingerprint", "<PASSWORD>beef");
      // TODO: same value as above
      //put("android_uuid", "deadbeef");

      // TODO: any way to detect if android pay is available?
      // put("android_pay_capable", false);

      // TODO: any way to detect if play services is available?
      // put("android_play_services_available", false);

      // TODO: any way to detect device orientaiton?
      // put("device_orientation", "portrait");
    }};
  }
}<commit_msg>Implemented koala super properties for android play services and device orientation.<commit_after>package com.kickstarter.libs;

import android.content.Context;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.kickstarter.libs.utils.KoalaUtils;
import com.kickstarter.libs.utils.MapUtils;
import com.kickstarter.models.User;
import com.mixpanel.android.mpmetrics.MixpanelAPI;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

public final class KoalaTrackingClient implements TrackingClientType {
  @Inject CurrentUser currentUser;
  @Nullable private User loggedInUser;
  private final @NonNull Context context;

  private final @NonNull MixpanelAPI mixpanel;

  // Cached values
  private @Nullable Boolean isGooglePlayServicesAvailable = null;

  public KoalaTrackingClient(@ForApplication @NonNull final Context context, @NonNull final CurrentUser currentUser) {
    this.context = context;
    this.currentUser = currentUser;

    // Cache the most recent logged in user for default Koala properties.
    this.currentUser.observable().subscribe(u -> loggedInUser = u);

    mixpanel = MixpanelAPI.getInstance(context, "koala");
  }

  @Override
  public void track(@NonNull final String eventName) {
    track(eventName, new HashMap<>());
  }

  @Override
  public void track(@NonNull final String eventName, @NonNull final Map<String, Object> properties) {
    final Map<String, Object> newProperties = new HashMap<>(properties);
    newProperties.putAll(defaultProperties());

    mixpanel.trackMap(eventName, MapUtils.compact(newProperties));
  }

  @NonNull
  @Override
  public Map<String, Object> defaultProperties() {

    return new HashMap<String, Object>() {{
      if (loggedInUser != null) {
        putAll(KoalaUtils.userProperties(loggedInUser));
      }

      put("client_type", "native");
      put("android_play_services_available", isGooglePlayServicesAvailable());
      put("client_platform", "android");
      put("device_orientation", orientation());

      // TODO: can we detect phone app running on tablets?
      put("device_format", "phone");

      // TODO: any equivalent to iOS's UIDevice.currentDevice.identifierForVendor.UUIDString?
      // put("device_fingerprint", "deadbeef");
      // TODO: same value as above
      //put("android_uuid", "deadbeef");

      // TODO: any way to detect if android pay is available?
      // put("android_pay_capable", false);
    }};
  }

  /**
   * Derives the device's orientation (portrait/landscape) from the `context`.
   */
  private @NonNull String orientation() {
    if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
      return "landscape";
    }
    return "portrait";
  }

  /**
   * Derives the availability of google play services from the `context`.
   */
  private boolean isGooglePlayServicesAvailable() {
    if (isGooglePlayServicesAvailable == null) {
      isGooglePlayServicesAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.context.getApplicationContext()) != ConnectionResult.SUCCESS;
    }
    return isGooglePlayServicesAvailable;
  }
}
{'fraction_non_alphanumeric': 0.10726159230096238, 'fraction_numerical': 0.0, 'mean_word_length': 4.2296431838975295, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3871320', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1560, 'n_words': 407}