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