OSDN Git Service

[automerger] DO NOT MERGE Fix unexpected behavior in Bluetooth pairing am: 4f58c19afa...
[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.nano.MetricsProto.MetricsEvent;
36 import com.android.internal.widget.LockPatternUtils;
37 import com.android.settings.overlay.FeatureFactory;
38 import com.android.settings.security.SecurityFeatureProvider;
39 import com.android.settings.trustagent.TrustAgentManager;
40 import com.android.settingslib.RestrictedLockUtils;
41 import com.android.settingslib.RestrictedSwitchPreference;
42
43 import java.util.List;
44
45 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
46
47 public class TrustAgentSettings extends SettingsPreferenceFragment implements
48         Preference.OnPreferenceChangeListener {
49     private static final String SERVICE_INTERFACE = TrustAgentService.SERVICE_INTERFACE;
50
51     private ArrayMap<ComponentName, AgentInfo> mAvailableAgents;
52     private final ArraySet<ComponentName> mActiveAgents = new ArraySet<ComponentName>();
53     private LockPatternUtils mLockPatternUtils;
54     private DevicePolicyManager mDpm;
55     private TrustAgentManager mTrustAgentManager;
56
57     public static final class AgentInfo {
58         CharSequence label;
59         ComponentName component; // service that implements ITrustAgent
60         SwitchPreference preference;
61         public Drawable icon;
62
63         @Override
64         public boolean equals(Object other) {
65             if (other instanceof AgentInfo) {
66                 return component.equals(((AgentInfo)other).component);
67             }
68             return true;
69         }
70
71         public int compareTo(AgentInfo other) {
72             return component.compareTo(other.component);
73         }
74     }
75
76     @Override
77     public int getMetricsCategory() {
78         return MetricsEvent.TRUST_AGENT;
79     }
80
81     @Override
82     public void onCreate(Bundle icicle) {
83         super.onCreate(icicle);
84         mDpm = getActivity().getSystemService(DevicePolicyManager.class);
85         mTrustAgentManager =
86             FeatureFactory.getFactory(getActivity()).getSecurityFeatureProvider()
87                 .getTrustAgentManager();
88
89         addPreferencesFromResource(R.xml.trust_agent_settings);
90     }
91
92     public void onResume() {
93         super.onResume();
94         removePreference("dummy_preference");
95         updateAgents();
96     };
97
98     private void updateAgents() {
99         final Context context = getActivity();
100         if (mAvailableAgents == null) {
101             mAvailableAgents = findAvailableTrustAgents();
102         }
103         if (mLockPatternUtils == null) {
104             mLockPatternUtils = new LockPatternUtils(getActivity());
105         }
106         loadActiveAgents();
107         PreferenceGroup category =
108                 (PreferenceGroup) getPreferenceScreen().findPreference("trust_agents");
109         category.removeAll();
110
111         final EnforcedAdmin admin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(context,
112                 DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS, UserHandle.myUserId());
113
114         final int count = mAvailableAgents.size();
115         for (int i = 0; i < count; i++) {
116             AgentInfo agent = mAvailableAgents.valueAt(i);
117             final RestrictedSwitchPreference preference =
118                     new RestrictedSwitchPreference(getPrefContext());
119             preference.useAdminDisabledSummary(true);
120             agent.preference = preference;
121             preference.setPersistent(false);
122             preference.setTitle(agent.label);
123             preference.setIcon(agent.icon);
124             preference.setPersistent(false);
125             preference.setOnPreferenceChangeListener(this);
126             preference.setChecked(mActiveAgents.contains(agent.component));
127
128             if (admin != null
129                     && mDpm.getTrustAgentConfiguration(null, agent.component) == null) {
130                 preference.setChecked(false);
131                 preference.setDisabledByAdmin(admin);
132             }
133
134             category.addPreference(agent.preference);
135         }
136     }
137
138     private void loadActiveAgents() {
139         List<ComponentName> activeTrustAgents = mLockPatternUtils.getEnabledTrustAgents(
140                 UserHandle.myUserId());
141         if (activeTrustAgents != null) {
142             mActiveAgents.addAll(activeTrustAgents);
143         }
144     }
145
146     private void saveActiveAgents() {
147         mLockPatternUtils.setEnabledTrustAgents(mActiveAgents,
148                 UserHandle.myUserId());
149     }
150
151     ArrayMap<ComponentName, AgentInfo> findAvailableTrustAgents() {
152         PackageManager pm = getActivity().getPackageManager();
153         Intent trustAgentIntent = new Intent(SERVICE_INTERFACE);
154         List<ResolveInfo> resolveInfos = pm.queryIntentServices(trustAgentIntent,
155                 PackageManager.GET_META_DATA);
156
157         ArrayMap<ComponentName, AgentInfo> agents = new ArrayMap<ComponentName, AgentInfo>();
158         final int count = resolveInfos.size();
159         agents.ensureCapacity(count);
160         for (int i = 0; i < count; i++ ) {
161             ResolveInfo resolveInfo = resolveInfos.get(i);
162             if (resolveInfo.serviceInfo == null) {
163                 continue;
164             }
165             if (!mTrustAgentManager.shouldProvideTrust(resolveInfo, pm)) {
166                 continue;
167             }
168             ComponentName name = TrustAgentUtils.getComponentName(resolveInfo);
169             AgentInfo agentInfo = new AgentInfo();
170             agentInfo.label = resolveInfo.loadLabel(pm);
171             agentInfo.icon = resolveInfo.loadIcon(pm);
172             agentInfo.component = name;
173             agents.put(name, agentInfo);
174         }
175         return agents;
176     }
177
178     @Override
179     public boolean onPreferenceChange(Preference preference, Object newValue) {
180         if (preference instanceof SwitchPreference) {
181             final int count = mAvailableAgents.size();
182             for (int i = 0; i < count; i++) {
183                 AgentInfo agent = mAvailableAgents.valueAt(i);
184                 if (agent.preference == preference) {
185                     if ((Boolean) newValue) {
186                         if (!mActiveAgents.contains(agent.component)) {
187                             mActiveAgents.add(agent.component);
188                         }
189                     } else {
190                         mActiveAgents.remove(agent.component);
191                     }
192                     saveActiveAgents();
193                     return true;
194                 }
195             }
196         }
197         return false;
198     }
199
200 }