diff --git a/cm/lib/main/java/org/cyanogenmod/platform/internal/ProfileManagerService.java b/cm/lib/main/java/org/cyanogenmod/platform/internal/ProfileManagerService.java index e969564..9c6d1b1 100644 --- a/cm/lib/main/java/org/cyanogenmod/platform/internal/ProfileManagerService.java +++ b/cm/lib/main/java/org/cyanogenmod/platform/internal/ProfileManagerService.java @@ -16,11 +16,17 @@ package org.cyanogenmod.platform.internal; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; import android.content.ComponentName; import android.content.ServiceConnection; import android.database.ContentObserver; import android.net.Uri; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; +import android.net.wifi.WifiSsid; import android.os.Message; +import android.util.ArraySet; import com.android.internal.policy.IKeyguardService; import cyanogenmod.providers.CMSettings; import org.xmlpull.v1.XmlPullParser; @@ -58,7 +64,9 @@ import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; /** @hide */ @@ -144,13 +152,69 @@ public class ProfileManagerService extends CMSystemService { mContext.sendBroadcastAsUser(newState, UserHandle.ALL); - + if (ProfileManager.PROFILES_STATE_ENABLED == msg.arg1) { + maybeApplyActiveProfile(); + } return true; } return false; } }; + private void maybeApplyActiveProfile() { + final List wiFiTriggers + = mActiveProfile.getTriggersFromType(Profile.TriggerType.WIFI); + final List blueToothTriggers + = mActiveProfile.getTriggersFromType(Profile.TriggerType.BLUETOOTH); + + boolean selectProfile = false; + if (wiFiTriggers.size() == 0 && blueToothTriggers.size() == 0) { + selectProfile = true; + } else { + final String activeSSID = getActiveSSID(); + if (activeSSID != null) { + for (Profile.ProfileTrigger trigger : wiFiTriggers) { + if (trigger.getState() == Profile.TriggerState.ON_CONNECT + && trigger.getId().equals(activeSSID)) { + selectProfile = true; + break; + } + } + } + if (!selectProfile && blueToothTriggers.size() > 0) { + final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + final Set pairedDevices = mBluetoothAdapter.getBondedDevices(); + final Set connectedBTDevices = new ArraySet<>(); + for (BluetoothDevice device : pairedDevices) { + if (device.isConnected()) connectedBTDevices.add(device.getAddress()); + } + for (Profile.ProfileTrigger trigger : blueToothTriggers) { + if (connectedBTDevices.contains(trigger.getId()) + && trigger.getState() == Profile.TriggerState.ON_CONNECT) { + selectProfile = true; + break; + } + } + } + } + + if (selectProfile) mActiveProfile.doSelect(mContext, mKeyguardService); + } + + private String getActiveSSID() { + final WifiManager wifiManager + = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); + final WifiInfo wifiinfo = wifiManager.getConnectionInfo(); + if (wifiinfo == null) { + return null; + } + final WifiSsid ssid = wifiinfo.getWifiSsid(); + if (ssid == null) { + return null; + } + return ssid.toString(); + } + private class ProfilesObserver extends ContentObserver { public ProfilesObserver(Handler handler) { super(handler); diff --git a/cm/lib/main/java/org/cyanogenmod/platform/internal/ProfileTriggerHelper.java b/cm/lib/main/java/org/cyanogenmod/platform/internal/ProfileTriggerHelper.java index 48110af..139307d 100644 --- a/cm/lib/main/java/org/cyanogenmod/platform/internal/ProfileTriggerHelper.java +++ b/cm/lib/main/java/org/cyanogenmod/platform/internal/ProfileTriggerHelper.java @@ -16,13 +16,13 @@ package org.cyanogenmod.platform.internal; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.ContentObserver; -import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.WifiSsid; @@ -30,11 +30,14 @@ import android.os.Bundle; import android.os.Handler; import android.os.UserHandle; import android.text.TextUtils; +import android.util.ArraySet; import android.util.Log; import cyanogenmod.app.Profile; +import cyanogenmod.app.Profile.ProfileTrigger; import cyanogenmod.app.ProfileManager; import cyanogenmod.providers.CMSettings; +import java.util.Set; import java.util.UUID; /** @@ -163,14 +166,23 @@ public class ProfileTriggerHelper extends BroadcastReceiver { if (!newProfileSelected) { //Does the active profile actually cares about this event? - for (Profile.ProfileTrigger trigger : activeProfile.getTriggersFromType(type)) { - if (trigger.getId().equals(id)) { + for (ProfileTrigger trigger : activeProfile.getTriggersFromType(type)) { + final String triggerID = trigger.getId(); + if (triggerID.equals(id)) { Intent intent = new Intent(ProfileManager.INTENT_ACTION_PROFILE_TRIGGER_STATE_CHANGED); intent.putExtra(ProfileManager.EXTRA_TRIGGER_ID, id); intent.putExtra(ProfileManager.EXTRA_TRIGGER_TYPE, type); intent.putExtra(ProfileManager.EXTRA_TRIGGER_STATE, newState); mContext.sendBroadcastAsUser(intent, UserHandle.ALL); + + final int triggerState = trigger.getState(); + if ((newState == Profile.TriggerState.ON_CONNECT + && triggerState == Profile.TriggerState.ON_CONNECT) || + (newState == Profile.TriggerState.ON_DISCONNECT + && triggerState == Profile.TriggerState.ON_DISCONNECT)) { + activeProfile.doSelect(mContext, null); + } break; } }