content: /**
   * Project: ANE-Google-Analytics
   *
   * Author:  Alessandro Bianco
   * Website: http://alessandrobianco.eu
   * Twitter: @alebianco
   * Created: 21/12/12 15.02
   *
   * Copyright © 2013 Alessandro Bianco
   */
  package eu.alebianco.air.extensions.analytics.functions;
  
  import com.adobe.fre.FREContext;
  import com.adobe.fre.FREFunction;
  import com.adobe.fre.FREObject;
  import com.google.analytics.tracking.android.GoogleAnalytics;
  import com.google.analytics.tracking.android.Tracker;
  import com.stackoverflow.util.StackTraceInfo;
  import eu.alebianco.air.extensions.utils.FREUtils;
  import eu.alebianco.air.extensions.utils.LogLevel;
  
  public class GetAppID implements FREFunction {
  
      @Override
      public FREObject call(FREContext context, FREObject[] args) {
          FREObject result;
  
          String trackingId;
          try {
              trackingId = args[0].getAsString();
          } catch (Exception e) {
              FREUtils.logEvent(context, LogLevel.FATAL,
                      "Unable to read the 'trackingId' parameter. [Exception:(type:%s, method:%s)]",
                      FREUtils.stripPackageFromClassName(e.toString()), FREUtils.stripPackageFromClassName(StackTraceInfo.getCurrentClassName()));
              return FREUtils.createRuntimeException("ArgumentError", 0, "Unable to read the 'trackingId' parameter on method '%s'.", FREUtils.stripPackageFromClassName(StackTraceInfo.getCurrentClassName()));
          }
  
          Tracker tracker = GoogleAnalytics.getInstance(context.getActivity()).getTracker(trackingId);
  
          try {
              result = FREObject.newObject(tracker.getAppId());
          } catch(Exception e) {
              FREUtils.logEvent(context, LogLevel.ERROR,
                      "Unable to create the return value. [Exception:(type:%s, method:%s)]",
                      FREUtils.stripPackageFromClassName(e.toString()), FREUtils.stripPackageFromClassName(StackTraceInfo.getCurrentClassName()));
              return FREUtils.createRuntimeException("ArgumentError", 0, "Unable to create the return value on method '%s'.", FREUtils.stripPackageFromClassName(StackTraceInfo.getCurrentClassName()));
          }
  
          return result;
      }
  }