Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
Show how to activate the JavaScript REPL in the stetho sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
potyl committed May 10, 2015
1 parent 38006f5 commit f2e0eea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions stetho-sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ android {
dependencies {
debugCompile project(':stetho')
debugCompile project(':stetho-urlconnection')
debugCompile project(':stetho-repl-js')

// We must use Maven dependency resolution to demonstrate our usage
// of optional dependencies to include stetho-urlconnection but not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@
package com.facebook.stetho.sample;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;
import com.facebook.stetho.DumperPluginsProvider;
import com.facebook.stetho.Stetho;
import com.facebook.stetho.common.LogUtil;
import com.facebook.stetho.dumpapp.DumperPlugin;
import com.facebook.stetho.inspector.console.js.JavaScriptRuntimeBuilder;
import org.mozilla.javascript.BaseFunction;
import org.mozilla.javascript.Scriptable;

public class SampleDebugApplication extends SampleApplication {
private static final String TAG = "SampleDebugApplication";
Expand All @@ -28,10 +36,33 @@ public void onCreate() {

long startTime = SystemClock.elapsedRealtime();
final Context context = this;

// Setup a javascript runtime
JavaScriptRuntimeBuilder jsRuntimeBuilder = new JavaScriptRuntimeBuilder(context);
jsRuntimeBuilder.importClass(R.class);
jsRuntimeBuilder.importPackage("android.content");
jsRuntimeBuilder.addVariable("flag", new AtomicBoolean(true));

Looper looper = Looper.getMainLooper();
final Handler handler = new Handler(looper);
jsRuntimeBuilder.addFunction("toast", new BaseFunction() {
@Override
public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
final String message = args[0].toString();
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
});
return org.mozilla.javascript.Context.getUndefinedValue();
}
});

Stetho.initialize(
Stetho.newInitializerBuilder(context)
.enableDumpapp(new SampleDumperPluginsProvider(context))
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(context))
.enableWebKitInspector(jsRuntimeBuilder.javaScriptInspectorModulesProvider())
.build());
long elapsed = SystemClock.elapsedRealtime() - startTime;
Log.i(TAG, "Stetho initialized in " + elapsed + " ms");
Expand Down

0 comments on commit f2e0eea

Please sign in to comment.