6e33e54141bd7e45cfd35939855ed47c50f62bab
[platform/framework/web/crosswalk.git] / src / xwalk / app / android / runtime_client_shell / src / org / xwalk / runtime / client / shell / XWalkRuntimeClientShellActivity.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.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 XWalkRuntimeClientShellActivity extends XWalkRuntimeActivityBase {
23     // TODO(yongsheng): Add one flag to hide the url bar.
24     private static final String TAG = XWalkRuntimeClientShellActivity.class.getName();
25
26     private EditText mUrlTextView;
27
28     @Override
29     public void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31     }
32
33     private static String sanitizeUrl(String url) {
34         if (url == null) return url;
35         if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url;
36         return url;
37     }
38
39     private void initializeUrlField() {
40         mUrlTextView = (EditText) findViewById(R.id.url);
41         mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
42             @Override
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)) {
47                     return false;
48                 }
49
50                 getRuntimeView().loadAppFromUrl(sanitizeUrl(mUrlTextView.getText().toString()));
51                 mUrlTextView.clearFocus();
52                 setKeyboardVisibilityForUrl(false);
53                 return true;
54             }
55         });
56         mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
57             @Override
58             public void onFocusChange(View v, boolean hasFocus) {
59                 setKeyboardVisibilityForUrl(hasFocus);
60                 if (!hasFocus) {
61                     // TODO(yongsheng): Fix this.
62                     // mUrlTextView.setText(mRuntimeView.getUrl());
63                 }
64             }
65         });
66     }
67
68     private void setKeyboardVisibilityForUrl(boolean visible) {
69         InputMethodManager imm = (InputMethodManager) getSystemService(
70                 Context.INPUT_METHOD_SERVICE);
71         if (visible) {
72             imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
73         } else {
74             imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
75         }
76     }
77
78     @Override
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);
86             initializeUrlField();
87         }
88     }
89 }