|
| 1 | +package com.telestax.restcomm_messenger; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.AlertDialog; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.DialogInterface; |
| 7 | +import android.content.Intent; |
| 8 | +import android.content.SharedPreferences; |
| 9 | +import android.media.AudioManager; |
| 10 | +import android.media.MediaPlayer; |
| 11 | +import android.opengl.GLSurfaceView; |
| 12 | +//import android.support.v7.app.ActionBarActivity; |
| 13 | +import android.os.Bundle; |
| 14 | +import android.preference.PreferenceManager; |
| 15 | +import android.util.Log; |
| 16 | +import android.view.Menu; |
| 17 | +import android.view.MenuItem; |
| 18 | +import android.view.View; |
| 19 | +import android.view.Window; |
| 20 | +import android.view.WindowManager; |
| 21 | +import android.widget.Button; |
| 22 | +import android.widget.CheckBox; |
| 23 | +import android.widget.CompoundButton; |
| 24 | +import android.widget.LinearLayout; |
| 25 | +import android.widget.RelativeLayout; |
| 26 | + |
| 27 | +import org.mobicents.restcomm.android.client.sdk.RCClient; |
| 28 | +import org.mobicents.restcomm.android.client.sdk.RCConnection; |
| 29 | +import org.mobicents.restcomm.android.client.sdk.RCConnectionListener; |
| 30 | +import org.mobicents.restcomm.android.client.sdk.RCDevice; |
| 31 | +import org.mobicents.restcomm.android.client.sdk.RCDeviceListener; |
| 32 | +import org.mobicents.restcomm.android.client.sdk.RCPresenceEvent; |
| 33 | + |
| 34 | +import java.util.HashMap; |
| 35 | + |
| 36 | + |
| 37 | +public class CallActivity extends Activity implements RCConnectionListener, View.OnClickListener, |
| 38 | + CompoundButton.OnCheckedChangeListener, AudioManager.OnAudioFocusChangeListener { |
| 39 | + |
| 40 | + //private GLSurfaceView videoView; |
| 41 | + private RCConnection connection, pendingConnection; |
| 42 | + SharedPreferences prefs; |
| 43 | + private static final String TAG = "CallActivity"; |
| 44 | + private HashMap<String, String> connectParams = new HashMap<String, String>(); |
| 45 | + private RCDevice device; |
| 46 | + private RelativeLayout parentLayout; |
| 47 | + MediaPlayer ringingPlayer; |
| 48 | + MediaPlayer callingPlayer; |
| 49 | + AudioManager audioManager; |
| 50 | + final String TAG_LOCAL_VIDEO_VIEW = "local-video-view"; |
| 51 | + |
| 52 | + CheckBox cbMuted; |
| 53 | + Button btnHangup; |
| 54 | + Button btnAnswer; |
| 55 | + Button btnDecline; |
| 56 | + Button btnCancel; |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void onCreate(Bundle savedInstanceState) { |
| 60 | + super.onCreate(savedInstanceState); |
| 61 | + // #webrtc |
| 62 | + // Set window styles for fullscreen-window size. Needs to be done before |
| 63 | + // adding content. |
| 64 | + requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 65 | + getWindow().addFlags( |
| 66 | + WindowManager.LayoutParams.FLAG_FULLSCREEN |
| 67 | + | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
| 68 | + | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
| 69 | + | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
| 70 | + | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); |
| 71 | + getWindow().getDecorView().setSystemUiVisibility( |
| 72 | + View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
| 73 | + | View.SYSTEM_UI_FLAG_FULLSCREEN |
| 74 | + | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); |
| 75 | + setContentView(R.layout.activity_call); |
| 76 | + |
| 77 | + // Initialize UI |
| 78 | + btnHangup = (Button)findViewById(R.id.button_hangup); |
| 79 | + btnHangup.setOnClickListener(this); |
| 80 | + btnAnswer = (Button)findViewById(R.id.button_answer); |
| 81 | + btnAnswer.setOnClickListener(this); |
| 82 | + btnDecline = (Button)findViewById(R.id.button_decline); |
| 83 | + btnDecline.setOnClickListener(this); |
| 84 | + btnCancel = (Button)findViewById(R.id.button_cancel); |
| 85 | + btnCancel.setOnClickListener(this); |
| 86 | + cbMuted = (CheckBox)findViewById(R.id.checkbox_muted); |
| 87 | + cbMuted.setOnCheckedChangeListener(this); |
| 88 | + parentLayout = (RelativeLayout) findViewById(R.id.layout_video_call); |
| 89 | + //videoView = (GLSurfaceView) findViewById(R.id.glview_call); |
| 90 | + |
| 91 | + audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); |
| 92 | + // volume control should be by default 'music' which will control the ringing sounds and 'voice call' when within a call |
| 93 | + setVolumeControlStream(AudioManager.STREAM_MUSIC); |
| 94 | + ringingPlayer = MediaPlayer.create(getApplicationContext(), R.raw.ringing); |
| 95 | + ringingPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); |
| 96 | + ringingPlayer.setLooping(true); |
| 97 | + callingPlayer = MediaPlayer.create(getApplicationContext(), R.raw.calling); |
| 98 | + callingPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); |
| 99 | + callingPlayer.setLooping(true); |
| 100 | + |
| 101 | + cbMuted.setEnabled(false); |
| 102 | + |
| 103 | + device = RCClient.getInstance().listDevices().get(0); |
| 104 | + |
| 105 | + PreferenceManager.setDefaultValues(this, "preferences.xml", MODE_PRIVATE, R.xml.preferences, false); |
| 106 | + prefs = PreferenceManager.getDefaultSharedPreferences(this); |
| 107 | + |
| 108 | + // Get Intent parameters. |
| 109 | + final Intent intent = getIntent(); |
| 110 | + if (intent.getAction() == RCDevice.OUTGOING_CALL) { |
| 111 | + connectParams.put("username", intent.getStringExtra(RCDevice.EXTRA_DID)); |
| 112 | + connection = device.connect(connectParams, this); |
| 113 | + |
| 114 | + if (connection == null) { |
| 115 | + Log.e(TAG, "Error: error connecting"); |
| 116 | + return; |
| 117 | + } |
| 118 | + } |
| 119 | + if (intent.getAction() == RCDevice.INCOMING_CALL) { |
| 120 | + int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); |
| 121 | + if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { |
| 122 | + ringingPlayer.start(); |
| 123 | + } |
| 124 | + pendingConnection = device.incomingConnection; |
| 125 | + pendingConnection.listenerReady(this); |
| 126 | + |
| 127 | + // the number from which we got the call |
| 128 | + String incomingCallDid = intent.getStringExtra(RCDevice.EXTRA_DID); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // UI Events |
| 133 | + public void onClick(View view) { |
| 134 | + if (view.getId() == R.id.button_hangup) { |
| 135 | + if (connection == null) { |
| 136 | + Log.e(TAG, "Error: not connected"); |
| 137 | + } |
| 138 | + else { |
| 139 | + connection.disconnect(); |
| 140 | + connection = null; |
| 141 | + pendingConnection = null; |
| 142 | + } |
| 143 | + } else if (view.getId() == R.id.button_answer) { |
| 144 | + if (pendingConnection != null) { |
| 145 | + pendingConnection.accept(); |
| 146 | + connection = this.pendingConnection; |
| 147 | + pendingConnection = null; |
| 148 | + ringingPlayer.pause(); |
| 149 | + // Abandon audio focus when playback complete |
| 150 | + audioManager.abandonAudioFocus(this); |
| 151 | + } |
| 152 | + } else if (view.getId() == R.id.button_decline) { |
| 153 | + if (pendingConnection != null) { |
| 154 | + pendingConnection.reject(); |
| 155 | + pendingConnection = null; |
| 156 | + ringingPlayer.pause(); |
| 157 | + // Abandon audio focus when playback complete |
| 158 | + audioManager.abandonAudioFocus(this); |
| 159 | + finish(); |
| 160 | + } |
| 161 | + } else if (view.getId() == R.id.button_cancel) { |
| 162 | + if (connection == null) { |
| 163 | + Log.e(TAG, "Error: not connected"); |
| 164 | + } |
| 165 | + else { |
| 166 | + connection.disconnect(); |
| 167 | + connection = null; |
| 168 | + pendingConnection = null; |
| 169 | + callingPlayer.pause(); |
| 170 | + // Abandon audio focus when playback complete |
| 171 | + audioManager.abandonAudioFocus(this); |
| 172 | + finish(); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 179 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 180 | + getMenuInflater().inflate(R.menu.menu_call, menu); |
| 181 | + return true; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 186 | + // Handle action bar item clicks here. The action bar will |
| 187 | + // automatically handle clicks on the Home/Up button, so long |
| 188 | + // as you specify a parent activity in AndroidManifest.xml. |
| 189 | + int id = item.getItemId(); |
| 190 | + |
| 191 | + //noinspection SimplifiableIfStatement |
| 192 | + if (id == R.id.action_settings) { |
| 193 | + return true; |
| 194 | + } |
| 195 | + |
| 196 | + return super.onOptionsItemSelected(item); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public void onResume() { |
| 201 | + super.onResume(); |
| 202 | + //videoView.onResume(); |
| 203 | + /* |
| 204 | + activityRunning = true; |
| 205 | + if (peerConnectionClient != null) { |
| 206 | + peerConnectionClient.startVideoSource(); |
| 207 | + } |
| 208 | + */ |
| 209 | + } |
| 210 | + |
| 211 | + // RCConnection Listeners |
| 212 | + public void onConnecting(RCConnection connection) |
| 213 | + { |
| 214 | + Log.i(TAG, "RCConnection connecting"); |
| 215 | + int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); |
| 216 | + if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { |
| 217 | + callingPlayer.start(); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + public void onConnected(RCConnection connection) { |
| 222 | + Log.i(TAG, "RCConnection connected"); |
| 223 | + cbMuted.setEnabled(true); |
| 224 | + if (!connection.isIncoming()) { |
| 225 | + callingPlayer.pause(); |
| 226 | + // Abandon audio focus when playback complete |
| 227 | + audioManager.abandonAudioFocus(this); |
| 228 | + } |
| 229 | + setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); |
| 230 | + } |
| 231 | + |
| 232 | + public void onDisconnected(RCConnection connection) { |
| 233 | + Log.i(TAG, "RCConnection disconnected"); |
| 234 | + cbMuted.setEnabled(false); |
| 235 | + |
| 236 | + this.connection = null; |
| 237 | + pendingConnection = null; |
| 238 | + setVolumeControlStream(AudioManager.STREAM_MUSIC); |
| 239 | + |
| 240 | + finish(); |
| 241 | + } |
| 242 | + |
| 243 | + public void onCancelled(RCConnection connection) { |
| 244 | + Log.i(TAG, "RCConnection cancelled"); |
| 245 | + if (connection.isIncoming() == true) { |
| 246 | + ringingPlayer.pause(); |
| 247 | + } |
| 248 | + else { |
| 249 | + callingPlayer.pause(); |
| 250 | + } |
| 251 | + // Abandon audio focus when playback complete |
| 252 | + audioManager.abandonAudioFocus(this); |
| 253 | + |
| 254 | + this.connection = null; |
| 255 | + pendingConnection = null; |
| 256 | + |
| 257 | + finish(); |
| 258 | + } |
| 259 | + |
| 260 | + public void onDeclined(RCConnection connection) { |
| 261 | + Log.i(TAG, "RCConnection declined"); |
| 262 | + callingPlayer.pause(); |
| 263 | + // Abandon audio focus when playback complete |
| 264 | + audioManager.abandonAudioFocus(this); |
| 265 | + |
| 266 | + |
| 267 | + this.connection = null; |
| 268 | + pendingConnection = null; |
| 269 | + |
| 270 | + finish(); |
| 271 | + } |
| 272 | + |
| 273 | + public void onDisconnected(RCConnection connection, int errorCode, String errorText) { |
| 274 | + if (errorCode == RCClient.ErrorCodes.NO_CONNECTIVITY.ordinal()) { |
| 275 | + showOkAlert("No Connectivity", errorText); |
| 276 | + } else if (errorCode == RCClient.ErrorCodes.GENERIC_ERROR.ordinal()) { |
| 277 | + showOkAlert("Generic Error", errorText); |
| 278 | + } else { |
| 279 | + showOkAlert("Unknown Error", "Unknown Restcomm Client error"); |
| 280 | + } |
| 281 | + this.connection = null; |
| 282 | + pendingConnection = null; |
| 283 | + } |
| 284 | + |
| 285 | + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) |
| 286 | + { |
| 287 | + if (buttonView.getId() == R.id.checkbox_muted) { |
| 288 | + if (connection != null) { |
| 289 | + connection.setMuted(isChecked); |
| 290 | + } |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + public void onReceiveLocalVideo(RCConnection connection, GLSurfaceView videoView) { |
| 295 | + if (videoView != null) { |
| 296 | + //show media on screen |
| 297 | + videoView.setTag(TAG_LOCAL_VIDEO_VIEW); |
| 298 | + if (parentLayout.findViewWithTag(TAG_LOCAL_VIDEO_VIEW) != null) { |
| 299 | + parentLayout.removeView(videoView); |
| 300 | + } |
| 301 | + parentLayout.addView(videoView, 0); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + // Callbacks for auio focus change events |
| 306 | + public void onAudioFocusChange(int focusChange) |
| 307 | + { |
| 308 | + Log.i(TAG, "onAudioFocusChange: " + focusChange); |
| 309 | + /* |
| 310 | + if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) { |
| 311 | + // Pause playback |
| 312 | + } |
| 313 | + else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { |
| 314 | + // Resume playback or raise it back to normal if we were ducked |
| 315 | + } |
| 316 | + else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { |
| 317 | + //am.unregisterMediaButtonEventReceiver(RemoteControlReceiver); |
| 318 | + audio.abandonAudioFocus(this); |
| 319 | + // Stop playback |
| 320 | + } |
| 321 | + else if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { |
| 322 | + // Lower the volume |
| 323 | + } |
| 324 | + */ |
| 325 | + } |
| 326 | + |
| 327 | + // Helpers |
| 328 | + private void showOkAlert(final String title, final String detail) { |
| 329 | + AlertDialog alertDialog = new AlertDialog.Builder(CallActivity.this).create(); |
| 330 | + alertDialog.setTitle(title); |
| 331 | + alertDialog.setMessage(detail); |
| 332 | + alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() { |
| 333 | + public void onClick(DialogInterface dialog, int which) { |
| 334 | + dialog.dismiss(); |
| 335 | + } |
| 336 | + }); |
| 337 | + alertDialog.show(); |
| 338 | + } |
| 339 | + |
| 340 | + |
| 341 | +} |
0 commit comments