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.client.embedded.shell;
7 import android.content.Context;
8 import android.os.Bundle;
9 import android.view.KeyEvent;
10 import android.view.View;
11 import android.view.View.OnFocusChangeListener;
12 import android.view.ViewGroup.LayoutParams;
13 import android.view.inputmethod.EditorInfo;
14 import android.view.inputmethod.InputMethodManager;
15 import android.widget.EditText;
16 import android.widget.LinearLayout;
17 import android.widget.TextView;
18 import android.widget.TextView.OnEditorActionListener;
20 import org.xwalk.app.XWalkRuntimeActivityBase;
22 public class XWalkRuntimeClientEmbeddedShellActivity extends XWalkRuntimeActivityBase {
23 // TODO(yongsheng): Add one flag to hide the url bar.
24 private static final String TAG = XWalkRuntimeClientEmbeddedShellActivity.class.getName();
26 private EditText mUrlTextView;
29 public void onCreate(Bundle savedInstanceState) {
30 super.onCreate(savedInstanceState);
34 public boolean onKeyUp(int keyCode, KeyEvent event) {
35 // Passdown the key-up event to runtime view.
36 if (getRuntimeView() != null &&
37 getRuntimeView().onKeyUp(keyCode, event)) {
41 return super.onKeyUp(keyCode, event);
44 private static String sanitizeUrl(String url) {
45 if (url == null) return url;
46 if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url;
50 private void initializeUrlField() {
51 mUrlTextView = (EditText) findViewById(R.id.url);
52 mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
54 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
55 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null ||
56 event.getKeyCode() != KeyEvent.KEYCODE_ENTER ||
57 event.getAction() != KeyEvent.ACTION_DOWN)) {
61 getRuntimeView().loadAppFromUrl(sanitizeUrl(mUrlTextView.getText().toString()));
62 mUrlTextView.clearFocus();
63 setKeyboardVisibilityForUrl(false);
67 mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
69 public void onFocusChange(View v, boolean hasFocus) {
70 setKeyboardVisibilityForUrl(hasFocus);
72 // TODO(yongsheng): Fix this.
73 // mUrlTextView.setText(mRuntimeView.getUrl());
79 private void setKeyboardVisibilityForUrl(boolean visible) {
80 InputMethodManager imm = (InputMethodManager) getSystemService(
81 Context.INPUT_METHOD_SERVICE);
83 imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
85 imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
90 protected void didTryLoadRuntimeView(View runtimeView) {
91 if (getRuntimeView().get() != null) {
92 setContentView(R.layout.testshell_activity);
93 LinearLayout container = (LinearLayout) findViewById(R.id.content_container);
94 container.addView(getRuntimeView().get(),
95 LayoutParams.MATCH_PARENT,
96 LayoutParams.MATCH_PARENT);