Ensures ProfileTrustAgent properly grants/revokes trust [2/2]

Notifies the ProfileTrustAgent when a WiFi/BT event was triggered even
if no new profile was selected so the trust agent can grant/revoke trust

Filters out the multiple network state change notifications to make sure
we notify the trust agent only when the event that the profile
is tracking actually happened

Change-Id: I047861a8b145762fff24568e341373a89ee01de9
TICKET: CYNGNOS-2719
This commit is contained in:
Luis Vidal 2016-06-06 13:10:00 -07:00 committed by Gerrit Code Review
parent 19de84cd1b
commit aa1f592e9f
2 changed files with 73 additions and 17 deletions

View File

@ -22,17 +22,18 @@ 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;
import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Log;
import cyanogenmod.app.Profile;
import cyanogenmod.app.ProfileManager;
import cyanogenmod.providers.CMSettings;
import org.cyanogenmod.platform.internal.ProfileManagerService;
import java.util.UUID;
@ -103,16 +104,27 @@ public class ProfileTriggerHelper extends BroadcastReceiver {
String action = intent.getAction();
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
String activeSSID = getActiveSSID();
int triggerState;
if (activeSSID != null) {
triggerState = Profile.TriggerState.ON_CONNECT;
mLastConnectedSSID = activeSSID;
} else {
triggerState = Profile.TriggerState.ON_DISCONNECT;
Bundle extras = intent.getExtras();
WifiInfo wifiInfo = extras.getParcelable(WifiManager.EXTRA_WIFI_INFO);
if (wifiInfo != null) {
String ssid = wifiInfo.getSSID();
if (ssid != null) {
// SSID will be surrounded by double quotation marks if it can be decoded
// as UTF-8
if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
ssid = ssid.substring(1, ssid.length()-1);
}
if (TextUtils.equals(ssid, WifiSsid.NONE)) {
checkTriggers(Profile.TriggerType.WIFI, mLastConnectedSSID,
Profile.TriggerState.ON_DISCONNECT);
mLastConnectedSSID = WifiSsid.NONE;
} else if (!TextUtils.equals(mLastConnectedSSID, ssid)) {
mLastConnectedSSID = ssid;
checkTriggers(Profile.TriggerType.WIFI, mLastConnectedSSID,
Profile.TriggerState.ON_CONNECT);
}
}
}
checkTriggers(Profile.TriggerType.WIFI, mLastConnectedSSID, triggerState);
} else if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)
|| action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
int triggerState = action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)
@ -133,16 +145,37 @@ public class ProfileTriggerHelper extends BroadcastReceiver {
}
private void checkTriggers(int type, String id, int newState) {
final Profile activeProfile = mManagerService.getActiveProfileInternal();
final UUID currentProfileUuid = activeProfile.getUuid();
boolean newProfileSelected = false;
for (Profile p : mManagerService.getProfileList()) {
if (newState != p.getTriggerState(type, id)) {
final int profileTriggerState = p.getTriggerState(type, id);
if (newState != profileTriggerState) {
continue;
}
UUID currentProfileUuid = mManagerService.getActiveProfileInternal().getUuid();
if (!currentProfileUuid.equals(p.getUuid())) {
mManagerService.setActiveProfileInternal(p, true);
newProfileSelected = true;
}
}
if (!newProfileSelected) {
//Does the active profile actually cares about this event?
for (Profile.ProfileTrigger trigger : activeProfile.getTriggersFromType(type)) {
if (trigger.getId().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);
break;
}
}
}
}
private String getActiveSSID() {

View File

@ -84,6 +84,29 @@ public class ProfileManager {
public static final String INTENT_ACTION_PROFILE_UPDATED =
"cyanogenmod.platform.intent.action.PROFILE_UPDATED";
/**
* @hide
*/
public static final String INTENT_ACTION_PROFILE_TRIGGER_STATE_CHANGED =
"cyanogenmod.platform.intent.action.INTENT_ACTION_PROFILE_TRIGGER_STATE_CHANGED";
/**
* @hide
*/
public static final String EXTRA_TRIGGER_ID = "trigger_id";
/**
* @hide
*/
public static final String EXTRA_TRIGGER_TYPE = "trigger_type";
/**
* @hide
*/
public static final String EXTRA_TRIGGER_STATE = "trigger_state";
/**
* Extra for {@link #INTENT_ACTION_PROFILE_SELECTED} and {@link #INTENT_ACTION_PROFILE_UPDATED}:
* The name of the newly activated or updated profile