Class StitchAppClientImpl
- java.lang.Object
-
- com.mongodb.stitch.android.core.internal.StitchAppClientImpl
-
- All Implemented Interfaces:
StitchAuthListener
,StitchAppClient
,AuthMonitor
,Closeable
,AutoCloseable
public final class StitchAppClientImpl extends Object implements StitchAppClient, AuthMonitor, StitchAuthListener
-
-
Constructor Summary
Constructors Constructor Description StitchAppClientImpl(String clientAppId, StitchAppClientConfiguration config)
Constructs an app client with the given configuration.
-
Method Summary
Modifier and Type Method Description Task<Void>
callFunction(String name, List<?> args)
Calls the specified Stitch function.<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Class<ResultT> resultClass)
Calls the specified Stitch function, and decodes the response into an instance of the specified type.<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Class<ResultT> resultClass, CodecRegistry codecRegistry)
Calls the specified Stitch function, and decodes the response into an instance of the specified type.Task<Void>
callFunction(String name, List<?> args, Long requestTimeout)
Calls the specified Stitch function.<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Long requestTimeout, Class<ResultT> resultClass)
Calls the specified Stitch function, and decodes the response into an instance of the specified type.<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Long requestTimeout, Class<ResultT> resultClass, CodecRegistry codecRegistry)
Calls the specified Stitch function, and decodes the response into an instance of the specified type.<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Long requestTimeout, Decoder<ResultT> resultDecoder)
<ResultT> Task<ResultT>
callFunction(String name, List<?> args, Decoder<ResultT> resultDecoder)
void
close()
Closes the client and shuts down all background operations.String
getActiveUserId()
Get the active user id from the applications auth request client.StitchAuth
getAuth()
Gets the authentication component of the app.StitchPush
getPush()
Gets the push notifications component of the app.<T> T
getServiceClient(NamedServiceClientFactory<T> factory, String serviceName)
Gets a client for the given named service.<T> T
getServiceClient(ServiceClientFactory<T> factory)
Gets a client for the given service.StitchServiceClient
getServiceClient(String serviceName)
Gets a general purpose client for the given named service.boolean
isLoggedIn()
Get whether or not the application client is currently logged in.void
onActiveUserChanged(StitchAuth auth, StitchUser currentActiveUser, StitchUser previousActiveUser)
Called whenever the active user changes.void
onAuthEvent(StitchAuth auth)
onAuthEvent is called any time a notable event regarding authentication happens.void
onUserLoggedIn(StitchAuth auth, StitchUser loggedInUser)
Called whenever a user is logged in.void
onUserLoggedOut(StitchAuth auth, StitchUser loggedOutUser)
Called whenever a user is logged out.void
onUserRemoved(StitchAuth auth, StitchUser removedUser)
Called whenever a user is removed from the list of users on the device.boolean
tryIsLoggedIn()
Get whether or not the application client is currently logged in.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.mongodb.stitch.android.core.auth.StitchAuthListener
onListenerRegistered, onUserAdded, onUserLinked
-
-
-
-
Constructor Detail
-
StitchAppClientImpl
public StitchAppClientImpl(String clientAppId, StitchAppClientConfiguration config)
Constructs an app client with the given configuration.- Parameters:
clientAppId
- the client app id for the app.config
- the configuration to use for the app client.
-
-
Method Detail
-
getAuth
public StitchAuth getAuth()
Description copied from interface:StitchAppClient
Gets the authentication component of the app. This is used for logging in and managing users.- Specified by:
getAuth
in interfaceStitchAppClient
- Returns:
- the authentication component of the app.
-
getPush
public StitchPush getPush()
Description copied from interface:StitchAppClient
Gets the push notifications component of the app. This is used for registering for push notifications.- Specified by:
getPush
in interfaceStitchAppClient
- Returns:
- the push notifications component of the app.
-
getServiceClient
public <T> T getServiceClient(NamedServiceClientFactory<T> factory, String serviceName)
Description copied from interface:StitchAppClient
Gets a client for the given named service.- Specified by:
getServiceClient
in interfaceStitchAppClient
- Type Parameters:
T
- the type of client to be returned by the factory.- Parameters:
factory
- the factory that will create a client for the service.serviceName
- the name of the service.- Returns:
- a client to interact with the service.
-
getServiceClient
public <T> T getServiceClient(ServiceClientFactory<T> factory)
Description copied from interface:StitchAppClient
Gets a client for the given service. Only some services offer a factory that requires no service name.- Specified by:
getServiceClient
in interfaceStitchAppClient
- Type Parameters:
T
- the type of client to be returned by the factory.- Parameters:
factory
- the factory that will create a client for the service.- Returns:
- a client to interact with the service.
-
getServiceClient
public StitchServiceClient getServiceClient(String serviceName)
Description copied from interface:StitchAppClient
Gets a general purpose client for the given named service.- Specified by:
getServiceClient
in interfaceStitchAppClient
- Parameters:
serviceName
- the name of the service.- Returns:
- a client to interact with the service.
-
callFunction
public Task<Void> callFunction(String name, List<?> args)
Description copied from interface:StitchAppClient
Calls the specified Stitch function.- Specified by:
callFunction
in interfaceStitchAppClient
- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.- Returns:
- a
Task
that completes when the function call completes.
-
callFunction
public Task<Void> callFunction(String name, List<?> args, Long requestTimeout)
Description copied from interface:StitchAppClient
Calls the specified Stitch function. Also accepts a timeout in milliseconds. Use this for functions that may run longer than the client-wide default timeout (15 seconds by default).- Specified by:
callFunction
in interfaceStitchAppClient
- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.requestTimeout
- the number of milliseconds the client should wait for a response from the server before failing with an error.- Returns:
- a
Task
containing the decoded value.
-
callFunction
public <ResultT> Task<ResultT> callFunction(String name, List<?> args, Class<ResultT> resultClass)
Description copied from interface:StitchAppClient
Calls the specified Stitch function, and decodes the response into an instance of the specified type. The response will be decoded using the codec registry specified when the client was configured. If no codec registry was configured, a default codec registry will be used. The default codec registry supports the mappings specified here- Specified by:
callFunction
in interfaceStitchAppClient
- Type Parameters:
ResultT
- the type into which the Stitch response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- he arguments to pass to the function.resultClass
- the class that the response should be decoded as.- Returns:
- a
Task
containing the decoded value.
-
callFunction
public <ResultT> Task<ResultT> callFunction(String name, List<?> args, Long requestTimeout, Class<ResultT> resultClass)
Description copied from interface:StitchAppClient
Calls the specified Stitch function, and decodes the response into an instance of the specified type. The response will be decoded using the codec registry specified when the client was configured. If no codec registry was configured, a default codec registry will be used. The default codec registry supports the mappings specified here Also accepts a timeout in milliseconds. Use this for functions that may run longer than the client-wide default timeout (15 seconds by default).- Specified by:
callFunction
in interfaceStitchAppClient
- Type Parameters:
ResultT
- the type into which the Stitch response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.requestTimeout
- the number of milliseconds the client should wait for a response from the server before failing with an error.resultClass
- the class that the response should be decoded as.- Returns:
- a
Task
containing the decoded value.
-
callFunction
public <ResultT> Task<ResultT> callFunction(String name, List<?> args, Class<ResultT> resultClass, CodecRegistry codecRegistry)
Description copied from interface:StitchAppClient
Calls the specified Stitch function, and decodes the response into an instance of the specified type. The response will be decoded using the codec registry given.- Specified by:
callFunction
in interfaceStitchAppClient
- Type Parameters:
ResultT
- the type into which the Stitch response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.resultClass
- the class that the response should be decoded as.codecRegistry
- the codec registry used for de/serialization of the function call.- Returns:
- a
Task
containing the decoded value.
-
callFunction
public <ResultT> Task<ResultT> callFunction(String name, List<?> args, Long requestTimeout, Class<ResultT> resultClass, CodecRegistry codecRegistry)
Description copied from interface:StitchAppClient
Calls the specified Stitch function, and decodes the response into an instance of the specified type. The response will be decoded using the codec registry given. Also accepts a timeout in milliseconds. Use this for functions that may run longer than the client-wide default timeout (15 seconds by default).- Specified by:
callFunction
in interfaceStitchAppClient
- Type Parameters:
ResultT
- the type into which the Stitch response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.requestTimeout
- the number of milliseconds the client should wait for a response from the server before failing with an error.resultClass
- the class that the response should be decoded as.codecRegistry
- the codec registry used for de/serialization of the function call.- Returns:
- a
Task
containing the decoded value.
-
callFunction
public <ResultT> Task<ResultT> callFunction(String name, List<?> args, Decoder<ResultT> resultDecoder)
Description copied from interface:StitchAppClient
Calls the specified Stitch function, and decodes the response into a value using the providedDecoder
orCodec
.- Specified by:
callFunction
in interfaceStitchAppClient
- Type Parameters:
ResultT
- the type into which the response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.resultDecoder
- theDecoder
orCodec
to use to decode the response into a value.- Returns:
- a
Task
containing the decoded value.
-
callFunction
public <ResultT> Task<ResultT> callFunction(String name, List<?> args, Long requestTimeout, Decoder<ResultT> resultDecoder)
Description copied from interface:StitchAppClient
Calls the specified Stitch function, and decodes the response into a value using the providedDecoder
orCodec
. Also accepts a timeout in milliseconds. Use this for functions that may run longer than the client-wide default timeout (15 seconds by default).- Specified by:
callFunction
in interfaceStitchAppClient
- Type Parameters:
ResultT
- the type into which the response will be decoded.- Parameters:
name
- the name of the Stitch function to call.args
- the arguments to pass to the function.requestTimeout
- the number of milliseconds the client should wait for a response from the server before failing with an error.resultDecoder
- theDecoder
orCodec
to use to decode the response into a value.- Returns:
- a
Task
containing the decoded value.
-
isLoggedIn
public boolean isLoggedIn() throws InterruptedException
Description copied from interface:AuthMonitor
Get whether or not the application client is currently logged in.- Specified by:
isLoggedIn
in interfaceAuthMonitor
- Returns:
- whether or not the application client is logged in
- Throws:
InterruptedException
- will throw interruptibly
-
tryIsLoggedIn
public boolean tryIsLoggedIn()
Description copied from interface:AuthMonitor
Get whether or not the application client is currently logged in.- Specified by:
tryIsLoggedIn
in interfaceAuthMonitor
- Returns:
- whether or not the application client is logged in, or false if the thread was interrupted
-
getActiveUserId
@Nullable public String getActiveUserId()
Description copied from interface:AuthMonitor
Get the active user id from the applications auth request client.- Specified by:
getActiveUserId
in interfaceAuthMonitor
- Returns:
- active user id if there is one, null if not
-
onAuthEvent
public void onAuthEvent(StitchAuth auth)
Description copied from interface:StitchAuthListener
onAuthEvent is called any time a notable event regarding authentication happens. Some of these events are:- When a user logs in.
- When a user logs out.
- When a user is linked to another identity, i.e. with
StitchUser.linkWithCredential(com.mongodb.stitch.core.auth.StitchCredential)
. - When a listener is registered. This is to handle the case where during registration an event happens that the registerer would otherwise miss out on.
- When switching active users, e.g. with
StitchAuth.switchToUserWithId(java.lang.String)
or when another user logs in.
- Specified by:
onAuthEvent
in interfaceStitchAuthListener
- Parameters:
auth
- the instance ofStitchAuth
where the event happened. It should be used to infer the current state of authentication.
-
onUserLoggedIn
public void onUserLoggedIn(StitchAuth auth, StitchUser loggedInUser)
Description copied from interface:StitchAuthListener
Called whenever a user is logged in. This will be called beforeStitchAuthListener.onActiveUserChanged(com.mongodb.stitch.android.core.auth.StitchAuth, com.mongodb.stitch.android.core.auth.StitchUser, com.mongodb.stitch.android.core.auth.StitchUser)
is called.Note: if an anonymous user was already logged in on the device, and you log in with an
AnonymousCredential
, this method will not be called, as the underlyingStitchAuth
will reuse the anonymous user's existing session, and will thus only triggerStitchAuthListener.onActiveUserChanged(com.mongodb.stitch.android.core.auth.StitchAuth, com.mongodb.stitch.android.core.auth.StitchUser, com.mongodb.stitch.android.core.auth.StitchUser)
.- Specified by:
onUserLoggedIn
in interfaceStitchAuthListener
- Parameters:
auth
- The instance ofStitchAuth
where the user was logged in. It can be used to infer the current state of authentication.loggedInUser
- The user that was logged in.
-
onUserLoggedOut
public void onUserLoggedOut(StitchAuth auth, StitchUser loggedOutUser)
Description copied from interface:StitchAuthListener
Called whenever a user is logged out.The user logged out is not necessarily the active user. If the user logged out was the active user, then
StitchAuthListener.onActiveUserChanged(com.mongodb.stitch.android.core.auth.StitchAuth, com.mongodb.stitch.android.core.auth.StitchUser, com.mongodb.stitch.android.core.auth.StitchUser)
will be called after this method.If the user was an anonymous user, that user will also be removed and
StitchAuthListener.onUserRemoved(com.mongodb.stitch.android.core.auth.StitchAuth, com.mongodb.stitch.android.core.auth.StitchUser)
will also be called.- Specified by:
onUserLoggedOut
in interfaceStitchAuthListener
- Parameters:
auth
- The instance ofStitchAuth
where the user was logged out. It can be used to infer the current state of authentication.loggedOutUser
- The user that was logged out.
-
onActiveUserChanged
public void onActiveUserChanged(StitchAuth auth, StitchUser currentActiveUser, @Nullable StitchUser previousActiveUser)
Description copied from interface:StitchAuthListener
Called whenever the active user changes.This may be due to a call to:
StitchAuth.loginWithCredential(com.mongodb.stitch.core.auth.StitchCredential)
StitchAuth.switchToUserWithId(java.lang.String)
StitchAuth.logout()
StitchAuth.logoutUserWithId(java.lang.String)
StitchAuth.removeUser()
StitchAuth.removeUserWithId(java.lang.String)
, or- Another method that issues a request, if the user's session is invalidated and they are forced to log out.
- Specified by:
onActiveUserChanged
in interfaceStitchAuthListener
- Parameters:
auth
- The instance ofStitchAuth
where the active user changed. It can be used to infer the current state of authentication.currentActiveUser
- The active user after the change.previousActiveUser
- The active user before the change.
-
onUserRemoved
public void onUserRemoved(StitchAuth auth, StitchUser removedUser)
Description copied from interface:StitchAuthListener
Called whenever a user is removed from the list of users on the device.- Specified by:
onUserRemoved
in interfaceStitchAuthListener
- Parameters:
auth
- The instance ofStitchAuth
where the user was removed. It can be used to infer the current state of authentication.removedUser
- The user that was removed.- See Also:
StitchAuth.listUsers()
-
close
public void close() throws IOException
Closes the client and shuts down all background operations.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceStitchAppClient
- Throws:
IOException
- if any thread shutdown errors happen.
-
-