Upstream version 9.38.204.0
[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.os.Looper;
10 import android.os.MessageQueue;
11 import android.view.KeyEvent;
12 import android.view.View;
13 import android.view.View.OnFocusChangeListener;
14 import android.view.ViewGroup.LayoutParams;
15 import android.view.inputmethod.EditorInfo;
16 import android.view.inputmethod.InputMethodManager;
17 import android.util.Log;
18 import android.widget.EditText;
19 import android.widget.LinearLayout;
20 import android.widget.TextView;
21 import android.widget.TextView.OnEditorActionListener;
22
23 import org.chromium.base.library_loader.LibraryLoader;
24 import org.chromium.content.browser.TracingControllerAndroid;
25 import org.xwalk.app.XWalkRuntimeActivityBase;
26
27 public class XWalkRuntimeClientEmbeddedShellActivity extends XWalkRuntimeActivityBase {
28     // TODO(yongsheng): Add one flag to hide the url bar.
29     private static final String TAG = XWalkRuntimeClientEmbeddedShellActivity.class.getName();
30
31     private EditText mUrlTextView;
32     private TracingControllerAndroid mTracingController;
33
34     TracingControllerAndroid getTracingController() {
35         if (mTracingController == null) {
36             mTracingController = new TracingControllerAndroid(this);
37         }
38         return mTracingController;
39     }
40
41     @Override
42     public void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44         registerTracingReceiverWhenIdle();
45     }
46
47     @Override
48     public void onDestroy() {
49         super.onDestroy();
50         unregisterTracingReceiver();
51     }
52
53     private void registerTracingReceiverWhenIdle() {
54         // Delay tracing receiver registration until the main loop is idle.
55         Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
56             @Override
57             public boolean queueIdle() {
58                 // Will retry if the native library is not initialized yet.
59                 if (!LibraryLoader.isInitialized()) return true;
60                 try {
61                     getTracingController().registerReceiver(XWalkRuntimeClientEmbeddedShellActivity.this);
62                 } catch (SecurityException e) {
63                     Log.w(TAG, "failed to register tracing receiver: " + e.getMessage());
64                 }
65                 return false;
66             }
67         });
68     }
69
70     private void unregisterTracingReceiver() {
71         try {
72             getTracingController().unregisterReceiver(this);
73         } catch (SecurityException e) {
74             Log.w(TAG, "failed to unregister tracing receiver: " + e.getMessage());
75         } catch (IllegalArgumentException e) {
76             Log.w(TAG, "failed to unregister tracing receiver: " + e.getMessage());
77         }
78     }
79
80     private static String sanitizeUrl(String url) {
81         if (url == null) return url;
82         if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url;
83         return url;
84     }
85
86     private void initializeUrlField() {
87         mUrlTextView = (EditText) findViewById(R.id.url);
88         mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
89             @Override
90             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
91                 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null ||
92                         event.getKeyCode() != KeyEvent.KEYCODE_ENTER ||
93                         event.getAction() != KeyEvent.ACTION_DOWN)) {
94                     return false;
95                 }
96
97                 getRuntimeView().loadAppFromUrl(sanitizeUrl(mUrlTextView.getText().toString()));
98                 mUrlTextView.clearFocus();
99                 setKeyboardVisibilityForUrl(false);
100                 return true;
101             }
102         });
103         mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
104             @Override
105             public void onFocusChange(View v, boolean hasFocus) {
106                 setKeyboardVisibilityForUrl(hasFocus);
107                 if (!hasFocus) {
108                     // TODO(yongsheng): Fix this.
109                     // mUrlTextView.setText(mRuntimeView.getUrl());
110                 }
111             }
112         });
113     }
114
115     private void setKeyboardVisibilityForUrl(boolean visible) {
116         InputMethodManager imm = (InputMethodManager) getSystemService(
117                 Context.INPUT_METHOD_SERVICE);
118         if (visible) {
119             imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
120         } else {
121             imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
122         }
123     }
124
125     @Override
126     protected void didTryLoadRuntimeView(View runtimeView) {
127         if (runtimeView != null) {
128             setContentView(R.layout.testshell_activity);
129             LinearLayout container = (LinearLayout) findViewById(R.id.content_container);
130             container.addView(runtimeView,
131                               LayoutParams.MATCH_PARENT,
132                               LayoutParams.MATCH_PARENT);
133             initializeUrlField();
134         }
135     }
136 }