- add third_party src.
[platform/framework/web/crosswalk.git] / src / xwalk / app / android / runtime_client_embedded_shell / src / org / xwalk / runtime / client / embedded / shell / XWalkRuntimeClientEmbeddedShellActivity.java
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.
4
5 package org.xwalk.runtime.client.embedded.shell;
6
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;
19
20 import org.xwalk.app.XWalkRuntimeActivityBase;
21
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();
25
26     private EditText mUrlTextView;
27
28     @Override
29     public void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31     }
32
33     @Override
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)) {
38             return true;
39         }
40
41         return super.onKeyUp(keyCode, event);
42     }
43
44     private static String sanitizeUrl(String url) {
45         if (url == null) return url;
46         if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url;
47         return url;
48     }
49
50     private void initializeUrlField() {
51         mUrlTextView = (EditText) findViewById(R.id.url);
52         mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
53             @Override
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)) {
58                     return false;
59                 }
60
61                 getRuntimeView().loadAppFromUrl(sanitizeUrl(mUrlTextView.getText().toString()));
62                 mUrlTextView.clearFocus();
63                 setKeyboardVisibilityForUrl(false);
64                 return true;
65             }
66         });
67         mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
68             @Override
69             public void onFocusChange(View v, boolean hasFocus) {
70                 setKeyboardVisibilityForUrl(hasFocus);
71                 if (!hasFocus) {
72                     // TODO(yongsheng): Fix this.
73                     // mUrlTextView.setText(mRuntimeView.getUrl());
74                 }
75             }
76         });
77     }
78
79     private void setKeyboardVisibilityForUrl(boolean visible) {
80         InputMethodManager imm = (InputMethodManager) getSystemService(
81                 Context.INPUT_METHOD_SERVICE);
82         if (visible) {
83             imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
84         } else {
85             imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
86         }
87     }
88
89     @Override
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);
97             initializeUrlField();
98         }
99     }
100 }