Skip to content

Commit 70e3a7f

Browse files
committed
feat: Execute code through console
1 parent 9669769 commit 70e3a7f

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

app/src/main/java/android/code/editor/activity/WebViewActivity.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
import android.graphics.Color;
2222
import android.graphics.drawable.Drawable;
2323
import android.os.Bundle;
24+
import android.view.KeyEvent;
2425
import android.view.Menu;
2526
import android.view.MenuItem;
2627
import android.view.MotionEvent;
2728
import android.view.View;
2829
import android.view.ViewGroup;
30+
import android.view.inputmethod.EditorInfo;
2931
import android.webkit.ConsoleMessage;
3032
import android.webkit.WebChromeClient;
3133
import android.webkit.WebView;
34+
import android.widget.EditText;
3235
import android.widget.LinearLayout;
3336
import android.widget.PopupMenu;
3437
import android.widget.ScrollView;
@@ -40,6 +43,7 @@ public class WebViewActivity extends BaseActivity {
4043
private WebView webview;
4144
private LinearLayout consoleView;
4245
private ScrollView console_content;
46+
private EditText executeCodeInWebView;
4347

4448
@Override
4549
protected void onCreate(Bundle savedInstanceState) {
@@ -98,6 +102,22 @@ public boolean onConsoleMessage(ConsoleMessage console) {
98102
&& getIntent().getStringExtra("type").equals("file")) {
99103
webview.loadUrl("file:".concat(getIntent().getStringExtra("data")));
100104
}
105+
106+
executeCodeInWebView = findViewById(R.id.execute);
107+
executeCodeInWebView.setOnEditorActionListener(
108+
new TextView.OnEditorActionListener() {
109+
@Override
110+
public boolean onEditorAction(TextView edittext, int action, KeyEvent event) {
111+
if (action == EditorInfo.IME_ACTION_DONE
112+
|| event.getAction() == KeyEvent.ACTION_DOWN
113+
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
114+
webview.loadUrl("javascript:".concat(edittext.getText().toString()));
115+
return true;
116+
}
117+
return false;
118+
}
119+
});
120+
101121
findViewById(R.id.console_slider)
102122
.setOnTouchListener(
103123
new View.OnTouchListener() {
@@ -159,7 +179,9 @@ public boolean onOptionsItemSelected(MenuItem arg0) {
159179
item -> {
160180
switch (item.getTitle().toString()) {
161181
case "Clear Console Log":
162-
consoleView.removeAllViews();
182+
if (consoleView != null) {
183+
consoleView.removeAllViews();
184+
}
163185
break;
164186
}
165187
return true;

app/src/main/res/layout/activity_web_view.xml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,27 @@
3535
android:id="@+id/console_content">
3636

3737
<LinearLayout
38-
android:id="@+id/console"
3938
android:layout_height="match_parent"
4039
android:layout_width="match_parent"
41-
android:orientation="vertical" />
40+
android:orientation="vertical">
41+
42+
<EditText
43+
android:layout_height="wrap_content"
44+
android:layout_width="match_parent"
45+
android:background="?attr/colorSurface"
46+
android:textColor="?attr/colorOnSurface"
47+
android:hint="Execute"
48+
android:id="@+id/execute"
49+
android:imeOptions="actionDone"
50+
android:singleLine="true" />
51+
52+
<LinearLayout
53+
android:id="@+id/console"
54+
android:layout_height="wrap_content"
55+
android:layout_width="match_parent"
56+
android:orientation="vertical" />
57+
58+
</LinearLayout>
4259

4360
</ScrollView>
4461

0 commit comments

Comments
 (0)