Upstream version 11.39.244.0
[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.BroadcastReceiver;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.IntentFilter;
11 import android.os.Bundle;
12 import android.view.KeyEvent;
13 import android.view.View;
14 import android.view.View.OnFocusChangeListener;
15 import android.view.ViewGroup.LayoutParams;
16 import android.view.inputmethod.EditorInfo;
17 import android.view.inputmethod.InputMethodManager;
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.xwalk.app.XWalkRuntimeActivityBase;
24 import org.xwalk.core.XWalkPreferences;
25
26 public class XWalkRuntimeClientShellActivity extends XWalkRuntimeActivityBase {
27     // TODO(yongsheng): Add one flag to hide the url bar.
28     private static final String TAG = XWalkRuntimeClientShellActivity.class.getName();
29
30     private EditText mUrlTextView;
31     private BroadcastReceiver mReceiver;
32
33     @Override
34     public void onCreate(Bundle savedInstanceState) {
35         IntentFilter intentFilter = new IntentFilter("org.xwalk.intent");
36         intentFilter.addAction("android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE");
37         intentFilter.addAction("android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE");
38         mReceiver = new BroadcastReceiver() {
39             @Override
40             public void onReceive(Context context, Intent intent) {
41                 Bundle bundle = intent.getExtras();
42                 if (bundle == null) return;
43
44                 XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING,
45                         Boolean.parseBoolean(bundle.getString("remotedebugging", "false")));
46             }
47         };
48         registerReceiver(mReceiver, intentFilter);
49         super.onCreate(savedInstanceState);
50     }
51
52     @Override
53     public void onDestroy() {
54         unregisterReceiver(mReceiver);
55         super.onDestroy();
56     }
57
58     private static String sanitizeUrl(String url) {
59         if (url == null) return url;
60         if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url;
61         return url;
62     }
63
64     private void initializeUrlField() {
65         mUrlTextView = (EditText) findViewById(R.id.url);
66         mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
67             @Override
68             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
69                 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null ||
70                         event.getKeyCode() != KeyEvent.KEYCODE_ENTER ||
71                         event.getAction() != KeyEvent.ACTION_DOWN)) {
72                     return false;
73                 }
74
75                 getRuntimeView().loadAppFromUrl(sanitizeUrl(mUrlTextView.getText().toString()));
76                 mUrlTextView.clearFocus();
77                 setKeyboardVisibilityForUrl(false);
78                 return true;
79             }
80         });
81         mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
82             @Override
83             public void onFocusChange(View v, boolean hasFocus) {
84                 setKeyboardVisibilityForUrl(hasFocus);
85                 if (!hasFocus) {
86                     // TODO(yongsheng): Fix this.
87                     // mUrlTextView.setText(mRuntimeView.getUrl());
88                 }
89             }
90         });
91     }
92
93     private void setKeyboardVisibilityForUrl(boolean visible) {
94         InputMethodManager imm = (InputMethodManager) getSystemService(
95                 Context.INPUT_METHOD_SERVICE);
96         if (visible) {
97             imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
98         } else {
99             imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
100         }
101     }
102
103     @Override
104     protected void didTryLoadRuntimeView(View runtimeView) {
105         if (runtimeView != null) {
106             setContentView(R.layout.testshell_activity);
107             LinearLayout container = (LinearLayout) findViewById(R.id.content_container);
108             container.addView(runtimeView,
109                               LayoutParams.MATCH_PARENT,
110                               LayoutParams.MATCH_PARENT);
111             initializeUrlField();
112         }
113     }
114 }