Move trust agent helper methods into TrustAgentManager

Bug: 32953042
Test: robotests
Change-Id: Ia8dae2e583f0faf7bded150dac65ed076f4ea576
This commit is contained in:
Fan Zhang
2017-11-03 10:06:21 -07:00
parent b916456984
commit 8e3f139cbb
4 changed files with 82 additions and 75 deletions

View File

@@ -23,12 +23,9 @@ import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Bundle;
@@ -38,7 +35,6 @@ import android.os.UserManager;
import android.os.storage.StorageManager;
import android.provider.SearchIndexableResource;
import android.provider.Settings;
import android.service.trust.TrustAgentService;
import android.support.annotation.VisibleForTesting;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
@@ -91,8 +87,6 @@ public class SecuritySettings extends SettingsPreferenceFragment
private static final String TAG = "SecuritySettings";
private static final String TRUST_AGENT_CLICK_INTENT = "trust_agent_click_intent";
private static final Intent TRUST_AGENT_INTENT =
new Intent(TrustAgentService.SERVICE_INTERFACE);
// Lock Settings
private static final String KEY_UNLOCK_SET_OR_CHANGE = "unlock_set_or_change";
@@ -135,9 +129,6 @@ public class SecuritySettings extends SettingsPreferenceFragment
KEY_SHOW_PASSWORD, KEY_UNIFICATION, KEY_VISIBLE_PATTERN_PROFILE
};
// Only allow one trust agent on the platform.
private static final boolean ONLY_ONE_TRUST_AGENT = true;
private static final int MY_USER_ID = UserHandle.myUserId();
private DashboardFeatureProvider mDashboardFeatureProvider;
@@ -466,11 +457,10 @@ public class SecuritySettings extends SettingsPreferenceFragment
// Return the number of trust agents being added
private int addTrustAgentSettings(PreferenceGroup securityCategory) {
final boolean hasSecurity = mLockPatternUtils.isSecure(MY_USER_ID);
ArrayList<TrustAgentComponentInfo> agents = getActiveTrustAgents(
getActivity(), mTrustAgentManager, mLockPatternUtils, mDPM);
for (int i = 0; i < agents.size(); i++) {
final TrustAgentComponentInfo agent = agents.get(i);
RestrictedPreference trustAgentPreference =
final List<TrustAgentComponentInfo> agents = mTrustAgentManager.getActiveTrustAgents(
getActivity(), mLockPatternUtils);
for (TrustAgentComponentInfo agent : agents) {
final RestrictedPreference trustAgentPreference =
new RestrictedPreference(securityCategory.getContext());
trustAgentPreference.setKey(KEY_TRUST_AGENT);
trustAgentPreference.setTitle(agent.title);
@@ -529,44 +519,6 @@ public class SecuritySettings extends SettingsPreferenceFragment
return false;
}
static ArrayList<TrustAgentComponentInfo> getActiveTrustAgents(Context context,
TrustAgentManager trustAgentManager, LockPatternUtils utils,
DevicePolicyManager dpm) {
PackageManager pm = context.getPackageManager();
ArrayList<TrustAgentComponentInfo> result = new ArrayList<>();
List<ResolveInfo> resolveInfos = pm.queryIntentServices(TRUST_AGENT_INTENT,
PackageManager.GET_META_DATA);
List<ComponentName> enabledTrustAgents = utils.getEnabledTrustAgents(MY_USER_ID);
EnforcedAdmin admin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(context,
DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS, UserHandle.myUserId());
if (enabledTrustAgents != null && !enabledTrustAgents.isEmpty()) {
for (int i = 0; i < resolveInfos.size(); i++) {
ResolveInfo resolveInfo = resolveInfos.get(i);
if (resolveInfo.serviceInfo == null) continue;
if (!trustAgentManager.shouldProvideTrust(resolveInfo, pm)) {
continue;
}
TrustAgentComponentInfo trustAgentComponentInfo =
trustAgentManager.getSettingsComponent(pm, resolveInfo);
if (trustAgentComponentInfo.componentName == null ||
!enabledTrustAgents.contains(
trustAgentManager.getComponentName(resolveInfo)) ||
TextUtils.isEmpty(trustAgentComponentInfo.title)) continue;
if (admin != null && dpm.getTrustAgentConfiguration(
null, trustAgentManager.getComponentName(resolveInfo)) == null) {
trustAgentComponentInfo.admin = admin;
}
result.add(trustAgentComponentInfo);
if (ONLY_ONE_TRUST_AGENT) break;
}
}
return result;
}
@Override
public void onGearClick(GearPreference p) {
if (KEY_UNLOCK_SET_OR_CHANGE.equals(p.getKey())) {
@@ -915,8 +867,7 @@ public class SecuritySettings extends SettingsPreferenceFragment
FeatureFactory.getFactory(context).getSecurityFeatureProvider()
.getTrustAgentManager();
final List<TrustAgentComponentInfo> agents =
getActiveTrustAgents(context, trustAgentManager, lockPatternUtils,
context.getSystemService(DevicePolicyManager.class));
trustAgentManager.getActiveTrustAgents(context, lockPatternUtils);
for (int i = 0; i < agents.size(); i++) {
final TrustAgentComponentInfo agent = agents.get(i);
data = new SearchIndexableRaw(context);

View File

@@ -19,7 +19,6 @@ package com.android.settings.security;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
@@ -40,8 +39,6 @@ import com.android.settings.password.ManagedLockPasswordProvider;
import com.android.settings.security.trustagent.TrustAgentManager;
import com.android.settingslib.RestrictedLockUtils;
import java.util.ArrayList;
public class SecuritySubSettings extends SettingsPreferenceFragment
implements Preference.OnPreferenceChangeListener,
OwnerInfoPreferenceController.OwnerInfoCallback {
@@ -74,9 +71,9 @@ public class SecuritySubSettings extends SettingsPreferenceFragment
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
SecurityFeatureProvider securityFeatureProvider =
FeatureFactory.getFactory(getActivity()).getSecurityFeatureProvider();
mTrustAgentManager = securityFeatureProvider.getTrustAgentManager();
mTrustAgentManager =
FeatureFactory.getFactory(
getActivity()).getSecurityFeatureProvider().getTrustAgentManager();
mLockPatternUtils = new LockPatternUtils(getContext());
mDPM = getContext().getSystemService(DevicePolicyManager.class);
mOwnerInfoPreferenceController =
@@ -131,8 +128,8 @@ public class SecuritySubSettings extends SettingsPreferenceFragment
// lock instantly on power key press
mPowerButtonInstantlyLocks = (SwitchPreference) findPreference(
KEY_POWER_INSTANTLY_LOCKS);
CharSequence trustAgentLabel = getActiveTrustAgentLabel(getContext(),
mTrustAgentManager, mLockPatternUtils, mDPM);
final CharSequence trustAgentLabel = mTrustAgentManager.getActiveTrustAgentLabel(
getContext(), mLockPatternUtils);
if (mPowerButtonInstantlyLocks != null && !TextUtils.isEmpty(trustAgentLabel)) {
mPowerButtonInstantlyLocks.setSummary(getString(
R.string.lockpattern_settings_power_button_instantly_locks_summary,
@@ -188,8 +185,8 @@ public class SecuritySubSettings extends SettingsPreferenceFragment
}
}
CharSequence trustAgentLabel = getActiveTrustAgentLabel(getContext(),
mTrustAgentManager, mLockPatternUtils, mDPM);
final CharSequence trustAgentLabel = mTrustAgentManager
.getActiveTrustAgentLabel(getContext(), mLockPatternUtils);
if (!TextUtils.isEmpty(trustAgentLabel)) {
if (Long.valueOf(values[best].toString()) == 0) {
summary = getString(R.string.lock_immediately_summary_with_exception,
@@ -252,12 +249,4 @@ public class SecuritySubSettings extends SettingsPreferenceFragment
}
return true;
}
private static CharSequence getActiveTrustAgentLabel(Context context,
TrustAgentManager trustAgentManager, LockPatternUtils utils,
DevicePolicyManager dpm) {
ArrayList<TrustAgentManager.TrustAgentComponentInfo> agents =
SecuritySettings.getActiveTrustAgents(context, trustAgentManager, utils, dpm);
return agents.isEmpty() ? null : agents.get(0).title;
}
}

View File

@@ -18,29 +18,41 @@ package com.android.settings.security.trustagent;
import static android.service.trust.TrustAgentService.TRUST_AGENT_META_DATA;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.os.UserHandle;
import android.service.trust.TrustAgentService;
import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Slog;
import android.util.Xml;
import com.android.internal.widget.LockPatternUtils;
import com.android.settingslib.RestrictedLockUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/** A manager for trust agent state. */
public class TrustAgentManager {
// Only allow one trust agent on the platform.
private static final boolean ONLY_ONE_TRUST_AGENT = true;
public static class TrustAgentComponentInfo {
public ComponentName componentName;
public String title;
@@ -49,6 +61,8 @@ public class TrustAgentManager {
}
private static final String TAG = "TrustAgentManager";
private static final Intent TRUST_AGENT_INTENT =
new Intent(TrustAgentService.SERVICE_INTERFACE);
@VisibleForTesting
static final String PERMISSION_PROVIDE_AGENT =
@@ -74,13 +88,66 @@ public class TrustAgentManager {
return true;
}
/**
* Return the display label for active trust agent.
*/
public CharSequence getActiveTrustAgentLabel(Context context, LockPatternUtils utils) {
final List<TrustAgentComponentInfo> agents = getActiveTrustAgents(context, utils);
return agents.isEmpty() ? null : agents.get(0).title;
}
/**
* Returns a list of trust agents.
*
* If {@link #ONLY_ONE_TRUST_AGENT} is set, the list will contain up to 1 agent instead of all
* available agents on device.
*/
public List<TrustAgentComponentInfo> getActiveTrustAgents(Context context,
LockPatternUtils utils) {
final int myUserId = UserHandle.myUserId();
final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
final PackageManager pm = context.getPackageManager();
final List<TrustAgentComponentInfo> result = new ArrayList<>();
final List<ResolveInfo> resolveInfos = pm.queryIntentServices(TRUST_AGENT_INTENT,
PackageManager.GET_META_DATA);
final List<ComponentName> enabledTrustAgents = utils.getEnabledTrustAgents(myUserId);
final RestrictedLockUtils.EnforcedAdmin admin = RestrictedLockUtils
.checkIfKeyguardFeaturesDisabled(
context, DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS, myUserId);
if (enabledTrustAgents != null && !enabledTrustAgents.isEmpty()) {
for (ResolveInfo resolveInfo : resolveInfos) {
if (resolveInfo.serviceInfo == null || !shouldProvideTrust(resolveInfo, pm)) {
continue;
}
final TrustAgentComponentInfo trustAgentComponentInfo =
getSettingsComponent(pm, resolveInfo);
if (trustAgentComponentInfo.componentName == null ||
!enabledTrustAgents.contains(getComponentName(resolveInfo)) ||
TextUtils.isEmpty(trustAgentComponentInfo.title)) {
continue;
}
if (admin != null && dpm.getTrustAgentConfiguration(
null, getComponentName(resolveInfo)) == null) {
trustAgentComponentInfo.admin = admin;
}
result.add(trustAgentComponentInfo);
if (ONLY_ONE_TRUST_AGENT) {
break;
}
}
}
return result;
}
public ComponentName getComponentName(ResolveInfo resolveInfo) {
if (resolveInfo == null || resolveInfo.serviceInfo == null) return null;
return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
}
public TrustAgentComponentInfo getSettingsComponent(
PackageManager pm, ResolveInfo resolveInfo) {
private TrustAgentComponentInfo getSettingsComponent(PackageManager pm,
ResolveInfo resolveInfo) {
if (resolveInfo == null || resolveInfo.serviceInfo == null
|| resolveInfo.serviceInfo.metaData == null) {
return null;

View File

@@ -35,7 +35,7 @@ import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class TrustAgentFeatureProviderTest {
public class TrustAgentManagerTest {
private static final String CANNED_PACKAGE_NAME = "com.test.package";