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.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;
23 import org.chromium.base.library_loader.LibraryLoader;
24 import org.chromium.content.browser.TracingControllerAndroid;
25 import org.xwalk.app.XWalkRuntimeActivityBase;
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();
31 private EditText mUrlTextView;
32 private TracingControllerAndroid mTracingController;
34 TracingControllerAndroid getTracingController() {
35 if (mTracingController == null) {
36 mTracingController = new TracingControllerAndroid(this);
38 return mTracingController;
42 public void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 registerTracingReceiverWhenIdle();
48 public void onDestroy() {
50 unregisterTracingReceiver();
53 private void registerTracingReceiverWhenIdle() {
54 // Delay tracing receiver registration until the main loop is idle.
55 Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
57 public boolean queueIdle() {
58 // Will retry if the native library is not initialized yet.
59 if (!LibraryLoader.isInitialized()) return true;
61 getTracingController().registerReceiver(XWalkRuntimeClientEmbeddedShellActivity.this);
62 } catch (SecurityException e) {
63 Log.w(TAG, "failed to register tracing receiver: " + e.getMessage());
70 private void unregisterTracingReceiver() {
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());
80 private static String sanitizeUrl(String url) {
81 if (url == null) return url;
82 if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url;
86 private void initializeUrlField() {
87 mUrlTextView = (EditText) findViewById(R.id.url);
88 mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
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)) {
97 getRuntimeView().loadAppFromUrl(sanitizeUrl(mUrlTextView.getText().toString()));
98 mUrlTextView.clearFocus();
99 setKeyboardVisibilityForUrl(false);
103 mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
105 public void onFocusChange(View v, boolean hasFocus) {
106 setKeyboardVisibilityForUrl(hasFocus);
108 // TODO(yongsheng): Fix this.
109 // mUrlTextView.setText(mRuntimeView.getUrl());
115 private void setKeyboardVisibilityForUrl(boolean visible) {
116 InputMethodManager imm = (InputMethodManager) getSystemService(
117 Context.INPUT_METHOD_SERVICE);
119 imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
121 imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
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();