Apply active profile if user enables system profiles

Ensures the active profile is applied when user enables
system profiles. The profile will be automatically applied
upon activation if:

- No triggers are defined (i.e the profile does not respond
  to events)
- A ON_CONNECT WiFi/BT trigger is defined and the device is
  currently connected to such network/device

If system profiles are already enabled and a WiFi/BT event
is fired, apply the profile overrides

Change-Id: I362893151e52d35636d2ac05ab35e986d1f7237e
TICKET: CYNGNOS-3104
This commit is contained in:
Luis Vidal 2016-06-27 14:41:12 -07:00 committed by Gerrit Code Review
parent 052af0a76d
commit 50571ba8bb
2 changed files with 80 additions and 4 deletions

View File

@ -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<Profile.ProfileTrigger> wiFiTriggers
= mActiveProfile.getTriggersFromType(Profile.TriggerType.WIFI);
final List<Profile.ProfileTrigger> 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<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
final Set<String> 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);

View File

@ -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;
}
}