Created
August 17, 2017 11:47
-
-
Save aschrijver/382f04c578717db4d30b3b2289de3252 to your computer and use it in GitHub Desktop.
React-native-node refactored to use J2V8
This file contains hidden or 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 my.example.nodeonandroid; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.content.res.AssetManager; | |
import android.os.IBinder; | |
import android.util.Log; | |
import com.eclipsesource.v8.NodeJS; | |
import com.eclipsesource.v8.V8; | |
import com.eclipsesource.v8.utils.V8Runnable; | |
import com.eclipsesource.v8.utils.V8Thread; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
public class RNNodeService extends Service { | |
private static final String TAG = "RNNodeService"; | |
private V8Thread thread; | |
@Override | |
public IBinder onBind(Intent intent) { | |
throw new UnsupportedOperationException("Not yet implemented"); | |
} | |
@Override | |
public void onDestroy() { | |
Log.d(TAG, "destroy"); | |
super.onDestroy(); | |
stopNode(); | |
} | |
public void stopNode() { | |
if (thread != null) { | |
thread.stop(); // TODO deprecated | |
thread = null; | |
} | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Log.d(TAG, "start"); | |
if (intent != null) { | |
if (thread == null) { | |
thread = new V8Thread(new V8Runnable() { | |
@Override | |
public void run(V8 engine) { | |
NodeJS nodeJs = NodeJS.createNodeJS(); | |
AssetManager am = getAssets(); | |
String cache = getCacheDir().getAbsolutePath(); | |
String jsPath = cache + "/node"; | |
copyAssets(am, "node", jsPath); | |
File appIndex = new File(jsPath, "index.js"); | |
if (!appIndex.exists()) { | |
throw new RuntimeException("Background app index not found at " + appIndex); | |
} | |
Log.d(TAG, "Executing " + appIndex); | |
nodeJs.exec(appIndex); | |
while (nodeJs.isRunning()) { | |
nodeJs.handleMessage(); | |
} | |
try { | |
nodeJs.release(); | |
} catch (IllegalStateException e) { | |
throw new RuntimeException("Failed to release NodeJS", e); | |
} | |
} | |
}); | |
thread.start(); | |
} | |
} | |
return START_STICKY; | |
} | |
private static void copyAssets (AssetManager am, String src, String dest) { | |
try { | |
copyAssetFile(am, src, dest); | |
} catch (Exception e) { | |
try { | |
File dir = new File(dest); | |
dir.mkdir(); | |
} catch (Exception e1) {} | |
try { | |
String[] files = am.list(src); | |
for (int i = 0; i < files.length; i++) { | |
copyAssets(am, src + "/" + files[i], dest + "/" + files[i]); | |
} | |
} catch (Exception e2) {} | |
} | |
} | |
private static void copyAssetFile(AssetManager am, String src, String dest) throws IOException { | |
InputStream in = am.open(src); | |
File destFile = new File(dest); | |
if (!destFile.exists()) destFile.createNewFile(); | |
FileOutputStream out = new FileOutputStream(dest); | |
byte[] buffer = new byte[1024]; | |
int length; | |
while ((length = in.read(buffer)) > 0) { | |
out.write(buffer, 0, length); | |
} | |
in.close(); | |
out.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment