package test.leindroid.sample;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import clojure.lang.Symbol;
import clojure.lang.Var;
import clojure.lang.RT;

import test.leindroid.sample.R;

public class SplashActivity extends Activity {

    private static boolean firstLaunch = true;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        if (firstLaunch) {
            firstLaunch = false;
            setupSplash();
            loadClojure();
        } else {
            proceed();
        }
    }

    public void setupSplash() {
        setContentView(R.layout.splashscreen);

        TextView appNameView = (TextView)findViewById(R.id.splash_app_name);
        appNameView.setText(R.string.app_name);

        Animation rotation = AnimationUtils.loadAnimation(this, R.anim.splash_rotation);
        ImageView circleView = (ImageView)findViewById(R.id.splash_circles);
        circleView.startAnimation(rotation);
    }

    public void proceed() {
        startActivity(new Intent("test.leindroid.sample.MAIN"));
        finish();
    }

    public void loadClojure() {
        new Thread(new Runnable(){
                @Override
                public void run() {
                    Symbol CLOJURE_MAIN = Symbol.intern("neko.application");
                    Var REQUIRE = RT.var("clojure.core", "require");
                    REQUIRE.invoke(CLOJURE_MAIN);

                    Var INIT = RT.var("neko.application", "init-application");
                    INIT.invoke(SplashActivity.this.getApplication());

                    proceed();
                }
            }).start();
    }
}

@@ -304,16 +304,41 @@
 extView;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -536,16 +536,51 @@
  = true;
+%0A    private String TAG = %22Splash%22;
 %0A%0A    @O
@@ -1848,24 +1848,319 @@
 cation());%0A%0A
+                    Class cl = null;%0A                    try %7B%0A                        cl = Class.forName(%22test.leindroid.sample.MainActivity%22);%0A                    %7D catch (ClassNotFoundException e) %7B%0A                        Log.e(TAG, %22Failed loading MainActivity%22, e);%0A                    %7D%0A%0A
             

Load MainActivity class in background during splash