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.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 XWalkRuntimeClientShellActivity extends XWalkRuntimeActivityBase {
23 // TODO(yongsheng): Add one flag to hide the url bar.
24 private static final String TAG = XWalkRuntimeClientShellActivity.class.getName();
26 private EditText mUrlTextView;
29 public void onCreate(Bundle savedInstanceState) {
30 super.onCreate(savedInstanceState);
33 private static String sanitizeUrl(String url) {
34 if (url == null) return url;
35 if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url;
39 private void initializeUrlField() {
40 mUrlTextView = (EditText) findViewById(R.id.url);
41 mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
43 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
44 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null ||
45 event.getKeyCode() != KeyEvent.KEYCODE_ENTER ||
46 event.getAction() != KeyEvent.ACTION_DOWN)) {
50 getRuntimeView().loadAppFromUrl(sanitizeUrl(mUrlTextView.getText().toString()));
51 mUrlTextView.clearFocus();
52 setKeyboardVisibilityForUrl(false);
56 mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
58 public void onFocusChange(View v, boolean hasFocus) {
59 setKeyboardVisibilityForUrl(hasFocus);
61 // TODO(yongsheng): Fix this.
62 // mUrlTextView.setText(mRuntimeView.getUrl());
68 private void setKeyboardVisibilityForUrl(boolean visible) {
69 InputMethodManager imm = (InputMethodManager) getSystemService(
70 Context.INPUT_METHOD_SERVICE);
72 imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
74 imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
79 protected void didTryLoadRuntimeView(View runtimeView) {
80 if (runtimeView != null) {
81 setContentView(R.layout.testshell_activity);
82 LinearLayout container = (LinearLayout) findViewById(R.id.content_container);
83 container.addView(runtimeView,
84 LayoutParams.MATCH_PARENT,
85 LayoutParams.MATCH_PARENT);