Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: remoting/android/java/src/org/chromium/chromoting/jni/JniInterface.java

Issue 19506004: Add the beginnings of a Chromoting Android app (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Gary's spacing and commenting concerns Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.jni;
6
7 import android.app.Activity;
8 import android.app.AlertDialog;
9 import android.content.Context;
10 import android.content.DialogInterface;
11 import android.graphics.Bitmap;
12 import android.text.InputType;
13 import android.util.Log;
14 import android.widget.EditText;
15 import android.widget.Toast;
16
17 import org.chromium.chromoting.R;
18
19 import java.nio.ByteBuffer;
20 import java.nio.ByteOrder;
21
22 /**
23 * Initializes the Chromium remoting library, and provides JNI calls into it.
24 * All interaction with the native code is centralized in this class.
25 */
26 public class JniInterface {
27 /** The status code indicating successful connection. */
28 private static final int SUCCESSFUL_CONNECTION = 3;
29
30 /** The application context. */
31 private static Activity sContext = null;
32
33 /*
34 * Library-loading state machine.
35 */
36 /** Whether we've already loaded the library. */
37 private static boolean sLoaded = false;
38
39 /** To be called once from the main Activity. */
40 public static synchronized void loadLibrary(Activity context) {
41 if (!sLoaded) {
42 System.loadLibrary("remoting_client_jni");
43 loadNative(context);
44 sContext = context;
45 sLoaded = true;
46 }
47 }
48
49 /** Performs the native portion of the initialization. */
50 private static native void loadNative(Context context);
51
52 /*
53 * API/OAuth2 keys access.
54 */
55 public static native String getApiKey();
56 public static native String getClientId();
57 public static native String getClientSecret();
58
59 /*
60 * Connection-initiating state machine.
61 */
62 /** Whether the native code is attempting a connection. */
63 private static boolean sConnected = false;
64
65 /** The callback to signal upon successful connection. */
66 private static Runnable sSuccessCallback = null;
67
68 /** Attempts to form a connection to the user-selected host. */
69 public static synchronized void connectToHost(String username, String authTo ken,
70 String hostJid, String hostId, String hostPubkey, Runnable successCa llback) {
71 if (sLoaded) {
72 if (sConnected) {
73 disconnectFromHost();
74 }
75
76 sSuccessCallback = successCallback;
77 connectNative(username, authToken, hostJid, hostId, hostPubkey);
78 sConnected = true;
79 } else {
80 Log.w("jniiface", "ConnectToHost called before loadLibrary!");
81 }
82 }
83
84 /** Severs the connection and cleans up. */
85 public static synchronized void disconnectFromHost() {
86 if (sLoaded && sConnected) {
87 disconnectNative();
88 sSuccessCallback = null;
89 sConnected = false;
90 }
91 }
92
93 /** Performs the native portion of the connection. */
94 private static native void connectNative(
95 String username, String authToken, String hostJid, String hostId, St ring hostPubkey);
96
97 /** Performs the native portion of the cleanup. */
98 private static native void disconnectNative();
99
100 /*
101 * Entry points *from* the native code.
102 */
103 /** Screen width of the video feed. */
104 private static int sWidth = 0;
105
106 /** Screen height of the video feed. */
107 private static int sHeight = 0;
108
109 /** Buffer holding the video feed. */
110 private static ByteBuffer sBuffer = null;
111
112 /** Reports whenever the connection status changes. */
113 private static void reportConnectionStatus(int state, int error) {
114 if (state==SUCCESSFUL_CONNECTION) {
115 sSuccessCallback.run();
116 }
117
118 Toast.makeText(sContext, sContext.getResources().getStringArray(
119 R.array.protoc_states)[state] + (error!=0 ? ": " +
120 sContext.getResources().getStringArray(R.array.protoc_er rors)[error] : ""),
121 Toast.LENGTH_SHORT).show();
122 }
123
124 /** Prompts the user to enter a PIN. */
125 private static void displayAuthenticationPrompt() {
126 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(sContext);
127 pinPrompt.setTitle(sContext.getString(R.string.pin_entry_title));
128 pinPrompt.setMessage(sContext.getString(R.string.pin_entry_message));
129
130 final EditText pinEntry = new EditText(sContext);
131 pinEntry.setInputType(
132 InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PA SSWORD);
133 pinPrompt.setView(pinEntry);
134
135 pinPrompt.setPositiveButton(
136 R.string.pin_entry_connect, new DialogInterface.OnClickListener( ) {
137 @Override
138 public void onClick(DialogInterface dialog, int which) {
139 Log.i("jniiface", "User provided a PIN code");
140 authenticationResponse(String.valueOf(pinEntry.getText()));
141 }
142 });
143
144 pinPrompt.setNegativeButton(
145 R.string.pin_entry_cancel, new DialogInterface.OnClickListener() {
146 @Override
147 public void onClick(DialogInterface dialog, int which) {
148 Log.i("jniiface", "User canceled pin entry prompt");
149 Toast.makeText(sContext,
150 sContext.getString(R.string.msg_pin_canceled),
151 Toast.LENGTH_LONG).show();
152 disconnectFromHost();
153 }
154 });
155
156 pinPrompt.show();
157 }
158
159 /** Forces the native graphics thread to redraw to the canvas. */
160 public static synchronized boolean redrawGraphics() {
161 if (!sConnected) {
162 return false;
163 }
164
165 scheduleRedrawNative();
166 return true;
167 }
168
169 /** Performs the redrawing callback. */
170 private static void redrawGraphicsInternal() {
171 // TODO(solb) Actually draw the image onto some canvas.
172 }
173
174 /** Performs the native response to the user's PIN. */
175 private static native void authenticationResponse(String pin);
176
177 /** Schedules a redraw on the native graphics thread. */
178 private static native void scheduleRedrawNative();
179 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698