package com.googlecode.pythonforandroid;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.ase.AseLog;
import com.google.ase.Constants;
import com.google.ase.activity.InterpreterInstaller;
import com.google.ase.activity.InterpreterUninstaller;
import com.google.ase.interpreter.python.PythonInterpreter;
import com.google.ase.interpreter.python.PythonInterpreterProcess;

public class Main extends Activity {
  private PythonInterpreter mInterpreter;
  private Button mButton;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mInterpreter = new PythonInterpreter();
    Intent intent = getIntent();
    if (intent.getAction().equals(Constants.ACTION_DISCOVER_INTERPRETERS)) {
      if (mInterpreter.isInstalled()) {
        setResult(RESULT_OK, buildResultIntent(mInterpreter));
      } else {
        setResult(RESULT_CANCELED);
      }
      finish();
      return;
    }
    setContentView(R.layout.main);
    mButton = (Button) findViewById(R.id.button);
    if (mInterpreter.isInstalled()) {
      prepareUninstallButton();
    } else {
      prepareInstallButton();
    }
  }

  private void prepareInstallButton() {
    mButton.setText("Install");
    mButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        install();
      }
    });
  }

  private void prepareUninstallButton() {
    mButton.setText("Uninstall");
    mButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        uninstall();
      }
    });
  }

  private void install() {
    Intent intent = new Intent(this, InterpreterInstaller.class);
    intent.putExtra(Constants.EXTRA_INTERPRETER_NAME, mInterpreter.getName());
    startActivityForResult(intent, 0);
  }

  private void uninstall() {
    Intent intent = new Intent(this, InterpreterUninstaller.class);
    intent.putExtra(Constants.EXTRA_INTERPRETER_NAME, mInterpreter.getName());
    startActivityForResult(intent, 1);
  }

  private Intent buildResultIntent(PythonInterpreter interpreter) {
    Intent result = new Intent();
    Bundle description = buildDescriptionBundle(interpreter);
    result.putExtra("description", description);
    Bundle environment = new Bundle();
    buildEnvironmentBundle(environment);
    result.putExtra("environment", environment);
    return result;
  }

  private void buildEnvironmentBundle(Bundle environment) {
    environment.putString("PYTHONHOME", PythonInterpreterProcess.PYTHON_HOME);
    environment.putString("PYTHONPATH", PythonInterpreterProcess.PYTHON_EXTRAS + ":"
        + Constants.SCRIPTS_ROOT);
    File tmp = new File(PythonInterpreterProcess.PYTHON_EXTRAS + "tmp/");
    if (!tmp.isDirectory()) {
      tmp.mkdir();
    }
    environment.putString("TEMP", tmp.getAbsolutePath());
  }

  private Bundle buildDescriptionBundle(PythonInterpreter interpreter) {
    Bundle description = new Bundle();
    description.putString("name", interpreter.getName());
    description.putString("niceName", interpreter.getNiceName());
    description.putString("extension", interpreter.getExtension());
    description.putString("binaryAbsolutePath", interpreter.getBinary().getAbsolutePath());
    description.putBoolean("hasInterpreterArchive", interpreter.hasInterpreterArchive());
    description
        .putBoolean("hasInterpreterExtrasArchive", interpreter.hasInterpreterExtrasArchive());
    description.putBoolean("hasScriptsArchive", interpreter.hasScriptsArchive());
    return description;
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
      // Interpreter installed.
      if (resultCode != RESULT_OK) {
        AseLog.v(this, "Installation failed.");
      } else {
        prepareUninstallButton();
      }
    } else {
      // Interpreter uninstalled.
      if (resultCode != RESULT_OK) {
        AseLog.v(this, "Uninstallation failed.");
      } else {
        prepareInstallButton();
      }
    }
  }
}
@@ -143,16 +143,47 @@
 Bundle;%0A
+import android.os.Environment;%0A
 import a
@@ -507,75 +507,8 @@
 ter;
-%0Aimport com.google.ase.interpreter.python.PythonInterpreterProcess;
 %0A%0Apu
@@ -2518,24 +2518,478 @@
 esult;%0A  %7D%0A%0A
+  private File getPythonHome() %7B%0A    return new File(getFilesDir(), %22python%22);%0A  %7D%0A%0A  private File getPythonExtras() %7B%0A    return new File(Environment.getExternalStorageDirectory(), %22ase/extras/python%22);%0A  %7D%0A%0A  private File getScriptsRoot() %7B%0A    return new File(Environment.getExternalStorageDirectory(), %22ase/scripts%22);%0A  %7D%0A%0A  private File getPythonTemp() %7B%0A    return new File(Environment.getExternalStorageDirectory(), %22ase/extras/python/tmp%22);%0A  %7D%0A%0A
   private vo
@@ -3080,44 +3080,41 @@
 E%22, 
+get
 Python
-InterpreterProcess.PYTHON_HOME
+Home().getAbsolutePath()
 );%0A 
@@ -3156,159 +3156,108 @@
 H%22, 
+get
 Python
-InterpreterProcess.PYTHON_EXTRAS + %22:%22%0A        + Constants.SCRIPTS_ROOT);%0A    File tmp = new File(PythonInterpreterProcess.PYTHON_EXTRAS + %22tmp/%22
+Extras().getAbsolutePath() + %22:%22%0A        + getScriptsRoot());%0A    File tmp = getPythonTemp(
 );%0A 

Change environment set up to not use ASE constants.