Initial UI for managing device policies.

More to come...  what is here now is very ugly, and completely missing the
confirmation screen for setting a device administrator.
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 2cb7772..5b7fdd1 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -374,6 +374,19 @@
             </intent-filter>
         </activity>
 
+        <activity android:name="DeviceAdminSettings"
+                android:label="@string/device_admin_settings_title"
+                android:clearTaskOnLaunch="true"
+                >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <action android:name="android.app.action.ADD_DEVICE_ADMIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.VOICE_LAUNCH" />
+                <category android:name="com.android.settings.SHORTCUT" />
+            </intent-filter>
+        </activity>
+
         <activity android:name="IccLockSettings" android:label="@string/sim_lock_settings"
                 android:process="com.android.phone">
             <intent-filter>
diff --git a/res/layout/device_admin_item.xml b/res/layout/device_admin_item.xml
new file mode 100644
index 0000000..d17ff24
--- /dev/null
+++ b/res/layout/device_admin_item.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+** Copyright 2010, 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.
+*/
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:orientation="horizontal"
+    android:paddingRight="6dip"
+    android:paddingLeft="6dip"
+    android:gravity="fill" >
+
+    <ImageView android:id="@+id/icon"
+        android:layout_width="@android:dimen/app_icon_size"
+        android:layout_height="@android:dimen/app_icon_size"
+        android:layout_marginLeft="5dip"
+        android:layout_marginRight="11dip"
+        android:layout_gravity="center_vertical"
+        android:scaleType="fitCenter"/>
+
+    <TextView android:id="@+id/name"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:layout_gravity="center_vertical"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:textStyle="bold"
+        android:singleLine="true"
+        android:ellipsize="marquee"
+        android:layout_marginBottom="2dip" />
+</LinearLayout>
diff --git a/res/layout/device_admin_settings.xml b/res/layout/device_admin_settings.xml
new file mode 100644
index 0000000..221e45f
--- /dev/null
+++ b/res/layout/device_admin_settings.xml
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 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">
+    <LinearLayout
+        android:id="@+id/active_layout"
+	    android:layout_width="match_parent"
+	    android:layout_height="match_parent"
+	    android:orientation="vertical"
+	    android:visibility="gone">
+	    <TextView
+	        android:layout_width="match_parent"
+	        android:layout_height="wrap_content"
+	        android:paddingLeft="20dip"
+	        android:paddingTop="5dip"
+	        android:text="@string/active_device_admin_msg"
+	        android:gravity="center"
+	        android:textAppearance="?android:attr/textAppearanceMedium" />
+        <LinearLayout
+	        android:layout_width="match_parent"
+	        android:layout_height="wrap_content"
+	        android:orientation="horizontal">
+	        <ImageView android:id="@+id/active_icon"
+	            android:layout_width="@android:dimen/app_icon_size"
+	            android:layout_height="@android:dimen/app_icon_size"
+	            android:layout_marginLeft="5dip"
+	            android:layout_marginRight="11dip"
+	            android:layout_gravity="center_vertical"
+	            android:scaleType="fitCenter"/>
+            <TextView android:id="@+id/active_name"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_weight="1"
+                android:layout_marginBottom="2dip"
+                android:layout_gravity="center_vertical"
+                android:textAppearance="?android:attr/textAppearanceMedium"
+                android:textStyle="bold"
+                android:singleLine="true"
+                android:ellipsize="marquee" />
+        </LinearLayout>
+        <TextView android:id="@+id/active_description"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content" />
+        <Button android:id="@+id/remove_button"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android_layout_gravity="center_vertical|east"
+            android:text="@string/remove_device_admin"
+        />
+    </LinearLayout>
+    
+    <LinearLayout
+        android:id="@+id/select_layout"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:orientation="vertical"
+        android:visibility="gone">
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:paddingLeft="20dip"
+            android:paddingTop="5dip"
+            android:text="@string/select_device_admin_msg"
+            android:gravity="center"
+            android:textAppearance="?android:attr/textAppearanceMedium" />
+        <ListView android:id="@android:id/list"
+                android:layout_width="match_parent" 
+                android:layout_height="match_parent"
+                android:drawSelectorOnTop="false"
+                android:fastScrollEnabled="true" />
+    </LinearLayout>
+</FrameLayout>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index b86950b..5a0b589 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -528,6 +528,12 @@
     <string name="pin_password_illegal_character">PIN must contain only digits 0-9</string>
     <!-- Error shown when in PASSWORD mode and user enters an invalid character -->
     <string name="pin_password_contains_non_digits">Password contains an illegal character</string>
