Skip to content

Commit

Permalink
Merge pull request stevegraham#1 from lylepratt/master
Browse files Browse the repository at this point in the history
Merge changes from Lyle's repository back into mine
  • Loading branch information
jefflinwood committed Aug 23, 2013
2 parents e6c9bdd + f23f0f3 commit e60d7dd
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import android.app.Activity;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.twilio.client.Connection;
import com.twilio.client.Device;
//import com.twilio.client.Connection;
//import com.twilio.client.Device;

/**
*
Expand All @@ -19,19 +20,21 @@ public class IncomingConnectionActivity extends Activity {
@Override
public void onNewIntent(Intent intent)
{
Log.d("TCPlugin", "ON NEW INTENT IN CONNECTION ACTIVITY");
super.onNewIntent(intent);
setIntent(intent);
}

@Override
public void onResume()
{
Log.d("TCPlugin", "ON RESUME IN CONNECTION ACTIVITY");
super.onResume();
Intent intent = getIntent();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
intent.setAction(ACTION_NAME);
lbm.sendBroadcast(intent);

finish();
}


Expand Down
126 changes: 72 additions & 54 deletions Android/src/com/phonegap/plugins/twilioclient/TCPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import android.R;
//import android.R;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
Expand All @@ -21,10 +21,12 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;


import com.twilio.client.Connection;
import com.twilio.client.ConnectionListener;
import com.twilio.client.Device;
Expand Down Expand Up @@ -54,6 +56,7 @@ public class TCPlugin extends CordovaPlugin implements DeviceListener,
private JSONArray mInitDeviceSetupArgs;
private int mCurrentNotificationId = 1;
private String mCurrentNotificationText;
private TCPlugin plugin = this;



Expand All @@ -62,38 +65,13 @@ public class TCPlugin extends CordovaPlugin implements DeviceListener,
public void onReceive(Context context, Intent intent) {
// mDevice = intent.getParcelableExtra(Device.EXTRA_DEVICE);
mConnection = intent.getParcelableExtra(Device.EXTRA_CONNECTION);
Log.d(TAG, "incoming intent received with connection: "
+ mConnection.getState().name());
mConnection.setConnectionListener(plugin);
Log.d(TAG, "incoming intent received with connection: "+ mConnection.getState().name());
JSONObject connection = new JSONObject();
try {
connection.putOpt("parameters", getJSONObject(mConnection.getParameters()));

if (mCurrentNotificationText != null) {


PackageManager pm = context.getPackageManager();
Intent notificationIntent = pm.getLaunchIntentForPackage(context.getPackageName());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.btn_star_big_on)
.setContentTitle("Answer")
.setContentText(mCurrentNotificationText)
.setContentIntent(pendingIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) TCPlugin.this.webView.getContext().getSystemService(Activity.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mCurrentNotificationId, mBuilder.build());
}

} catch (JSONException e) {
Log.e(TAG,e.getLocalizedMessage(), e);
String constate = mConnection.getState().name();
if(constate.equals("PENDING")) {
TCPlugin.this.javascriptCallback("onincoming", connection, mInitCallbackContext);
}
TCPlugin.this.javascriptCallback("onincoming", connection, mInitCallbackContext);
}
};

Expand Down Expand Up @@ -153,13 +131,17 @@ public boolean execute(final String action, final JSONArray args,
connectionStatus(callbackContext);
return true;
} else if ("rejectConnection".equals(action)) {
rejectConnection(args, callbackContext);
return true;
} else if ("showNotification".equals(action)) {
showNotification(args,callbackContext);
return true;
} else if ("cancelNotification".equals(action)) {
cancelNotification(args,callbackContext);
return true;
} else if ("setSpeaker".equals(action)) {
setSpeaker(args,callbackContext);
return true;
}

return false;
Expand Down Expand Up @@ -188,21 +170,13 @@ private void deviceSetup(JSONArray arguments,
}
mDevice = Twilio.createDevice(arguments.optString(0), this);

// handle incoming phone requests
// 1) configure Twilio
Intent intent = new Intent(this.cordova.getActivity(),
IncomingConnectionActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this.cordova.getActivity(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent = new Intent(this.cordova.getActivity(), IncomingConnectionActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mDevice.setIncomingIntent(pendingIntent);
// 2) configure the local broadcast manager
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(cordova
.getActivity());
lbm.registerReceiver(mBroadcastReceiver, new IntentFilter(
IncomingConnectionActivity.ACTION_NAME));

LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(cordova.getActivity());
lbm.registerReceiver(mBroadcastReceiver, new IntentFilter(IncomingConnectionActivity.ACTION_NAME));
javascriptCallback("onready", callbackContext);

}

private void connect(JSONArray arguments, CallbackContext callbackContext) {
Expand Down Expand Up @@ -253,6 +227,11 @@ private void acceptConnection(JSONArray arguments, CallbackContext callbackConte
callbackContext.success();
}

private void rejectConnection(JSONArray arguments, CallbackContext callbackContext) {
mConnection.reject();
callbackContext.success();
}

private void disconnectConnection(JSONArray arguments, CallbackContext callbackContext) {
mConnection.disconnect();
callbackContext.success();
Expand Down Expand Up @@ -336,10 +315,28 @@ private void connectionStatus(CallbackContext callbackContext) {


private void showNotification(JSONArray arguments, CallbackContext context) {
Context acontext = TCPlugin.this.webView.getContext();
NotificationManager mNotifyMgr =
(NotificationManager) TCPlugin.this.webView.getContext().getSystemService(Activity.NOTIFICATION_SERVICE);
(NotificationManager) acontext.getSystemService(Activity.NOTIFICATION_SERVICE);
mNotifyMgr.cancelAll();
mCurrentNotificationText = arguments.optString(0);
mCurrentNotificationText = arguments.optString(0);


PackageManager pm = acontext.getPackageManager();
Intent notificationIntent = pm.getLaunchIntentForPackage(acontext.getPackageName());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("notificationTag", "BVNotification");

PendingIntent pendingIntent = PendingIntent.getActivity(acontext, 0, notificationIntent, 0);
int notification_icon = acontext.getResources().getIdentifier("notification", "drawable", acontext.getPackageName());
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(acontext)
.setSmallIcon(notification_icon)
.setContentTitle("Incoming Call")
.setContentText(mCurrentNotificationText)
.setContentIntent(pendingIntent);
mNotifyMgr.notify(mCurrentNotificationId, mBuilder.build());

context.success();
}

Expand All @@ -350,6 +347,28 @@ private void cancelNotification(JSONArray arguments, CallbackContext context) {
context.success();
}

/**
* Changes sound from earpiece to speaker and back
*
* @param mode Speaker Mode
* */
public void setSpeaker(JSONArray arguments, final CallbackContext callbackContext) {
Context context = cordova.getActivity().getApplicationContext();
AudioManager m_amAudioManager;
m_amAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
String mode = arguments.optString(0);
if(mode.equals("on")) {
Log.d("TCPlugin", "SPEAKER");
m_amAudioManager.setMode(AudioManager.MODE_NORMAL);
m_amAudioManager.setSpeakerphoneOn(true);
}
else {
Log.d("TCPlugin", "EARPIECE");
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
}
}

