Skip to content

Commit

Permalink
Working on #382: Host the SDK inside an Android Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonis Tsakiridis committed Sep 7, 2016
1 parent 6ea5715 commit 4988dc3
Show file tree
Hide file tree
Showing 17 changed files with 556 additions and 405 deletions.
4 changes: 2 additions & 2 deletions Examples/restcomm-olympus/app/olympus-version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Wed Sep 07 13:48:59 EEST 2016
VERSION_CODE=574
#Wed Sep 07 18:07:37 EEST 2016
VERSION_CODE=590
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@
package com.telestax.restcomm_olympus;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
Expand All @@ -56,18 +57,18 @@
import org.mobicents.restcomm.android.client.sdk.RCConnection;
import org.mobicents.restcomm.android.client.sdk.RCConnectionListener;
import org.mobicents.restcomm.android.client.sdk.RCDevice;
import org.mobicents.restcomm.android.client.sdk.util.PercentFrameLayout;


public class CallActivity extends AppCompatActivity implements RCConnectionListener, View.OnClickListener,
KeypadFragment.OnFragmentInteractionListener {
KeypadFragment.OnFragmentInteractionListener, ServiceConnection {

private RCConnection connection, pendingConnection;
SharedPreferences prefs;
private static final String TAG = "CallActivity";
private HashMap<String, Object> connectParams; // = new HashMap<String, Object>();
private HashMap<String, Object> acceptParams; // = new HashMap<String, Object>();
private RCDevice device;
boolean serviceBound = false;
private boolean pendingError = false;
private boolean activityVisible = false;
private boolean muteAudio = false;
Expand Down Expand Up @@ -124,7 +125,7 @@ protected void onCreate(Bundle savedInstanceState) {

alertDialog = new AlertDialog.Builder(CallActivity.this).create();

device = RCClient.listDevices().get(0);
//device = RCClient.listDevices().get(0);

PreferenceManager.setDefaultValues(this, "preferences.xml", MODE_PRIVATE, R.xml.preferences, false);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
Expand Down Expand Up @@ -154,8 +155,6 @@ protected void onCreate(Bundle savedInstanceState) {
ft.add(R.id.keypad_fragment_container, keypadFragment);
ft.hide(keypadFragment);
ft.commit();

handleCall(intent);
}

@Override
Expand All @@ -175,13 +174,21 @@ protected void onStart() {
activityVisible = true;

//handleCall(getIntent());
bindService(new Intent(this, RCDevice.class), this, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "%% onStop");
activityVisible = false;

// Unbind from the service
if (serviceBound) {
device.detach();
unbindService(this);
serviceBound = false;
}
}

@Override
Expand Down Expand Up @@ -226,6 +233,28 @@ protected void onDestroy()
}
}

// Callbacks for service binding, passed to bindService()
@Override
public void onServiceConnected(ComponentName className, IBinder service)
{
Log.i(TAG, "%% onServiceConnected");
// We've bound to LocalService, cast the IBinder and get LocalService instance
RCDevice.RCDeviceBinder binder = (RCDevice.RCDeviceBinder) service;
device = binder.getService();

// We have the device reference, let's handle the call
handleCall(getIntent());

serviceBound = true;
}

@Override
public void onServiceDisconnected(ComponentName arg0)
{
Log.i(TAG, "%% onServiceDisconnected");
serviceBound = false;
}

private void handleCall(Intent intent) {
isVideo = intent.getBooleanExtra(RCDevice.EXTRA_VIDEO_ENABLED, false);
if (intent.getAction().equals(RCDevice.OUTGOING_CALL)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@

import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
Expand All @@ -49,21 +50,22 @@
import org.mobicents.restcomm.android.client.sdk.RCDeviceListener;
import org.mobicents.restcomm.android.client.sdk.RCPresenceEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.telestax.restcomm_olympus.ContactsController.CONTACT_KEY;
import static com.telestax.restcomm_olympus.ContactsController.CONTACT_VALUE;

public class MainActivity extends AppCompatActivity
implements MainFragment.Callbacks, RCDeviceListener,
View.OnClickListener, SharedPreferences.OnSharedPreferenceChangeListener,
AddUserDialogFragment.ContactDialogListener, ActionFragment.ActionListener {
AddUserDialogFragment.ContactDialogListener, ActionFragment.ActionListener,
ServiceConnection {

private RCDevice device = null;
boolean serviceBound = false;

private static final String TAG = "MainActivity";
SharedPreferences prefs;
private RCDevice device;
private HashMap<String, Object> params;
private MainFragment listFragment;
private AlertDialog alertDialog;
Expand Down Expand Up @@ -99,40 +101,8 @@ protected void onCreate(Bundle savedInstanceState)
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
prefs = PreferenceManager.getDefaultSharedPreferences(this);

RCClient.setLogLevel(Log.VERBOSE);
RCClient.initialize(getApplicationContext(), new RCClient.RCInitListener() {
public void onInitialized()
{
Log.i(TAG, "RCClient initialized");
}

public void onError(Exception exception)
{
Log.e(TAG, "RCClient initialization error");
}
});

params = new HashMap<String, Object>();
params.put(RCDevice.ParameterKeys.SIGNALING_DOMAIN, prefs.getString(RCDevice.ParameterKeys.SIGNALING_DOMAIN, ""));
params.put(RCDevice.ParameterKeys.SIGNALING_USERNAME, prefs.getString(RCDevice.ParameterKeys.SIGNALING_USERNAME, "android-sdk"));
params.put(RCDevice.ParameterKeys.SIGNALING_PASSWORD, prefs.getString(RCDevice.ParameterKeys.SIGNALING_PASSWORD, "1234"));
params.put(RCDevice.ParameterKeys.MEDIA_ICE_URL, prefs.getString(RCDevice.ParameterKeys.MEDIA_ICE_URL, ""));
params.put(RCDevice.ParameterKeys.MEDIA_ICE_USERNAME, prefs.getString(RCDevice.ParameterKeys.MEDIA_ICE_USERNAME, ""));
params.put(RCDevice.ParameterKeys.MEDIA_ICE_PASSWORD, prefs.getString(RCDevice.ParameterKeys.MEDIA_ICE_PASSWORD, ""));
params.put(RCDevice.ParameterKeys.MEDIA_TURN_ENABLED, prefs.getBoolean(RCDevice.ParameterKeys.MEDIA_TURN_ENABLED, true));
params.put(RCDevice.ParameterKeys.SIGNALING_SECURE_ENABLED, prefs.getBoolean(RCDevice.ParameterKeys.SIGNALING_SECURE_ENABLED, false));
// The SDK provides the user with default sounds for calling, ringing, busy (declined) and message, but the user can override them
// by providing their own resource files (i.e. .wav, .mp3, etc) at res/raw passing them with Resource IDs like R.raw.user_provided_calling_sound
//params.put(RCDevice.ParameterKeys.RESOURCE_SOUND_CALLING, R.raw.user_provided_calling_sound);
//params.put(RCDevice.ParameterKeys.RESOURCE_SOUND_RINGING, R.raw.user_provided_ringing_sound);
//params.put(RCDevice.ParameterKeys.RESOURCE_SOUND_DECLINED, R.raw.user_provided_declined_sound);
//params.put(RCDevice.ParameterKeys.RESOURCE_SOUND_MESSAGE, R.raw.user_provided_message_sound);

// This is for debugging purposes, not for release builds
//params.put(RCDevice.ParameterKeys.SIGNALING_JAIN_SIP_LOGGING_ENABLED, prefs.getBoolean(RCDevice.ParameterKeys.SIGNALING_JAIN_SIP_LOGGING_ENABLED, true));
device = RCClient.createDevice(params, this);
device.setPendingIntents(new Intent(getApplicationContext(), CallActivity.class),
new Intent(getApplicationContext(), MessageActivity.class));
// Start Service
startService(new Intent(this, RCDevice.class));

// preferences
prefs.registerOnSharedPreferenceChangeListener(this);
Expand All @@ -148,6 +118,8 @@ protected void onStart()
super.onStart();
// The activity is about to become visible.
Log.i(TAG, "%% onStart");

bindService(new Intent(this, RCDevice.class), this, Context.BIND_AUTO_CREATE);
}

@Override
Expand All @@ -158,19 +130,10 @@ protected void onResume()
// The activity has become visible (it is now "resumed").
Log.i(TAG, "%% onResume");

if (device.getState() == RCDevice.DeviceState.OFFLINE) {
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorTextSecondary)));
}
else {
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));
handleExternalCall();
}