+    <!-- In the security screen, the header title for settings related to device admins -->
+    <string name="device_admin_title">Device administration</string>
+    <!-- Title of preference to manage device admins -->
+    <string name="manage_device_admin">Select device administrators</string>
+    <!-- Summary of preference to manage device policies -->
+    <string name="manage_device_admin_summary">Add or remove device administrators</string>
 
     <!-- Bluetooth settings -->
     <!-- Bluetooth settings check box title on Main Settings screen -->
@@ -2304,6 +2310,16 @@
     <!-- Dialog title for confirmation to erase backup data from server -->
     <string name="backup_erase_dialog_message">Are you sure you want to stop backing up your settings and erase all copies on Google servers?</string>
 
+    <!-- Device admin settings screen --><skip/>
+    <!-- Device admin settings activity title -->
+    <string name="device_admin_settings_title">Device administration settings</string>
+    <!-- Label for screen showing the active device policy -->
+    <string name="active_device_admin_msg">Active device administrator</string>
+    <!-- Label for button to remove the active device admin -->
+    <string name="remove_device_admin">Remove</string>
+    <!-- Label for screen showing to select device policy -->
+    <string name="select_device_admin_msg">Select device administrator</string>
+    
     <!-- Name to assign to a Network Access Point that was saved without a name -->
     <string name="untitled_apn">Untitled</string>
 </resources>
