Created
August 30, 2013 09:33
-
-
Save fiskurgit/6388096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fiskur.p5; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import android.app.Activity; | |
import android.content.res.AssetManager; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.util.Log; | |
import android.view.MenuItem; | |
import android.view.Window; | |
import android.webkit.WebView; | |
import android.widget.Toast; | |
public class SketchActivity extends Activity { | |
private WebView webview; | |
private AssetManager mAssetManager; | |
public static final String SKETCHDIR = Environment.getExternalStorageDirectory().getPath() + "/sketch/"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
//We want fullscreen so hide actionbar | |
getWindow().requestFeature(Window.FEATURE_ACTION_BAR); | |
getActionBar().hide(); | |
setContentView(R.layout.activity_sketch); | |
//Needed to copy processingjs and html file from assets/ | |
mAssetManager = getResources().getAssets(); | |
//The sourcecode passed in from the coding edittext | |
String source = getIntent().getStringExtra("source"); | |
webview = (WebView) findViewById(R.id.webview); | |
setupSketch(source); | |
} | |
//Copy ProcessingJS and html file from assets/ if they've not been copied already | |
//Save sketch.pde file containing source to run | |
private void setupSketch(String source) { | |
File sketchDir = new File(SKETCHDIR); | |
//If first-run create output directory | |
if(!sketchDir.exists()){ | |
sketchDir.mkdir(); | |
} | |
//The files contained in assets/ | |
String[] files = new String[] { "processing-1.4.1.min.js", "sketch.html" }; | |
for (String filename : files) { | |
File outFile = new File(sketchDir, filename); | |
if (!outFile.exists()) { | |
try { | |
InputStream in = mAssetManager.open(filename); | |
OutputStream out = new FileOutputStream(outFile); | |
copyFile(in, out); | |
in.close(); | |
in = null; | |
out.flush(); | |
out.close(); | |
out = null; | |
} catch (IOException e) { | |
showToast("Failed to copy asset file: " + filename + "\n" + e.toString()); | |
return; | |
} | |
} | |
} | |
//Save sketch.pde | |
File sketch = new File(sketchDir, "sketch.pde"); | |
try { | |
FileWriter writer = new FileWriter(sketch); | |
writer.append(source); | |
writer.close(); | |
} catch (IOException e) { | |
showToast("Failed to load sketch: " + e.toString()); | |
return; | |
} | |
//Finally pass the file location to the WebView | |
String path = sketchDir.getPath() + "/sketch.html"; | |
webview.getSettings().setAllowUniversalAccessFromFileURLs(true); | |
webview.getSettings().setJavaScriptEnabled(true); | |
webview.loadUrl("file://" + path); | |
} | |
public static void copyFile(InputStream in, OutputStream out) throws IOException { | |
byte[] buffer = new byte[1024]; | |
int read; | |
while ((read = in.read(buffer)) != -1) { | |
out.write(buffer, 0, read); | |
} | |
} | |
//Convenience method for displaying messages to user | |
private void showToast(String error){ | |
Toast.makeText(SketchActivity.this, error, Toast.LENGTH_LONG).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment