Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / remoting / android / java / src / org / chromium / chromoting / Desktop.java
1 // Copyright 2013 The Chromium Authors. 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.chromium.chromoting;
6
7 import android.annotation.SuppressLint;
8 import android.app.Activity;
9 import android.content.res.Configuration;
10 import android.os.Build;
11 import android.os.Bundle;
12 import android.view.KeyCharacterMap;
13 import android.view.KeyEvent;
14 import android.view.Menu;
15 import android.view.MenuItem;
16 import android.view.View;
17 import android.view.inputmethod.InputMethodManager;
18 import android.widget.ImageButton;
19
20 import org.chromium.chromoting.jni.JniInterface;
21
22 import java.util.Set;
23 import java.util.TreeSet;
24
25 /**
26  * A simple screen that does nothing except display a DesktopView and notify it of rotations.
27  */
28 public class Desktop extends Activity implements View.OnSystemUiVisibilityChangeListener {
29     /** Web page to be displayed in the Help screen when launched from this activity. */
30     private static final String HELP_URL =
31             "http://support.google.com/chrome/?p=mobile_crd_connecthost";
32
33     /** The surface that displays the remote host's desktop feed. */
34     private DesktopView mRemoteHostDesktop;
35
36     /** The button used to show the action bar. */
37     private ImageButton mOverlayButton;
38
39     /** Set of pressed keys for which we've sent TextEvent. */
40     private Set<Integer> mPressedTextKeys = new TreeSet<Integer>();
41
42     /** Called when the activity is first created. */
43     @Override
44     public void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         setContentView(R.layout.desktop);
47         mRemoteHostDesktop = (DesktopView)findViewById(R.id.desktop_view);
48         mOverlayButton = (ImageButton)findViewById(R.id.desktop_overlay_button);
49         mRemoteHostDesktop.setDesktop(this);
50
51         // Ensure the button is initially hidden.
52         showActionBar();
53
54         View decorView = getWindow().getDecorView();
55         decorView.setOnSystemUiVisibilityChangeListener(this);
56     }
57
58     /** Called when the activity is finally finished. */
59     @Override
60     public void onDestroy() {
61         super.onDestroy();
62         JniInterface.disconnectFromHost();
63     }
64
65     /** Called when the display is rotated (as registered in the manifest). */
66     @Override
67     public void onConfigurationChanged(Configuration newConfig) {
68         super.onConfigurationChanged(newConfig);
69         mRemoteHostDesktop.onScreenConfigurationChanged();
70     }
71
72     /** Called to initialize the action bar. */
73     @Override
74     public boolean onCreateOptionsMenu(Menu menu) {
75         getMenuInflater().inflate(R.menu.desktop_actionbar, menu);
76         return super.onCreateOptionsMenu(menu);
77     }
78
79     /** Called whenever the visibility of the system status bar or navigation bar changes. */
80     @Override
81     public void onSystemUiVisibilityChange(int visibility) {
82         // Ensure the action-bar's visibility matches that of the system controls. This
83         // minimizes the number of states the UI can be in, to keep things simple for the user.
84
85         // Determine if the system is in fullscreen/lights-out mode. LOW_PROFILE is needed since
86         // it's the only flag supported in 4.0. But it is not sufficient in itself; when
87         // IMMERSIVE_STICKY mode is used, the system clears this flag (leaving the FULLSCREEN flag
88         // set) when the user swipes the edge to reveal the bars temporarily. When this happens,
89         // the action-bar should remain hidden.
90         int fullscreenFlags = getSystemUiFlags();
91         if ((visibility & fullscreenFlags) != 0) {
92             hideActionBar();
93         } else {
94             showActionBar();
95         }
96     }
97
98     @SuppressLint("InlinedApi")
99     private int getSystemUiFlags() {
100         int flags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
101         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
102             flags |= View.SYSTEM_UI_FLAG_FULLSCREEN;
103         }
104         return flags;
105     }
106
107     public void showActionBar() {
108         mOverlayButton.setVisibility(View.INVISIBLE);
109         getActionBar().show();
110
111         View decorView = getWindow().getDecorView();
112         decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
113     }
114
115     public void hideActionBar() {
116         mOverlayButton.setVisibility(View.VISIBLE);
117         getActionBar().hide();
118
119         View decorView = getWindow().getDecorView();
120
121         // LOW_PROFILE gives the status and navigation bars a "lights-out" appearance.
122         // FULLSCREEN hides the status bar on supported devices (4.1 and above).
123         int flags = getSystemUiFlags();
124
125         // HIDE_NAVIGATION hides the navigation bar. However, if the user touches the screen, the
126         // event is not seen by the application and instead the navigation bar is re-shown.
127         // IMMERSIVE(_STICKY) fixes this problem and allows the user to interact with the app while
128         // keeping the navigation controls hidden. This flag was introduced in 4.4, later than
129         // HIDE_NAVIGATION, and so a runtime check is needed before setting either of these flags.
130         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
131             flags |= (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
132         }
133
134         decorView.setSystemUiVisibility(flags);
135     }
136
137     /** The overlay button's onClick handler. */
138     public void onOverlayButtonPressed(View view) {
139         showActionBar();
140     }
141
142     /** Called whenever an action bar button is pressed. */
143     @Override
144     public boolean onOptionsItemSelected(MenuItem item) {
145         int id = item.getItemId();
146         if (id == R.id.actionbar_keyboard) {
147             ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0, 0);
148             return true;
149         }
150         if (id == R.id.actionbar_hide) {
151             hideActionBar();
152             return true;
153         }
154         if (id == R.id.actionbar_disconnect) {
155             JniInterface.disconnectFromHost();
156             return true;
157         }
158         if (id == R.id.actionbar_send_ctrl_alt_del) {
159             int[] keys = {
160                 KeyEvent.KEYCODE_CTRL_LEFT,
161                 KeyEvent.KEYCODE_ALT_LEFT,
162                 KeyEvent.KEYCODE_FORWARD_DEL,
163             };
164             for (int key : keys) {
165                 JniInterface.sendKeyEvent(key, true);
166             }
167             for (int key : keys) {
168                 JniInterface.sendKeyEvent(key, false);
169             }
170             return true;
171         }
172         if (id == R.id.actionbar_help) {
173             HelpActivity.launch(this, HELP_URL);
174             return true;
175         }
176         return super.onOptionsItemSelected(item);
177     }
178
179     /**
180      * Called once when a keyboard key is pressed, then again when that same key is released. This
181      * is not guaranteed to be notified of all soft keyboard events: certian keyboards might not
182      * call it at all, while others might skip it in certain situations (e.g. swipe input).
183      */
184     @Override
185     public boolean dispatchKeyEvent(KeyEvent event) {
186         int keyCode = event.getKeyCode();
187
188         // Dispatch the back button to the system to handle navigation
189         if (keyCode == KeyEvent.KEYCODE_BACK) {
190             return super.dispatchKeyEvent(event);
191         }
192
193         // Send TextEvent in two cases:
194         //   1. This is an ACTION_MULTIPLE event.
195         //   2. The event was generated by on-screen keyboard and Ctrl, Alt and
196         //      Meta are not pressed.
197         // This ensures that on-screen keyboard always injects input that
198         // correspond to what user sees on the screen, while physical keyboard
199         // acts as if it is connected to the remote host.
200         if (event.getAction() == KeyEvent.ACTION_MULTIPLE) {
201             JniInterface.sendTextEvent(event.getCharacters());
202             return true;
203         }
204
205         boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN;
206
207         // For Enter getUnicodeChar() returns 10 (line feed), but we still
208         // want to send it as KeyEvent.
209         int unicode = keyCode != KeyEvent.KEYCODE_ENTER ? event.getUnicodeChar() : 0;
210
211         boolean no_modifiers = !event.isAltPressed() &&
212                                !event.isCtrlPressed() && !event.isMetaPressed();
213
214         if (event.getDeviceId() == KeyCharacterMap.VIRTUAL_KEYBOARD &&
215             pressed && unicode != 0 && no_modifiers) {
216             mPressedTextKeys.add(keyCode);
217             int[] codePoints = { unicode };
218             JniInterface.sendTextEvent(new String(codePoints, 0, 1));
219             return true;
220         }
221
222         if (!pressed && mPressedTextKeys.contains(keyCode)) {
223             mPressedTextKeys.remove(keyCode);
224             return true;
225         }
226
227         switch (keyCode) {
228             case KeyEvent.KEYCODE_AT:
229                 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT, pressed);
230                 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_2, pressed);
231                 return true;
232
233             case KeyEvent.KEYCODE_POUND:
234                 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT, pressed);
235                 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_3, pressed);
236                 return true;
237
238             case KeyEvent.KEYCODE_STAR:
239                 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT, pressed);
240                 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_8, pressed);
241                 return true;
242
243             case KeyEvent.KEYCODE_PLUS:
244                 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT, pressed);
245                 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_EQUALS, pressed);
246                 return true;
247
248             default:
249                 // We try to send all other key codes to the host directly.
250                 return JniInterface.sendKeyEvent(keyCode, pressed);
251         }
252     }
253 }