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

Commit 253ce319 authored by Robert Greenwalt's avatar Robert Greenwalt Committed by Android (Google) Code Review
Browse files

Merge "resolved conflicts for merge of 2d457a32 to master"

parents 409a90cd f333123d
Loading
Loading
Loading
Loading
+6 −20
Original line number Diff line number Diff line
@@ -41,6 +41,12 @@ public interface NetworkStateTracker {
     * -------------------------------------------------------------
     */

    // Share the event space with ConnectivityService (which we can't see, but
    // must send events to).  If you change these, change ConnectivityService
    // too.
    static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
    static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;

    /**
     * The network state has changed and the NetworkInfo object
     * contains the new state.
@@ -62,26 +68,6 @@ public interface NetworkStateTracker {
     */
    public static final int EVENT_RESTORE_DEFAULT_NETWORK = 6;

    /**
     * USED by ConnectivityService only
     *
     * msg.what = EVENT_CLEAR_NET_TRANSITION_WAKELOCK
     * msg.arg1 = mNetTransitionWakeLockSerialNumber
     */
    public static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 7;

    /**
     * msg.arg1 = network type
     * msg.arg2 = condition (0 bad, 100 good)
     */
    public static final int EVENT_INET_CONDITION_CHANGE = 8;

    /**
     * msg.arg1 = network type
     * msg.arg2 = default connection sequence number
     */
    public static final int EVENT_INET_CONDITION_HOLD_END = 9;

    /**
     * -------------------------------------------------------------
     * Control Interface
+210 −96
Original line number Diff line number Diff line
@@ -114,6 +114,71 @@ public class ConnectivityService extends IConnectivityManager.Stub {
    private boolean mTestMode;
    private static ConnectivityService sServiceInstance;

    private static final int ENABLED  = 1;
    private static final int DISABLED = 0;

    // Share the event space with NetworkStateTracker (which can't see this
    // internal class but sends us events).  If you change these, change
    // NetworkStateTracker.java too.
    private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
    private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;

    /**
     * used internally as a delayed event to make us switch back to the
     * default network
     */
    private static final int EVENT_RESTORE_DEFAULT_NETWORK =
            MAX_NETWORK_STATE_TRACKER_EVENT + 1;

    /**
     * used internally to change our mobile data enabled flag
     */
    private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
            MAX_NETWORK_STATE_TRACKER_EVENT + 2;

    /**
     * used internally to change our network preference setting
     * arg1 = networkType to prefer
     */
    private static final int EVENT_SET_NETWORK_PREFERENCE =
            MAX_NETWORK_STATE_TRACKER_EVENT + 3;

    /**
     * used internally to synchronize inet condition reports
     * arg1 = networkType
     * arg2 = condition (0 bad, 100 good)
     */
    private static final int EVENT_INET_CONDITION_CHANGE =
            MAX_NETWORK_STATE_TRACKER_EVENT + 4;

    /**
     * used internally to mark the end of inet condition hold periods
     * arg1 = networkType
     */
    private static final int EVENT_INET_CONDITION_HOLD_END =
            MAX_NETWORK_STATE_TRACKER_EVENT + 5;

    /**
     * used internally to set the background data preference
     * arg1 = TRUE for enabled, FALSE for disabled
     */
    private static final int EVENT_SET_BACKGROUND_DATA =
            MAX_NETWORK_STATE_TRACKER_EVENT + 6;

    /**
     * used internally to set enable/disable cellular data
     * arg1 = ENBALED or DISABLED
     */
    private static final int EVENT_SET_MOBILE_DATA =
            MAX_NETWORK_STATE_TRACKER_EVENT + 7;

    /**
     * used internally to clear a wakelock when transitioning
     * from one net to another
     */
    private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
            MAX_NETWORK_STATE_TRACKER_EVENT + 8;

    private Handler mHandler;

    // list of DeathRecipients used to make sure features are turned off when
@@ -354,28 +419,34 @@ public class ConnectivityService extends IConnectivityManager.Stub {
     * Sets the preferred network.
     * @param preference the new preference
     */
    public synchronized void setNetworkPreference(int preference) {
    public void setNetworkPreference(int preference) {
        enforceChangePermission();

        mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
    }

    public int getNetworkPreference() {
        enforceAccessPermission();
        int preference;
        synchronized(this) {
            preference = mNetworkPreference;
        }
        return preference;
    }

    private void handleSetNetworkPreference(int preference) {
        if (ConnectivityManager.isNetworkTypeValid(preference) &&
                mNetAttributes[preference] != null &&
                mNetAttributes[preference].isDefault()) {
            if (mNetworkPreference != preference) {
                persistNetworkPreference(preference);
                final ContentResolver cr = mContext.getContentResolver();
                Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
                synchronized(this) {
                    mNetworkPreference = preference;
                enforcePreference();
                }
                enforcePreference();
            }
        }

    public int getNetworkPreference() {
        enforceAccessPermission();
        return mNetworkPreference;
    }

    private void persistNetworkPreference(int networkPreference) {
        final ContentResolver cr = mContext.getContentResolver();
        Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
                networkPreference);
    }

    private int getPersistedNetworkPreference() {
@@ -628,8 +699,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
                        mNetRequestersPids[usedNetworkType].add(currentPid);
                    }
                }
                mHandler.sendMessageDelayed(mHandler.obtainMessage(
                        NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
                mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
                        f), getRestoreDefaultNetworkDelay());


@@ -871,16 +941,19 @@ public class ConnectivityService extends IConnectivityManager.Stub {
                android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
                "ConnectivityService");

        if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
        mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
                (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
    }

    private void handleSetBackgroundData(boolean enabled) {
        if (enabled != getBackgroundDataSetting()) {
            Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.BACKGROUND_DATA,
                allowBackgroundDataUsage ? 1 : 0);

                    Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
            Intent broadcast = new Intent(
                    ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
            mContext.sendBroadcast(broadcast);
        }
    }

    /**
     * @see ConnectivityManager#getMobileDataEnabled()
@@ -896,10 +969,15 @@ public class ConnectivityService extends IConnectivityManager.Stub {
    /**
     * @see ConnectivityManager#setMobileDataEnabled(boolean)
     */
    public synchronized void setMobileDataEnabled(boolean enabled) {
    public void setMobileDataEnabled(boolean enabled) {
        enforceChangePermission();
        if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");

        mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
            (enabled ? ENABLED : DISABLED), 0));
    }

    private void handleSetMobileData(boolean enabled) {
        if (getMobileDataEnabled() == enabled) return;

        Settings.Secure.putInt(mContext.getContentResolver(),
@@ -907,7 +985,9 @@ public class ConnectivityService extends IConnectivityManager.Stub {

        if (enabled) {
            if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
                if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
                if (DBG) {
                    Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
                }
                mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
            }
        } else {
@@ -1267,7 +1347,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
                // new network
                if (mNetTransitionWakeLock.isHeld()) {
                    mHandler.sendMessageDelayed(mHandler.obtainMessage(
                            NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
                            EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
                            mNetTransitionWakeLockSerialNumber, 0),
                            1000);
                }
@@ -1705,11 +1785,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
                    type = info.getType();
                    handleDnsConfigurationChange(type);
                    break;
                case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
                    FeatureUser u = (FeatureUser)msg.obj;
                    u.expire();
                    break;
                case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
                case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
                    String causedBy = null;
                    synchronized (ConnectivityService.this) {
                        if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
@@ -1723,70 +1799,42 @@ public class ConnectivityService extends IConnectivityManager.Stub {
                                causedBy + " released by timeout");
                    }
                    break;
                case NetworkStateTracker.EVENT_INET_CONDITION_CHANGE:
                    if (DBG) {
                        Slog.d(TAG, "Inet connectivity change, net=" +
                                msg.arg1 + ", condition=" + msg.arg2 +
                                ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
                    }
                    if (mActiveDefaultNetwork == -1) {
                        if (DBG) Slog.d(TAG, "no active default network - aborting");
                        break;
                    }
                    if (mActiveDefaultNetwork != msg.arg1) {
                        if (DBG) Slog.d(TAG, "given net not default - aborting");
                case EVENT_RESTORE_DEFAULT_NETWORK:
                    FeatureUser u = (FeatureUser)msg.obj;
                    u.expire();
                    break;
                    }
                    mDefaultInetCondition = msg.arg2;
                    int delay;
                    if (mInetConditionChangeInFlight == false) {
                        if (DBG) Slog.d(TAG, "starting a change hold");
                        // setup a new hold to debounce this
                        if (mDefaultInetCondition > 50) {
                            delay = Settings.Secure.getInt(mContext.getContentResolver(),
                                    Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
                        } else {
                            delay = Settings.Secure.getInt(mContext.getContentResolver(),
                                    Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
                        }
                        mInetConditionChangeInFlight = true;
                        sendMessageDelayed(obtainMessage(
                                NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END,
                                mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
                    } else {
                        // we've set the new condition, when this hold ends that will get
                        // picked up
                        if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
                    }
                case EVENT_INET_CONDITION_CHANGE:
                {
                    int netType = msg.arg1;
                    int condition = msg.arg2;
                    handleInetConditionChange(netType, condition);
                    break;
                case NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END:
                    if (DBG) {
                        Slog.d(TAG, "Inet hold end, net=" + msg.arg1 +
                                ", condition =" + mDefaultInetCondition +
                                ", published condition =" + mDefaultInetConditionPublished);
                }
                    mInetConditionChangeInFlight = false;

                    if (mActiveDefaultNetwork == -1) {
                        if (DBG) Slog.d(TAG, "no active default network - aborting");
                case EVENT_INET_CONDITION_HOLD_END:
                {
                    int netType = msg.arg1;
                    int sequence = msg.arg2;
                    handleInetConditionHoldEnd(netType, sequence);
                    break;
                }
                    if (mDefaultConnectionSequence != msg.arg2) {
                        if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
                case EVENT_SET_NETWORK_PREFERENCE:
                {
                    int preference = msg.arg1;
                    handleSetNetworkPreference(preference);
                    break;
                }
                    if (mDefaultInetConditionPublished == mDefaultInetCondition) {
                        if (DBG) Slog.d(TAG, "no change in condition - aborting");
                case EVENT_SET_BACKGROUND_DATA:
                {
                    boolean enabled = (msg.arg1 == ENABLED);
                    handleSetBackgroundData(enabled);
                    break;
                }
                    NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
                    if (networkInfo.isConnected() == false) {
                        if (DBG) Slog.d(TAG, "default network not connected - aborting");
                case EVENT_SET_MOBILE_DATA:
                {
                    boolean enabled = (msg.arg1 == ENABLED);
                    handleSetMobileData(enabled);
                    break;
                }
                    mDefaultInetConditionPublished = mDefaultInetCondition;
                    sendInetConditionBroadcast(networkInfo);
                    break;
            }
        }
    }
@@ -1893,7 +1941,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
            mNetTransitionWakeLockCausedBy = forWhom;
        }
        mHandler.sendMessageDelayed(mHandler.obtainMessage(
                NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
                EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
                mNetTransitionWakeLockSerialNumber, 0),
                mNetTransitionWakeLockTimeout);
        return;
@@ -1918,6 +1966,72 @@ public class ConnectivityService extends IConnectivityManager.Stub {
            }
        }
        mHandler.sendMessage(mHandler.obtainMessage(
            NetworkStateTracker.EVENT_INET_CONDITION_CHANGE, networkType, percentage));
            EVENT_INET_CONDITION_CHANGE, networkType, percentage));
    }

    private void handleInetConditionChange(int netType, int condition) {
        if (DBG) {
            Slog.d(TAG, "Inet connectivity change, net=" +
                    netType + ", condition=" + condition +
                    ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
        }
        if (mActiveDefaultNetwork == -1) {
            if (DBG) Slog.d(TAG, "no active default network - aborting");
            return;
        }
        if (mActiveDefaultNetwork != netType) {
            if (DBG) Slog.d(TAG, "given net not default - aborting");
            return;
        }
        mDefaultInetCondition = condition;
        int delay;
        if (mInetConditionChangeInFlight == false) {
            if (DBG) Slog.d(TAG, "starting a change hold");
            // setup a new hold to debounce this
            if (mDefaultInetCondition > 50) {
                delay = Settings.Secure.getInt(mContext.getContentResolver(),
                        Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
            } else {
                delay = Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
            }
            mInetConditionChangeInFlight = true;
            mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
                    mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
        } else {
            // we've set the new condition, when this hold ends that will get
            // picked up
            if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
        }
    }

    private void handleInetConditionHoldEnd(int netType, int sequence) {
        if (DBG) {
            Slog.d(TAG, "Inet hold end, net=" + netType +
                    ", condition =" + mDefaultInetCondition +
                    ", published condition =" + mDefaultInetConditionPublished);
        }
        mInetConditionChangeInFlight = false;

        if (mActiveDefaultNetwork == -1) {
            if (DBG) Slog.d(TAG, "no active default network - aborting");
            return;
        }
        if (mDefaultConnectionSequence != sequence) {
            if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
            return;
        }
        if (mDefaultInetConditionPublished == mDefaultInetCondition) {
            if (DBG) Slog.d(TAG, "no change in condition - aborting");
            return;
        }
        NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
        if (networkInfo.isConnected() == false) {
            if (DBG) Slog.d(TAG, "default network not connected - aborting");
            return;
        }
        mDefaultInetConditionPublished = mDefaultInetCondition;
        sendInetConditionBroadcast(networkInfo);
        return;
    }
}