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

Commit f4b6ca7a authored by markchien's avatar markchien Committed by Mark Chien
Browse files

Remove sendMessage inside EntitlementManager

EntitlementManager and its callers(Tethering and UpstreamNetworkMonitor)
run in the same threads.

Bug: 141256482
Test: atest TetheringTests
Change-Id: I0a376d28b123eaab2e8d00a98a4719ce983d3bb2
parent 197e3519
Loading
Loading
Loading
Loading
+11 −81
Original line number Original line Diff line number Diff line
@@ -38,8 +38,6 @@ import android.content.IntentFilter;
import android.net.util.SharedLog;
import android.net.util.SharedLog;
import android.os.Bundle;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Parcel;
import android.os.Parcel;
import android.os.PersistableBundle;
import android.os.PersistableBundle;
import android.os.ResultReceiver;
import android.os.ResultReceiver;
@@ -75,11 +73,6 @@ public class EntitlementManager {


    private final ComponentName mSilentProvisioningService;
    private final ComponentName mSilentProvisioningService;
    private static final int MS_PER_HOUR = 60 * 60 * 1000;
    private static final int MS_PER_HOUR = 60 * 60 * 1000;
    private static final int EVENT_START_PROVISIONING = 0;
    private static final int EVENT_STOP_PROVISIONING = 1;
    private static final int EVENT_UPSTREAM_CHANGED = 2;
    private static final int EVENT_MAYBE_RUN_PROVISIONING = 3;
    private static final int EVENT_GET_ENTITLEMENT_VALUE = 4;


    // The ArraySet contains enabled downstream types, ex:
    // The ArraySet contains enabled downstream types, ex:
    // {@link TetheringManager.TETHERING_WIFI}
    // {@link TetheringManager.TETHERING_WIFI}
@@ -90,7 +83,7 @@ public class EntitlementManager {
    private final int mPermissionChangeMessageCode;
    private final int mPermissionChangeMessageCode;
    private final SharedLog mLog;
    private final SharedLog mLog;
    private final SparseIntArray mEntitlementCacheValue;
    private final SparseIntArray mEntitlementCacheValue;
    private final EntitlementHandler mHandler;
    private final Handler mHandler;
    private final StateMachine mTetherMasterSM;
    private final StateMachine mTetherMasterSM;
    // Key: TetheringManager.TETHERING_*(downstream).
    // Key: TetheringManager.TETHERING_*(downstream).
    // Value: TetheringManager.TETHER_ERROR_{NO_ERROR or PROVISION_FAILED}(provisioning result).
    // Value: TetheringManager.TETHER_ERROR_{NO_ERROR or PROVISION_FAILED}(provisioning result).
@@ -112,10 +105,7 @@ public class EntitlementManager {
        mEntitlementCacheValue = new SparseIntArray();
        mEntitlementCacheValue = new SparseIntArray();
        mTetherMasterSM = tetherMasterSM;
        mTetherMasterSM = tetherMasterSM;
        mPermissionChangeMessageCode = permissionChangeMessageCode;
        mPermissionChangeMessageCode = permissionChangeMessageCode;
        final Handler masterHandler = tetherMasterSM.getHandler();
        mHandler = tetherMasterSM.getHandler();
        // Create entitlement's own handler which is associated with TetherMaster thread
        // let all entitlement processes run in the same thread.
        mHandler = new EntitlementHandler(masterHandler.getLooper());
        mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_PROVISIONING_ALARM),
        mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_PROVISIONING_ALARM),
                null, mHandler);
                null, mHandler);
        mSilentProvisioningService = ComponentName.unflattenFromString(
        mSilentProvisioningService = ComponentName.unflattenFromString(
@@ -172,14 +162,9 @@ public class EntitlementManager {
     *        provisioning app UI if there is one.
     *        provisioning app UI if there is one.
     */
     */
    public void startProvisioningIfNeeded(int downstreamType, boolean showProvisioningUi) {
    public void startProvisioningIfNeeded(int downstreamType, boolean showProvisioningUi) {
        mHandler.sendMessage(mHandler.obtainMessage(EVENT_START_PROVISIONING,
        if (!isValidDownstreamType(downstreamType)) return;
                downstreamType, encodeBool(showProvisioningUi)));
    }

    private void handleStartProvisioningIfNeeded(int type, boolean showProvisioningUi) {
        if (!isValidDownstreamType(type)) return;


        if (!mCurrentTethers.contains(type)) mCurrentTethers.add(type);
        if (!mCurrentTethers.contains(downstreamType)) mCurrentTethers.add(downstreamType);


        final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration();
        final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration();
        if (isTetherProvisioningRequired(config)) {
        if (isTetherProvisioningRequired(config)) {
@@ -192,9 +177,9 @@ public class EntitlementManager {
            // till upstream change to cellular.
            // till upstream change to cellular.
            if (mUsingCellularAsUpstream) {
            if (mUsingCellularAsUpstream) {
                if (showProvisioningUi) {
                if (showProvisioningUi) {
                    runUiTetherProvisioning(type, config.activeDataSubId);
                    runUiTetherProvisioning(downstreamType, config.activeDataSubId);
                } else {
                } else {
                    runSilentTetherProvisioning(type, config.activeDataSubId);
                    runSilentTetherProvisioning(downstreamType, config.activeDataSubId);
                }
                }
                mNeedReRunProvisioningUi = false;
                mNeedReRunProvisioningUi = false;
            } else {
            } else {
@@ -211,10 +196,6 @@ public class EntitlementManager {
     * @param type tethering type from TetheringManager.TETHERING_{@code *}
     * @param type tethering type from TetheringManager.TETHERING_{@code *}
     */
     */
    public void stopProvisioningIfNeeded(int type) {
    public void stopProvisioningIfNeeded(int type) {
        mHandler.sendMessage(mHandler.obtainMessage(EVENT_STOP_PROVISIONING, type, 0));
    }

    private void handleStopProvisioningIfNeeded(int type) {
        if (!isValidDownstreamType(type)) return;
        if (!isValidDownstreamType(type)) return;


        mCurrentTethers.remove(type);
        mCurrentTethers.remove(type);
@@ -230,11 +211,6 @@ public class EntitlementManager {
     * @param isCellular whether tethering upstream is cellular.
     * @param isCellular whether tethering upstream is cellular.
     */
     */
    public void notifyUpstream(boolean isCellular) {
    public void notifyUpstream(boolean isCellular) {
        mHandler.sendMessage(mHandler.obtainMessage(
                EVENT_UPSTREAM_CHANGED, encodeBool(isCellular), 0));
    }

    private void handleNotifyUpstream(boolean isCellular) {
        if (DBG) {
        if (DBG) {
            mLog.i("notifyUpstream: " + isCellular
            mLog.i("notifyUpstream: " + isCellular
                    + ", mCellularUpstreamPermitted: " + mCellularUpstreamPermitted
                    + ", mCellularUpstreamPermitted: " + mCellularUpstreamPermitted
@@ -244,16 +220,17 @@ public class EntitlementManager {


        if (mUsingCellularAsUpstream) {
        if (mUsingCellularAsUpstream) {
            final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration();
            final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration();
            handleMaybeRunProvisioning(config);
            maybeRunProvisioning(config);
        }
        }
    }
    }


    /** Run provisioning if needed */
    /** Run provisioning if needed */
    public void maybeRunProvisioning() {
    public void maybeRunProvisioning() {
        mHandler.sendMessage(mHandler.obtainMessage(EVENT_MAYBE_RUN_PROVISIONING));
        final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration();
        maybeRunProvisioning(config);
    }
    }


    private void handleMaybeRunProvisioning(final TetheringConfiguration config) {
    private void maybeRunProvisioning(final TetheringConfiguration config) {
        if (mCurrentTethers.size() == 0 || !isTetherProvisioningRequired(config)) {
        if (mCurrentTethers.size() == 0 || !isTetherProvisioningRequired(config)) {
            return;
            return;
        }
        }
@@ -319,7 +296,7 @@ public class EntitlementManager {
        }
        }


        if (mUsingCellularAsUpstream) {
        if (mUsingCellularAsUpstream) {
            handleMaybeRunProvisioning(config);
            maybeRunProvisioning(config);
        }
        }
    }
    }


@@ -494,46 +471,6 @@ public class EntitlementManager {
        }
        }
    };
    };


    private class EntitlementHandler extends Handler {
        EntitlementHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EVENT_START_PROVISIONING:
                    handleStartProvisioningIfNeeded(msg.arg1, toBool(msg.arg2));
                    break;
                case EVENT_STOP_PROVISIONING:
                    handleStopProvisioningIfNeeded(msg.arg1);
                    break;
                case EVENT_UPSTREAM_CHANGED:
                    handleNotifyUpstream(toBool(msg.arg1));
                    break;
                case EVENT_MAYBE_RUN_PROVISIONING:
                    final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration();
                    handleMaybeRunProvisioning(config);
                    break;
                case EVENT_GET_ENTITLEMENT_VALUE:
                    handleRequestLatestTetheringEntitlementValue(msg.arg1,
                            (ResultReceiver) msg.obj, toBool(msg.arg2));
                    break;
                default:
                    mLog.log("Unknown event: " + msg.what);
                    break;
            }
        }
    }

    private static boolean toBool(int encodedBoolean) {
        return encodedBoolean != 0;
    }

    private static int encodeBool(boolean b) {
        return b ? 1 : 0;
    }

    private static boolean isValidDownstreamType(int type) {
    private static boolean isValidDownstreamType(int type) {
        switch (type) {
        switch (type) {
            case TETHERING_BLUETOOTH:
            case TETHERING_BLUETOOTH:
@@ -644,13 +581,6 @@ public class EntitlementManager {
    /** Get the last value of the tethering entitlement check. */
    /** Get the last value of the tethering entitlement check. */
    public void requestLatestTetheringEntitlementResult(int downstream, ResultReceiver receiver,
    public void requestLatestTetheringEntitlementResult(int downstream, ResultReceiver receiver,
            boolean showEntitlementUi) {
            boolean showEntitlementUi) {
        mHandler.sendMessage(mHandler.obtainMessage(EVENT_GET_ENTITLEMENT_VALUE,
                downstream, encodeBool(showEntitlementUi), receiver));

    }

    private void handleRequestLatestTetheringEntitlementValue(int downstream,
            ResultReceiver receiver, boolean showEntitlementUi) {
        if (!isValidDownstreamType(downstream)) {
        if (!isValidDownstreamType(downstream)) {
            receiver.send(TETHER_ERROR_ENTITLEMENT_UNKNOWN, null);
            receiver.send(TETHER_ERROR_ENTITLEMENT_UNKNOWN, null);
            return;
            return;
+4 −2
Original line number Original line Diff line number Diff line
@@ -1946,10 +1946,12 @@ public class Tethering {
    /** Get the latest value of the tethering entitlement check. */
    /** Get the latest value of the tethering entitlement check. */
    void requestLatestTetheringEntitlementResult(int type, ResultReceiver receiver,
    void requestLatestTetheringEntitlementResult(int type, ResultReceiver receiver,
            boolean showEntitlementUi) {
            boolean showEntitlementUi) {
        if (receiver != null) {
        if (receiver == null) return;

        mHandler.post(() -> {
            mEntitlementMgr.requestLatestTetheringEntitlementResult(type, receiver,
            mEntitlementMgr.requestLatestTetheringEntitlementResult(type, receiver,
                    showEntitlementUi);
                    showEntitlementUi);
        }
        });
    }
    }


    /** Register tethering event callback */
    /** Register tethering event callback */