Merge "TrustAgentSettings use DashboardFragment"

This commit is contained in:
TreeHugger Robot
2018-04-23 21:57:05 +00:00
committed by Android (Google) Code Review
9 changed files with 541 additions and 180 deletions

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security.trustagent;
import android.content.ComponentName;
import android.graphics.drawable.Drawable;
public class TrustAgentInfo implements Comparable<TrustAgentInfo> {
private final CharSequence mLabel;
private final ComponentName mComponentName;
private final Drawable mIcon;
public TrustAgentInfo(CharSequence label, ComponentName componentName, Drawable icon) {
mLabel = label;
mComponentName = componentName;
mIcon = icon;
}
public CharSequence getLabel() {
return mLabel;
}
public ComponentName getComponentName() {
return mComponentName;
}
public Drawable getIcon() {
return mIcon;
}
@Override
public boolean equals(Object other) {
if (other instanceof TrustAgentInfo) {
return mComponentName.equals(((TrustAgentInfo) other).getComponentName());
}
return false;
}
@Override
public int compareTo(TrustAgentInfo other) {
return mComponentName.compareTo(other.getComponentName());
}
}

View File

@@ -16,62 +16,22 @@
package com.android.settings.security.trustagent;
import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
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.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.UserHandle;
import android.service.trust.TrustAgentService;
import androidx.preference.SwitchPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.provider.SearchIndexableResource;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
public class TrustAgentSettings extends SettingsPreferenceFragment implements
Preference.OnPreferenceChangeListener {
private static final String SERVICE_INTERFACE = TrustAgentService.SERVICE_INTERFACE;
private ArrayMap<ComponentName, AgentInfo> mAvailableAgents;
private final ArraySet<ComponentName> mActiveAgents = new ArraySet<ComponentName>();
private LockPatternUtils mLockPatternUtils;
private DevicePolicyManager mDpm;
private TrustAgentManager mTrustAgentManager;
public static final class AgentInfo {
CharSequence label;
ComponentName component; // service that implements ITrustAgent
SwitchPreference preference;
public Drawable icon;
@Override
public boolean equals(Object other) {
if (other instanceof AgentInfo) {
return component.equals(((AgentInfo)other).component);
}
return true;
}
public int compareTo(AgentInfo other) {
return component.compareTo(other.component);
}
}
@SearchIndexable
public class TrustAgentSettings extends DashboardFragment {
private static final String TAG = "TrustAgentSettings";
@Override
public int getMetricsCategory() {
@@ -84,122 +44,25 @@ public class TrustAgentSettings extends SettingsPreferenceFragment implements
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mDpm = getActivity().getSystemService(DevicePolicyManager.class);
mTrustAgentManager =
FeatureFactory.getFactory(getActivity()).getSecurityFeatureProvider()
.getTrustAgentManager();
addPreferencesFromResource(R.xml.trust_agent_settings);
}
public void onResume() {
super.onResume();
removePreference("dummy_preference");
updateAgents();
}
private void updateAgents() {
final Context context = getActivity();
if (mAvailableAgents == null) {
mAvailableAgents = findAvailableTrustAgents();
}
if (mLockPatternUtils == null) {
mLockPatternUtils = new LockPatternUtils(getActivity());
}
loadActiveAgents();
PreferenceGroup category =
(PreferenceGroup) getPreferenceScreen().findPreference("trust_agents");
category.removeAll();
final EnforcedAdmin admin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(context,
DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS, UserHandle.myUserId());
final int count = mAvailableAgents.size();
for (int i = 0; i < count; i++) {
AgentInfo agent = mAvailableAgents.valueAt(i);
final RestrictedSwitchPreference preference =
new RestrictedSwitchPreference(getPrefContext());
preference.useAdminDisabledSummary(true);
agent.preference = preference;
preference.setPersistent(false);
preference.setTitle(agent.label);
preference.setIcon(agent.icon);
preference.setPersistent(false);
preference.setOnPreferenceChangeListener(this);
preference.setChecked(mActiveAgents.contains(agent.component));
if (admin != null
&& mDpm.getTrustAgentConfiguration(null, agent.component) == null) {
preference.setChecked(false);
preference.setDisabledByAdmin(admin);
}
category.addPreference(agent.preference);
}
}
private void loadActiveAgents() {
List<ComponentName> activeTrustAgents = mLockPatternUtils.getEnabledTrustAgents(
UserHandle.myUserId());
if (activeTrustAgents != null) {
mActiveAgents.addAll(activeTrustAgents);
}
}
private void saveActiveAgents() {
mLockPatternUtils.setEnabledTrustAgents(mActiveAgents,
UserHandle.myUserId());
}
private ArrayMap<ComponentName, AgentInfo> findAvailableTrustAgents() {
PackageManager pm = getActivity().getPackageManager();
Intent trustAgentIntent = new Intent(SERVICE_INTERFACE);
List<ResolveInfo> resolveInfos = pm.queryIntentServices(trustAgentIntent,
PackageManager.GET_META_DATA);
ArrayMap<ComponentName, AgentInfo> agents = new ArrayMap<ComponentName, AgentInfo>();
final int count = resolveInfos.size();
agents.ensureCapacity(count);
for (int i = 0; i < count; i++ ) {
ResolveInfo resolveInfo = resolveInfos.get(i);
if (resolveInfo.serviceInfo == null) {
continue;
}
if (!mTrustAgentManager.shouldProvideTrust(resolveInfo, pm)) {
continue;
}
ComponentName name = mTrustAgentManager.getComponentName(resolveInfo);
AgentInfo agentInfo = new AgentInfo();
agentInfo.label = resolveInfo.loadLabel(pm);
agentInfo.icon = resolveInfo.loadIcon(pm);
agentInfo.component = name;
agents.put(name, agentInfo);
}
return agents;
protected String getLogTag() {
return TAG;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference instanceof SwitchPreference) {
final int count = mAvailableAgents.size();
for (int i = 0; i < count; i++) {
AgentInfo agent = mAvailableAgents.valueAt(i);
if (agent.preference == preference) {
if ((Boolean) newValue) {
if (!mActiveAgents.contains(agent.component)) {
mActiveAgents.add(agent.component);
}
} else {
mActiveAgents.remove(agent.component);
}
saveActiveAgents();
return true;
}
}
}
return false;
protected int getPreferenceScreenResId() {
return R.xml.trust_agent_settings;
}
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(
Context context, boolean enabled) {
final List<SearchIndexableResource> result = new ArrayList<>();
final SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.trust_agent_settings;
result.add(sir);
return result;
}
};
}

View File

@@ -0,0 +1,193 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security.trustagent;
import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
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.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.service.trust.TrustAgentService;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.IconDrawableFactory;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.security.SecurityFeatureProvider;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import java.util.List;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
public class TrustAgentsPreferenceController extends BasePreferenceController
implements Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart {
private static final Intent TRUST_AGENT_INTENT =
new Intent(TrustAgentService.SERVICE_INTERFACE);
private final ArrayMap<ComponentName, TrustAgentInfo> mAvailableAgents;
private final ArraySet<ComponentName> mActiveAgents;
private final DevicePolicyManager mDevicePolicyManager;
private final IconDrawableFactory mIconDrawableFactory;
private final LockPatternUtils mLockPatternUtils;
private final PackageManager mPackageManager;
private final TrustAgentManager mTrustAgentManager;
private PreferenceScreen mScreen;
public TrustAgentsPreferenceController(Context context, String key) {
super(context, key);
mAvailableAgents = new ArrayMap<>();
mActiveAgents = new ArraySet<>();
mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
mIconDrawableFactory = IconDrawableFactory.newInstance(context);
final SecurityFeatureProvider securityFeatureProvider =
FeatureFactory.getFactory(context).getSecurityFeatureProvider();
mTrustAgentManager = securityFeatureProvider.getTrustAgentManager();
mLockPatternUtils = securityFeatureProvider.getLockPatternUtils(context);
mPackageManager = context.getPackageManager();
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mScreen = screen;
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void onStart() {
updateAgents();
}
private void updateAgents() {
findAvailableTrustAgents();
loadActiveAgents();
removeUselessExistingPreferences();
final EnforcedAdmin admin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(mContext,
DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS, UserHandle.myUserId());
for (TrustAgentInfo agent : mAvailableAgents.values()) {
final ComponentName componentName = agent.getComponentName();
RestrictedSwitchPreference preference = (RestrictedSwitchPreference)
mScreen.findPreference(componentName.flattenToString());
if (preference == null) {
preference = new RestrictedSwitchPreference(mScreen.getContext());
}
preference.setKey(componentName.flattenToString());
preference.useAdminDisabledSummary(true);
preference.setTitle(agent.getLabel());
preference.setIcon(agent.getIcon());
preference.setOnPreferenceChangeListener(this);
preference.setChecked(mActiveAgents.contains(componentName));
if (admin != null && mDevicePolicyManager.getTrustAgentConfiguration(null /* admin */,
componentName) == null) {
preference.setChecked(false);
preference.setDisabledByAdmin(admin);
}
mScreen.addPreference(preference);
}
}
private void loadActiveAgents() {
final List<ComponentName> activeTrustAgents = mLockPatternUtils.getEnabledTrustAgents(
UserHandle.myUserId());
if (activeTrustAgents != null) {
mActiveAgents.addAll(activeTrustAgents);
}
}
private void saveActiveAgents() {
mLockPatternUtils.setEnabledTrustAgents(mActiveAgents, UserHandle.myUserId());
}
private void findAvailableTrustAgents() {
final List<ResolveInfo> resolveInfos = mPackageManager.queryIntentServices(
TRUST_AGENT_INTENT, PackageManager.GET_META_DATA);
mAvailableAgents.clear();
for (ResolveInfo resolveInfo : resolveInfos) {
if (resolveInfo.serviceInfo == null) {
continue;
}
if (!mTrustAgentManager.shouldProvideTrust(resolveInfo, mPackageManager)) {
continue;
}
final CharSequence label = resolveInfo.loadLabel(mPackageManager);
final ComponentName componentName = mTrustAgentManager.getComponentName(resolveInfo);
final Drawable icon = mIconDrawableFactory.getBadgedIcon(
resolveInfo.getComponentInfo().applicationInfo);
final TrustAgentInfo agentInfo = new TrustAgentInfo(label, componentName, icon);
mAvailableAgents.put(componentName, agentInfo);
}
}
private void removeUselessExistingPreferences() {
final int count = mScreen.getPreferenceCount();
if (count <= 0) {
return;
}
for (int i = count - 1; i >= 0; i--) {
final Preference pref = mScreen.getPreference(i);
final String[] names = TextUtils.split(pref.getKey(), "/");
final ComponentName componentName = new ComponentName(names[0], names[1]);
if (!mAvailableAgents.containsKey(componentName)) {
mScreen.removePreference(pref);
mActiveAgents.remove(componentName);
}
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (!(preference instanceof SwitchPreference)) {
return false;
}
for (TrustAgentInfo agent : mAvailableAgents.values()) {
final ComponentName componentName = agent.getComponentName();
if (!TextUtils.equals(preference.getKey(), componentName.flattenToString())) {
continue;
}
if ((Boolean) newValue && !mActiveAgents.contains(componentName)) {
mActiveAgents.add(componentName);
} else {
mActiveAgents.remove(componentName);
}
saveActiveAgents();
return true;
}
return false;
}
}