diff --git a/firebase-database/gradle.properties b/firebase-database/gradle.properties index b89cd4c0c11..2c062a9f856 100644 --- a/firebase-database/gradle.properties +++ b/firebase-database/gradle.properties @@ -13,5 +13,5 @@ # limitations under the License. version=21.0.0 -latestReleasedVersion=20.3.1 +latestReleasedVersion=20.3.2 android.enableUnitTestBinaryResources=true diff --git a/firebase-database/src/androidTest/java/com/google/firebase/database/connection/ConnectionTest.java b/firebase-database/src/androidTest/java/com/google/firebase/database/connection/ConnectionTest.java index 041aa7ef993..c0177ea9efc 100644 --- a/firebase-database/src/androidTest/java/com/google/firebase/database/connection/ConnectionTest.java +++ b/firebase-database/src/androidTest/java/com/google/firebase/database/connection/ConnectionTest.java @@ -66,7 +66,7 @@ public void onCacheHost(String s) {} IntegrationTestValues.getNamespace(), /*secure=*/ true); DatabaseConfig config = IntegrationTestHelpers.newFrozenTestConfig(); - Connection conn = new Connection(config.getConnectionContext(), info, null, del, null, ""); + Connection conn = new Connection(config.getConnectionContext(), info, null, del, null, "", false); conn.open(); IntegrationTestHelpers.waitFor(valSemaphore); } diff --git a/firebase-database/src/androidTest/java/com/google/firebase/database/core/SynchronousConnection.java b/firebase-database/src/androidTest/java/com/google/firebase/database/core/SynchronousConnection.java index 720a2ce2937..2322968f76a 100644 --- a/firebase-database/src/androidTest/java/com/google/firebase/database/core/SynchronousConnection.java +++ b/firebase-database/src/androidTest/java/com/google/firebase/database/core/SynchronousConnection.java @@ -55,7 +55,7 @@ public SynchronousConnection(String host) { RepoInfo repoInfo = parsed.repoInfo; HostInfo hostInfo = new HostInfo(repoInfo.host, repoInfo.namespace, repoInfo.secure); - connection = new PersistentConnectionImpl(context.getConnectionContext(), hostInfo, this); + connection = new PersistentConnectionImpl(context.getConnectionContext(), hostInfo, this, false); this.connectSemaphore = new Semaphore(0); } diff --git a/firebase-database/src/main/java/com/google/firebase/database/FirebaseDatabase.java b/firebase-database/src/main/java/com/google/firebase/database/FirebaseDatabase.java index e1191e4efcd..4b1fdeb1878 100644 --- a/firebase-database/src/main/java/com/google/firebase/database/FirebaseDatabase.java +++ b/firebase-database/src/main/java/com/google/firebase/database/FirebaseDatabase.java @@ -313,12 +313,13 @@ public synchronized void setPersistenceCacheSizeBytes(long cacheSizeInBytes) { * @param port the emulator port (for example, 9000) */ public void useEmulator(@NonNull String host, int port) { + System.out.println("Using Emulator"); if (this.repo != null) { throw new IllegalStateException( "Cannot call useEmulator() after instance has already been initialized."); } - this.emulatorSettings = new EmulatedServiceSettings(host, port); + this.config.setUsingEmulator(true); } /** @return The semver version for this build of the Firebase Database client */ diff --git a/firebase-database/src/main/java/com/google/firebase/database/android/AndroidPlatform.java b/firebase-database/src/main/java/com/google/firebase/database/android/AndroidPlatform.java index 1ddd20e4c96..3855196e782 100644 --- a/firebase-database/src/main/java/com/google/firebase/database/android/AndroidPlatform.java +++ b/firebase-database/src/main/java/com/google/firebase/database/android/AndroidPlatform.java @@ -114,8 +114,9 @@ public PersistentConnection newPersistentConnection( ConnectionContext connectionContext, HostInfo info, PersistentConnection.Delegate delegate) { + final PersistentConnection connection = - new PersistentConnectionImpl(connectionContext, info, delegate); + new PersistentConnectionImpl(connectionContext, info, delegate, context.isUsingEmulator()); // TODO: Ideally we would remove this listener at some point, but right now // there's no cleanup path for PersistentConnection (or Repo, etc.). They live forever. diff --git a/firebase-database/src/main/java/com/google/firebase/database/connection/Connection.java b/firebase-database/src/main/java/com/google/firebase/database/connection/Connection.java index 1ec17c5c4b8..6e67a7b9898 100644 --- a/firebase-database/src/main/java/com/google/firebase/database/connection/Connection.java +++ b/firebase-database/src/main/java/com/google/firebase/database/connection/Connection.java @@ -70,18 +70,22 @@ private enum State { private State state; private final LogWrapper logger; + private boolean isUsingEmulator; + public Connection( ConnectionContext context, HostInfo hostInfo, String cachedHost, Delegate delegate, String optLastSessionId, - String appCheckToken) { + String appCheckToken, + boolean isUsingEmulator) { long connId = connectionIds++; this.hostInfo = hostInfo; this.delegate = delegate; this.logger = new LogWrapper(context.getLogger(), "Connection", "conn_" + connId); this.state = State.REALTIME_CONNECTING; + this.isUsingEmulator = isUsingEmulator; this.conn = new WebsocketConnection( context, hostInfo, cachedHost, appCheckToken, this, optLastSessionId); @@ -205,7 +209,9 @@ private void onConnectionShutdown(String reason) { private void onHandshake(Map handshake) { long timestamp = (Long) handshake.get(SERVER_HELLO_TIMESTAMP); String host = (String) handshake.get(SERVER_HELLO_HOST); - delegate.onCacheHost(host); + if(!this.isUsingEmulator) { + delegate.onCacheHost(host); + } String sessionId = (String) handshake.get(SERVER_HELLO_SESSION_ID); if (state == State.REALTIME_CONNECTING) { diff --git a/firebase-database/src/main/java/com/google/firebase/database/connection/PersistentConnectionImpl.java b/firebase-database/src/main/java/com/google/firebase/database/connection/PersistentConnectionImpl.java index 0363f259e9d..24d2ed885e1 100644 --- a/firebase-database/src/main/java/com/google/firebase/database/connection/PersistentConnectionImpl.java +++ b/firebase-database/src/main/java/com/google/firebase/database/connection/PersistentConnectionImpl.java @@ -327,10 +327,13 @@ private enum ConnectionState { private long lastWriteTimestamp; private boolean hasOnDisconnects; + private boolean isUsingEmulator; + public PersistentConnectionImpl( - ConnectionContext context, HostInfo info, final Delegate delegate) { + ConnectionContext context, HostInfo info, final Delegate delegate, boolean isUsingEmulator) { this.delegate = delegate; this.context = context; + this.isUsingEmulator = isUsingEmulator; this.executorService = context.getExecutorService(); this.authTokenProvider = context.getAuthTokenProvider(); this.appCheckTokenProvider = context.getAppCheckTokenProvider(); @@ -829,7 +832,7 @@ public void openNetworkConnection(String authToken, String appCheckToken) { this.connectionState = ConnectionState.Connecting; realtime = new Connection( - this.context, this.hostInfo, this.cachedHost, this, this.lastSessionId, appCheckToken); + this.context, this.hostInfo, this.cachedHost, this, this.lastSessionId, appCheckToken, this.isUsingEmulator); realtime.open(); } diff --git a/firebase-database/src/main/java/com/google/firebase/database/core/Context.java b/firebase-database/src/main/java/com/google/firebase/database/core/Context.java index a8203311a85..6ceab797892 100644 --- a/firebase-database/src/main/java/com/google/firebase/database/core/Context.java +++ b/firebase-database/src/main/java/com/google/firebase/database/core/Context.java @@ -54,6 +54,8 @@ public class Context { private Platform platform; + protected boolean isUsingEmulator = false; + private Platform getPlatform() { if (platform == null) { initializeAndroidPlatform(); @@ -65,6 +67,10 @@ private synchronized void initializeAndroidPlatform() { platform = new AndroidPlatform(this.firebaseApp); } + public boolean isUsingEmulator() { + return this.isUsingEmulator; + } + public boolean isFrozen() { return frozen; } @@ -139,6 +145,7 @@ public LogWrapper getLogger(String component, String prefix) { return new LogWrapper(logger, component, prefix); } + // TODO(mtewani): fill this in instead of repoinfo public ConnectionContext getConnectionContext() { return new ConnectionContext( this.getLogger(), @@ -211,8 +218,9 @@ public TokenProvider getAppCheckTokenProvider() { return this.appCheckTokenProvider; } + public PersistentConnection newPersistentConnection( - HostInfo info, PersistentConnection.Delegate delegate) { + HostInfo info, PersistentConnection.Delegate delegate) { return getPlatform().newPersistentConnection(this, this.getConnectionContext(), info, delegate); } diff --git a/firebase-database/src/main/java/com/google/firebase/database/core/DatabaseConfig.java b/firebase-database/src/main/java/com/google/firebase/database/core/DatabaseConfig.java index ee13c6b4d3e..ee9a3714d1d 100644 --- a/firebase-database/src/main/java/com/google/firebase/database/core/DatabaseConfig.java +++ b/firebase-database/src/main/java/com/google/firebase/database/core/DatabaseConfig.java @@ -173,6 +173,14 @@ public synchronized void setPersistenceCacheSizeBytes(long cacheSizeInBytes) { this.cacheSize = cacheSizeInBytes; } + /** + * @hide + * @param isUsingEmulator + */ + public synchronized void setUsingEmulator(boolean isUsingEmulator) { + this.isUsingEmulator = isUsingEmulator; + } + public synchronized void setFirebaseApp(FirebaseApp app) { this.firebaseApp = app; } diff --git a/firebase-database/src/main/java/com/google/firebase/database/core/RepoInfo.java b/firebase-database/src/main/java/com/google/firebase/database/core/RepoInfo.java index 3b324e2739b..6b26734f463 100644 --- a/firebase-database/src/main/java/com/google/firebase/database/core/RepoInfo.java +++ b/firebase-database/src/main/java/com/google/firebase/database/core/RepoInfo.java @@ -28,6 +28,7 @@ public final class RepoInfo { public String namespace; public String internalHost; + @Override public String toString() { return "http" + (secure ? "s" : "") + "://" + host;