Skip to content

webex/spark-android-sdk

Repository files navigation

This repo is DEPRECATED. Check our newer Android SDK: https://github.com/webex/webex-android-sdk

Cisco Spark Android SDK

Travis CI license

The Cisco Spark™ Android SDK

The Cisco Spark Android SDK makes it easy to integrate secure and convenient Cisco Spark messaging and calling features in your Android apps.

This SDK is built with Android SDK Tools 27 and requires Android API Level 21 or later.

Table of Contents

Install

Assuming you already have an Android project, e.g. MySparkApp, for your Android app, here are the steps to integrate the Cisco Spark Android SDK into your project using Gradle:

  1. Add the following repository to your top-level build.gradle file:

    allprojects {
        repositories {
            jcenter()
            maven {
                url 'https://devhub.cisco.com/artifactory/sparksdk/'
            }
        }
    }
  2. Add the spark-android-sdk library as a dependency for your app in the build.gradle file:

    dependencies { 
        compile('com.ciscospark:androidsdk:1.4.0@aar', {
            transitive = true
        })
    }
  3. Enable multidex support for your app:

    android {
        defaultConfig {
            multiDexEnabled true
        }
    }
  4. Exclude rxjava.properties in your packagingOptions :

    packagingOptions {
        exclude 'META-INF/rxjava.properties'
    }

Usage

To use the SDK, you will need Cisco Spark integration credentials. If you do not already have a Cisco Spark account, visit the Cisco Spark for Developers portal to create your account and register an integration. Your app will need to authenticate users via an OAuth grant flow for existing Cisco Spark users or a JSON Web Token for guest users without a Cisco Spark account.

See the Android SDK area of the Cisco Spark for Developers site for more information about this SDK.

Examples

