Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.wmakit.samataxi.activities;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.ActivityInfo;
- import android.content.res.Configuration;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.media.RingtoneManager;
- import android.net.Uri;
- import android.os.PowerManager;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.sinch.android.rtc.Sinch;
- import com.sinch.android.rtc.SinchClient;
- import com.squareup.picasso.Picasso;
- import com.wmakit.samataxi.R;
- import com.wmakit.samataxi.config.Constant;
- import com.wmakit.samataxi.config.PreferencesManager;
- import com.wmakit.samataxi.ebanx.SwipeButton;
- import com.wmakit.samataxi.modelmanager.ModelManager;
- import com.wmakit.samataxi.modelmanager.ModelManagerListener;
- import com.wmakit.samataxi.utility.AppUtil;
- import com.wmakit.samataxi.volleynetwork.ControllerRequest;
- import java.util.Timer;
- import java.util.TimerTask;
- import cn.iwgang.countdownview.CountdownView;
- import de.hdodenhof.circleimageview.CircleImageView;
- public abstract class AbstractCallActivity extends AppCompatActivity {
- protected SinchClient sinchClient;
- protected PreferencesManager preferencesManager;
- protected CircleImageView profileImage;
- protected TextView otherPeopleName;
- protected TextView otherPeoplePhone;
- protected TextView activityInfo;
- protected TextView connecting;
- protected ImageView speakerBtn;
- protected SwipeButton answerCall;
- protected ImageView hangupCall;
- protected MediaPlayer mPlayer;
- protected CountdownView countdownView;
- protected boolean speakerEnabled = false;
- protected boolean speakerDefaultStateEnabled = false;
- protected boolean progressing = false;
- protected float SPEAKER_DISABLED_OPACITY = 0.3f;
- protected int CONNECTION_TIMEOUT = 10000;
- protected int RE_RERUN__TIMEOUT = 2000;
- protected boolean finishing = false;
- protected boolean callTook = false;
- protected Timer timer;
- public ProgressDialog dialog;
- protected boolean shouldReRunOnDestroy;
- protected boolean alreadyNotifiyed;
- protected boolean callAlreadyStarted;
- protected boolean clientConnected;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- onScreen();
- AppUtil.setStatusBarColor(this);
- Log.d("__sinch", "creating activity");
- setContentView(R.layout.activity_abstract_call);
- initUI();
- preferencesManager = PreferencesManager.getInstance(this);
- alreadyNotifiyed = preferencesManager.getPartenerReadyForCall();
- preferencesManager.setIsReceivingCall(true);
- initData();
- sinchClient = getSinchClinet();
- initTimer();
- showLoader();
- }
- @Override
- public void onResume() {
- super.onResume();
- }
- protected void onScreen(){
- PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
- try {
- shouldReRunOnDestroy = !pm.isScreenOn();
- } catch (Exception e) {
- shouldReRunOnDestroy = true;
- e.printStackTrace();
- }
- Window window = this.getWindow();
- window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
- window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
- window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
- window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- }
- private void initTimer(){
- new android.os.Handler().postDelayed(
- () -> finishIfNoConnection(),
- CONNECTION_TIMEOUT);
- new android.os.Handler().postDelayed(
- () -> shouldReRunOnDestroy = false,
- RE_RERUN__TIMEOUT);
- timer = new Timer();
- timer.scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- connectionEndCheck();
- }
- }, 0, 1000);
- }
- protected void showLoader(){
- Log.d("__sinch", "showing loader");
- if(dialog == null){
- //dialog = ProgressDialog.show(this, "Appel en cour","Mise en relation", true);
- //dialog.show();
- }
- answerCall.setVisibility(View.INVISIBLE);
- /*profileImage.setVisibility(View.INVISIBLE);
- otherPeopleName.setVisibility(View.INVISIBLE);
- otherPeoplePhone.setVisibility(View.INVISIBLE);
- activityInfo.setVisibility(View.INVISIBLE);*/
- }
- protected void hideLoader(){
- Log.d("__sinch", "hidding loader");
- answerCall.setVisibility(View.VISIBLE);
- connecting.setVisibility(View.INVISIBLE);
- /*profileImage.setVisibility(View.VISIBLE);
- otherPeopleName.setVisibility(View.VISIBLE);
- otherPeoplePhone.setVisibility(View.VISIBLE);
- activityInfo.setVisibility(View.VISIBLE);
- activityInfo.setVisibility(View.VISIBLE);*/
- if(!this.isFinishing()){
- //dialog.dismiss();
- }
- }
- abstract void finishIfNoConnection();
- abstract void connectionEndCheck();
- private void initUI(){
- profileImage = (CircleImageView)findViewById(R.id.profile_image);
- otherPeopleName = (TextView) findViewById(R.id.other_people_name);
- otherPeoplePhone = (TextView) findViewById(R.id.other_people_phone);
- connecting = (TextView) findViewById(R.id.connecting_txt);
- activityInfo = (TextView) findViewById(R.id.activity_info);
- speakerBtn = (ImageView) findViewById(R.id.speaker_btn);
- answerCall = (SwipeButton) findViewById(R.id.answer_call);
- hangupCall = (ImageView) findViewById(R.id.hangup_my_call);
- countdownView = (CountdownView) findViewById(R.id.countdown);
- countdownView.setVisibility(View.GONE);
- hangupCall.setVisibility(View.GONE);
- speakerBtn.setVisibility(View.GONE);
- speakerBtn.setEnabled(false);
- speakerBtn.setOnClickListener(v -> {
- if(sinchClient != null){
- if(speakerEnabled){
- speakerEnabled = false;
- sinchClient.getAudioController().disableSpeaker();
- speakerBtn.setAlpha(1f);
- //speakerBtn.setImageDrawable(getResources().getDrawable(R.drawable.speaker_disabled));
- }
- else{
- speakerEnabled = true;
- sinchClient.getAudioController().enableSpeaker();
- //speakerBtn.setImageDrawable(getResources().getDrawable(R.drawable.speaker_enabled));
- speakerBtn.setAlpha(SPEAKER_DISABLED_OPACITY);
- }
- }
- });
- Intent intent = getIntent();
- Log.e("onMessage activity", intent.getStringExtra("userName") + intent);
- otherPeopleName.setText(intent.getStringExtra("userName"));
- otherPeoplePhone.setText(String.format("%s %s", getString(R.string.appel), intent.getStringExtra("phone")));
- try {
- Picasso.with(this).load(intent.getStringExtra("image")).into(profileImage);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void initData(){
- if(preferencesManager.isDriver()){
- speakerDefaultStateEnabled = true;
- }
- }
- public SinchClient getSinchClinet(){
- sinchClient = Sinch.getSinchClientBuilder()
- .context(this)
- .userId(preferencesManager.getUserID())
- .applicationKey(Constant.SINCH_APP_KEY)
- .applicationSecret(Constant.SINCH_APP_SECRET)
- .environmentHost(Constant.SINCH_ENVIRONMENT)
- .build();
- Log.d("__sinch__", "my userID " + preferencesManager.getUserID() + "");
- sinchClient.setSupportCalling(true);
- return sinchClient;
- }
- protected void playRington(final int song){
- if( mPlayer == null){
- Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
- mPlayer = new MediaPlayer();
- mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- ControllerRequest.getInstance().setVolumeToUp();
- try {
- mPlayer.setVolume(100f, 100f);
- mPlayer.setDataSource(this, uri);
- mPlayer.prepare();
- } catch (Exception e) {
- e.printStackTrace();
- mPlayer = null;
- return;
- }
- mPlayer.setLooping(true);
- mPlayer.start();
- }
- }
- protected void stopRington(){
- if (mPlayer != null) {
- mPlayer.setVolume(0.01f, 0.01f);
- mPlayer.stop();
- mPlayer.release();
- mPlayer = null;
- }
- }
- protected void startCountDow(){
- countdownView.setVisibility(View.VISIBLE);
- speakerBtn.setVisibility(View.VISIBLE);
- speakerBtn.setEnabled(true);
- final int[] current = {0};
- countdownView.start(current[0]);
- new Timer().scheduleAtFixedRate(new TimerTask(){
- @Override
- public void run(){
- runOnUiThread(() -> {
- current[0] += 1000;
- countdownView.updateShow(current[0]);
- });
- }
- },1000,1000);
- }
- void notifyPartenerListenStarted(){
- Log.d("onMessage", "notifying...");
- ModelManager.notifyPartenerListenStarted(preferencesManager.getToken(),
- this, false, new ModelManagerListener() {
- @Override
- public void onSuccess(String json) {
- Log.d("onMessage", "notifying result " + json);
- }
- @Override
- public void onError() {
- notifyPartenerListenStarted();
- }
- }, getIntent().getStringExtra("id"));
- }
- void notifyPartenerCallHangupStarted(){
- Log.d("onMessage", "notifying...");
- ModelManager.notifyPartenerCallHangupStarted(preferencesManager.getToken(),
- this, false, new ModelManagerListener() {
- @Override
- public void onSuccess(String json) {
- Log.d("onMessage", "notifying result " + json);
- }
- @Override
- public void onError() {
- notifyPartenerCallHangupStarted();
- }
- }, getIntent().getStringExtra("id"), preferencesManager.getUserID());
- }
- @Override
- protected void onPause() {
- super.onPause();
- if(dialog != null){
- dialog.dismiss();
- }
- }
- @Override
- protected void onStop() {
- super.onStop();
- preferencesManager.setIsReceivingCall(false);
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement