Upstream version 5.34.104.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.app.Activity;
8 import android.content.res.Configuration;
9 import android.os.Bundle;
10 import android.view.KeyEvent;
11 import android.view.Menu;
12 import android.view.MenuItem;
13 import android.view.View;
14 import android.view.inputmethod.InputMethodManager;
15 import android.widget.ImageButton;
16
17 import org.chromium.chromoting.jni.JniInterface;
18
19 /**
20  * A simple screen that does nothing except display a DesktopView and notify it of rotations.
21  */
22 public class Desktop extends Activity {
23     /** The surface that displays the remote host's desktop feed. */
24     private DesktopView mRemoteHostDesktop;
25
26     /** The button used to show the action bar. */
27     private ImageButton mOverlayButton;
28
29     /** Called when the activity is first created. */
30     @Override
31     public void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         setContentView(R.layout.desktop);
34         mRemoteHostDesktop = (DesktopView)findViewById(R.id.desktop_view);
35         mOverlayButton = (ImageButton)findViewById(R.id.desktop_overlay_button);
36         mRemoteHostDesktop.setDesktop(this);
37
38         // Ensure the button is initially hidden.
39         showActionBar();
40     }
41
42     /** Called when the activity is finally finished. */
43     @Override
44     public void onDestroy() {
45         super.onDestroy();
46         JniInterface.disconnectFromHost();
47     }
48
49     /** Called when the display is rotated (as registered in the manifest). */
50     @Override
51     public void onConfigurationChanged(Configuration newConfig) {
52         super.onConfigurationChanged(newConfig);
53         mRemoteHostDesktop.onScreenConfigurationChanged();
54     }
55
56     /** Called to initialize the action bar. */
57     @Override
58     public boolean onCreateOptionsMenu(Menu menu) {
59         getMenuInflater().inflate(R.menu.desktop_actionbar, menu);
60         return super.onCreateOptionsMenu(menu);
61     }
62
63     public void showActionBar() {
64         mOverlayButton.setVisibility(View.INVISIBLE);
65         getActionBar().show();
66     }
67
68     public void hideActionBar() {
69         mOverlayButton.setVisibility(View.VISIBLE);
70         getActionBar().hide();
71     }
72
73     /** The overlay button's onClick handler. */
74     public void onOverlayButtonPressed(View view) {
75         showActionBar();
76     }
77
78     /** Called whenever an action bar button is pressed. */
79     @Override
80     public boolean onOptionsItemSelected(MenuItem item) {
81         switch (item.getItemId()) {
82             case R.id.actionbar_keyboard:
83                 ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0, 0);
84                 return true;
85
86             case R.id.actionbar_hide:
87                 hideActionBar();
88                 return true;
89
90             case R.id.actionbar_disconnect:
91                 JniInterface.disconnectFromHost();
92                 return true;
93
94             case R.id.actionbar_send_ctrl_alt_del:
95                 {
96                     int[] keys = {
97                         KeyEvent.KEYCODE_CTRL_LEFT,
98                         KeyEvent.KEYCODE_ALT_LEFT,
99                         KeyEvent.KEYCODE_FORWARD_DEL,
100                     };
101                     for (int key : keys) {
102                         JniInterface.keyboardAction(key, true);
103                     }
104                     for (int key : keys) {
105                         JniInterface.keyboardAction(key, false);
106                     }
107                 }
108                 return true;
109
110             default:
111                 return super.onOptionsItemSelected(item);
112         }
113     }
114
115     /**
116      * Called once when a keyboard key is pressed, then again when that same key is released. This
117      * is not guaranteed to be notified of all soft keyboard events: certian keyboards might not
118      * call it at all, while others might skip it in certain situations (e.g. swipe input).
119      */
120     @Override
121     public boolean dispatchKeyEvent(KeyEvent event) {
122         boolean depressed = event.getAction() == KeyEvent.ACTION_DOWN;
123
124         switch (event.getKeyCode()) {
125             case KeyEvent.KEYCODE_AT:
126                 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
127                 JniInterface.keyboardAction(KeyEvent.KEYCODE_2, depressed);
128                 break;
129
130             case KeyEvent.KEYCODE_POUND:
131                 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
132                 JniInterface.keyboardAction(KeyEvent.KEYCODE_3, depressed);
133                 break;
134
135             case KeyEvent.KEYCODE_STAR:
136                 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
137                 JniInterface.keyboardAction(KeyEvent.KEYCODE_8, depressed);
138                 break;
139
140             case KeyEvent.KEYCODE_PLUS:
141                 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
142                 JniInterface.keyboardAction(KeyEvent.KEYCODE_EQUALS, depressed);
143                 break;
144
145             default:
146                 // We try to send all other key codes to the host directly.
147                 JniInterface.keyboardAction(event.getKeyCode(), depressed);
148         }
149
150         return super.dispatchKeyEvent(event);
151     }
152 }