From 7f89b7025779be7d67487fcafc53a14fed480a50 Mon Sep 17 00:00:00 2001 From: lambroslambrou Date: Fri, 18 Dec 2015 15:24:15 -0800 Subject: Move JniInterface.ConnectionListener to top-level class This is to simplify refactoring jni/JniInterface code into jni/Client. BUG=526336 Review URL: https://codereview.chromium.org/1534193003 Cr-Commit-Position: refs/heads/master@{#366206} --- .../src/org/chromium/chromoting/Chromoting.java | 6 +- .../org/chromium/chromoting/SessionConnector.java | 24 +++---- .../chromoting/jni/ConnectionListener.java | 81 ++++++++++++++++++++++ .../org/chromium/chromoting/jni/JniInterface.java | 75 -------------------- 4 files changed, 96 insertions(+), 90 deletions(-) create mode 100644 remoting/android/java/src/org/chromium/chromoting/jni/ConnectionListener.java (limited to 'remoting/android') diff --git a/remoting/android/java/src/org/chromium/chromoting/Chromoting.java b/remoting/android/java/src/org/chromium/chromoting/Chromoting.java index b4356e0..800be2e 100644 --- a/remoting/android/java/src/org/chromium/chromoting/Chromoting.java +++ b/remoting/android/java/src/org/chromium/chromoting/Chromoting.java @@ -36,6 +36,7 @@ import org.chromium.chromoting.accountswitcher.AccountSwitcher; import org.chromium.chromoting.accountswitcher.AccountSwitcherFactory; import org.chromium.chromoting.help.HelpContext; import org.chromium.chromoting.help.HelpSingleton; +import org.chromium.chromoting.jni.ConnectionListener; import org.chromium.chromoting.jni.JniInterface; import java.util.ArrayList; @@ -45,7 +46,7 @@ import java.util.Arrays; * The user interface for querying and displaying a user's host list from the directory server. It * also requests and renews authentication tokens using the system account manager. */ -public class Chromoting extends AppCompatActivity implements JniInterface.ConnectionListener, +public class Chromoting extends AppCompatActivity implements ConnectionListener, AccountSwitcher.Callback, HostListLoader.Callback, View.OnClickListener { private static final String TAG = "Chromoting"; @@ -571,8 +572,7 @@ public class Chromoting extends AppCompatActivity implements JniInterface.Connec } @Override - public void onConnectionState(JniInterface.ConnectionListener.State state, - JniInterface.ConnectionListener.Error error) { + public void onConnectionState(ConnectionListener.State state, ConnectionListener.Error error) { boolean dismissProgress = false; switch (state) { case INITIALIZING: diff --git a/remoting/android/java/src/org/chromium/chromoting/SessionConnector.java b/remoting/android/java/src/org/chromium/chromoting/SessionConnector.java index cb57a21..5bb53f5 100644 --- a/remoting/android/java/src/org/chromium/chromoting/SessionConnector.java +++ b/remoting/android/java/src/org/chromium/chromoting/SessionConnector.java @@ -4,15 +4,16 @@ package org.chromium.chromoting; +import org.chromium.chromoting.jni.ConnectionListener; import org.chromium.chromoting.jni.JniInterface; /** * This class manages making a connection to a host, with logic for reloading the host list and * retrying the connection in the case of a stale host JID. */ -public class SessionConnector implements JniInterface.ConnectionListener, +public class SessionConnector implements ConnectionListener, HostListLoader.Callback { - private JniInterface.ConnectionListener mConnectionCallback; + private ConnectionListener mConnectionCallback; private HostListLoader.Callback mHostListCallback; private HostListLoader mHostListLoader; private SessionAuthenticator mAuthenticator; @@ -36,7 +37,7 @@ public class SessionConnector implements JniInterface.ConnectionListener, * @param hostListCallback Object to be notified whenever the host list is reloaded. * @param hostListLoader The object used for reloading the host list. */ - public SessionConnector(JniInterface.ConnectionListener connectionCallback, + public SessionConnector(ConnectionListener connectionCallback, HostListLoader.Callback hostListCallback, HostListLoader hostListLoader) { mConnectionCallback = connectionCallback; mHostListCallback = hostListCallback; @@ -72,13 +73,12 @@ public class SessionConnector implements JniInterface.ConnectionListener, } @Override - public void onConnectionState(JniInterface.ConnectionListener.State state, - JniInterface.ConnectionListener.Error error) { + public void onConnectionState(ConnectionListener.State state, ConnectionListener.Error error) { boolean connected = mConnected; - mConnected = (state == JniInterface.ConnectionListener.State.CONNECTED); + mConnected = (state == ConnectionListener.State.CONNECTED); - if (!connected && state == JniInterface.ConnectionListener.State.FAILED - && error == JniInterface.ConnectionListener.Error.PEER_IS_OFFLINE) { + if (!connected && state == ConnectionListener.State.FAILED + && error == ConnectionListener.Error.PEER_IS_OFFLINE) { // The host is offline, which may mean the JID is out of date, so refresh the host list // and try to connect again. reloadHostListAndConnect(); @@ -105,8 +105,8 @@ public class SessionConnector implements JniInterface.ConnectionListener, || hostIncomplete(foundHost)) { // Cannot reconnect to this host, or there's no point in trying because the JID is // unchanged, so report the original failure to the client. - mConnectionCallback.onConnectionState(JniInterface.ConnectionListener.State.FAILED, - JniInterface.ConnectionListener.Error.PEER_IS_OFFLINE); + mConnectionCallback.onConnectionState(ConnectionListener.State.FAILED, + ConnectionListener.Error.PEER_IS_OFFLINE); } else { // Reconnect to the host, but use the original callback directly, instead of this // wrapper object, so the host list is not loaded again. @@ -119,8 +119,8 @@ public class SessionConnector implements JniInterface.ConnectionListener, public void onError(HostListLoader.Error error) { // Connection failed and reloading the host list also failed, so report the connection // error. - mConnectionCallback.onConnectionState(JniInterface.ConnectionListener.State.FAILED, - JniInterface.ConnectionListener.Error.PEER_IS_OFFLINE); + mConnectionCallback.onConnectionState(ConnectionListener.State.FAILED, + ConnectionListener.Error.PEER_IS_OFFLINE); // Notify the caller that the host list failed to load, so the UI is updated accordingly. // The currently-displayed host list is not likely to be valid any more. diff --git a/remoting/android/java/src/org/chromium/chromoting/jni/ConnectionListener.java b/remoting/android/java/src/org/chromium/chromoting/jni/ConnectionListener.java new file mode 100644 index 0000000..f1575cd --- /dev/null +++ b/remoting/android/java/src/org/chromium/chromoting/jni/ConnectionListener.java @@ -0,0 +1,81 @@ +// Copyright 2015 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.jni; + +import org.chromium.chromoting.R; + +/** Interface used for connection state notifications. */ +public interface ConnectionListener { + /** + * This enum must match the C++ enumeration remoting::protocol::ConnectionToHost::State. + */ + public enum State { + INITIALIZING(0), + CONNECTING(1), + AUTHENTICATED(2), + CONNECTED(3), + FAILED(4), + CLOSED(5); + + private final int mValue; + + State(int value) { + mValue = value; + } + + public int value() { + return mValue; + } + + public static State fromValue(int value) { + return values()[value]; + } + } + + /** + * This enum must match the C++ enumeration remoting::protocol::ErrorCode. + */ + public enum Error { + OK(0, 0), + PEER_IS_OFFLINE(1, R.string.error_host_is_offline), + SESSION_REJECTED(2, R.string.error_invalid_access_code), + INCOMPATIBLE_PROTOCOL(3, R.string.error_incompatible_protocol), + AUTHENTICATION_FAILED(4, R.string.error_invalid_access_code), + CHANNEL_CONNECTION_ERROR(5, R.string.error_p2p_failure), + SIGNALING_ERROR(6, R.string.error_p2p_failure), + SIGNALING_TIMEOUT(7, R.string.error_p2p_failure), + HOST_OVERLOAD(8, R.string.error_host_overload), + MAX_SESSION_LENGTH(9, R.string.error_max_session_length), + HOST_CONFIGURATION_ERROR(10, R.string.error_host_configuration_error), + UNKNOWN_ERROR(11, R.string.error_unexpected); + + private final int mValue; + private final int mMessage; + + Error(int value, int message) { + mValue = value; + mMessage = message; + } + + public int value() { + return mValue; + } + + public int message() { + return mMessage; + } + + public static Error fromValue(int value) { + return values()[value]; + } + } + + /** + * Notified on connection state change. + * @param state The new connection state. + * @param error The error code, if state is FAILED. + */ + void onConnectionState(State state, Error error); +} 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 288fd78..90c76e1 100644 --- a/remoting/android/java/src/org/chromium/chromoting/jni/JniInterface.java +++ b/remoting/android/java/src/org/chromium/chromoting/jni/JniInterface.java @@ -14,7 +14,6 @@ import org.chromium.base.Log; import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.JNINamespace; import org.chromium.chromoting.CapabilityManager; -import org.chromium.chromoting.R; import org.chromium.chromoting.SessionAuthenticator; import java.nio.ByteBuffer; @@ -37,80 +36,6 @@ public class JniInterface { /** Used for authentication-related UX during connection. Accessed on the UI thread. */ private static SessionAuthenticator sAuthenticator; - /** Interface used for connection state notifications. */ - public interface ConnectionListener { - /** - * This enum must match the C++ enumeration remoting::protocol::ConnectionToHost::State. - */ - public enum State { - INITIALIZING(0), - CONNECTING(1), - AUTHENTICATED(2), - CONNECTED(3), - FAILED(4), - CLOSED(5); - - private final int mValue; - - State(int value) { - mValue = value; - } - - public int value() { - return mValue; - } - - public static State fromValue(int value) { - return values()[value]; - } - } - - /** - * This enum must match the C++ enumeration remoting::protocol::ErrorCode. - */ - public enum Error { - OK(0, 0), - PEER_IS_OFFLINE(1, R.string.error_host_is_offline), - SESSION_REJECTED(2, R.string.error_invalid_access_code), - INCOMPATIBLE_PROTOCOL(3, R.string.error_incompatible_protocol), - AUTHENTICATION_FAILED(4, R.string.error_invalid_access_code), - CHANNEL_CONNECTION_ERROR(5, R.string.error_p2p_failure), - SIGNALING_ERROR(6, R.string.error_p2p_failure), - SIGNALING_TIMEOUT(7, R.string.error_p2p_failure), - HOST_OVERLOAD(8, R.string.error_host_overload), - MAX_SESSION_LENGTH(9, R.string.error_max_session_length), - HOST_CONFIGURATION_ERROR(10, R.string.error_host_configuration_error), - UNKNOWN_ERROR(11, R.string.error_unexpected); - - private final int mValue; - private final int mMessage; - - Error(int value, int message) { - mValue = value; - mMessage = message; - } - - public int value() { - return mValue; - } - - public int message() { - return mMessage; - } - - public static Error fromValue(int value) { - return values()[value]; - } - } - - /** - * Notified on connection state change. - * @param state The new connection state. - * @param error The error code, if state is STATE_FAILED. - */ - void onConnectionState(State state, Error error); - } - /* * Connection-initiating state machine. */ -- cgit v1.1