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.
5 package org.chromium.chromoting;
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;
20 import org.chromium.chromoting.jni.JniInterface;
23 import java.util.TreeSet;
26 * A simple screen that does nothing except display a DesktopView and notify it of rotations.
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";
33 /** The surface that displays the remote host's desktop feed. */
34 private DesktopView mRemoteHostDesktop;
36 /** The button used to show the action bar. */
37 private ImageButton mOverlayButton;
39 /** Set of pressed keys for which we've sent TextEvent. */
40 private Set<Integer> mPressedTextKeys = new TreeSet<Integer>();
42 /** Called when the activity is first created. */
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);
51 // Ensure the button is initially hidden.
54 View decorView = getWindow().getDecorView();
55 decorView.setOnSystemUiVisibilityChangeListener(this);
58 /** Called when the activity is finally finished. */
60 public void onDestroy() {
62 JniInterface.disconnectFromHost();
65 /** Called when the display is rotated (as registered in the manifest). */
67 public void onConfigurationChanged(Configuration newConfig) {
68 super.onConfigurationChanged(newConfig);
69 mRemoteHostDesktop.onScreenConfigurationChanged();
72 /** Called to initialize the action bar. */
74 public boolean onCreateOptionsMenu(Menu menu) {
75 getMenuInflater().inflate(R.menu.desktop_actionbar, menu);
76 return super.onCreateOptionsMenu(menu);
79 /** Called whenever the visibility of the system status bar or navigation bar changes. */
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.
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) {
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;
107 public void showActionBar() {
108 mOverlayButton.setVisibility(View.INVISIBLE);
109 getActionBar().show();
111 View decorView = getWindow().getDecorView();
112 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
115 public void hideActionBar() {
116 mOverlayButton.setVisibility(View.VISIBLE);
117 getActionBar().hide();
119 View decorView = getWindow().getDecorView();
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();
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);
134 decorView.setSystemUiVisibility(flags);
137 /** The overlay button's onClick handler. */
138 public void onOverlayButtonPressed(View view) {
142 /** Called whenever an action bar button is pressed. */
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);
150 if (id == R.id.actionbar_hide) {
154 if (id == R.id.actionbar_disconnect) {
155 JniInterface.disconnectFromHost();
158 if (id == R.id.actionbar_send_ctrl_alt_del) {
160 KeyEvent.KEYCODE_CTRL_LEFT,
161 KeyEvent.KEYCODE_ALT_LEFT,
162 KeyEvent.KEYCODE_FORWARD_DEL,
164 for (int key : keys) {
165 JniInterface.sendKeyEvent(key, true);
167 for (int key : keys) {
168 JniInterface.sendKeyEvent(key, false);
172 if (id == R.id.actionbar_help) {
173 HelpActivity.launch(this, HELP_URL);
176 return super.onOptionsItemSelected(item);
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).
185 public boolean dispatchKeyEvent(KeyEvent event) {
186 int keyCode = event.getKeyCode();
188 // Dispatch the back button to the system to handle navigation
189 if (keyCode == KeyEvent.KEYCODE_BACK) {
190 return super.dispatchKeyEvent(event);
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());
205 boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN;
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;
211 boolean no_modifiers = !event.isAltPressed() &&
212 !event.isCtrlPressed() && !event.isMetaPressed();
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));
222 if (!pressed && mPressedTextKeys.contains(keyCode)) {
223 mPressedTextKeys.remove(keyCode);
228 case KeyEvent.KEYCODE_AT:
229 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT, pressed);
230 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_2, pressed);
233 case KeyEvent.KEYCODE_POUND:
234 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT, pressed);
235 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_3, pressed);
238 case KeyEvent.KEYCODE_STAR:
239 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT, pressed);
240 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_8, pressed);
243 case KeyEvent.KEYCODE_PLUS:
244 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT, pressed);
245 JniInterface.sendKeyEvent(KeyEvent.KEYCODE_EQUALS, pressed);
249 // We try to send all other key codes to the host directly.
250 return JniInterface.sendKeyEvent(keyCode, pressed);