SHARE
TWEET

Untitled

a guest Jun 26th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.brainstation.revera.common.service;
  2.  
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.os.Binder;
  6. import android.os.IBinder;
  7. import android.util.Log;
  8.  
  9. import com.brainstation.revera.common.App;
  10. import com.brainstation.revera.common.Constants;
  11. import com.brainstation.revera.common.database.AppDatabase;
  12. import com.brainstation.revera.common.session.Session;
  13. import com.brainstation.revera.feature.videocall.VideoCallActivity;
  14. import com.sinch.android.rtc.AudioController;
  15. import com.sinch.android.rtc.ClientRegistration;
  16. import com.sinch.android.rtc.NotificationResult;
  17. import com.sinch.android.rtc.Sinch;
  18. import com.sinch.android.rtc.SinchClient;
  19. import com.sinch.android.rtc.SinchClientListener;
  20. import com.sinch.android.rtc.SinchError;
  21. import com.sinch.android.rtc.calling.Call;
  22. import com.sinch.android.rtc.calling.CallClient;
  23. import com.sinch.android.rtc.calling.CallClientListener;
  24. import com.sinch.android.rtc.messaging.MessageClientListener;
  25. import com.sinch.android.rtc.messaging.WritableMessage;
  26. import com.sinch.android.rtc.video.VideoController;
  27. import com.sinch.android.rtc.video.VideoScalingType;
  28.  
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. import java.util.Objects;
  32.  
  33. import javax.inject.Inject;
  34.  
  35. public class SinchService extends Service {
  36.  
  37.     private static final String APP_KEY = "f75e178a-9ccb-4061-9de1-df71bd6c8525";
  38.     private static final String APP_SECRET = "UB15mCMsFEKJG8WQmxBI1g==";
  39.     private static final String ENVIRONMENT = "sandbox.sinch.com";
  40.  
  41.     static final String TAG = SinchService.class.getSimpleName();
  42.  
  43.     @Inject
  44.     Session session;
  45.  
  46.     @Inject
  47.     AppDatabase database;
  48.  
  49.     private SinchServiceInterface mSinchServiceInterface = new SinchServiceInterface();
  50.     private SinchClient mSinchClient;
  51.  
  52.     private ClientStartListener mListener;
  53.  
  54.     private Map<String, String> callHeader;
  55.  
  56.     @Override
  57.     public void onCreate() {
  58.         super.onCreate();
  59.         App.getInstance().getAppComponent().inject(this);
  60.         attemptAutoStart();
  61.     }
  62.  
  63.     @Override
  64.     public void onDestroy() {
  65.         if (mSinchClient != null && mSinchClient.isStarted()) {
  66.             mSinchClient.terminate();
  67.         }
  68.         super.onDestroy();
  69.     }
  70.  
  71.     @Override
  72.     public IBinder onBind(Intent intent) {
  73.         return mSinchServiceInterface;
  74.     }
  75.  
  76.     private void attemptAutoStart() {
  77.         startClient(session.getUser().getProfileName());
  78.     }
  79.  
  80.     private void createClient(String username) {
  81.         mSinchClient = Sinch.getSinchClientBuilder().context(getApplicationContext()).userId(username)
  82.                 .applicationKey(APP_KEY)
  83.                 .applicationSecret(APP_SECRET)
  84.                 .environmentHost(ENVIRONMENT).build();
  85.  
  86.         mSinchClient.setSupportCalling(true);
  87.         mSinchClient.setSupportMessaging(true);
  88.         mSinchClient.setSupportManagedPush(true);
  89.         mSinchClient.startListeningOnActiveConnection();
  90.         if (session.getUser().getUserType().equals(Constants.USER_TYPE_PATIENT)) {
  91.             mSinchClient.setPushNotificationDisplayName(String.format("%s %s", session.getContact().getFirstName(), session.getContact().getLastName()));
  92.         } else {
  93.             mSinchClient.setPushNotificationDisplayName(database.reveraDoctorsDao().getReveraDoctorName(session.getContact().getId()));
  94.         }
  95.  
  96.         mSinchClient.getVideoController().setResizeBehaviour(VideoScalingType.ASPECT_BALANCED);
  97.  
  98.         mSinchClient.addSinchClientListener(new MySinchClientListener());
  99.         mSinchClient.getCallClient().addCallClientListener(new SinchCallClientListener());
  100.     }
  101.  
  102.     private void startClient(String username) {
  103.         if (mSinchClient == null) {
  104.             createClient(username);
  105.         }
  106.  
  107.         Log.d(TAG, "Starting SinchClient");
  108.         mSinchClient.start();
  109.     }
  110.  
  111.     private void stopClient() {
  112.         if (mSinchClient != null) {
  113.             mSinchClient.unregisterManagedPush();
  114.             mSinchClient.stopListeningOnActiveConnection();
  115.             mSinchClient.terminate();
  116.             mSinchClient = null;
  117.         }
  118.     }
  119.  
  120.     private boolean isClientStarted() {
  121.         return (mSinchClient != null && mSinchClient.isStarted());
  122.     }
  123.  
  124.     private void sendMessage(String recipientUserId, String textBody) {
  125.         if (isClientStarted()) {
  126.             WritableMessage message = new WritableMessage(recipientUserId, textBody);
  127.             mSinchClient.getMessageClient().send(message);
  128.         }
  129.     }
  130.  
  131.     private void addMessageClientListener(MessageClientListener listener) {
  132.         if (mSinchClient != null) {
  133.             mSinchClient.getMessageClient().addMessageClientListener(listener);
  134.         }
  135.     }
  136.  
  137.     private void removeMessageClientListener(MessageClientListener listener) {
  138.         if (mSinchClient != null) {
  139.             mSinchClient.getMessageClient().removeMessageClientListener(listener);
  140.         }
  141.     }
  142.  
  143.     public class SinchServiceInterface extends Binder {
  144.  
  145.         public void startClient(String userName) {
  146.             SinchService.this.startClient(userName);
  147.         }
  148.  
  149.         public void stopClient() {
  150.             SinchService.this.stopClient();
  151.         }
  152.  
  153.         public void retryStartAfterPermissionGranted() {
  154.             SinchService.this.attemptAutoStart();
  155.         }
  156.  
  157.         public boolean isClientStarted() {
  158.             return SinchService.this.isClientStarted();
  159.         }
  160.  
  161.         public void setStartListener(ClientStartListener listener) {
  162.             mListener = listener;
  163.         }
  164.  
  165.         public Call callUserVideo(String userId, Map<String, String> headers) {
  166.             return mSinchClient.getCallClient().callUserVideo(userId, headers);
  167.         }
  168.  
  169.         public String getUserName() {
  170.             return mSinchClient != null ? mSinchClient.getLocalUserId() : "";
  171.         }
  172.  
  173.         public Call getCall(String callId) {
  174.             return mSinchClient.getCallClient().getCall(callId);
  175.         }
  176.  
  177.         public VideoController getVideoController() {
  178.             if (!isClientStarted()) {
  179.                 return null;
  180.             }
  181.             return mSinchClient.getVideoController();
  182.         }
  183.  
  184.         public AudioController getAudioController() {
  185.             if (!isClientStarted()) {
  186.                 return null;
  187.             }
  188.             return mSinchClient.getAudioController();
  189.         }
  190.  
  191.         public void sendMessage(String recipientUserId, String textBody) {
  192.             SinchService.this.sendMessage(recipientUserId, textBody);
  193.         }
  194.  
  195.         public void addMessageClientListener(MessageClientListener listener) {
  196.             SinchService.this.addMessageClientListener(listener);
  197.         }
  198.  
  199.         public void removeMessageClientListener(MessageClientListener listener) {
  200.             SinchService.this.removeMessageClientListener(listener);
  201.         }
  202.  
  203.         public NotificationResult relayRemotePushNotificationPayload(final Map<String, String> payload, Map<String, String> callHeader) {
  204.             if (mSinchClient == null) {
  205.                 createClient(session.getUser().getProfileName());
  206.             }
  207.             SinchService.this.callHeader = new HashMap<>(callHeader);
  208.             return mSinchClient.relayRemotePushNotificationPayload(payload);
  209.         }
  210.     }
  211.  
  212.     public interface ClientStartListener {
  213.  
  214.         void onClientStarted();
  215.  
  216.         void onClientStartFailed(SinchError error);
  217.     }
  218.  
  219.     private class MySinchClientListener implements SinchClientListener {
  220.  
  221.         @Override
  222.         public void onClientStarted(SinchClient client) {
  223.             Log.d(TAG, "SinchClient started");
  224.             if (mListener != null) {
  225.                 mListener.onClientStarted();
  226.             }
  227.         }
  228.  
  229.         @Override
  230.         public void onClientFailed(SinchClient client, SinchError error) {
  231.             if (mListener != null) {
  232.                 mListener.onClientStartFailed(error);
  233.             }
  234.             mSinchClient.terminate();
  235.             mSinchClient = null;
  236.         }
  237.  
  238.         @Override
  239.         public void onClientStopped(SinchClient client) {
  240.             Log.d(TAG, "SinchClient stopped");
  241.         }
  242.  
  243.         @Override
  244.         public void onLogMessage(int level, String area, String message) {
  245.             switch (level) {
  246.                 case Log.DEBUG:
  247.                     Log.d(area, message);
  248.                     break;
  249.                 case Log.ERROR:
  250.                     Log.e(area, message);
  251.                     break;
  252.                 case Log.INFO:
  253.                     Log.i(area, message);
  254.                     break;
  255.                 case Log.VERBOSE:
  256.                     Log.v(area, message);
  257.                     break;
  258.                 case Log.WARN:
  259.                     Log.w(area, message);
  260.                     break;
  261.             }
  262.         }
  263.  
  264.         @Override
  265.         public void onRegistrationCredentialsRequired(SinchClient client, ClientRegistration clientRegistration) {
  266.         }
  267.     }
  268.  
  269.     private class SinchCallClientListener implements CallClientListener {
  270.  
  271.         @Override
  272.         public void onIncomingCall(CallClient callClient, Call call) {
  273.             Log.d(TAG, "Incoming call from " + call.getRemoteUserId());
  274.             Map<String, String> headers = call.getHeaders();
  275.             Log.d(TAG, "Call Hearders: " + headers);
  276.             if (headers.isEmpty()) {
  277.                 headers = new HashMap<>(callHeader);
  278.                 Log.d(TAG, "Pushed Call Hearders: " + headers);
  279.             }
  280.             Intent intent = new Intent(SinchService.this, VideoCallActivity.class);
  281.             intent.putExtra("EXTRA_CALL_ID", call.getCallId());
  282.             intent.putExtra("EXTRA_INCOMING_CALL", true);
  283.             intent.putExtra("EXTRA_APPOINTMENT_ID", Integer.parseInt(Objects.requireNonNull(headers.get("appointment_id"))));
  284.             intent.putExtra("EXTRA_CALLER_NAME", Objects.requireNonNull(headers.get("caller_name")));
  285.             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  286.             startActivity(intent);
  287.         }
  288.     }
  289. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top