1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.runtime.shell;
7 import android.app.Activity;
8 import android.content.Intent;
9 import android.content.Context;
10 import android.os.Bundle;
11 import android.util.Log;
12 import android.view.KeyEvent;
13 import android.view.View;
14 import android.view.View.OnFocusChangeListener;
15 import android.view.inputmethod.EditorInfo;
16 import android.view.inputmethod.InputMethodManager;
17 import android.widget.EditText;
18 import android.widget.FrameLayout;
19 import android.widget.LinearLayout;
20 import android.widget.TextView;
21 import android.widget.TextView.OnEditorActionListener;
23 import org.chromium.base.CommandLine;
24 import org.chromium.base.BaseSwitches;
25 import org.xwalk.runtime.XWalkRuntimeView;
27 public class XWalkRuntimeShellActivity extends Activity {
28 // TODO(yongsheng): Add one flag to hide the url bar.
29 public static final String COMMAND_LINE_FILE = "/data/local/tmp/runtime-shell-command-line";
30 private static final String TAG = XWalkRuntimeShellActivity.class.getName();
31 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
33 private EditText mUrlTextView;
34 private XWalkRuntimeView mRuntimeView;
37 public void onCreate(Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
40 if (!CommandLine.isInitialized()) {
41 CommandLine.initFromFile(COMMAND_LINE_FILE);
42 String[] commandLineParams = getCommandLineParamsFromIntent(getIntent());
43 if (commandLineParams != null) {
44 CommandLine.getInstance().appendSwitchesAndArguments(commandLineParams);
48 waitForDebuggerIfNeeded();
50 setContentView(R.layout.testshell_activity);
51 mRuntimeView = (XWalkRuntimeView) findViewById(R.id.content_container);
57 protected void onPause() {
59 mRuntimeView.onPause();
63 protected void onResume() {
65 mRuntimeView.onResume();
69 protected void onDestroy() {
71 mRuntimeView.onDestroy();
75 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
76 mRuntimeView.onActivityResult(requestCode, resultCode, data);
79 private void waitForDebuggerIfNeeded() {
80 if (CommandLine.getInstance().hasSwitch(BaseSwitches.WAIT_FOR_JAVA_DEBUGGER)) {
81 Log.e(TAG, "Waiting for Java debugger to connect...");
82 android.os.Debug.waitForDebugger();
83 Log.e(TAG, "Java debugger connected. Resuming execution.");
87 private static String[] getCommandLineParamsFromIntent(Intent intent) {
88 return intent != null ? intent.getStringArrayExtra(COMMAND_LINE_ARGS_KEY) : null;
91 private static String sanitizeUrl(String url) {
92 if (url == null) return url;
93 if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url;
97 private void initializeUrlField() {
98 mUrlTextView = (EditText) findViewById(R.id.url);
99 mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
101 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
102 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null ||
103 event.getKeyCode() != KeyEvent.KEYCODE_ENTER ||
104 event.getAction() != KeyEvent.ACTION_DOWN)) {
108 mRuntimeView.loadAppFromUrl(sanitizeUrl(mUrlTextView.getText().toString()));
109 mUrlTextView.clearFocus();
110 setKeyboardVisibilityForUrl(false);
114 mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
116 public void onFocusChange(View v, boolean hasFocus) {
117 setKeyboardVisibilityForUrl(hasFocus);
119 // TODO(yongsheng): Fix this.
120 // mUrlTextView.setText(mRuntimeView.getUrl());
126 private void setKeyboardVisibilityForUrl(boolean visible) {
127 InputMethodManager imm = (InputMethodManager) getSystemService(
128 Context.INPUT_METHOD_SERVICE);
130 imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
132 imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);