Chromium Code Reviews| Index: remoting/android/java/src/org/chromium/chromoting/jni/JniInterface.java |
| diff --git a/remoting/android/java/src/org/chromium/chromoting/jni/JniInterface.java b/remoting/android/java/src/org/chromium/chromoting/jni/JniInterface.java |
| index ada724882f5fa49b461fc66f0da63628e06d701a..fc62cea0c714ec4cdeb2f9c53c2ea294c5a7e927 100644 |
| --- a/remoting/android/java/src/org/chromium/chromoting/jni/JniInterface.java |
| +++ b/remoting/android/java/src/org/chromium/chromoting/jni/JniInterface.java |
| @@ -6,7 +6,6 @@ package org.chromium.chromoting.jni; |
| import android.app.Activity; |
| import android.app.AlertDialog; |
| -import android.app.ProgressDialog; |
| import android.content.Context; |
| import android.content.DialogInterface; |
| import android.content.SharedPreferences; |
| @@ -33,9 +32,6 @@ import java.nio.ByteOrder; |
| */ |
| @JNINamespace("remoting") |
| public class JniInterface { |
| - /** The status code indicating successful connection. */ |
| - private static final int SUCCESSFUL_CONNECTION = 3; |
| - |
| /* |
| * Library-loading state machine. |
| */ |
| @@ -45,22 +41,48 @@ public class JniInterface { |
| /** The application context. Accessed on the UI thread. */ |
| private static Activity sContext = null; |
| + /** Interface used for connection state notifications. */ |
| + public interface ConnectionListener { |
| + /** |
| + * These values must match the C++ enumeration remoting::protocol::ConnectionToHost::State. |
| + */ |
| + int STATE_INITIALIZING = 0; |
|
Sergey Ulanov
2014/01/08 20:30:45
Can we use Java enums here? In the past it was rec
Lambros
2014/01/08 22:30:01
Done.
|
| + int STATE_CONNECTING = 1; |
| + int STATE_AUTHENTICATED = 2; |
| + int STATE_CONNECTED = 3; |
| + int STATE_FAILED = 4; |
| + int STATE_CLOSED = 5; |
| + |
| + /** |
| + * These values must match the C++ enumeration remoting::protocol::ErrorCode. |
| + */ |
| + int ERROR_OK = 0; |
| + int ERROR_PEER_IS_OFFLINE = 1; |
| + int ERROR_SESSION_REJECTED = 2; |
| + int ERROR_INCOMPATIBLE_PROTOCOL = 3; |
| + int ERROR_AUTHENTICATION_FAILED = 4; |
| + int ERROR_CHANNEL_CONNECTION_ERROR = 5; |
| + int ERROR_SIGNALING_ERROR = 6; |
| + int ERROR_SIGNALING_TIMEOUT = 7; |
| + int ERROR_HOST_OVERLOAD = 8; |
| + int ERROR_UNKNOWN_ERROR = 9; |
| + |
| + /** |
| + * Notified on connection state change. |
| + * @param state The new connection state. |
| + * @param error The error code, if state is STATE_FAILED. |
| + */ |
| + void onConnectionState(int state, int error); |
| + } |
| + |
| /* |
| * Connection-initiating state machine. |
| */ |
| /** Whether the native code is attempting a connection. Accessed on the UI thread. */ |
| private static boolean sConnected = false; |
| - /** Callback to signal upon successful connection. Accessed on the UI thread. */ |
| - private static Runnable sSuccessCallback = null; |
| - |
| - /** Dialog for reporting connection progress. Accessed on the UI thread. */ |
| - private static ProgressDialog sProgressIndicator = null; |
| - |
| - // Protects access to |sProgressIndicator|. Used only to silence FindBugs warnings - the |
| - // variable it protects is only accessed on a single thread. |
| - // TODO(lambroslambrou): Refactor the ProgressIndicator into a separate class. |
| - private static Object sProgressIndicatorLock = new Object(); |
| + /** Notified upon successful connection or disconnection. Accessed on the UI thread. */ |
| + private static ConnectionListener sConnectionListener = null; |
| /** |
| * Callback invoked on the graphics thread to repaint the desktop. Accessed on the UI and |
| @@ -108,10 +130,10 @@ public class JniInterface { |
| /** Attempts to form a connection to the user-selected host. Called on the UI thread. */ |
| public static void connectToHost(String username, String authToken, |
| - String hostJid, String hostId, String hostPubkey, Runnable successCallback) { |
| + String hostJid, String hostId, String hostPubkey, ConnectionListener listener) { |
| disconnectFromHost(); |
| - sSuccessCallback = successCallback; |
| + sConnectionListener = listener; |
| SharedPreferences prefs = sContext.getPreferences(Activity.MODE_PRIVATE); |
| nativeConnect(username, authToken, hostJid, hostId, hostPubkey, |
| prefs.getString(hostId + "_id", ""), prefs.getString(hostId + "_secret", "")); |
| @@ -126,15 +148,10 @@ public class JniInterface { |
| public static void disconnectFromHost() { |
| if (!sConnected) return; |
| - synchronized (sProgressIndicatorLock) { |
| - if (sProgressIndicator != null) { |
| - sProgressIndicator.dismiss(); |
| - sProgressIndicator = null; |
| - } |
| - } |
| + sConnectionListener.onConnectionState(ConnectionListener.STATE_CLOSED, 0); |
| nativeDisconnect(); |
| - sSuccessCallback = null; |
| + sConnectionListener = null; |
| sConnected = false; |
| // Drop the reference to free the Bitmap for GC. |
| @@ -149,47 +166,7 @@ public class JniInterface { |
| /** Reports whenever the connection status changes. Called on the UI thread. */ |
| @CalledByNative |
| private static void reportConnectionStatus(int state, int error) { |
| - if (state < SUCCESSFUL_CONNECTION && error == 0) { |
| - // The connection is still being established, so we'll report the current progress. |
| - synchronized (sProgressIndicatorLock) { |
| - if (sProgressIndicator == null) { |
| - sProgressIndicator = ProgressDialog.show(sContext, sContext. |
| - getString(R.string.progress_title), sContext.getResources(). |
| - getStringArray(R.array.protoc_states)[state], true, true, |
| - new DialogInterface.OnCancelListener() { |
| - @Override |
| - public void onCancel(DialogInterface dialog) { |
| - Log.i("jniiface", "User canceled connection initiation"); |
| - disconnectFromHost(); |
| - } |
| - }); |
| - } else { |
| - sProgressIndicator.setMessage( |
| - sContext.getResources().getStringArray(R.array.protoc_states)[state]); |
| - } |
| - } |
| - } else { |
| - // The connection is complete or has failed, so we can lose the progress indicator. |
| - synchronized (sProgressIndicatorLock) { |
| - if (sProgressIndicator != null) { |
| - sProgressIndicator.dismiss(); |
| - sProgressIndicator = null; |
| - } |
| - } |
| - |
| - if (state == SUCCESSFUL_CONNECTION) { |
| - Toast.makeText(sContext, sContext.getResources(). |
| - getStringArray(R.array.protoc_states)[state], Toast.LENGTH_SHORT).show(); |
| - |
| - // Actually display the remote desktop. |
| - sSuccessCallback.run(); |
| - } else { |
| - Toast.makeText(sContext, sContext.getResources().getStringArray( |
| - R.array.protoc_states)[state] + (error == 0 ? "" : ": " + |
| - sContext.getResources().getStringArray(R.array.protoc_errors)[error]), |
| - Toast.LENGTH_LONG).show(); |
| - } |
| - } |
| + sConnectionListener.onConnectionState(state, error); |
| } |
| /** Prompts the user to enter a PIN. Called on the UI thread. */ |