OSDN Git Service

d0a25b97f8a9078235770bcc9a2e67fae911909d
[android-x86/packages-apps-Settings.git] / src / com / android / settings / TrustAgentSettings.java
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.settings;
18
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;
34
35 import com.android.internal.logging.MetricsLogger;
36 import com.android.internal.widget.LockPatternUtils;
37
38 import java.util.List;
39
40 public class TrustAgentSettings extends SettingsPreferenceFragment implements
41         Preference.OnPreferenceChangeListener {
42     private static final String SERVICE_INTERFACE = TrustAgentService.SERVICE_INTERFACE;
43
44     private ArrayMap<ComponentName, AgentInfo> mAvailableAgents;
45     private final ArraySet<ComponentName> mActiveAgents = new ArraySet<ComponentName>();
46     private LockPatternUtils mLockPatternUtils;
47     private DevicePolicyManager mDpm;
48
49     public static final class AgentInfo {
50         CharSequence label;
51         ComponentName component; // service that implements ITrustAgent
52         SwitchPreference preference;
53         public Drawable icon;
54
55         @Override
56         public boolean equals(Object other) {
57             if (other instanceof AgentInfo) {
58                 return component.equals(((AgentInfo)other).component);
59             }
60             return true;
61         }
62
63         public int compareTo(AgentInfo other) {
64             return component.compareTo(other.component);
65         }
66     }
67
68     @Override
69     protected int getMetricsCategory() {
70         return MetricsLogger.TRUST_AGENT;
71     }
72
73     @Override
74     public void onCreate(Bundle icicle) {
75         super.onCreate(icicle);
76         mDpm = getActivity().getSystemService(DevicePolicyManager.class);
77         addPreferencesFromResource(R.xml.trust_agent_settings);
78     }
79
80     public void onResume() {
81         super.onResume();
82         removePreference("dummy_preference");
83         updateAgents();
84     };
85
86     private void updateAgents() {
87         final Context context = getActivity();
88         if (mAvailableAgents == null) {
89             mAvailableAgents = findAvailableTrustAgents();
90         }
91         if (mLockPatternUtils == null) {
92             mLockPatternUtils = new LockPatternUtils(getActivity());
93         }
94         loadActiveAgents();
95         PreferenceGroup category =
96                 (PreferenceGroup) getPreferenceScreen().findPreference("trust_agents");
97         category.removeAll();
98
99         boolean disabledByDevicePolicy = (mDpm.getKeyguardDisabledFeatures(null)
100                 & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0;
101
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));
113
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);
119             }
120
121             category.addPreference(agent.preference);
122         }
123     }
124
125     private void loadActiveAgents() {
126         List<ComponentName> activeTrustAgents = mLockPatternUtils.getEnabledTrustAgents(
127                 UserHandle.myUserId());
128         if (activeTrustAgents != null) {
129             mActiveAgents.addAll(activeTrustAgents);
130         }
131     }
132
133     private void saveActiveAgents() {
134         mLockPatternUtils.setEnabledTrustAgents(mActiveAgents,
135                 UserHandle.myUserId());
136     }
137
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);
143
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);
157         }
158         return agents;
159     }
160
161     @Override
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);
171                         }
172                     } else {
173                         mActiveAgents.remove(agent.component);
174                     }
175                     saveActiveAgents();
176                     return true;
177                 }
178             }
179         }
180         return false;
181     }
182
183 }