2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings;
19 import android.app.admin.DevicePolicyManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.graphics.drawable.Drawable;
26 import android.os.Bundle;
27 import android.os.UserHandle;
28 import android.service.trust.TrustAgentService;
29 import android.support.v14.preference.SwitchPreference;
30 import android.support.v7.preference.Preference;
31 import android.support.v7.preference.PreferenceGroup;
32 import android.util.ArrayMap;
33 import android.util.ArraySet;
35 import com.android.internal.logging.MetricsLogger;
36 import com.android.internal.widget.LockPatternUtils;
38 import java.util.List;
40 public class TrustAgentSettings extends SettingsPreferenceFragment implements
41 Preference.OnPreferenceChangeListener {
42 private static final String SERVICE_INTERFACE = TrustAgentService.SERVICE_INTERFACE;
44 private ArrayMap<ComponentName, AgentInfo> mAvailableAgents;
45 private final ArraySet<ComponentName> mActiveAgents = new ArraySet<ComponentName>();
46 private LockPatternUtils mLockPatternUtils;
47 private DevicePolicyManager mDpm;
49 public static final class AgentInfo {
51 ComponentName component; // service that implements ITrustAgent
52 SwitchPreference preference;
56 public boolean equals(Object other) {
57 if (other instanceof AgentInfo) {
58 return component.equals(((AgentInfo)other).component);
63 public int compareTo(AgentInfo other) {
64 return component.compareTo(other.component);
69 protected int getMetricsCategory() {
70 return MetricsLogger.TRUST_AGENT;
74 public void onCreate(Bundle icicle) {
75 super.onCreate(icicle);
76 mDpm = getActivity().getSystemService(DevicePolicyManager.class);
77 addPreferencesFromResource(R.xml.trust_agent_settings);
80 public void onResume() {
82 removePreference("dummy_preference");
86 private void updateAgents() {
87 final Context context = getActivity();
88 if (mAvailableAgents == null) {
89 mAvailableAgents = findAvailableTrustAgents();
91 if (mLockPatternUtils == null) {
92 mLockPatternUtils = new LockPatternUtils(getActivity());
95 PreferenceGroup category =
96 (PreferenceGroup) getPreferenceScreen().findPreference("trust_agents");
99 boolean disabledByDevicePolicy = (mDpm.getKeyguardDisabledFeatures(null)
100 & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0;
102 final int count = mAvailableAgents.size();
103 for (int i = 0; i < count; i++) {
104 AgentInfo agent = mAvailableAgents.valueAt(i);
105 final SwitchPreference preference = new SwitchPreference(getPrefContext());
106 agent.preference = preference;
107 preference.setPersistent(false);
108 preference.setTitle(agent.label);
109 preference.setIcon(agent.icon);
110 preference.setPersistent(false);
111 preference.setOnPreferenceChangeListener(this);
112 preference.setChecked(mActiveAgents.contains(agent.component));
114 if (disabledByDevicePolicy
115 && mDpm.getTrustAgentConfiguration(null, agent.component) == null) {
116 preference.setChecked(false);
117 preference.setEnabled(false);
118 preference.setSummary(R.string.trust_agent_disabled_device_admin);
121 category.addPreference(agent.preference);
125 private void loadActiveAgents() {
126 List<ComponentName> activeTrustAgents = mLockPatternUtils.getEnabledTrustAgents(
127 UserHandle.myUserId());
128 if (activeTrustAgents != null) {
129 mActiveAgents.addAll(activeTrustAgents);
133 private void saveActiveAgents() {
134 mLockPatternUtils.setEnabledTrustAgents(mActiveAgents,
135 UserHandle.myUserId());
138 ArrayMap<ComponentName, AgentInfo> findAvailableTrustAgents() {
139 PackageManager pm = getActivity().getPackageManager();
140 Intent trustAgentIntent = new Intent(SERVICE_INTERFACE);
141 List<ResolveInfo> resolveInfos = pm.queryIntentServices(trustAgentIntent,
142 PackageManager.GET_META_DATA);
144 ArrayMap<ComponentName, AgentInfo> agents = new ArrayMap<ComponentName, AgentInfo>();
145 final int count = resolveInfos.size();
146 agents.ensureCapacity(count);
147 for (int i = 0; i < count; i++ ) {
148 ResolveInfo resolveInfo = resolveInfos.get(i);
149 if (resolveInfo.serviceInfo == null) continue;
150 if (!TrustAgentUtils.checkProvidePermission(resolveInfo, pm)) continue;
151 ComponentName name = TrustAgentUtils.getComponentName(resolveInfo);
152 AgentInfo agentInfo = new AgentInfo();
153 agentInfo.label = resolveInfo.loadLabel(pm);
154 agentInfo.icon = resolveInfo.loadIcon(pm);
155 agentInfo.component = name;
156 agents.put(name, agentInfo);
162 public boolean onPreferenceChange(Preference preference, Object newValue) {
163 if (preference instanceof SwitchPreference) {
164 final int count = mAvailableAgents.size();
165 for (int i = 0; i < count; i++) {
166 AgentInfo agent = mAvailableAgents.valueAt(i);
167 if (agent.preference == preference) {
168 if ((Boolean) newValue) {
169 if (!mActiveAgents.contains(agent.component)) {
170 mActiveAgents.add(agent.component);
173 mActiveAgents.remove(agent.component);