Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / runtime_shell / src / org / xwalk / runtime / shell / XWalkRuntimeShellActivity.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.shell;
6
7 import android.app.Activity;
8 import android.content.Intent;
9 import android.content.Context;
10 import android.os.Bundle;
11 import android.util.Log;
12 import android.view.KeyEvent;
13 import android.view.View;
14 import android.view.View.OnFocusChangeListener;
15 import android.view.inputmethod.EditorInfo;
16 import android.view.inputmethod.InputMethodManager;
17 import android.widget.EditText;
18 import android.widget.FrameLayout;
19 import android.widget.LinearLayout;
20 import android.widget.TextView;
21 import android.widget.TextView.OnEditorActionListener;
22
23 import org.chromium.base.CommandLine;
24 import org.chromium.base.BaseSwitches;
25 import org.xwalk.runtime.XWalkRuntimeView;
26
27 public class XWalkRuntimeShellActivity extends Activity {
28     // TODO(yongsheng): Add one flag to hide the url bar.
29     public static final String COMMAND_LINE_FILE = "/data/local/tmp/runtime-shell-command-line";
30     private static final String TAG = XWalkRuntimeShellActivity.class.getName();
31     public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
32
33     private EditText mUrlTextView;
34     private XWalkRuntimeView mRuntimeView;
35
36     @Override
37     public void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39
40         if (!CommandLine.isInitialized()) {
41             CommandLine.initFromFile(COMMAND_LINE_FILE);
42             String[] commandLineParams = getCommandLineParamsFromIntent(getIntent());
43             if (commandLineParams != null) {
44                 CommandLine.getInstance().appendSwitchesAndArguments(commandLineParams);
45             }
46         }
47
48         waitForDebuggerIfNeeded();
49
50         setContentView(R.layout.testshell_activity);
51         mRuntimeView = (XWalkRuntimeView) findViewById(R.id.content_container);
52
53         initializeUrlField();
54     }
55
56     @Override
57     protected void onPause() {
58         super.onPause();
59         mRuntimeView.onPause();
60     }
61
62     @Override
63     protected void onResume() {
64         super.onResume();
65         mRuntimeView.onResume();
66     }
67
68     @Override
69     protected void onDestroy() {
70         super.onDestroy();
71         mRuntimeView.onDestroy();
72     }
73
74     @Override
75     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
76         mRuntimeView.onActivityResult(requestCode, resultCode, data);
77     }
78
79     private void waitForDebuggerIfNeeded() {
80         if (CommandLine.getInstance().hasSwitch(BaseSwitches.WAIT_FOR_JAVA_DEBUGGER)) {
81             Log.e(TAG, "Waiting for Java debugger to connect...");
82             android.os.Debug.waitForDebugger();
83             Log.e(TAG, "Java debugger connected. Resuming execution.");
84         }
85     }
86
87     private static String[] getCommandLineParamsFromIntent(Intent intent) {
88         return intent != null ? intent.getStringArrayExtra(COMMAND_LINE_ARGS_KEY) : null;
89     }
90
91     private static String sanitizeUrl(String url) {
92         if (url == null) return url;
93         if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url;
94         return url;
95     }
96
97     private void initializeUrlField() {
98         mUrlTextView = (EditText) findViewById(R.id.url);
99         mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
100             @Override
101             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
102                 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null ||
103                         event.getKeyCode() != KeyEvent.KEYCODE_ENTER ||
104                         event.getAction() != KeyEvent.ACTION_DOWN)) {
105                     return false;
106                 }
107
108                 mRuntimeView.loadAppFromUrl(sanitizeUrl(mUrlTextView.getText().toString()));
109                 mUrlTextView.clearFocus();
110                 setKeyboardVisibilityForUrl(false);
111                 return true;
112             }
113         });
114         mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
115             @Override
116             public void onFocusChange(View v, boolean hasFocus) {
117                 setKeyboardVisibilityForUrl(hasFocus);
118                 if (!hasFocus) {
119                     // TODO(yongsheng): Fix this.
120                     // mUrlTextView.setText(mRuntimeView.getUrl());
121                 }
122             }
123         });
124     }
125
126     private void setKeyboardVisibilityForUrl(boolean visible) {
127         InputMethodManager imm = (InputMethodManager) getSystemService(
128                 Context.INPUT_METHOD_SERVICE);
129         if (visible) {
130             imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
131         } else {
132             imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
133         }
134     }
135 }