if (device != null) {
// needed if we are returning from Message screen that becomes the Device listener
device.setDeviceListener(this);
}

}

@Override
Expand All @@ -187,6 +150,13 @@ protected void onStop()
super.onStop();
// The activity is no longer visible (it is now "stopped")
Log.i(TAG, "%% onStop");

// Unbind from the service
if (serviceBound) {
device.detach();
unbindService(this);
serviceBound = false;
}
}

@Override
Expand All @@ -195,8 +165,10 @@ protected void onDestroy()
super.onDestroy();
// The activity is about to be destroyed.
Log.i(TAG, "%% onDestroy");
/*
RCClient.shutdown();
device = null;
*/
prefs.unregisterOnSharedPreferenceChangeListener(this);
}

Expand All @@ -207,6 +179,57 @@ public void onNewIntent(Intent intent)
setIntent(intent);
}

// Callbacks for service binding, passed to bindService()
@Override
public void onServiceConnected(ComponentName className, IBinder service)
{
Log.i(TAG, "%% onServiceConnected");
// We've bound to LocalService, cast the IBinder and get LocalService instance
RCDevice.RCDeviceBinder binder = (RCDevice.RCDeviceBinder) service;
device = binder.getService();

HashMap<String, Object> params = new HashMap<String, Object>();
params.put(RCDevice.ParameterKeys.INTENT_INCOMING_CALL, new Intent(RCDevice.INCOMING_CALL, null, getApplicationContext(), CallActivity.class));
params.put(RCDevice.ParameterKeys.INTENT_INCOMING_MESSAGE, new Intent(RCDevice.INCOMING_MESSAGE, null, getApplicationContext(), MessageActivity.class));
params.put(RCDevice.ParameterKeys.SIGNALING_DOMAIN, prefs.getString(RCDevice.ParameterKeys.SIGNALING_DOMAIN, ""));
params.put(RCDevice.ParameterKeys.SIGNALING_USERNAME, prefs.getString(RCDevice.ParameterKeys.SIGNALING_USERNAME, "android-sdk"));
params.put(RCDevice.ParameterKeys.SIGNALING_PASSWORD, prefs.getString(RCDevice.ParameterKeys.SIGNALING_PASSWORD, "1234"));
params.put(RCDevice.ParameterKeys.MEDIA_ICE_URL, prefs.getString(RCDevice.ParameterKeys.MEDIA_ICE_URL, ""));
params.put(RCDevice.ParameterKeys.MEDIA_ICE_USERNAME, prefs.getString(RCDevice.ParameterKeys.MEDIA_ICE_USERNAME, ""));
params.put(RCDevice.ParameterKeys.MEDIA_ICE_PASSWORD, prefs.getString(RCDevice.ParameterKeys.MEDIA_ICE_PASSWORD, ""));
params.put(RCDevice.ParameterKeys.MEDIA_TURN_ENABLED, prefs.getBoolean(RCDevice.ParameterKeys.MEDIA_TURN_ENABLED, true));
params.put(RCDevice.ParameterKeys.SIGNALING_SECURE_ENABLED, prefs.getBoolean(RCDevice.ParameterKeys.SIGNALING_SECURE_ENABLED, false));
// The SDK provides the user with default sounds for calling, ringing, busy (declined) and message, but the user can override them
// by providing their own resource files (i.e. .wav, .mp3, etc) at res/raw passing them with Resource IDs like R.raw.user_provided_calling_sound
//params.put(RCDevice.ParameterKeys.RESOURCE_SOUND_CALLING, R.raw.user_provided_calling_sound);
//params.put(RCDevice.ParameterKeys.RESOURCE_SOUND_RINGING, R.raw.user_provided_ringing_sound);
//params.put(RCDevice.ParameterKeys.RESOURCE_SOUND_DECLINED, R.raw.user_provided_declined_sound);
//params.put(RCDevice.ParameterKeys.RESOURCE_SOUND_MESSAGE, R.raw.user_provided_message_sound);

// This is for debugging purposes, not for release builds
//params.put(RCDevice.ParameterKeys.SIGNALING_JAIN_SIP_LOGGING_ENABLED, prefs.getBoolean(RCDevice.ParameterKeys.SIGNALING_JAIN_SIP_LOGGING_ENABLED, true));

device.attach(getApplicationContext(), params, this);
device.setLogLevel(Log.VERBOSE);

if (device.getState() == RCDevice.DeviceState.OFFLINE) {
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorTextSecondary)));
}
else {
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));
handleExternalCall();
}

serviceBound = true;
}

@Override
public void onServiceDisconnected(ComponentName arg0)
{
Log.i(TAG, "%% onServiceDisconnected");
serviceBound = false;
}

/**
* MainFragment Callbacks
*/
Expand Down Expand Up @@ -470,6 +493,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,

}


/**
* Helpers
*/
Expand Down
Loading

0 comments on commit 4988dc3

Please sign in to comment.