/*
 * Copyright 2014 Prateek Srivastava
 *
 * 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.segment.analytics;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;

/**
 * This is a base activity that will track all activity lifecycle events.
 * <p/>
 * You should only use this class if you are targeting devices running pre-ICS. If the device is
 * running ICS or higher, this class will do nothing, since we automatically register {@link
 * android.app.Application.ActivityLifecycleCallbacks} to track the lifecycle instead.
 * <p/>
 * Subclasses may override {@link #getSegmentInstance()} to provide their own instance of {@link
 * Analytics}. It uses the singleton instance by default.
 */
public class TrackedActivity extends Activity {
  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (shouldTrackLifecycleEvent()) {
      getSegmentInstance().onActivityCreated(this, savedInstanceState);
    }
  }

  @Override protected void onStart() {
    super.onStart();
    if (shouldTrackLifecycleEvent()) getSegmentInstance().onActivityStarted(this);
  }

  @Override protected void onResume() {
    super.onResume();
    if (shouldTrackLifecycleEvent()) getSegmentInstance().onActivityResumed(this);
  }

  @Override protected void onPause() {
    super.onPause();
    if (shouldTrackLifecycleEvent()) getSegmentInstance().onActivityPaused(this);
  }

  @Override protected void onStop() {
    super.onStop();
    if (shouldTrackLifecycleEvent()) getSegmentInstance().onActivityStopped(this);
  }

  @Override protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (shouldTrackLifecycleEvent()) {
      getSegmentInstance().onActivitySaveInstanceState(this, outState);
    }
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    if (shouldTrackLifecycleEvent()) getSegmentInstance().onActivityDestroyed(this);
  }

  /**
   * Subclasses may override this to provide their own instance of {@link Analytics}. It uses the
   * singleton instance by default.
   */
  protected Analytics getSegmentInstance() {
    return Analytics.with(this);
  }

  boolean shouldTrackLifecycleEvent() {
    return Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH;
  }
}

@@ -1117,31 +1117,25 @@
 ink #get
-SegmentInstance
+Analytics
 ()%7D to p
@@ -1417,43 +1417,24 @@
 ycle
-Event()) %7B%0A      getSegmentInstance
+()) getAnalytics
 ().o
@@ -1469,38 +1469,32 @@
 InstanceState);%0A
-    %7D%0A
   %7D%0A%0A  @Override
@@ -1574,35 +1574,24 @@
 ycle
-Event
 ()) get
-SegmentInstance
+Analytics
 ().o
@@ -1713,35 +1713,24 @@
 ycle
-Event
 ()) get
-SegmentInstance
+Analytics
 ().o
@@ -1850,35 +1850,24 @@
 ycle
-Event
 ()) get
-SegmentInstance
+Analytics
 ().o
@@ -1984,35 +1984,24 @@
 ycle
-Event
 ()) get
-SegmentInstance
+Analytics
 ().o
@@ -2168,43 +2168,24 @@
 ycle
-Event()) %7B%0A      getSegmentInstance
+()) getAnalytics
 ().o
@@ -2232,14 +2232,8 @@
 e);%0A
-    %7D%0A
   %7D%0A
@@ -2329,35 +2329,24 @@
 ycle
-Event
 ()) get
-SegmentInstance
+Analytics
 ().o
@@ -2551,23 +2551,17 @@
  get
-SegmentInstance
+Analytics
 () %7B
@@ -2601,16 +2601,22 @@
 %0A  %7D%0A%0A  
+final 
 boolean 
@@ -2639,17 +2639,151 @@
 ycle
-Event() %7B
+() %7B%0A    // Don't track lifecycle if we're running ICS or higher, since we register with%0A    // Application#ActivityLifecycleCallbacks instead.
 %0A   

Rename `shouldTrackLifecycleEvent` to `shouldTrackLifecycle` and `getSegmentInstance` to `getAnalytics` in TrackedActivity