// Copyright 2015-present 650 Industries. All rights reserved.

package host.exp.exponent.experience;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Debug;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.facebook.soloader.SoLoader;
import com.squareup.leakcanary.LeakCanary;

import java.util.Arrays;
import java.util.List;

import de.greenrobot.event.EventBus;
import expo.core.interfaces.Package;
import expo.modules.barcodescanner.BarCodeScannerPackage;
import expo.modules.constants.ConstantsPackage;
import expo.modules.filesystem.FileSystemPackage;
import expo.modules.font.FontLoaderPackage;
import expo.modules.keepawake.KeepAwakePackage;
import expo.modules.permissions.PermissionsPackage;
import host.exp.exponent.Constants;
import host.exp.exponent.RNObject;
import host.exp.exponent.analytics.Analytics;
import host.exp.exponent.kernel.Kernel;
import host.exp.expoview.BuildConfig;

public class HomeActivity extends BaseExperienceActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mShouldDestroyRNInstanceOnExit = false;
    mSDKVersion = RNObject.UNVERSIONED;

    EventBus.getDefault().registerSticky(this);
    mKernel.startJSKernel();
    showLoadingScreen(null);

    tryInstallLeakCanary(true);
  }

  private void tryInstallLeakCanary(boolean shouldAskForPermissions) {
    if (BuildConfig.DEBUG && Constants.ENABLE_LEAK_CANARY) {
      // Leak canary needs WRITE_EXTERNAL_STORAGE permission
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (shouldAskForPermissions && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
          requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1248919246);
        } else {
          LeakCanary.install(getApplication());
        }
      } else {
        LeakCanary.install(getApplication());
      }
    }
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    tryInstallLeakCanary(false);
  }

  @Override
  protected void onResume() {
    super.onResume();

    SoLoader.init(this, false);

    Analytics.logEvent("HOME_APPEARED");

    registerForNotifications();
  }

  public void onEventMainThread(Kernel.KernelStartedRunningEvent event) {
    mReactInstanceManager.assign(mKernel.getReactInstanceManager());
    mReactRootView.assign(mKernel.getReactRootView());
    mReactInstanceManager.onHostResume(this, this);
    setView((View) mReactRootView.get());
    checkForReactViews();

    if (Constants.DEBUG_COLD_START_METHOD_TRACING) {
      Debug.stopMethodTracing();
    }
  }

  @Override
  protected void onError(final Intent intent) {
    intent.putExtra(ErrorActivity.IS_HOME_KEY, true);
    mKernel.setHasError();
  }

  public static List<Package> homeExpoPackages() {
    return Arrays.<Package>asList(
        new ConstantsPackage(),
        new PermissionsPackage(),
        new FileSystemPackage(),
        new FontLoaderPackage(),
        new BarCodeScannerPackage(),
        new KeepAwakePackage()
    );
  }
}

@@ -544,32 +544,90 @@
 rfaces.Package;%0A
+import expo.modules.analytics.amplitude.AmplitudePackage;%0A
 import expo.modu
@@ -3428,16 +3428,48 @@
 ackage()
+,%0A        new AmplitudePackage()
 %0A    );%0A

add AmplitudePackage to home