Merge \"Handle Concurrency issues in Connection\" into nyc-dev

am: df41725e53

Change-Id: I0332df9dc96086a3094dc4ab53573b5cf57f8730
This commit is contained in:
Brad Ebinger
2016-06-15 22:48:06 +00:00
committed by android-build-merger
2 changed files with 85 additions and 52 deletions

View File

@@ -1243,6 +1243,7 @@ public abstract class Connection extends Conferenceable {
private Conference mConference;
private ConnectionService mConnectionService;
private Bundle mExtras;
private final Object mExtrasLock = new Object();
/**
* Tracks the key set for the extras bundle provided on the last invocation of
@@ -1401,7 +1402,13 @@ public abstract class Connection extends Conferenceable {
* @return The extras associated with this connection.
*/
public final Bundle getExtras() {
return mExtras;
Bundle extras = null;
synchronized (mExtrasLock) {
if (mExtras != null) {
extras = new Bundle(mExtras);
}
}
return extras;
}
/**
@@ -1936,14 +1943,20 @@ public abstract class Connection extends Conferenceable {
if (extras == null) {
return;
}
if (mExtras == null) {
mExtras = new Bundle();
// Creating a duplicate bundle so we don't have to synchronize on mExtrasLock while calling
// the listeners.
Bundle listenerExtras;
synchronized (mExtrasLock) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
listenerExtras = new Bundle(mExtras);
}
mExtras.putAll(extras);
for (Listener l : mListeners) {
l.onExtrasChanged(this, extras);
// Create a new clone of the extras for each listener so that they don't clobber
// each other
l.onExtrasChanged(this, new Bundle(listenerExtras));
}
}
@@ -1992,18 +2005,16 @@ public abstract class Connection extends Conferenceable {
* @param keys The keys of the extras to remove.
*/
public final void removeExtras(List<String> keys) {
if (mExtras != null) {
for (String key : keys) {
mExtras.remove(key);
}
if (mExtras.size() == 0) {
mExtras = null;
synchronized (mExtrasLock) {
if (mExtras != null) {
for (String key : keys) {
mExtras.remove(key);
}
}
}
List<String> unmodifiableKeys = Collections.unmodifiableList(keys);
for (Listener l : mListeners) {
l.onExtrasRemoved(this, keys);
l.onExtrasRemoved(this, unmodifiableKeys);
}
}
@@ -2291,8 +2302,14 @@ public abstract class Connection extends Conferenceable {
* @hide
*/
final void handleExtrasChanged(Bundle extras) {
mExtras = extras;
onExtrasChanged(mExtras);
Bundle b = null;
synchronized (mExtrasLock) {
mExtras = extras;
if (mExtras != null) {
b = new Bundle(mExtras);
}
}
onExtrasChanged(b);
}
/**