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

Commit a2a1b911 authored by Irfan Sheriff's avatar Irfan Sheriff
Browse files

Refactor WifiStateTracker

Implement WifiStateTracker as a HSM.

Change-Id: Ic12fd78f1f183b5c4dea8ad2301002267ceff0cb
parent 7d72e5ad
Loading
Loading
Loading
Loading
+83 −43
Original line number Diff line number Diff line
@@ -144,8 +144,10 @@ public class NetworkInfo implements Parcelable {
     * @return the network type
     */
    public int getType() {
        synchronized (this) {
            return mNetworkType;
        }
    }

    /**
     * Return a network-type-specific integer describing the subtype
@@ -153,13 +155,17 @@ public class NetworkInfo implements Parcelable {
     * @return the network subtype
     */
    public int getSubtype() {
        synchronized (this) {
            return mSubtype;
        }
    }

    void setSubtype(int subtype, String subtypeName) {
        synchronized (this) {
            mSubtype = subtype;
            mSubtypeName = subtypeName;
        }
    }

    /**
     * Return a human-readable name describe the type of the network,
@@ -167,16 +173,20 @@ public class NetworkInfo implements Parcelable {
     * @return the name of the network type
     */
    public String getTypeName() {
        synchronized (this) {
            return mTypeName;
        }
    }

    /**
     * Return a human-readable name describing the subtype of the network.
     * @return the name of the network subtype
     */
    public String getSubtypeName() {
        synchronized (this) {
            return mSubtypeName;
        }
    }

    /**
     * Indicates whether network connectivity exists or is in the process
@@ -188,8 +198,10 @@ public class NetworkInfo implements Parcelable {
     * of being established, {@code false} otherwise.
     */
    public boolean isConnectedOrConnecting() {
        synchronized (this) {
            return mState == State.CONNECTED || mState == State.CONNECTING;
        }
    }

    /**
     * Indicates whether network connectivity exists and it is possible to establish
@@ -197,8 +209,10 @@ public class NetworkInfo implements Parcelable {
     * @return {@code true} if network connectivity exists, {@code false} otherwise.
     */
    public boolean isConnected() {
        synchronized (this) {
            return mState == State.CONNECTED;
        }
    }

    /**
     * Indicates whether network connectivity is possible. A network is unavailable
@@ -213,8 +227,10 @@ public class NetworkInfo implements Parcelable {
     * @return {@code true} if the network is available, {@code false} otherwise
     */
    public boolean isAvailable() {
        synchronized (this) {
            return mIsAvailable;
        }
    }

    /**
     * Sets if the network is available, ie, if the connectivity is possible.
@@ -223,8 +239,10 @@ public class NetworkInfo implements Parcelable {
     * @hide
     */
    public void setIsAvailable(boolean isAvailable) {
        synchronized (this) {
            mIsAvailable = isAvailable;
        }
    }

    /**
     * Indicates whether the current attempt to connect to the network
@@ -234,8 +252,10 @@ public class NetworkInfo implements Parcelable {
     * otherwise.
     */
    public boolean isFailover() {
        synchronized (this) {
            return mIsFailover;
        }
    }

    /**
     * Set the failover boolean.
@@ -244,8 +264,10 @@ public class NetworkInfo implements Parcelable {
     * @hide
     */
    public void setFailover(boolean isFailover) {
        synchronized (this) {
            mIsFailover = isFailover;
        }
    }

    /**
     * Indicates whether the device is currently roaming on this network.
@@ -254,28 +276,36 @@ public class NetworkInfo implements Parcelable {
     * @return {@code true} if roaming is in effect, {@code false} otherwise.
     */
    public boolean isRoaming() {
        synchronized (this) {
            return mIsRoaming;
        }
    }

    void setRoaming(boolean isRoaming) {
        synchronized (this) {
            mIsRoaming = isRoaming;
        }
    }

    /**
     * Reports the current coarse-grained state of the network.
     * @return the coarse-grained state
     */
    public State getState() {
        synchronized (this) {
            return mState;
        }
    }

    /**
     * Reports the current fine-grained state of the network.
     * @return the fine-grained state
     */
    public DetailedState getDetailedState() {
        synchronized (this) {
            return mDetailedState;
        }
    }

    /**
     * Sets the fine-grained state of the network.
@@ -287,11 +317,13 @@ public class NetworkInfo implements Parcelable {
     * @hide
     */
    public void setDetailedState(DetailedState detailedState, String reason, String extraInfo) {
        synchronized (this) {
            this.mDetailedState = detailedState;
            this.mState = stateMap.get(detailedState);
            this.mReason = reason;
            this.mExtraInfo = extraInfo;
        }
    }

    /**
     * Report the reason an attempt to establish connectivity failed,
@@ -299,8 +331,10 @@ public class NetworkInfo implements Parcelable {
     * @return the reason for failure, or null if not available
     */
    public String getReason() {
        synchronized (this) {
            return mReason;
        }
    }

    /**
     * Report the extra information about the network state, if any was
@@ -309,11 +343,14 @@ public class NetworkInfo implements Parcelable {
     * @return the extra information, or null if not available
     */
    public String getExtraInfo() {
        synchronized (this) {
            return mExtraInfo;
        }
    }

    @Override
    public String toString() {
        synchronized (this) {
            StringBuilder builder = new StringBuilder("NetworkInfo: ");
            builder.append("type: ").append(getTypeName()).append("[").append(getSubtypeName()).
            append("], state: ").append(mState).append("/").append(mDetailedState).
@@ -324,6 +361,7 @@ public class NetworkInfo implements Parcelable {
            append(", isAvailable: ").append(mIsAvailable);
            return builder.toString();
        }
    }

    /**
     * Implement the Parcelable interface
@@ -338,6 +376,7 @@ public class NetworkInfo implements Parcelable {
     * @hide
     */
    public void writeToParcel(Parcel dest, int flags) {
        synchronized (this) {
            dest.writeInt(mNetworkType);
            dest.writeInt(mSubtype);
            dest.writeString(mTypeName);
@@ -350,6 +389,7 @@ public class NetworkInfo implements Parcelable {
            dest.writeString(mReason);
            dest.writeString(mExtraInfo);
        }
    }

    /**
     * Implement the Parcelable interface.
+6 −0
Original line number Diff line number Diff line
@@ -98,6 +98,11 @@ static jstring doStringCommand(JNIEnv *env, const char *cmd)
    }
}

static jboolean android_net_wifi_isDriverLoaded(JNIEnv* env, jobject clazz)
{
    return (jboolean)(::is_wifi_driver_loaded() == 1);
}

static jboolean android_net_wifi_loadDriver(JNIEnv* env, jobject clazz)
{
    return (jboolean)(::wifi_load_driver() == 0);
@@ -524,6 +529,7 @@ static JNINativeMethod gWifiMethods[] = {
    /* name, signature, funcPtr */

    { "loadDriver", "()Z",  (void *)android_net_wifi_loadDriver },
    { "isDriverLoaded", "()Z",  (void *)android_net_wifi_isDriverLoaded},
    { "unloadDriver", "()Z",  (void *)android_net_wifi_unloadDriver },
    { "startSupplicant", "()Z",  (void *)android_net_wifi_startSupplicant },
    { "stopSupplicant", "()Z",  (void *)android_net_wifi_stopSupplicant },
+5 −0
Original line number Diff line number Diff line
@@ -88,6 +88,8 @@ public class ConnectivityService extends IConnectivityManager.Stub {
     */
    private List mNetRequestersPids[];

    private WifiWatchdogService mWifiWatchdogService;

    // priority order of the nettrackers
    // (excluding dynamically set mNetworkPreference)
    // TODO - move mNetworkTypePreference into this
@@ -318,6 +320,9 @@ public class ConnectivityService extends IConnectivityManager.Stub {
                mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
                wst.startMonitoring();

                //TODO: as part of WWS refactor, create only when needed
                mWifiWatchdogService = new WifiWatchdogService(context, wst);

                break;
            case ConnectivityManager.TYPE_MOBILE:
                mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
+140 −1264

File changed.

Preview size limit exceeded, changes collapsed.

+11 −16
Original line number Diff line number Diff line
@@ -275,12 +275,13 @@ public class WifiWatchdogService {
    /**
     * Unregister broadcasts and quit the watchdog thread
     */
    private void quit() {
        unregisterForWifiBroadcasts();
        mContext.getContentResolver().unregisterContentObserver(mContentObserver);
        mHandler.removeAllActions();
        mHandler.getLooper().quit();
    }
    //TODO: Change back to running WWS when needed
//    private void quit() {
//        unregisterForWifiBroadcasts();
//        mContext.getContentResolver().unregisterContentObserver(mContentObserver);
//        mHandler.removeAllActions();
//        mHandler.getLooper().quit();
//    }

    /**
     * Waits for the main watchdog thread to create the handler.
@@ -751,7 +752,7 @@ public class WifiWatchdogService {
        // Black list this "bad" AP, this will cause an attempt to connect to another
        blacklistAp(ap.bssid);
        // Initiate an association to an alternate AP
        mWifiStateTracker.reassociate();
        mWifiStateTracker.reassociateCommand();
    }

    private void blacklistAp(String bssid) {
@@ -762,10 +763,7 @@ public class WifiWatchdogService {
        // Before taking action, make sure we should not cancel our processing
        if (shouldCancel()) return;
        
        if (!mWifiStateTracker.addToBlacklist(bssid)) {
            // There's a known bug where this method returns failure on success
            //Slog.e(TAG, "Blacklisting " + bssid + " failed");
        }
        mWifiStateTracker.addToBlacklist(bssid);

        if (D) {
            myLogD("Blacklisting " + bssid);
@@ -860,10 +858,7 @@ public class WifiWatchdogService {
             * (and blacklisted them). Clear the blacklist so the AP with best
             * signal is chosen.
             */
            if (!mWifiStateTracker.clearBlacklist()) {
                // There's a known bug where this method returns failure on success
                //Slog.e(TAG, "Clearing blacklist failed");
            }
            mWifiStateTracker.clearBlacklist();
            
            if (V) {
                myLogV("handleSleep: Set state to SLEEP and cleared blacklist");
@@ -1151,7 +1146,7 @@ public class WifiWatchdogService {

        private void handleWifiStateChanged(int wifiState) {
            if (wifiState == WifiManager.WIFI_STATE_DISABLED) {
                quit();
                onDisconnected();
            } else if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
                onEnabled();
            }
Loading