Here are some examples of how to use the Android SDK in your app.

  1. Create a new Spark instance using Spark ID authentication (OAuth-based):

    String clientId = "YOUR_CLIENT_ID";
    String clientSecret = "YOUR_CLIENT_SECRET";
    String scope = "spark:all";
    String redirectUri = "Sparkdemoapp://response";
    
    OAuthWebViewAuthenticator authenticator = new OAuthWebViewAuthenticator(clientId, clientSecret, scope, redirectUri);
    Spark spark = new Spark(activity.getApplication(), authenticator)
    if (!authenticator.isAuthorized()) {
        authenticator.authorize(webView, new CompletionHandler<Void>() {
            @Override
            public void onComplete(Result<Void> result) {
                if (!result.isSuccessful()) {
                    System.out.println("User not authorized");
                }
            }
        });
    }
  2. Create a new Spark instance using Guest ID authentication (JWT-based):

    JWTAuthenticator authenticator = new JWTAuthenticator();
    Spark spark = new Spark(activity.getApplication(), authenticator);
    if (!authenticator.isAuthorized()) {
        authenticator.authorize(myJwt);
    }
  3. Register the device to send and receive calls:

    spark.phone().register(new CompletionHandler<Void>() {
        @Override
        public void onComplete(Result<Void> result) {
            if (result.isSuccessful()) {
                // Device registered
            }
            else {
                // Device not registered, and calls will not be sent or received
            }
        }
    });
  4. Create a new Cisco Spark space, add users to the space, and send a message:

    // Create a Cisco Spark space:
    
    spark.rooms().create("Hello World", null, new CompletionHandler<Room>() {
        @Override
        public void onComplete(Result<Room> result) {
            if (result.isSuccessful()) {
                Room room = result.getData();
            }
            else {
                SparkError error = result.getError();
            }
        }
    });
    
    // Add a user to a space:
    
    spark.memberships().create(roomId, null, "person@example.com", true, new CompletionHandler<Membership>() {
        @Override
        public void onComplete(Result<Membership> result) {
            if (result.isSuccessful()) {
                Membership membership = result.getData();
            }
            else {
                SparkError error = result.getError();
            }
        }
    });
    
    // Send a message to a space:
    
    spark.messages().post(roomId, null, null, "Hello there", null, null, new CompletionHandler<Message>() {
        @Override
        public void onComplete(Result<Message> result) {
            if (result.isSuccessful()) {
                Message message = result.getData();
            }
            else {
                SparkError error = result.getError();
            }
        }
    });
  5. Make an outgoing call:

    spark.phone().dial("person@example.com", MediaOption.audioVideo(local, remote), new CompletionHandler<Call>() {
        @Override
        public void onComplete(Result<Call> result) {
            Call call = result.getData();
            if (call != null) {
                call.setObserver(new CallObserver() {
                    @Override
                    public void onRinging(Call call) {
    
                    }
    
                    @Override
                    public void onConnected(Call call) {
    
                    }
    
                    @Override
                    public void onDisconnected(CallDisconnectedEvent callDisconnectedEvent) {
    
                    }
    
                    @Override
                    public void onMediaChanged(MediaChangedEvent mediaChangedEvent) {
    
                    }
                });
            }
            else {
                SparkError error = result.getError();
            }
        }
    });
  6. Receive a call:

    spark.phone().setIncomingCallListener(new Phone.IncomingCallListener() {
        @Override
        public void onIncomingCall(Call call) {
            call.answer(MediaOption.audioVideo(local, remote), new CompletionHandler<Void>() {
                @Override
                public void onComplete(Result<Void> result) {
                    if (result.isSuccessful()) {
                        // success
                    }
                    else {
                        SparkError error = result.getError();
                    }
                }
            });
        }
    });
  7. Make an room call:

    spark.phone().dial(roomId, MediaOption.audioVideoSharing(new Pair<>(localView,remoteView),shareView), new CompletionHandler<Call>() {
        @Override
        public void onComplete(Result<Call> result) {
            Call call = result.getData();
            if (call != null) {
                call.setObserver(new CallObserver() {
                    @Override
                    public void onConnected(Call call) {
    
                    }
    
                 	//...
    
                    @Override
                    public void onCallMembershipChanged(CallMembershipChangedEvent callMembershipChangeEvent) {
                        CallMembership membership = callMembershipChangeEvent.getCallMembership();
                        if (callMembershipChangeEvent instanceof MembershipJoinedEvent) {
    
                        } else if (callMembershipChangeEvent instanceof MembershipLeftEvent) {
    
                        } else if (callMembershipChangeEvent instanceof MembershipDeclinedEvent) {
    
                        } else if (callMembershipChangeEvent instanceof MembershipSendingVideoEvent) {
    
                        } else if (callMembershipChangeEvent instanceof MembershipSendingAudioEvent) {
    
                        } else if (callMembershipChangeEvent instanceof MembershipSendingSharingEvent) {
    
                        }
                    }
                });
            }
            else {
                SparkError error = result.getError();
            }
        }
    });
  8. Receive screen share:

    spark.phone().dial(roomId, MediaOption.audioVideoSharing(new Pair<>(localView,remoteView),shareView), new CompletionHandler<Call>() {
        @Override
        public void onComplete(Result<Call> result) {
            Call call = result.getData();
            if (call != null) {
                call.setObserver(new CallObserver() {
                    @Override
                    public void onConnected(Call call) {
    
                    }
    
                 	//...
    
                    @Override
                    public void onMediaChanged(MediaChangedEvent mediaChangedEvent) {
                        if (mediaChangedEvent instanceof RemoteSendingSharingEvent) {
                            if (((RemoteSendingSharingEvent) mediaChangedEvent).isSending()) {
                                mediaChangedEvent.getCall().setSharingRenderView(shareView);
                            } else if (!((RemoteSendingSharingEvent) mediaChangedEvent).isSending()) {
                                mediaChangedEvent.getCall().setSharingRenderView(null);
                            }
                        }
                    }
                });
            }
            else {
                SparkError error = result.getError();
            }
        }
    });
  9. Start/stop sharing screen:

    activeCall.startSharing(r -> Ln.d("startSharing result: " + r));
    boolean isSharing = activeCall.isSendingSharing();
    activeCall.stopSharing(r -> Ln.d("stopSharing result: " + r));
  10. Post a message

    spark.message().post(
        idOrEmail,  // person id, email or room id
        message,    // text message to be sent
        mentions,   // list of Mention object
        files,      // list of files to be sent
        new CompletionHandler<Message>() {
            @Override
            public void onComplete(Result<Message> result) {
                if (result.isSuccessful()) {
                    // message sent success
                } else {
                    // message sent failed
                }
            }
        }));
  11. Receive a message

    spark.message().setMessageObserver(
        new MessageObserver() {
            void onEvent(MessageEvent event) {
                if (event instanceof MessageArrived) {
                    Message message = event.getMessage();
                    // new message arrived
                } else if (event instanceof MessageDeleted) {
                    // message deleted
                }
            }
        }
    );

Contribute

Pull requests welcome. To suggest changes to the SDK, please fork this repository and submit a pull request with your changes. Your request will be reviewed by one of the project maintainers.

License

© 2016-2018 Cisco Systems, Inc. and/or its affiliates. All Rights Reserved.

See LICENSE for details.