diff options
author | Jim Miller <jaggies@google.com> | 2014-08-08 18:30:20 -0700 |
---|---|---|
committer | Jim Miller <jaggies@google.com> | 2014-08-13 19:43:20 +0000 |
commit | 5a10a8df0efcf034077cbfcd28dd2e495b8d0441 (patch) | |
tree | b366563c1389390f1aa9e305a4cadeef101e1933 /src/com/android/settings/TrustAgentSettings.java | |
parent | b37ad3d0e903a602272c8ce6e5c51630b5ca063b (diff) | |
download | packages_apps_Settings-5a10a8df0efcf034077cbfcd28dd2e495b8d0441.zip packages_apps_Settings-5a10a8df0efcf034077cbfcd28dd2e495b8d0441.tar.gz packages_apps_Settings-5a10a8df0efcf034077cbfcd28dd2e495b8d0441.tar.bz2 |
Use SwitchPreference in TrustAgentSettings.
Fixes bug 16900059
Change-Id: I52dcfd2a2651f8f770477df3c72176303a9d1b39
Diffstat (limited to 'src/com/android/settings/TrustAgentSettings.java')
-rw-r--r-- | src/com/android/settings/TrustAgentSettings.java | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/src/com/android/settings/TrustAgentSettings.java b/src/com/android/settings/TrustAgentSettings.java new file mode 100644 index 0000000..54fef36 --- /dev/null +++ b/src/com/android/settings/TrustAgentSettings.java @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2014 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; + +import java.util.List; + +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.preference.Preference; +import android.preference.PreferenceGroup; +import android.preference.SwitchPreference; +import android.service.trust.TrustAgentService; +import android.util.ArrayMap; +import android.util.ArraySet; + +import com.android.internal.widget.LockPatternUtils; + +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; + + 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); + } + } + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + addPreferencesFromResource(R.xml.trust_agent_settings); + } + + public void onResume() { + super.onResume(); + 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 int count = mAvailableAgents.size(); + for (int i = 0; i < count; i++) { + AgentInfo agent = mAvailableAgents.valueAt(i); + final SwitchPreference preference = new SwitchPreference(context); + 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)); + category.addPreference(agent.preference); + } + } + + private void loadActiveAgents() { + List<ComponentName> activeTrustAgents = mLockPatternUtils.getEnabledTrustAgents(); + if (activeTrustAgents != null) { + mActiveAgents.addAll(activeTrustAgents); + } + } + + private void saveActiveAgents() { + mLockPatternUtils.setEnabledTrustAgents(mActiveAgents); + } + + 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 (!TrustAgentUtils.checkProvidePermission(resolveInfo, pm)) continue; + ComponentName name = TrustAgentUtils.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; + } + + @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; + } + +} |