diff --git a/telephony/java/android/telephony/PhoneStateListener.java b/telephony/java/android/telephony/PhoneStateListener.java index ae130d4ac34fe..bb2b4478433d9 100644 --- a/telephony/java/android/telephony/PhoneStateListener.java +++ b/telephony/java/android/telephony/PhoneStateListener.java @@ -32,6 +32,7 @@ import android.telephony.PreciseDataConnectionState; import com.android.internal.telephony.IPhoneStateListener; import java.util.List; +import java.lang.ref.WeakReference; /** * A listener class for monitoring changes in specific telephony states @@ -533,84 +534,101 @@ public class PhoneStateListener { /** * The callback methods need to be called on the handler thread where * this object was created. If the binder did that for us it'd be nice. + * + * Using a static class and weak reference here to avoid memory leak caused by the + * IPhoneStateListener.Stub callback retaining references to the outside PhoneStateListeners: + * even caller has been destroyed and "un-registered" the PhoneStateListener, it is still not + * eligible for GC given the references coming from: + * Native Stack --> PhoneStateListener --> Context (Activity). + * memory of caller's context will be collected after GC from service side get triggered */ - IPhoneStateListener callback = new IPhoneStateListener.Stub() { + private static class IPhoneStateListenerStub extends IPhoneStateListener.Stub { + private WeakReference mPhoneStateListenerWeakRef; + + public IPhoneStateListenerStub(PhoneStateListener phoneStateListener) { + mPhoneStateListenerWeakRef = new WeakReference(phoneStateListener); + } + + private void send(int what, int arg1, int arg2, Object obj) { + PhoneStateListener listener = mPhoneStateListenerWeakRef.get(); + if (listener != null) { + Message.obtain(listener.mHandler, what, arg1, arg2, obj).sendToTarget(); + } + } + public void onServiceStateChanged(ServiceState serviceState) { - Message.obtain(mHandler, LISTEN_SERVICE_STATE, 0, 0, serviceState).sendToTarget(); + send(LISTEN_SERVICE_STATE, 0, 0, serviceState); } public void onSignalStrengthChanged(int asu) { - Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTH, asu, 0, null).sendToTarget(); + send(LISTEN_SIGNAL_STRENGTH, asu, 0, null); } public void onMessageWaitingIndicatorChanged(boolean mwi) { - Message.obtain(mHandler, LISTEN_MESSAGE_WAITING_INDICATOR, mwi ? 1 : 0, 0, null) - .sendToTarget(); + send(LISTEN_MESSAGE_WAITING_INDICATOR, mwi ? 1 : 0, 0, null); } public void onCallForwardingIndicatorChanged(boolean cfi) { - Message.obtain(mHandler, LISTEN_CALL_FORWARDING_INDICATOR, cfi ? 1 : 0, 0, null) - .sendToTarget(); + send(LISTEN_CALL_FORWARDING_INDICATOR, cfi ? 1 : 0, 0, null); } public void onCellLocationChanged(Bundle bundle) { CellLocation location = CellLocation.newFromBundle(bundle); - Message.obtain(mHandler, LISTEN_CELL_LOCATION, 0, 0, location).sendToTarget(); + send(LISTEN_CELL_LOCATION, 0, 0, location); } public void onCallStateChanged(int state, String incomingNumber) { - Message.obtain(mHandler, LISTEN_CALL_STATE, state, 0, incomingNumber).sendToTarget(); + send(LISTEN_CALL_STATE, state, 0, incomingNumber); } public void onDataConnectionStateChanged(int state, int networkType) { - Message.obtain(mHandler, LISTEN_DATA_CONNECTION_STATE, state, networkType). - sendToTarget(); + send(LISTEN_DATA_CONNECTION_STATE, state, networkType, null); } public void onDataActivity(int direction) { - Message.obtain(mHandler, LISTEN_DATA_ACTIVITY, direction, 0, null).sendToTarget(); + send(LISTEN_DATA_ACTIVITY, direction, 0, null); } public void onSignalStrengthsChanged(SignalStrength signalStrength) { - Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTHS, 0, 0, signalStrength).sendToTarget(); + send(LISTEN_SIGNAL_STRENGTHS, 0, 0, signalStrength); } public void onOtaspChanged(int otaspMode) { - Message.obtain(mHandler, LISTEN_OTASP_CHANGED, otaspMode, 0).sendToTarget(); + send(LISTEN_OTASP_CHANGED, otaspMode, 0, null); } public void onCellInfoChanged(List cellInfo) { - Message.obtain(mHandler, LISTEN_CELL_INFO, 0, 0, cellInfo).sendToTarget(); + send(LISTEN_CELL_INFO, 0, 0, cellInfo); } public void onPreciseCallStateChanged(PreciseCallState callState) { - Message.obtain(mHandler, LISTEN_PRECISE_CALL_STATE, 0, 0, callState).sendToTarget(); + send(LISTEN_PRECISE_CALL_STATE, 0, 0, callState); } public void onPreciseDataConnectionStateChanged( PreciseDataConnectionState dataConnectionState) { - Message.obtain(mHandler, LISTEN_PRECISE_DATA_CONNECTION_STATE, 0, 0, - dataConnectionState).sendToTarget(); + send(LISTEN_PRECISE_DATA_CONNECTION_STATE, 0, 0, dataConnectionState); } public void onDataConnectionRealTimeInfoChanged( DataConnectionRealTimeInfo dcRtInfo) { - Message.obtain(mHandler, LISTEN_DATA_CONNECTION_REAL_TIME_INFO, 0, 0, - dcRtInfo).sendToTarget(); + send(LISTEN_DATA_CONNECTION_REAL_TIME_INFO, 0, 0, dcRtInfo); } public void onVoLteServiceStateChanged(VoLteServiceState lteState) { - Message.obtain(mHandler, LISTEN_VOLTE_STATE, 0, 0, lteState).sendToTarget(); + send(LISTEN_VOLTE_STATE, 0, 0, lteState); } public void onOemHookRawEvent(byte[] rawData) { - Message.obtain(mHandler, LISTEN_OEM_HOOK_RAW_EVENT, 0, 0, rawData).sendToTarget(); + send(LISTEN_OEM_HOOK_RAW_EVENT, 0, 0, rawData); } public void onCarrierNetworkChange(boolean active) { - Message.obtain(mHandler, LISTEN_CARRIER_NETWORK_CHANGE, 0, 0, active).sendToTarget(); + send(LISTEN_CARRIER_NETWORK_CHANGE, 0, 0, active); } - }; + } + + IPhoneStateListener callback = new IPhoneStateListenerStub(this); private void log(String s) { Rlog.d(LOG_TAG, s);