| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chromoting.jni; | 5 package org.chromium.chromoting.jni; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.app.AlertDialog; | 8 import android.app.AlertDialog; |
| 9 import android.app.ProgressDialog; | 9 import android.app.ProgressDialog; |
| 10 import android.content.Context; | 10 import android.content.Context; |
| 11 import android.content.DialogInterface; | 11 import android.content.DialogInterface; |
| 12 import android.content.SharedPreferences; |
| 12 import android.graphics.Bitmap; | 13 import android.graphics.Bitmap; |
| 13 import android.os.Looper; | 14 import android.os.Looper; |
| 14 import android.text.InputType; | 15 import android.text.InputType; |
| 15 import android.util.Log; | 16 import android.util.Log; |
| 16 import android.view.KeyEvent; | 17 import android.view.KeyEvent; |
| 18 import android.view.View; |
| 17 import android.view.inputmethod.EditorInfo; | 19 import android.view.inputmethod.EditorInfo; |
| 18 import android.widget.EditText; | 20 import android.widget.CheckBox; |
| 19 import android.widget.TextView; | 21 import android.widget.TextView; |
| 20 import android.widget.Toast; | 22 import android.widget.Toast; |
| 21 | 23 |
| 22 import org.chromium.chromoting.R; | 24 import org.chromium.chromoting.R; |
| 23 | 25 |
| 24 import java.nio.ByteBuffer; | 26 import java.nio.ByteBuffer; |
| 25 import java.nio.ByteOrder; | 27 import java.nio.ByteOrder; |
| 26 | 28 |
| 27 /** | 29 /** |
| 28 * Initializes the Chromium remoting library, and provides JNI calls into it. | 30 * Initializes the Chromium remoting library, and provides JNI calls into it. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 String hostJid, String hostId, String hostPubkey, Runnable successCa
llback) { | 87 String hostJid, String hostId, String hostPubkey, Runnable successCa
llback) { |
| 86 synchronized(JniInterface.class) { | 88 synchronized(JniInterface.class) { |
| 87 if (!sLoaded) return; | 89 if (!sLoaded) return; |
| 88 | 90 |
| 89 if (sConnected) { | 91 if (sConnected) { |
| 90 disconnectFromHost(); | 92 disconnectFromHost(); |
| 91 } | 93 } |
| 92 } | 94 } |
| 93 | 95 |
| 94 sSuccessCallback = successCallback; | 96 sSuccessCallback = successCallback; |
| 95 connectNative(username, authToken, hostJid, hostId, hostPubkey); | 97 SharedPreferences prefs = sContext.getPreferences(Activity.MODE_PRIVATE)
; |
| 98 connectNative(username, authToken, hostJid, hostId, hostPubkey, |
| 99 prefs.getString(hostId + "_id", ""), prefs.getString(hostId + "_
secret", "")); |
| 96 sConnected = true; | 100 sConnected = true; |
| 97 } | 101 } |
| 98 | 102 |
| 99 /** Severs the connection and cleans up. */ | 103 /** Severs the connection and cleans up. */ |
| 100 public static void disconnectFromHost() { | 104 public static void disconnectFromHost() { |
| 101 synchronized(JniInterface.class) { | 105 synchronized(JniInterface.class) { |
| 102 if (!sLoaded || !sConnected) return; | 106 if (!sLoaded || !sConnected) return; |
| 103 | 107 |
| 104 if (sProgressIndicator != null) { | 108 if (sProgressIndicator != null) { |
| 105 sProgressIndicator.dismiss(); | 109 sProgressIndicator.dismiss(); |
| 106 sProgressIndicator = null; | 110 sProgressIndicator = null; |
| 107 } | 111 } |
| 108 } | 112 } |
| 109 | 113 |
| 110 disconnectNative(); | 114 disconnectNative(); |
| 111 sSuccessCallback = null; | 115 sSuccessCallback = null; |
| 112 sConnected = false; | 116 sConnected = false; |
| 113 } | 117 } |
| 114 | 118 |
| 115 /** Performs the native portion of the connection. */ | 119 /** Performs the native portion of the connection. */ |
| 116 private static native void connectNative( | 120 private static native void connectNative(String username, String authToken,
String hostJid, |
| 117 String username, String authToken, String hostJid, String hostId, St
ring hostPubkey); | 121 String hostId, String hostPubkey, String pairId, String pairSecret); |
| 118 | 122 |
| 119 /** Performs the native portion of the cleanup. */ | 123 /** Performs the native portion of the cleanup. */ |
| 120 private static native void disconnectNative(); | 124 private static native void disconnectNative(); |
| 121 | 125 |
| 122 /* | 126 /* |
| 123 * Entry points *from* the native code. | 127 * Entry points *from* the native code. |
| 124 */ | 128 */ |
| 125 /** Callback to signal whenever we need to redraw. */ | 129 /** Callback to signal whenever we need to redraw. */ |
| 126 private static Runnable sRedrawCallback = null; | 130 private static Runnable sRedrawCallback = null; |
| 127 | 131 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 } | 185 } |
| 182 } | 186 } |
| 183 | 187 |
| 184 /** Prompts the user to enter a PIN. */ | 188 /** Prompts the user to enter a PIN. */ |
| 185 private static void displayAuthenticationPrompt() { | 189 private static void displayAuthenticationPrompt() { |
| 186 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(sContext); | 190 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(sContext); |
| 187 pinPrompt.setTitle(sContext.getString(R.string.pin_entry_title)); | 191 pinPrompt.setTitle(sContext.getString(R.string.pin_entry_title)); |
| 188 pinPrompt.setMessage(sContext.getString(R.string.pin_entry_message)); | 192 pinPrompt.setMessage(sContext.getString(R.string.pin_entry_message)); |
| 189 pinPrompt.setIcon(android.R.drawable.ic_lock_lock); | 193 pinPrompt.setIcon(android.R.drawable.ic_lock_lock); |
| 190 | 194 |
| 191 final EditText pinEntry = new EditText(sContext); | 195 final View pinEntry = sContext.getLayoutInflater().inflate(R.layout.pin_
dialog, null); |
| 192 pinEntry.setInputType( | |
| 193 InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PA
SSWORD); | |
| 194 pinEntry.setImeOptions(EditorInfo.IME_ACTION_DONE); | |
| 195 pinPrompt.setView(pinEntry); | 196 pinPrompt.setView(pinEntry); |
| 196 | 197 |
| 197 pinPrompt.setPositiveButton( | 198 pinPrompt.setPositiveButton( |
| 198 R.string.pin_entry_connect, new DialogInterface.OnClickListener(
) { | 199 R.string.pin_entry_connect, new DialogInterface.OnClickListener(
) { |
| 199 @Override | 200 @Override |
| 200 public void onClick(DialogInterface dialog, int which) { | 201 public void onClick(DialogInterface dialog, int which) { |
| 201 Log.i("jniiface", "User provided a PIN code"); | 202 Log.i("jniiface", "User provided a PIN code"); |
| 202 authenticationResponse(String.valueOf(pinEntry.getText()
)); | 203 authenticationResponse(String.valueOf( |
| 204 ((TextView) |
| 205 pinEntry.findViewById(R.id.pin_dialog_te
xt)).getText()), |
| 206 ((CheckBox) |
| 207 pinEntry.findViewById(R.id.pin_dialog_ch
eck)).isChecked()); |
| 203 } | 208 } |
| 204 }); | 209 }); |
| 205 | 210 |
| 206 pinPrompt.setNegativeButton( | 211 pinPrompt.setNegativeButton( |
| 207 R.string.pin_entry_cancel, new DialogInterface.OnClickListener()
{ | 212 R.string.pin_entry_cancel, new DialogInterface.OnClickListener()
{ |
| 208 @Override | 213 @Override |
| 209 public void onClick(DialogInterface dialog, int which) { | 214 public void onClick(DialogInterface dialog, int which) { |
| 210 Log.i("jniiface", "User canceled pin entry prompt"); | 215 Log.i("jniiface", "User canceled pin entry prompt"); |
| 211 Toast.makeText(sContext, | 216 Toast.makeText(sContext, |
| 212 sContext.getString(R.string.msg_pin_canceled), | 217 sContext.getString(R.string.msg_pin_canceled), |
| 213 Toast.LENGTH_LONG).show(); | 218 Toast.LENGTH_LONG).show(); |
| 214 disconnectFromHost(); | 219 disconnectFromHost(); |
| 215 } | 220 } |
| 216 }); | 221 }); |
| 217 | 222 |
| 218 final AlertDialog pinDialog = pinPrompt.create(); | 223 final AlertDialog pinDialog = pinPrompt.create(); |
| 219 | 224 |
| 220 pinEntry.setOnEditorActionListener( | 225 ((TextView)pinEntry.findViewById(R.id.pin_dialog_text)).setOnEditorActio
nListener( |
| 221 new TextView.OnEditorActionListener() { | 226 new TextView.OnEditorActionListener() { |
| 222 @Override | 227 @Override |
| 223 public boolean onEditorAction(TextView v, int actionId, KeyE
vent event) { | 228 public boolean onEditorAction(TextView v, int actionId, KeyE
vent event) { |
| 224 // The user pressed enter on the keypad (equivalent to t
he connect button). | 229 // The user pressed enter on the keypad (equivalent to t
he connect button). |
| 225 pinDialog.getButton(AlertDialog.BUTTON_POSITIVE).perform
Click(); | 230 pinDialog.getButton(AlertDialog.BUTTON_POSITIVE).perform
Click(); |
| 226 pinDialog.dismiss(); | 231 pinDialog.dismiss(); |
| 227 return true; | 232 return true; |
| 228 } | 233 } |
| 229 }); | 234 }); |
| 230 | 235 |
| 231 pinDialog.setOnCancelListener( | 236 pinDialog.setOnCancelListener( |
| 232 new DialogInterface.OnCancelListener() { | 237 new DialogInterface.OnCancelListener() { |
| 233 @Override | 238 @Override |
| 234 public void onCancel(DialogInterface dialog) { | 239 public void onCancel(DialogInterface dialog) { |
| 235 // The user backed out of the dialog (equivalent to the
cancel button). | 240 // The user backed out of the dialog (equivalent to the
cancel button). |
| 236 pinDialog.getButton(AlertDialog.BUTTON_NEGATIVE).perform
Click(); | 241 pinDialog.getButton(AlertDialog.BUTTON_NEGATIVE).perform
Click(); |
| 237 } | 242 } |
| 238 }); | 243 }); |
| 239 | 244 |
| 240 pinDialog.show(); | 245 pinDialog.show(); |
| 241 } | 246 } |
| 242 | 247 |
| 248 /** Saves newly-received pairing credentials to permanent storage. */ |
| 249 private static void commitPairingCredentials(String host, byte[] id, byte[]
secret) { |
| 250 synchronized (sContext) { |
| 251 sContext.getPreferences(Activity.MODE_PRIVATE).edit(). |
| 252 putString(host + "_id", new String(id)). |
| 253 putString(host + "_secret", new String(secret)). |
| 254 apply(); |
| 255 } |
| 256 } |
| 257 |
| 243 /** | 258 /** |
| 244 * Sets the redraw callback to the provided functor. Provide a value of null
whenever the | 259 * Sets the redraw callback to the provided functor. Provide a value of null
whenever the |
| 245 * window is no longer visible so that we don't continue to draw onto it. | 260 * window is no longer visible so that we don't continue to draw onto it. |
| 246 */ | 261 */ |
| 247 public static void provideRedrawCallback(Runnable redrawCallback) { | 262 public static void provideRedrawCallback(Runnable redrawCallback) { |
| 248 sRedrawCallback = redrawCallback; | 263 sRedrawCallback = redrawCallback; |
| 249 } | 264 } |
| 250 | 265 |
| 251 /** Forces the native graphics thread to redraw to the canvas. */ | 266 /** Forces the native graphics thread to redraw to the canvas. */ |
| 252 public static boolean redrawGraphics() { | 267 public static boolean redrawGraphics() { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 /** Presses and releases the specified (nonnegative) key. */ | 312 /** Presses and releases the specified (nonnegative) key. */ |
| 298 public static void keyboardAction(int keyCode, boolean keyDown) { | 313 public static void keyboardAction(int keyCode, boolean keyDown) { |
| 299 if (!sConnected) { | 314 if (!sConnected) { |
| 300 return; | 315 return; |
| 301 } | 316 } |
| 302 | 317 |
| 303 keyboardActionNative(keyCode, keyDown); | 318 keyboardActionNative(keyCode, keyDown); |
| 304 } | 319 } |
| 305 | 320 |
| 306 /** Performs the native response to the user's PIN. */ | 321 /** Performs the native response to the user's PIN. */ |
| 307 private static native void authenticationResponse(String pin); | 322 private static native void authenticationResponse(String pin, boolean create
Pair); |
| 308 | 323 |
| 309 /** Schedules a redraw on the native graphics thread. */ | 324 /** Schedules a redraw on the native graphics thread. */ |
| 310 private static native void scheduleRedrawNative(); | 325 private static native void scheduleRedrawNative(); |
| 311 | 326 |
| 312 /** Passes mouse information to the native handling code. */ | 327 /** Passes mouse information to the native handling code. */ |
| 313 private static native void mouseActionNative(int x, int y, int whichButton,
boolean buttonDown); | 328 private static native void mouseActionNative(int x, int y, int whichButton,
boolean buttonDown); |
| 314 | 329 |
| 315 /** Passes key press information to the native handling code. */ | 330 /** Passes key press information to the native handling code. */ |
| 316 private static native void keyboardActionNative(int keyCode, boolean keyDown
); | 331 private static native void keyboardActionNative(int keyCode, boolean keyDown
); |
| 317 } | 332 } |
| OLD | NEW |