Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2dadfbd4 authored by fionaxu's avatar fionaxu Committed by android-build-merger
Browse files

Merge " fix Memory Leak caused by PhoneStateListener" into nyc-dev

am: 3c11a5b3

* commit '3c11a5b3':
  fix Memory Leak caused by PhoneStateListener
parents 195ed1e6 3c11a5b3
Loading
Loading
Loading
Loading
+43 −25
Original line number Original line Diff line number Diff line
@@ -32,6 +32,7 @@ import android.telephony.PreciseDataConnectionState;


import com.android.internal.telephony.IPhoneStateListener;
import com.android.internal.telephony.IPhoneStateListener;
import java.util.List;
import java.util.List;
import java.lang.ref.WeakReference;


/**
/**
 * A listener class for monitoring changes in specific telephony states
 * 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
     * 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.
     * 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<PhoneStateListener> mPhoneStateListenerWeakRef;

        public IPhoneStateListenerStub(PhoneStateListener phoneStateListener) {
            mPhoneStateListenerWeakRef = new WeakReference<PhoneStateListener>(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) {
        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) {
        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) {
        public void onMessageWaitingIndicatorChanged(boolean mwi) {
            Message.obtain(mHandler, LISTEN_MESSAGE_WAITING_INDICATOR, mwi ? 1 : 0, 0, null)
            send(LISTEN_MESSAGE_WAITING_INDICATOR, mwi ? 1 : 0, 0, null);
                    .sendToTarget();
        }
        }


        public void onCallForwardingIndicatorChanged(boolean cfi) {
        public void onCallForwardingIndicatorChanged(boolean cfi) {
            Message.obtain(mHandler, LISTEN_CALL_FORWARDING_INDICATOR, cfi ? 1 : 0, 0, null)
            send(LISTEN_CALL_FORWARDING_INDICATOR, cfi ? 1 : 0, 0, null);
                    .sendToTarget();
        }
        }


        public void onCellLocationChanged(Bundle bundle) {
        public void onCellLocationChanged(Bundle bundle) {
            CellLocation location = CellLocation.newFromBundle(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) {
        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) {
        public void onDataConnectionStateChanged(int state, int networkType) {
            Message.obtain(mHandler, LISTEN_DATA_CONNECTION_STATE, state, networkType).
            send(LISTEN_DATA_CONNECTION_STATE, state, networkType, null);
                    sendToTarget();
        }
        }


        public void onDataActivity(int direction) {
        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) {
        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) {
        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> cellInfo) {
        public void onCellInfoChanged(List<CellInfo> cellInfo) {
            Message.obtain(mHandler, LISTEN_CELL_INFO, 0, 0, cellInfo).sendToTarget();
            send(LISTEN_CELL_INFO, 0, 0, cellInfo);
        }
        }


        public void onPreciseCallStateChanged(PreciseCallState callState) {
        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(
        public void onPreciseDataConnectionStateChanged(
                PreciseDataConnectionState dataConnectionState) {
                PreciseDataConnectionState dataConnectionState) {
            Message.obtain(mHandler, LISTEN_PRECISE_DATA_CONNECTION_STATE, 0, 0,
            send(LISTEN_PRECISE_DATA_CONNECTION_STATE, 0, 0, dataConnectionState);
                    dataConnectionState).sendToTarget();
        }
        }


        public void onDataConnectionRealTimeInfoChanged(
        public void onDataConnectionRealTimeInfoChanged(
                DataConnectionRealTimeInfo dcRtInfo) {
                DataConnectionRealTimeInfo dcRtInfo) {
            Message.obtain(mHandler, LISTEN_DATA_CONNECTION_REAL_TIME_INFO, 0, 0,
            send(LISTEN_DATA_CONNECTION_REAL_TIME_INFO, 0, 0, dcRtInfo);
                    dcRtInfo).sendToTarget();
        }
        }


        public void onVoLteServiceStateChanged(VoLteServiceState lteState) {
        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) {
        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) {
        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) {
    private void log(String s) {
        Rlog.d(LOG_TAG, s);
        Rlog.d(LOG_TAG, s);