summaryrefslogtreecommitdiffstats
path: root/remoting/android/java/src/org/chromium/chromoting/Desktop.java
blob: 0eedf3a6ebe36d947e2c1dc0ac75132e79f2a36f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chromoting;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;

import org.chromium.chromoting.jni.JniInterface;

/**
 * A simple screen that does nothing except display a DesktopView and notify it of rotations.
 */
public class Desktop extends Activity {
    /** The surface that displays the remote host's desktop feed. */
    private DesktopView remoteHostDesktop;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        remoteHostDesktop = new DesktopView(this);
        setContentView(remoteHostDesktop);
    }

    /** Called when the activity is finally finished. */
    @Override
    public void onDestroy() {
        super.onDestroy();
        JniInterface.disconnectFromHost();
    }

    /** Called when the display is rotated (as registered in the manifest). */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        remoteHostDesktop.onScreenConfigurationChanged();
    }

    /** Called to initialize the action bar. */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.desktop_actionbar, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /** Called whenever an action bar button is pressed. */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.actionbar_keyboard:
                ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0, 0);
                return true;
            case R.id.actionbar_hide:
                getActionBar().hide();
                return true;
            case R.id.actionbar_disconnect:
                JniInterface.disconnectFromHost();
                return true;
            case R.id.actionbar_send_ctrl_alt_del:
                {
                    int[] keys = {
                        KeyEvent.KEYCODE_CTRL_LEFT,
                        KeyEvent.KEYCODE_ALT_LEFT,
                        KeyEvent.KEYCODE_FORWARD_DEL,
                    };
                    for (int key : keys) {
                        JniInterface.keyboardAction(key, true);
                    }
                    for (int key : keys) {
                        JniInterface.keyboardAction(key, false);
                    }
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * Called once when a keyboard key is pressed, then again when that same key is released. This
     * is not guaranteed to be notified of all soft keyboard events: certian keyboards might not
     * call it at all, while others might skip it in certain situations (e.g. swipe input).
     */
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        boolean depressed = event.getAction() == KeyEvent.ACTION_DOWN;

        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_AT:
                JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
                JniInterface.keyboardAction(KeyEvent.KEYCODE_2, depressed);
                break;
            case KeyEvent.KEYCODE_POUND:
                JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
                JniInterface.keyboardAction(KeyEvent.KEYCODE_3, depressed);
                break;
            case KeyEvent.KEYCODE_STAR:
                JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
                JniInterface.keyboardAction(KeyEvent.KEYCODE_8, depressed);
                break;
            case KeyEvent.KEYCODE_PLUS:
                JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
                JniInterface.keyboardAction(KeyEvent.KEYCODE_EQUALS, depressed);
                break;
            default:
                // We try to send all other key codes to the host directly.
                JniInterface.keyboardAction(event.getKeyCode(), depressed);
        }

        return super.dispatchKeyEvent(event);
    }
}