// DeviceListener methods

@Override
Expand All @@ -371,20 +390,20 @@ public void onPresenceChanged(Device device, PresenceEvent presenceEvent) {
@Override
public void onStartListening(Device device) {
// What to do here? The JS library doesn't have an event for this.

}

@Override
public void onStopListening(Device device) {
// TODO Auto-generated method stub

Log.d(TAG, "onStopListening");
}

@Override
public void onStopListening(Device device, int errorCode,
String errorMessage) {
// this.javascriptErrorback(errorCode, errorMessage);

Log.d(TAG, "onStopListeningWithError");
}

@Override
Expand All @@ -407,7 +426,7 @@ public void onInitialized() {

// Twilio Connection Listener methods
@Override
public void onConnected(Connection connection) {
public void onConnected(Connection connection) {
Log.d(TAG, "onConnected()");
fireDocumentEvent("onconnect");
if (connection.isIncoming()) {
Expand All @@ -433,8 +452,7 @@ public void onDisconnected(Connection connection) {
}

@Override
public void onDisconnected(Connection connection, int errorCode,
String errorMessage) {
public void onDisconnected(Connection connection, int errorCode, String errorMessage) {
// TODO: Pass error back
Log.d(TAG, "onDisconnected(): " + errorMessage);
onDisconnected(connection);
Expand Down Expand Up @@ -466,8 +484,8 @@ private void javascriptCallback(String event,
javascriptCallback(event, null, callbackContext);
}

private void javascriptErrorback(int errorCode, String errorMessage,
CallbackContext callbackContext) {

private void javascriptErrorback(int errorCode, String errorMessage, CallbackContext callbackContext) {
JSONObject object = new JSONObject();
try {
object.putOpt("message", errorMessage);
Expand Down
6 changes: 5 additions & 1 deletion Android/src/com/phonegap/plugins/twilioclient/tcPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
var status = Cordova.exec(null,null,"TCPlugin","deviceStatus",[]);
}

// Noops until I figure out why the hell using sounds in Phonegap gives EXC_BAD_ACCESS
TwilioPlugin.Device.prototype.sounds = {
incoming: function(boolean) {},
outgoing: function(boolean) {},
Expand All @@ -99,6 +98,11 @@
Cordova.exec(null, null, "TCPlugin", "cancelNotification", []);
}

TwilioPlugin.Connection.prototype.setSpeaker = function(mode) {
// "on" or "off"
Cordova.exec(null, null, "TCPlugin", "setSpeaker", [mode]);
}

TwilioPlugin.Connection.prototype.reject = function() {
Cordova.exec(null,null,"TCPlugin","rejectConnection",[]);
}
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ These are Phonegap plugins that expose the same JS API as Twilio Client for web
</application>
```
- Add tcPlugin.js to your application's www folder (in the assets directory)
- You need to add a notification.png file to your applications res/drawable-ldpi, res/drawable-mdpi & res/drawable-hdpi or res/drawable-xhdpi directories (depending on what resolutions you want to support).
- Copy the two plugin .java files into your application's src folder, keeping the com/phonegap/plugins/twilioclient directory structure
- Last, add the plugin to config.xml (in res/xml)

Expand All @@ -42,6 +43,24 @@ These are Phonegap plugins that expose the same JS API as Twilio Client for web
</feature>
```

## Additional Features

In addition to the standard features of the Twilio Client JS Library, you can also use the included showNotification and cancelNotification functions to display a UILocalNotifcation to the user when during an incoming call while the app is running in the background:

```javascript
Twilio.Connection.showNotification("Notification Text", "notification_sound.wav");
```

```javascript
Twilio.Connection.cancelNotification();
```

You can also turn the device's speaker phone on or off during a call using the following method:

```javascript
Twilio.Connection.setSpeaker("on");
```

## Limitations

This is plugin is a first cut and should be considered alpha. Please use it and break it :) Report any issues using the Github issue tracker.
Expand Down
6 changes: 5 additions & 1 deletion iOS/TCPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
-(void)disconnectAll:(NSArray *)arguments withDict:(NSMutableDictionary *)options;
-(void)acceptConnection:(NSArray *)arguments withDict:(NSMutableDictionary *)options;
-(void)disconnectConnection:(NSArray *)arguments withDict:(NSMutableDictionary *)options;
-(void)rejectConnection:(NSArray *)arguments withDict:(NSMutableDictionary *)options;
-(void)muteConnection:(NSArray *)arguments withDict:(NSMutableDictionary *)options;
-(void)sendDigits:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options;
-(void)sendDigits:(CDVInvokedUrlCommand*)command;
-(void)showNotification:(CDVInvokedUrlCommand*)command;
-(void)cancelNotification:(CDVInvokedUrlCommand*)command;
-(void)setSpeaker:(CDVInvokedUrlCommand*)command;

@end
Loading

0 comments on commit e60d7dd

Please sign in to comment.