Advertisement
Guest User

AbstractCallActivity

a guest
Jan 22nd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.52 KB | None | 0 0
  1. package com.wmakit.samataxi.activities;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.pm.ActivityInfo;
  7. import android.content.res.Configuration;
  8. import android.media.AudioManager;
  9. import android.media.MediaPlayer;
  10. import android.media.RingtoneManager;
  11. import android.net.Uri;
  12. import android.os.PowerManager;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.os.Bundle;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.view.Window;
  18. import android.view.WindowManager;
  19. import android.widget.ImageView;
  20. import android.widget.TextView;
  21.  
  22. import com.sinch.android.rtc.Sinch;
  23. import com.sinch.android.rtc.SinchClient;
  24. import com.squareup.picasso.Picasso;
  25. import com.wmakit.samataxi.R;
  26. import com.wmakit.samataxi.config.Constant;
  27. import com.wmakit.samataxi.config.PreferencesManager;
  28. import com.wmakit.samataxi.ebanx.SwipeButton;
  29. import com.wmakit.samataxi.modelmanager.ModelManager;
  30. import com.wmakit.samataxi.modelmanager.ModelManagerListener;
  31. import com.wmakit.samataxi.utility.AppUtil;
  32. import com.wmakit.samataxi.volleynetwork.ControllerRequest;
  33.  
  34. import java.util.Timer;
  35. import java.util.TimerTask;
  36.  
  37. import cn.iwgang.countdownview.CountdownView;
  38. import de.hdodenhof.circleimageview.CircleImageView;
  39.  
  40. public abstract class AbstractCallActivity extends AppCompatActivity {
  41.  
  42.     protected SinchClient sinchClient;
  43.     protected PreferencesManager preferencesManager;
  44.  
  45.     protected CircleImageView profileImage;
  46.     protected TextView otherPeopleName;
  47.     protected TextView otherPeoplePhone;
  48.     protected TextView activityInfo;
  49.     protected TextView connecting;
  50.     protected ImageView speakerBtn;
  51.     protected SwipeButton answerCall;
  52.     protected ImageView hangupCall;
  53.     protected MediaPlayer mPlayer;
  54.     protected CountdownView countdownView;
  55.  
  56.     protected boolean speakerEnabled = false;
  57.     protected boolean speakerDefaultStateEnabled = false;
  58.     protected boolean progressing = false;
  59.  
  60.     protected float SPEAKER_DISABLED_OPACITY = 0.3f;
  61.     protected int CONNECTION_TIMEOUT = 10000;
  62.     protected int RE_RERUN__TIMEOUT = 2000;
  63.     protected boolean finishing = false;
  64.     protected boolean callTook = false;
  65.  
  66.     protected Timer timer;
  67.     public ProgressDialog dialog;
  68.     protected boolean shouldReRunOnDestroy;
  69.     protected boolean alreadyNotifiyed;
  70.     protected boolean callAlreadyStarted;
  71.     protected boolean clientConnected;
  72.  
  73.  
  74.  
  75.  
  76.     @Override
  77.     protected void onCreate(Bundle savedInstanceState) {
  78.         super.onCreate(savedInstanceState);
  79.  
  80.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  81.         onScreen();
  82.  
  83.         AppUtil.setStatusBarColor(this);
  84.  
  85.  
  86.         Log.d("__sinch", "creating activity");
  87.         setContentView(R.layout.activity_abstract_call);
  88.  
  89.         initUI();
  90.         preferencesManager = PreferencesManager.getInstance(this);
  91.  
  92.         alreadyNotifiyed = preferencesManager.getPartenerReadyForCall();
  93.  
  94.         preferencesManager.setIsReceivingCall(true);
  95.         initData();
  96.         sinchClient = getSinchClinet();
  97.  
  98.         initTimer();
  99.  
  100.         showLoader();
  101.     }
  102.  
  103.     @Override
  104.     public void onResume() {
  105.         super.onResume();
  106.  
  107.     }
  108.  
  109.     protected void onScreen(){
  110.         PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
  111.         try {
  112.             shouldReRunOnDestroy = !pm.isScreenOn();
  113.         } catch (Exception e) {
  114.             shouldReRunOnDestroy = true;
  115.             e.printStackTrace();
  116.         }
  117.  
  118.         Window window = this.getWindow();
  119.         window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
  120.         window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
  121.         window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
  122.         window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  123.     }
  124.  
  125.     private void initTimer(){
  126.  
  127.         new android.os.Handler().postDelayed(
  128.                 () -> finishIfNoConnection(),
  129.                 CONNECTION_TIMEOUT);
  130.  
  131.         new android.os.Handler().postDelayed(
  132.                 () -> shouldReRunOnDestroy = false,
  133.                 RE_RERUN__TIMEOUT);
  134.  
  135.         timer = new Timer();
  136.  
  137.         timer.scheduleAtFixedRate(new TimerTask() {
  138.             @Override
  139.             public void run() {
  140.                 connectionEndCheck();
  141.             }
  142.         }, 0, 1000);
  143.     }
  144.  
  145.     protected void showLoader(){
  146.  
  147.         Log.d("__sinch", "showing loader");
  148.  
  149.         if(dialog == null){
  150.             //dialog = ProgressDialog.show(this, "Appel en cour","Mise en relation", true);
  151.             //dialog.show();
  152.         }
  153.  
  154.         answerCall.setVisibility(View.INVISIBLE);
  155.         /*profileImage.setVisibility(View.INVISIBLE);
  156.         otherPeopleName.setVisibility(View.INVISIBLE);
  157.         otherPeoplePhone.setVisibility(View.INVISIBLE);
  158.         activityInfo.setVisibility(View.INVISIBLE);*/
  159.  
  160.     }
  161.  
  162.     protected void hideLoader(){
  163.         Log.d("__sinch", "hidding loader");
  164.         answerCall.setVisibility(View.VISIBLE);
  165.         connecting.setVisibility(View.INVISIBLE);
  166.  
  167.         /*profileImage.setVisibility(View.VISIBLE);
  168.         otherPeopleName.setVisibility(View.VISIBLE);
  169.         otherPeoplePhone.setVisibility(View.VISIBLE);
  170.         activityInfo.setVisibility(View.VISIBLE);
  171.         activityInfo.setVisibility(View.VISIBLE);*/
  172.  
  173.         if(!this.isFinishing()){
  174.             //dialog.dismiss();
  175.         }
  176.     }
  177.  
  178.     abstract void finishIfNoConnection();
  179.  
  180.     abstract void connectionEndCheck();
  181.  
  182.     private void initUI(){
  183.         profileImage = (CircleImageView)findViewById(R.id.profile_image);
  184.         otherPeopleName = (TextView) findViewById(R.id.other_people_name);
  185.         otherPeoplePhone = (TextView) findViewById(R.id.other_people_phone);
  186.         connecting = (TextView) findViewById(R.id.connecting_txt);
  187.         activityInfo = (TextView) findViewById(R.id.activity_info);
  188.         speakerBtn = (ImageView) findViewById(R.id.speaker_btn);
  189.         answerCall = (SwipeButton) findViewById(R.id.answer_call);
  190.         hangupCall = (ImageView) findViewById(R.id.hangup_my_call);
  191.         countdownView = (CountdownView) findViewById(R.id.countdown);
  192.  
  193.         countdownView.setVisibility(View.GONE);
  194.         hangupCall.setVisibility(View.GONE);
  195.         speakerBtn.setVisibility(View.GONE);
  196.         speakerBtn.setEnabled(false);
  197.  
  198.         speakerBtn.setOnClickListener(v -> {
  199.             if(sinchClient != null){
  200.                 if(speakerEnabled){
  201.                     speakerEnabled = false;
  202.                     sinchClient.getAudioController().disableSpeaker();
  203.                     speakerBtn.setAlpha(1f);
  204.                     //speakerBtn.setImageDrawable(getResources().getDrawable(R.drawable.speaker_disabled));
  205.                 }
  206.                 else{
  207.                     speakerEnabled = true;
  208.                     sinchClient.getAudioController().enableSpeaker();
  209.                     //speakerBtn.setImageDrawable(getResources().getDrawable(R.drawable.speaker_enabled));
  210.                     speakerBtn.setAlpha(SPEAKER_DISABLED_OPACITY);
  211.                 }
  212.             }
  213.         });
  214.  
  215.  
  216.         Intent intent = getIntent();
  217.         Log.e("onMessage activity", intent.getStringExtra("userName") + intent);
  218.         otherPeopleName.setText(intent.getStringExtra("userName"));
  219.         otherPeoplePhone.setText(String.format("%s %s", getString(R.string.appel), intent.getStringExtra("phone")));
  220.  
  221.         try {
  222.             Picasso.with(this).load(intent.getStringExtra("image")).into(profileImage);
  223.         } catch (Exception e) {
  224.             e.printStackTrace();
  225.         }
  226.     }
  227.  
  228.     private void initData(){
  229.         if(preferencesManager.isDriver()){
  230.             speakerDefaultStateEnabled = true;
  231.         }
  232.     }
  233.  
  234.     public SinchClient getSinchClinet(){
  235.         sinchClient = Sinch.getSinchClientBuilder()
  236.                 .context(this)
  237.                 .userId(preferencesManager.getUserID())
  238.                 .applicationKey(Constant.SINCH_APP_KEY)
  239.                 .applicationSecret(Constant.SINCH_APP_SECRET)
  240.                 .environmentHost(Constant.SINCH_ENVIRONMENT)
  241.                 .build();
  242.  
  243.         Log.d("__sinch__", "my userID " + preferencesManager.getUserID() + "");
  244.  
  245.         sinchClient.setSupportCalling(true);
  246.  
  247.         return  sinchClient;
  248.     }
  249.  
  250.     protected void playRington(final int song){
  251.  
  252.         if( mPlayer == null){
  253.             Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
  254.             mPlayer = new MediaPlayer();
  255.             mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  256.  
  257.             ControllerRequest.getInstance().setVolumeToUp();
  258.  
  259.             try {
  260.                 mPlayer.setVolume(100f, 100f);
  261.  
  262.                 mPlayer.setDataSource(this, uri);
  263.                 mPlayer.prepare();
  264.             } catch (Exception e) {
  265.                 e.printStackTrace();
  266.                 mPlayer = null;
  267.                 return;
  268.             }
  269.  
  270.  
  271.             mPlayer.setLooping(true);
  272.             mPlayer.start();
  273.         }
  274.     }
  275.  
  276.     protected void stopRington(){
  277.         if (mPlayer != null) {
  278.             mPlayer.setVolume(0.01f, 0.01f);
  279.             mPlayer.stop();
  280.             mPlayer.release();
  281.             mPlayer = null;
  282.         }
  283.     }
  284.  
  285.     protected void startCountDow(){
  286.         countdownView.setVisibility(View.VISIBLE);
  287.         speakerBtn.setVisibility(View.VISIBLE);
  288.         speakerBtn.setEnabled(true);
  289.  
  290.         final int[] current = {0};
  291.         countdownView.start(current[0]);
  292.  
  293.         new Timer().scheduleAtFixedRate(new TimerTask(){
  294.             @Override
  295.             public void run(){
  296.                 runOnUiThread(() -> {
  297.                     current[0] += 1000;
  298.                     countdownView.updateShow(current[0]);
  299.                 });
  300.             }
  301.         },1000,1000);
  302.     }
  303.  
  304.  
  305.     void notifyPartenerListenStarted(){
  306.         Log.d("onMessage", "notifying...");
  307.         ModelManager.notifyPartenerListenStarted(preferencesManager.getToken(),
  308.                 this, false, new ModelManagerListener() {
  309.                     @Override
  310.                     public void onSuccess(String json) {
  311.                         Log.d("onMessage", "notifying result " + json);
  312.                     }
  313.  
  314.                     @Override
  315.                     public void onError() {
  316.                         notifyPartenerListenStarted();
  317.                     }
  318.                 }, getIntent().getStringExtra("id"));
  319.     }
  320.  
  321.     void notifyPartenerCallHangupStarted(){
  322.         Log.d("onMessage", "notifying...");
  323.         ModelManager.notifyPartenerCallHangupStarted(preferencesManager.getToken(),
  324.                 this, false, new ModelManagerListener() {
  325.                     @Override
  326.                     public void onSuccess(String json) {
  327.                         Log.d("onMessage", "notifying result " + json);
  328.                     }
  329.  
  330.                     @Override
  331.                     public void onError() {
  332.                         notifyPartenerCallHangupStarted();
  333.                     }
  334.                 }, getIntent().getStringExtra("id"), preferencesManager.getUserID());
  335.     }
  336.  
  337.     @Override
  338.     protected void onPause() {
  339.         super.onPause();
  340.         if(dialog != null){
  341.             dialog.dismiss();
  342.         }
  343.     }
  344.  
  345.     @Override
  346.     protected void onStop() {
  347.         super.onStop();
  348.         preferencesManager.setIsReceivingCall(false);
  349.     }
  350.  
  351.  
  352.  
  353.  
  354.     @Override
  355.     public void onConfigurationChanged(Configuration newConfig) {
  356.         super.onConfigurationChanged(newConfig);
  357.     }
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement