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

Commit 44d22ba5 authored by David van Tonder's avatar David van Tonder Committed by Gerrit Code Review
Browse files

Merge "[1/2] Add triggers to change profile based on AP" into cm-10.1

parents 7a300d21 9037eadd
Loading
Loading
Loading
Loading
+62 −2
Original line number Diff line number Diff line
@@ -67,6 +67,8 @@ public final class Profile implements Parcelable, Comparable {

    private Map<Integer, StreamSettings> streams = new HashMap<Integer, StreamSettings>();

    private Map<String, Integer> mWifiTriggers = new HashMap<String, Integer>();

    private Map<Integer, ConnectionSettings> connections = new HashMap<Integer, ConnectionSettings>();

    private RingModeSettings mRingMode = new RingModeSettings();
@@ -82,6 +84,13 @@ public final class Profile implements Parcelable, Comparable {
        public static final int DISABLE = 2;
    }

    /** @hide */
    public static class TriggerState {
        public static final int ON_CONNECT = 0;
        public static final int ON_DISCONNECT = 1;
        public static final int DISABLED = 2;
    }

    /** @hide */
    public static final Parcelable.Creator<Profile> CREATOR = new Parcelable.Creator<Profile>() {
        public Profile createFromParcel(Parcel in) {
@@ -111,8 +120,26 @@ public final class Profile implements Parcelable, Comparable {
        readFromParcel(in);
    }

    public int compareTo(Object obj)
    {
    public int getWifiTrigger(String ssid) {
        if (ssid != null && mWifiTriggers.containsKey(ssid)) {
            return mWifiTriggers.get(ssid);
        }
        return TriggerState.DISABLED;
    }

    public void setWifiTrigger(String ssid, int value) {
        if (ssid == null || value < TriggerState.ON_CONNECT || value > TriggerState.DISABLED) {
            return;
        }
        if (value == TriggerState.DISABLED && mWifiTriggers.containsKey(ssid)) {
            mWifiTriggers.remove(ssid);
        } else {
            mWifiTriggers.put(ssid, value);
        }
        mDirty = true;
    }

    public int compareTo(Object obj) {
        Profile tmp = (Profile) obj;
        if (mName.compareTo(tmp.mName) < 0) {
            return -1;
@@ -185,6 +212,7 @@ public final class Profile implements Parcelable, Comparable {
        dest.writeParcelable(mRingMode, flags);
        dest.writeParcelable(mAirplaneMode, flags);
        dest.writeInt(mScreenLockMode);
        dest.writeMap(mWifiTriggers);
    }

    /** @hide */
@@ -217,6 +245,7 @@ public final class Profile implements Parcelable, Comparable {
        mRingMode = (RingModeSettings) in.readParcelable(null);
        mAirplaneMode = (AirplaneModeSettings) in.readParcelable(null);
        mScreenLockMode = in.readInt();
        in.readMap(mWifiTriggers, null);
    }

    public String getName() {
@@ -401,6 +430,18 @@ public final class Profile implements Parcelable, Comparable {
        for (ConnectionSettings cs : connections.values()) {
            cs.getXmlString(builder, context);
        }
        if (!mWifiTriggers.isEmpty()) {
            builder.append("<triggers>\n");
            for (Map.Entry<String,Integer> e : mWifiTriggers.entrySet()) {
                builder.append("<wifiAP ssid=\"");
                builder.append(e.getKey());
                builder.append("\" state=\"");
                builder.append(e.getValue());
                builder.append("\"></wifiAP>\n");
            }
            builder.append("</triggers>\n");
        }

        builder.append("</profile>\n");
        mDirty = false;
    }
@@ -428,6 +469,22 @@ public final class Profile implements Parcelable, Comparable {
        return uuids;
    }

    private static HashMap<String, Integer> readWifiTriggersFromXml(XmlPullParser xpp, Context context)
            throws XmlPullParserException,
            IOException {
        int event = xpp.next();
        HashMap<String, Integer> triggers = new HashMap<String, Integer>();
        while (event != XmlPullParser.END_TAG || xpp.getName().equals("wifiAP")) {
            if (event == XmlPullParser.START_TAG){
                String ssid = xpp.getAttributeValue(null, "ssid");
                String state = xpp.getAttributeValue(null, "state");
                triggers.put(ssid, Integer.valueOf(state));
            }
            event = xpp.next();
        }
        return triggers;
    }

    /** @hide */
    public static Profile fromXml(XmlPullParser xpp, Context context)
            throws XmlPullParserException, IOException {
@@ -502,6 +559,9 @@ public final class Profile implements Parcelable, Comparable {
                    ConnectionSettings cs = ConnectionSettings.fromXml(xpp, context);
                    profile.connections.put(cs.getConnectionId(), cs);
                }
                if (name.equals("triggers")) {
                    profile.mWifiTriggers = readWifiTriggersFromXml(xpp, context);
                }
            }
            event = xpp.next();
        }
+36 −0
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.XmlResourceParser;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiManager;
import android.os.RemoteException;
import android.os.UserHandle;
import android.text.TextUtils;
@@ -86,6 +88,9 @@ public class ProfileManagerService extends IProfileManager.Stub {
    private Context mContext;
    private boolean mDirty;

    private WifiManager mWifiManager;
    private String mlastConnectedSSID;

    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -96,12 +101,38 @@ public class ProfileManagerService extends IProfileManager.Stub {
                initialize();
            } else if (action.equals(Intent.ACTION_SHUTDOWN)) {
                persistIfDirty();

            } else if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
                SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
                int triggerState;
                switch (state) {
                    case COMPLETED:
                        triggerState = Profile.TriggerState.ON_CONNECT;
                        mlastConnectedSSID = getActiveSSID();
                        break;
                    case DISCONNECTED:
                        triggerState = Profile.TriggerState.ON_DISCONNECT;
                        break;
                    default:
                        return;
                }
                for (Profile p : mProfiles.values()) {
                    if (triggerState ==  p.getWifiTrigger(mlastConnectedSSID)) {
                        try {
                            setActiveProfile(p, true);
                        } catch (RemoteException e) {
                            Log.e(TAG, "Could not update profile on wifi AP change", e);
                        }
                    }
                }
            }
        }
    };

    public ProfileManagerService(Context context) {
        mContext = context;
        mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
        mlastConnectedSSID = getActiveSSID();

        mWildcardGroup = new NotificationGroup(
                context.getString(com.android.internal.R.string.wildcardProfile),
@@ -113,6 +144,7 @@ public class ProfileManagerService extends IProfileManager.Stub {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
        filter.addAction(Intent.ACTION_SHUTDOWN);
        filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        mContext.registerReceiver(mIntentReceiver, filter);
    }

@@ -149,6 +181,10 @@ public class ProfileManagerService extends IProfileManager.Stub {
        }
    }

    private String getActiveSSID() {
        return mWifiManager.getConnectionInfo().getSSID().replace("\"", "");
    }

    @Override
    public void resetAll() {
        enforceChangePermissions();