diff options
| author | 2018-02-12 19:15:59 -0800 | |
|---|---|---|
| committer | 2018-02-13 11:06:25 -0800 | |
| commit | abbfac26bb88ed3043fce6967e3dc2c1e68a4ad7 (patch) | |
| tree | 61252a7573c73230acdaadaa027394b491dd9140 | |
| parent | 1168c31c4a554d37e405c2f940707de0f91eab57 (diff) | |
Add death recipient to TelephonyRegistry.
Add death recipient to TelephonyRegistry so that all binders that
connect to TelephonyRegistry will be cleaned when they are died.
Test: manual
Bug: 73107019
Change-Id: Ib44f72b769272cef9f36f7b49097091a93bcc70e
| -rw-r--r-- | services/core/java/com/android/server/TelephonyRegistry.java | 105 |
1 files changed, 73 insertions, 32 deletions
diff --git a/services/core/java/com/android/server/TelephonyRegistry.java b/services/core/java/com/android/server/TelephonyRegistry.java index 6747be340d46..5e5eacb5c767 100644 --- a/services/core/java/com/android/server/TelephonyRegistry.java +++ b/services/core/java/com/android/server/TelephonyRegistry.java @@ -64,6 +64,7 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.NoSuchElementException; /** * Since phone process can be restarted, this class provides a centralized place @@ -90,6 +91,8 @@ class TelephonyRegistry extends ITelephonyRegistry.Stub { IBinder binder; + TelephonyRegistryDeathRecipient deathRecipient; + IPhoneStateListener callback; IOnSubscriptionsChangedListener onSubscriptionsChangedListenerCallback; @@ -251,6 +254,21 @@ class TelephonyRegistry extends ITelephonyRegistry.Stub { } }; + private class TelephonyRegistryDeathRecipient implements IBinder.DeathRecipient { + + private final IBinder binder; + + TelephonyRegistryDeathRecipient(IBinder binder) { + this.binder = binder; + } + + @Override + public void binderDied() { + if (DBG) log("binderDied " + binder); + remove(binder); + } + } + private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -378,23 +396,14 @@ class TelephonyRegistry extends ITelephonyRegistry.Stub { } } - Record r; synchronized (mRecords) { // register - find_and_add: { - IBinder b = callback.asBinder(); - final int N = mRecords.size(); - for (int i = 0; i < N; i++) { - r = mRecords.get(i); - if (b == r.binder) { - break find_and_add; - } - } - r = new Record(); - r.binder = b; - mRecords.add(r); - if (DBG) log("listen oscl: add new record"); + IBinder b = callback.asBinder(); + Record r = add(b); + + if (r == null) { + return; } r.onSubscriptionsChangedListenerCallback = callback; @@ -496,20 +505,11 @@ class TelephonyRegistry extends ITelephonyRegistry.Stub { synchronized (mRecords) { // register - Record r; - find_and_add: { - IBinder b = callback.asBinder(); - final int N = mRecords.size(); - for (int i = 0; i < N; i++) { - r = mRecords.get(i); - if (b == r.binder) { - break find_and_add; - } - } - r = new Record(); - r.binder = b; - mRecords.add(r); - if (DBG) log("listen: add new record"); + IBinder b = callback.asBinder(); + Record r = add(b); + + if (r == null) { + return; } r.callback = callback; @@ -697,16 +697,57 @@ class TelephonyRegistry extends ITelephonyRegistry.Stub { return record.canReadPhoneState ? mCallIncomingNumber[phoneId] : ""; } + private Record add(IBinder binder) { + Record r; + + synchronized (mRecords) { + final int N = mRecords.size(); + for (int i = 0; i < N; i++) { + r = mRecords.get(i); + if (binder == r.binder) { + // Already existed. + return r; + } + } + r = new Record(); + r.binder = binder; + r.deathRecipient = new TelephonyRegistryDeathRecipient(binder); + + try { + binder.linkToDeath(r.deathRecipient, 0); + } catch (RemoteException e) { + if (VDBG) log("LinkToDeath remote exception sending to r=" + r + " e=" + e); + // Binder already died. Return null. + return null; + } + + mRecords.add(r); + if (DBG) log("add new record"); + } + + return r; + } + private void remove(IBinder binder) { synchronized (mRecords) { final int recordCount = mRecords.size(); for (int i = 0; i < recordCount; i++) { - if (mRecords.get(i).binder == binder) { + Record r = mRecords.get(i); + if (r.binder == binder) { if (DBG) { - Record r = mRecords.get(i); - log("remove: binder=" + binder + "r.callingPackage" + r.callingPackage - + "r.callback" + r.callback); + log("remove: binder=" + binder + " r.callingPackage " + r.callingPackage + + " r.callback " + r.callback); } + + if (r.deathRecipient != null) { + try { + binder.unlinkToDeath(r.deathRecipient, 0); + } catch (NoSuchElementException e) { + if (VDBG) log("UnlinkToDeath NoSuchElementException sending to r=" + + r + " e=" + e); + } + } + mRecords.remove(i); return; } |