diff --git a/src/com/android/settings/DeviceAdminSettings.java b/src/com/android/settings/DeviceAdminSettings.java
new file mode 100644
index 0000000..e3d733e
--- /dev/null
+++ b/src/com/android/settings/DeviceAdminSettings.java
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2010 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 org.xmlpull.v1.XmlPullParserException;
+
+import android.app.Activity;
+import android.app.DeviceAdmin;
+import android.app.DeviceAdminInfo;
+import android.app.DevicePolicyManager;
+import android.app.ListActivity;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.ImageView;
+import android.widget.ListView;
+import android.widget.TextView;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class DeviceAdminSettings extends ListActivity {
+    static final String TAG = "DeviceAdminSettings";
+    
+    DevicePolicyManager mDPM;
+    DeviceAdminInfo mCurrentAdmin;
+    
+    View mActiveLayout;
+    ImageView mActiveIcon;
+    TextView mActiveName;
+    TextView mActiveDescription;
+    
+    View mSelectLayout;
+    ArrayList<DeviceAdminInfo> mAvailablePolicies
+            = new ArrayList<DeviceAdminInfo>();
+    
+    @Override
+    protected void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+
+        mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
+        mCurrentAdmin = mDPM.getActiveAdminInfo();
+        
+        setContentView(R.layout.device_admin_settings);
+        
+        mActiveLayout = findViewById(R.id.active_layout);
+        mActiveIcon = (ImageView)findViewById(R.id.active_icon);
+        mActiveName = (TextView)findViewById(R.id.active_name);
+        mActiveDescription = (TextView)findViewById(R.id.active_description);
+        findViewById(R.id.remove_button).setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                if (mCurrentAdmin != null) {
+                    mDPM.removeActiveAdmin(mCurrentAdmin.getComponent());
+                    finish();
+                }
+            }
+        });
+        
+        mSelectLayout = findViewById(R.id.select_layout);
+        getListView().setDivider(null);
+
+        updateLayout();
+        
+        if (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN.equals(getIntent().getAction())) {
+            ComponentName cn = (ComponentName)getIntent().getParcelableExtra(
+                    DevicePolicyManager.EXTRA_DEVICE_ADMIN);
+            if (cn == null) {
+                Log.w(TAG, "No component specified in " + getIntent().getAction());
+                finish();
+                return;
+            }
+            if (cn.equals(mCurrentAdmin)) {
+                setResult(Activity.RESULT_OK);
+                finish();
+                return;
+            }
+            if (mCurrentAdmin != null) {
+                Log.w(TAG, "Admin already set, can't do " + getIntent().getAction());
+                finish();
+                return;
+            }
+            
+            try {
+                mDPM.setActiveAdmin(cn);
+                setResult(Activity.RESULT_OK);
+            } catch (RuntimeException e) {
+                Log.w(TAG, "Unable to set admin " + cn, e);
+                setResult(Activity.RESULT_CANCELED);
+            }
+            finish();
+        }
+    }
+    
+    void updateLayout() {
+        if (mCurrentAdmin != null) {
+            mActiveLayout.setVisibility(View.VISIBLE);
+            mSelectLayout.setVisibility(View.GONE);
+            mActiveIcon.setImageDrawable(mCurrentAdmin.loadIcon(getPackageManager()));
+            mActiveName.setText(mCurrentAdmin.loadLabel(getPackageManager()));
+        } else {
+            mActiveLayout.setVisibility(View.GONE);
+            mSelectLayout.setVisibility(View.VISIBLE);
+            mAvailablePolicies.clear();
+            List<ResolveInfo> avail = getPackageManager().queryBroadcastReceivers(
+                    new Intent(DeviceAdmin.ACTION_DEVICE_ADMIN_ENABLED),
+                    PackageManager.GET_META_DATA);
+            for (int i=0; i<avail.size(); i++) {
+                ResolveInfo ri = avail.get(i);
+                try {
+                    DeviceAdminInfo dpi = new DeviceAdminInfo(this, ri);
+                    mAvailablePolicies.add(dpi);
+                } catch (XmlPullParserException e) {
+                    Log.w(TAG, "Skipping " + ri.activityInfo, e);
+                } catch (IOException e) {
+                    Log.w(TAG, "Skipping " + ri.activityInfo, e);
+                }
+            }
+            getListView().setAdapter(new PolicyListAdapter());
+        }
+    }
+    
+    @Override
+    protected void onListItemClick(ListView l, View v, int position, long id) {
+        DeviceAdminInfo dpi = (DeviceAdminInfo)l.getAdapter().getItem(position);
+        mDPM.setActiveAdmin(dpi.getComponent());
+        finish();
+    }
+    
+    static class ViewHolder {
+        ImageView icon;
+        TextView name;
+    }
+    
+    class PolicyListAdapter extends BaseAdapter {
+        final LayoutInflater mInflater;
+        
+        PolicyListAdapter() {
+            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+        }
+
+        public boolean hasStableIds() {
+            return true;
+        }
+        
+        public int getCount() {
+            return mAvailablePolicies.size();
+        }
+
+        public Object getItem(int position) {
+            return mAvailablePolicies.get(position);
+        }
+
+        public long getItemId(int position) {
+            return position;
+        }
+
+        public boolean areAllItemsEnabled() {
+            return false;
+        }
+
+        public boolean isEnabled(int position) {
+            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.device_admin_item, parent, false);
+            ViewHolder h = new ViewHolder();
+            h.icon = (ImageView)v.findViewById(R.id.icon);
+            h.name = (TextView)v.findViewById(R.id.name);
+            v.setTag(h);
+            return v;
+        }
+        
+        public void bindView(View view, int position) {
+            ViewHolder vh = (ViewHolder) view.getTag();
+            DeviceAdminInfo item = mAvailablePolicies.get(position);
+            vh.icon.setImageDrawable(item.loadIcon(getPackageManager()));
+            vh.name.setText(item.loadLabel(getPackageManager()));
+        }
+    }
+}
diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java
index fb284fd..6564e3e 100644
--- a/src/com/android/settings/SecuritySettings.java
+++ b/src/com/android/settings/SecuritySettings.java
@@ -184,6 +184,19 @@
         showPassword.setPersistent(false);
         passwordsCat.addPreference(showPassword);
 
+        // Device policies
+        PreferenceCategory devicePoliciesCat = new PreferenceCategory(this);
+        devicePoliciesCat.setTitle(R.string.device_admin_title);
+        root.addPreference(devicePoliciesCat);
+
+        Preference deviceAdminButton = new Preference(this);
+        deviceAdminButton.setTitle(R.string.manage_device_admin);
+        deviceAdminButton.setSummary(R.string.manage_device_admin_summary);
+        Intent deviceAdminIntent = new Intent();
+        deviceAdminIntent.setClass(this, DeviceAdminSettings.class);
+        deviceAdminButton.setIntent(deviceAdminIntent);
+        devicePoliciesCat.addPreference(deviceAdminButton);
+
         // Credential storage
         PreferenceCategory credentialsCat = new PreferenceCategory(this);
         credentialsCat.setTitle(R.string.credentials_category);