/*
 * Copyright (C) 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.ase;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

public class AseAnalytics {
  private static GoogleAnalyticsTracker mTracker;
  private static SharedPreferences mPrefs;
  private static String mAseVersion;

  private AseAnalytics() {
    // Utility class.
  }

  public static void start(Context context) {
    mAseVersion = AseVersion.getVersion(context);
    mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    mTracker = GoogleAnalyticsTracker.getInstance();
    // Start with a dispatch interval so that dispatches happen (hopefully?) in the background.
    mTracker.start("UA-158835-13", 20, context);
  }

  private static class PageNameBuilder {
    private final StringBuilder mmName = new StringBuilder();

    void add(String pathPart) {
      mmName.append("/");
      mmName.append(pathPart);
    }

    String build() {
      return mmName.toString();
    }
  }

  public static void track(String... nameParts) {
    if (mPrefs.getBoolean("usagetracking", false)) {
      PageNameBuilder builder = new PageNameBuilder();
      builder.add(mAseVersion);
      for (String part : nameParts) {
        builder.add(part);
      }
      String name = builder.build();
      AseLog.v("Tracking " + name);
      mTracker.trackPageView(name);
    }
  }

  public static void trackActivity(Activity activity) {
    String name = activity.getClass().getSimpleName();
    track(name);
  }

  public static void stop() {
    mTracker.stop();
  }
}

@@ -617,16 +617,101 @@
 e.ase;%0A%0A
+import java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0A%0A
 import a
@@ -1074,16 +1074,60 @@
 Version;
+%0A  private static ExecutorService mWorkPool;
 %0A%0A  priv
@@ -1396,104 +1396,8 @@
 ();%0A
-    // Start with a dispatch interval so that dispatches happen (hopefully?) in the background.%0A
     
@@ -1427,17 +1427,17 @@
 35-13%22, 
-2
+1
 0, conte
@@ -1441,16 +1441,65 @@
 ntext);%0A
+    mWorkPool = Executors.newCachedThreadPool();%0A
   %7D%0A%0A  p
@@ -1786,16 +1786,22 @@
 d track(
+final 
 String..
@@ -1864,24 +1864,97 @@
 , false)) %7B%0A
+      mWorkPool.execute(new Runnable() %7B%0A        public void run() %7B%0A    
       PageNa
@@ -1998,24 +1998,28 @@
 er();%0A      
+ 
+   
 builder.add(
@@ -2032,16 +2032,20 @@
 rsion);%0A
+    
       fo
@@ -2074,32 +2074,36 @@
 arts) %7B%0A        
+ 
+   
 builder.add(part
@@ -2115,16 +2115,20 @@
     
+    
 %7D%0A      
 Stri
@@ -2119,24 +2119,28 @@
     %7D%0A      
+ 
+   
 String name 
@@ -2164,16 +2164,20 @@
 ;%0A      
+ 
+   
 AseLog.v
@@ -2200,24 +2200,28 @@
 ame);%0A      
+ 
+   
 mTracker.tra
@@ -2234,24 +2234,45 @@
 View(name);%0A
+        %7D;%0A      %7D);%0A
     %7D%0A  %7D%0A%0A 

Change tracking to use a thread pool and work in the background. It's now snappy enough to work with API usage tracking.