Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5a10a8df authored by Jim Miller's avatar Jim Miller
Browse files

Use SwitchPreference in TrustAgentSettings.

Fixes bug 16900059

Change-Id: I52dcfd2a2651f8f770477df3c72176303a9d1b39
parent b37ad3d0
Loading
Loading
Loading
Loading
+0 −37
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ 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
  -->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingStart="@dimen/settings_side_margin"
        android:paddingEnd="@dimen/settings_side_margin">

    <ListView android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"
            android:fastScrollEnabled="true" />

    <TextView android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/no_trust_agents"
            android:textAppearance="?android:attr/textAppearanceMedium" />

</FrameLayout>
+1 −1
Original line number Diff line number Diff line
@@ -107,7 +107,7 @@
                android:title="@string/manage_trust_agents"
                android:summary="@string/manage_trust_agents_summary"
                android:persistent="false"
                android:fragment="com.android.settings.AdvancedSecuritySettings"/>
                android:fragment="com.android.settings.TrustAgentSettings"/>

        <PreferenceScreen
                android:key="screen_pinning_settings"
+19 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="trust_agents"
    android:title="@string/manage_trust_agents" />
+157 −0
Original line number Diff line number Diff line
@@ -11,46 +11,41 @@
 * 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
 * limitations under the License.
 */

package com.android.settings;

import com.android.internal.widget.LockPatternUtils;
import java.util.List;

import android.app.ListFragment;
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 android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.List;

public class AdvancedSecuritySettings extends ListFragment implements View.OnClickListener {
    static final String TAG = "AdvancedSecuritySettings";
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 final ArrayMap<ComponentName, AgentInfo> mAvailableAgents
            = new ArrayMap<ComponentName, AgentInfo>();

    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) {
@@ -68,74 +63,42 @@ public class AdvancedSecuritySettings extends ListFragment implements View.OnCli
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        addPreferencesFromResource(R.xml.trust_agent_settings);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
	if (mLockPatternUtils == null) {
            mLockPatternUtils = new LockPatternUtils(
                    container.getContext().getApplicationContext());
        }
        setListAdapter(new AgentListAdapter());
        return inflater.inflate(R.layout.advanced_security_settings, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        updateList();
    }

    void updateList() {
        Context context = getActivity();
        if (context == null) {
            return;
        }

        loadActiveAgents();
        updateAgents();
    };

        PackageManager pm = getActivity().getPackageManager();
        Intent trustAgentIntent = new Intent(SERVICE_INTERFACE);
        List<ResolveInfo> resolveInfos = pm.queryIntentServices(trustAgentIntent,
                PackageManager.GET_META_DATA);

        mAvailableAgents.clear();
        mAvailableAgents.ensureCapacity(resolveInfos.size());

        for (ResolveInfo resolveInfo : resolveInfos) {
            if (resolveInfo.serviceInfo == null) continue;
            if (!TrustAgentUtils.checkProvidePermission(resolveInfo, pm)) continue;
            ComponentName name = TrustAgentUtils.getComponentName(resolveInfo);
            if (!mAvailableAgents.containsKey(name)) {
                AgentInfo agentInfo = new AgentInfo();
                agentInfo.label = resolveInfo.loadLabel(pm);
                agentInfo.component = name;
                mAvailableAgents.put(name, agentInfo);
    private void updateAgents() {
        final Context context = getActivity();
        if (mAvailableAgents == null) {
            mAvailableAgents = findAvailableTrustAgents();
        }
        if (mLockPatternUtils == null) {
            mLockPatternUtils = new LockPatternUtils(getActivity());
        }
        ((BaseAdapter) getListAdapter()).notifyDataSetChanged();
    }

    @Override
    public void onClick(View view) {
        ViewHolder h = (ViewHolder) view.getTag();

        if (view.getId() == R.id.clickable) {
            boolean wasActive = mActiveAgents.contains(h.agentInfo.component);
        loadActiveAgents();
            if (!wasActive) {
                mActiveAgents.add(h.agentInfo.component);
            } else {
                mActiveAgents.remove(h.agentInfo.component);
            }
            saveActiveAgents();
            ((BaseAdapter) getListAdapter()).notifyDataSetChanged();
        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() {
        mActiveAgents.clear();
        List<ComponentName> activeTrustAgents = mLockPatternUtils.getEnabledTrustAgents();
        if (activeTrustAgents != null) {
            mActiveAgents.addAll(activeTrustAgents);
@@ -146,76 +109,49 @@ public class AdvancedSecuritySettings extends ListFragment implements View.OnCli
        mLockPatternUtils.setEnabledTrustAgents(mActiveAgents);
    }

    static class ViewHolder {
        TextView name;
        CheckBox checkbox;
        TextView description;
        AgentInfo agentInfo;
        View clickable;
    }

    class AgentListAdapter extends BaseAdapter {
        final LayoutInflater mInflater;

        AgentListAdapter() {
            mInflater = (LayoutInflater)
                    getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public boolean hasStableIds() {
            return false;
        }
    ArrayMap<ComponentName, AgentInfo> findAvailableTrustAgents() {
        PackageManager pm = getActivity().getPackageManager();
        Intent trustAgentIntent = new Intent(SERVICE_INTERFACE);
        List<ResolveInfo> resolveInfos = pm.queryIntentServices(trustAgentIntent,
                PackageManager.GET_META_DATA);

        public int getCount() {
            return mAvailableAgents.size();
        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);
        }

        public Object getItem(int position) {
            return mAvailableAgents.valueAt(position);
        return agents;
    }

        public long getItemId(int position) {
            return position;
    @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);
                        }

        public boolean areAllItemsEnabled() {
            return false;
                    } else {
                        mActiveAgents.remove(agent.component);
                    }

        public boolean isEnabled(int position) {
                    saveActiveAgents();
                    return true;
                }

        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            if (convertView == null) {
                v = newView(parent);
            } else {
                v = convertView;
            }
            bindView(v, position);
            return v;
        }

        public View newView(ViewGroup parent) {
            View v = mInflater.inflate(R.layout.trust_agent_item, parent, false);
            ViewHolder h = new ViewHolder();
            h.name = (TextView)v.findViewById(R.id.name);
            h.checkbox = (CheckBox)v.findViewById(R.id.checkbox);
            h.clickable = v.findViewById(R.id.clickable);
            h.clickable.setOnClickListener(AdvancedSecuritySettings.this);
            h.description = (TextView)v.findViewById(R.id.description);
            v.setTag(h);
            h.clickable.setTag(h);
            return v;
        return false;
    }

        public void bindView(View view, int position) {
            ViewHolder vh = (ViewHolder) view.getTag();
            AgentInfo item = mAvailableAgents.valueAt(position);
            vh.name.setText(item.label);
            vh.checkbox.setChecked(mActiveAgents.contains(item.component));
            vh.agentInfo = item;
        }
    }
}