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 17package com.android.settings; 18 19import java.util.List; 20 21import android.content.ComponentName; 22import android.content.Context; 23import android.content.Intent; 24import android.content.pm.PackageManager; 25import android.content.pm.ResolveInfo; 26import android.graphics.drawable.Drawable; 27import android.os.Bundle; 28import android.preference.Preference; 29import android.preference.PreferenceGroup; 30import android.preference.SwitchPreference; 31import android.service.trust.TrustAgentService; 32import android.util.ArrayMap; 33import android.util.ArraySet; 34 35import com.android.internal.widget.LockPatternUtils; 36 37public class TrustAgentSettings extends SettingsPreferenceFragment implements 38 Preference.OnPreferenceChangeListener { 39 private static final String SERVICE_INTERFACE = TrustAgentService.SERVICE_INTERFACE; 40 private ArrayMap<ComponentName, AgentInfo> mAvailableAgents; 41 private final ArraySet<ComponentName> mActiveAgents = new ArraySet<ComponentName>(); 42 private LockPatternUtils mLockPatternUtils; 43 44 public static final class AgentInfo { 45 CharSequence label; 46 ComponentName component; // service that implements ITrustAgent 47 SwitchPreference preference; 48 public Drawable icon; 49 50 @Override 51 public boolean equals(Object other) { 52 if (other instanceof AgentInfo) { 53 return component.equals(((AgentInfo)other).component); 54 } 55 return true; 56 } 57 58 public int compareTo(AgentInfo other) { 59 return component.compareTo(other.component); 60 } 61 } 62 63 @Override 64 public void onCreate(Bundle icicle) { 65 super.onCreate(icicle); 66 addPreferencesFromResource(R.xml.trust_agent_settings); 67 } 68 69 public void onResume() { 70 super.onResume(); 71 updateAgents(); 72 }; 73 74 private void updateAgents() { 75 final Context context = getActivity(); 76 if (mAvailableAgents == null) { 77 mAvailableAgents = findAvailableTrustAgents(); 78 } 79 if (mLockPatternUtils == null) { 80 mLockPatternUtils = new LockPatternUtils(getActivity()); 81 } 82 loadActiveAgents(); 83 PreferenceGroup category = 84 (PreferenceGroup) getPreferenceScreen().findPreference("trust_agents"); 85 category.removeAll(); 86 final int count = mAvailableAgents.size(); 87 for (int i = 0; i < count; i++) { 88 AgentInfo agent = mAvailableAgents.valueAt(i); 89 final SwitchPreference preference = new SwitchPreference(context); 90 agent.preference = preference; 91 preference.setPersistent(false); 92 preference.setTitle(agent.label); 93 preference.setIcon(agent.icon); 94 preference.setPersistent(false); 95 preference.setOnPreferenceChangeListener(this); 96 preference.setChecked(mActiveAgents.contains(agent.component)); 97 category.addPreference(agent.preference); 98 } 99 } 100 101 private void loadActiveAgents() { 102 List<ComponentName> activeTrustAgents = mLockPatternUtils.getEnabledTrustAgents(); 103 if (activeTrustAgents != null) { 104 mActiveAgents.addAll(activeTrustAgents); 105 } 106 } 107 108 private void saveActiveAgents() { 109 mLockPatternUtils.setEnabledTrustAgents(mActiveAgents); 110 } 111 112 ArrayMap<ComponentName, AgentInfo> findAvailableTrustAgents() { 113 PackageManager pm = getActivity().getPackageManager(); 114 Intent trustAgentIntent = new Intent(SERVICE_INTERFACE); 115 List<ResolveInfo> resolveInfos = pm.queryIntentServices(trustAgentIntent, 116 PackageManager.GET_META_DATA); 117 118 ArrayMap<ComponentName, AgentInfo> agents = new ArrayMap<ComponentName, AgentInfo>(); 119 final int count = resolveInfos.size(); 120 agents.ensureCapacity(count); 121 for (int i = 0; i < count; i++ ) { 122 ResolveInfo resolveInfo = resolveInfos.get(i); 123 if (resolveInfo.serviceInfo == null) continue; 124 if (!TrustAgentUtils.checkProvidePermission(resolveInfo, pm)) continue; 125 ComponentName name = TrustAgentUtils.getComponentName(resolveInfo); 126 AgentInfo agentInfo = new AgentInfo(); 127 agentInfo.label = resolveInfo.loadLabel(pm); 128 agentInfo.icon = resolveInfo.loadIcon(pm); 129 agentInfo.component = name; 130 agents.put(name, agentInfo); 131 } 132 return agents; 133 } 134 135 @Override 136 public boolean onPreferenceChange(Preference preference, Object newValue) { 137 if (preference instanceof SwitchPreference) { 138 final int count = mAvailableAgents.size(); 139 for (int i = 0; i < count; i++) { 140 AgentInfo agent = mAvailableAgents.valueAt(i); 141 if (agent.preference == preference) { 142 if ((Boolean) newValue) { 143 if (!mActiveAgents.contains(agent.component)) { 144 mActiveAgents.add(agent.component); 145 } 146 } else { 147 mActiveAgents.remove(agent.component); 148 } 149 saveActiveAgents(); 150 return true; 151 } 152 } 153 } 154 return false; 155 } 156 157} 158