OLD | NEW |
---|---|
(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) { | |
garykac
2013/07/17 23:52:28
Tab?
solb
2013/07/18 01:23:36
Done.
| |
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 sWidth of the video feed. */ | |
garykac
2013/07/17 23:52:28
Screen width
solb
2013/07/18 01:23:36
Done.
| |
104 private static int sWidth = 0; | |
105 | |
106 /** Screen sHeight of the video feed. */ | |
garykac
2013/07/17 23:52:28
Screen height
solb
2013/07/18 01:23:36
Done.
| |
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, | |
119 sContext.getResources().getStringArray(R.array.protoc_states)[st ate]+(error!=0 ? | |
garykac
2013/07/17 23:52:28
Can this be formatted better?
sContext.getRes
solb
2013/07/18 01:23:36
Done.
| |
120 ": "+sContext.getResources().getStringArray(R.array.protoc_error s)[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(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_ VARIATION_PASSWORD); | |
garykac
2013/07/17 23:52:28
spaces around |
solb
2013/07/18 01:23:36
Done.
| |
132 pinPrompt.setView(pinEntry); | |
133 | |
134 pinPrompt.setPositiveButton( | |
135 R.string.pin_entry_connect, new DialogInterface.OnClickListener( ) { | |
136 @Override | |
137 public void onClick(DialogInterface dialog, int which) { | |
138 Log.i("jniiface", "User provided a PIN code"); | |
139 authenticationResponse(String.valueOf(pinEntry.getText())); | |
140 } | |
141 }); | |
142 | |
143 pinPrompt.setNegativeButton( | |
144 R.string.pin_entry_cancel, new DialogInterface.OnClickListener() { | |
145 @Override | |
146 public void onClick(DialogInterface dialog, int which) { | |
147 Log.i("jniiface", "User canceled pin entry prompt"); | |
148 Toast.makeText(sContext, | |
149 sContext.getString(R.string.msg_pin_canceled), | |
150 Toast.LENGTH_LONG).show(); | |
151 disconnectFromHost(); | |
152 } | |
153 }); | |
154 | |
155 pinPrompt.show(); | |
156 } | |
157 | |
158 /** Forces the native graphics thread to redraw to the canvas. */ | |
159 public static synchronized boolean redrawGraphics() { | |
160 if (!sConnected) { | |
161 return false; | |
162 } | |
163 | |
164 scheduleRedrawNative(); | |
165 return true; | |
166 } | |
167 | |
168 /** Performs the redrawing callback. */ | |
169 private static void redrawGraphicsInternal() { | |
170 // TODO(solb) Actually draw the image onto some canvas. | |
171 } | |
172 | |
173 /** Performs the native response to the user's PIN. */ | |
174 private static native void authenticationResponse(String pin); | |
175 | |
176 /** Schedules a redraw on the native graphics thread. */ | |
177 private static native void scheduleRedrawNative(); | |
178 } | |
OLD | NEW |