Пример #1
0
        //==============================================================================================
        // Getters / Setters
        //==============================================================================================

        /**
         * Gets or creates a LayerClient, using a default set of LayerClient.Options and flavor-specific
         * App ID and Options from the `generateLayerClient` method.  Returns `null` if the flavor was
         * unable to create a LayerClient (due to no App ID, etc.).
         *
         * @return New or existing LayerClient, or `null` if a LayerClient could not be constructed.
         * @see Flavor#generateLayerClient(Context, LayerClient.Options)
         */
        public static LayerClient GetLayerClient()
        {
            if (sLayerClient == null)
            {
                // Custom options for constructing a LayerClient
                LayerClient.Options options = new LayerClient.Options()

                                              /* Fetch the minimum amount per conversation when first authenticated */
                                              .InvokeHistoricSyncPolicy(LayerClient.Options.HistoricSyncPolicy.FromLastMessage)

                                              /* Automatically download text and ThreePartImage info/preview */
                                              .InvokeAutoDownloadMimeTypes(new List <string> {
                    TextCellFactory.MimeType,
                    ThreePartImageUtils.MimeTypeInfo,
                    ThreePartImageUtils.MimeTypePreview
                });

                // Allow flavor to specify Layer App ID and customize Options.
                sLayerClient = sFlavor.GenerateLayerClient(sInstance, options);

                // Flavor was unable to generate Layer Client (no App ID, etc.)
                if (sLayerClient == null)
                {
                    return(null);
                }

                /* Register AuthenticationProvider for handling authentication challenges */
                sLayerClient.RegisterAuthenticationListener(GetAuthenticationProvider());
            }
            return(sLayerClient);
        }
        //Checks to see if the SDK is connected to Layer and whether a user is authenticated
        //The respective callbacks are executed in MyConnectionListener and MyAuthenticationListener
        private void _LoadLayerClient()
        {
            // Check if Sample App is using a valid app ID.
            if (_IsValidAppID())
            {
                if (layerClient == null)
                {
                    //Used for debugging purposes ONLY. DO NOT include this option in Production Builds.
                    LayerClient.SetLoggingEnabled(this.ApplicationContext, true);

                    // Initializes a LayerClient object with the Google Project Number
                    LayerClient.Options options = new LayerClient.Options();

                    //Sets the GCM sender id allowing for push notifications
                    options.InvokeGoogleCloudMessagingSenderId(GCM_PROJECT_NUMBER);

                    //By default, only unread messages are synced after a user is authenticated, but you
                    // can change that behavior to all messages or just the last message in a conversation
                    options.InvokeHistoricSyncPolicy(LayerClient.Options.HistoricSyncPolicy.AllMessages);


                    layerClient = LayerClient.NewInstance(this, LAYER_APP_ID, options);

                    //Register the connection and authentication listeners
                    layerClient.RegisterConnectionListener(connectionListener);
                    layerClient.RegisterAuthenticationListener(authenticationListener);
                }

                //Check the current state of the SDK. The client must be CONNECTED and the user must
                // be AUTHENTICATED in order to send and receive messages. Note: it is possible to be
                // authenticated, but not connected, and vice versa, so it is a best practice to check
                // both states when your app launches or comes to the foreground.
                if (!layerClient.IsConnected)
                {
                    //If Layer is not connected, make sure we connect in order to send/receive messages.
                    // MyConnectionListener.java handles the callbacks associated with Connection, and
                    // will start the Authentication process once the connection is established
                    layerClient.Connect();
                }
                else if (!layerClient.IsAuthenticated)
                {
                    //If the client is already connected, try to authenticate a user on this device.
                    // MyAuthenticationListener.java handles the callbacks associated with Authentication
                    // and will start the Conversation View once the user is authenticated
                    layerClient.Authenticate();
                }
                else
                {
                    // If the client is to Layer and the user is authenticated, start the Conversation
                    // View. This will be called when the app moves from the background to the foreground,
                    // for example.
                    OnUserAuthenticated();
                }
            }
        }