More device admin.

- Clean up device policy manager APIs.
- Implement lockNow().  For now this just turns the screen off to lock the device.
diff --git a/services/java/com/android/server/DevicePolicyManagerService.java b/services/java/com/android/server/DevicePolicyManagerService.java
index 7b8645f..17a3ab8 100644
--- a/services/java/com/android/server/DevicePolicyManagerService.java
+++ b/services/java/com/android/server/DevicePolicyManagerService.java
@@ -41,8 +41,10 @@
 import android.os.RemoteCallback;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.SystemClock;
 import android.util.Log;
 import android.util.Xml;
+import android.view.WindowManagerPolicy;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -161,30 +163,35 @@
         return null;
     }
     
-    ActiveAdmin getActiveAdminForCallerLocked(ComponentName who)
-            throws SecurityException {
-        ActiveAdmin admin = mAdminMap.get(who);
-        if (admin != null && admin.getUid() == Binder.getCallingUid()) {
-            if (who != null) {
-                if (!who.getPackageName().equals(admin.info.getActivityInfo().packageName)
-                        || !who.getClassName().equals(admin.info.getActivityInfo().name)) {
-                    throw new SecurityException("Current admin is not " + who);
-                }
-            }
-            return admin;
-        }
-        throw new SecurityException("Current admin is not owned by uid " + Binder.getCallingUid());
-    }
-    
     ActiveAdmin getActiveAdminForCallerLocked(ComponentName who, int reqPolicy)
             throws SecurityException {
-        ActiveAdmin admin = getActiveAdminForCallerLocked(who);
-        if (!admin.info.usesPolicy(reqPolicy)) {
-            throw new SecurityException("Admin " + admin.info.getComponent()
-                    + " did not specify uses-policy for: "
-                    + admin.info.getTagForPolicy(reqPolicy));
+        final int callingUid = Binder.getCallingUid();
+        if (who != null) {
+            ActiveAdmin admin = mAdminMap.get(who);
+            if (admin == null) {
+                throw new SecurityException("No active admin " + who);
+            }
+            if (admin.getUid() != callingUid) {
+                throw new SecurityException("Admin " + who + " is not owned by uid "
+                        + Binder.getCallingUid());
+            }
+            if (!admin.info.usesPolicy(reqPolicy)) {
+                throw new SecurityException("Admin " + admin.info.getComponent()
+                        + " did not specify uses-policy for: "
+                        + admin.info.getTagForPolicy(reqPolicy));
+            }
+            return admin;
+        } else {
+            final int N = mAdminList.size();
+            for (int i=0; i<N; i++) {
+                ActiveAdmin admin = mAdminList.get(i);
+                if (admin.getUid() == callingUid && admin.info.usesPolicy(reqPolicy)) {
+                    return admin;
+                }
+            }
+            throw new SecurityException("No active admin owned by uid "
+                    + Binder.getCallingUid() + " for policy #" + reqPolicy);
         }
-        return admin;
     }
     
     void sendAdminCommandLocked(ActiveAdmin admin, String action) {
@@ -346,7 +353,7 @@
             // Ignore
         }
 
-        long timeMs = getMaximumTimeToLock();
+        long timeMs = getMaximumTimeToLock(null);
         if (timeMs <= 0) {
             timeMs = Integer.MAX_VALUE;
         }
@@ -355,7 +362,6 @@
         } catch (RemoteException e) {
             Log.w(TAG, "Failure talking with power manager", e);
         }
-        
     }
 
     public void systemReady() {
@@ -443,10 +449,16 @@
         }
     }
     
-    public int getPasswordMode() {
+    public int getPasswordMode(ComponentName who) {
         synchronized (this) {
-            final int N = mAdminList.size();
             int mode = DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED;
+            
+            if (who != null) {
+                ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
+                return admin != null ? admin.passwordMode : mode;
+            }
+            
+            final int N = mAdminList.size();
             for  (int i=0; i<N; i++) {
                 ActiveAdmin admin = mAdminList.get(i);
                 if (mode < admin.passwordMode) {
@@ -457,7 +469,7 @@
         }
     }
     
-    public void setMinimumPasswordLength(ComponentName who, int length) {
+    public void setPasswordMinimumLength(ComponentName who, int length) {
         synchronized (this) {
             if (who == null) {
                 throw new NullPointerException("ComponentName is null");
@@ -471,10 +483,16 @@
         }
     }
     
-    public int getMinimumPasswordLength() {
+    public int getPasswordMinimumLength(ComponentName who) {
         synchronized (this) {
-            final int N = mAdminList.size();
             int length = 0;
+            
+            if (who != null) {
+                ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
+                return admin != null ? admin.minimumPasswordLength : length;
+            }
+            
+            final int N = mAdminList.size();
             for  (int i=0; i<N; i++) {
                 ActiveAdmin admin = mAdminList.get(i);
                 if (length < admin.minimumPasswordLength) {
@@ -491,8 +509,8 @@
             // so try to retrieve it to check that the caller is one.
             getActiveAdminForCallerLocked(null,
                     DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
-            return mActivePasswordMode >= getPasswordMode()
-                    && mActivePasswordLength >= getMinimumPasswordLength();
+            return mActivePasswordMode >= getPasswordMode(null)
+                    && mActivePasswordLength >= getPasswordMinimumLength(null);
         }
     }
     
@@ -521,10 +539,16 @@
         }
     }
     
-    public int getMaximumFailedPasswordsForWipe() {
+    public int getMaximumFailedPasswordsForWipe(ComponentName who) {
         synchronized (this) {
-            final int N = mAdminList.size();
             int count = 0;
+            
+            if (who != null) {
+                ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
+                return admin != null ? admin.maximumFailedPasswordsForWipe : count;
+            }
+            
+            final int N = mAdminList.size();
             for  (int i=0; i<N; i++) {
                 ActiveAdmin admin = mAdminList.get(i);
                 if (count == 0) {
@@ -545,8 +569,8 @@
             // so try to retrieve it to check that the caller is one.
             getActiveAdminForCallerLocked(null,
                     DeviceAdminInfo.USES_POLICY_RESET_PASSWORD);
-            mode = getPasswordMode();
-            if (password.length() < getMinimumPasswordLength()) {
+            mode = getPasswordMode(null);
+            if (password.length() < getPasswordMinimumLength(null)) {
                 return false;
             }
         }
@@ -577,9 +601,12 @@
                 long ident = Binder.clearCallingIdentity();
                 try {
                     saveSettingsLocked();
+                    
+                    timeMs = getMaximumTimeToLock(null);
                     if (timeMs <= 0) {
                         timeMs = Integer.MAX_VALUE;
                     }
+                    
                     try {
                         getIPowerManager().setMaximumScreenOffTimeount((int)timeMs);
                     } catch (RemoteException e) {
@@ -592,10 +619,16 @@
         }
     }
     
-    public long getMaximumTimeToLock() {
+    public long getMaximumTimeToLock(ComponentName who) {
         synchronized (this) {
-            final int N = mAdminList.size();
             long time = 0;
+            
+            if (who != null) {
+                ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
+                return admin != null ? admin.maximumTimeToUnlock : time;
+            }
+            
+            final int N = mAdminList.size();
             for  (int i=0; i<N; i++) {
                 ActiveAdmin admin = mAdminList.get(i);
                 if (time == 0) {
@@ -615,7 +648,14 @@
             // so try to retrieve it to check that the caller is one.
             getActiveAdminForCallerLocked(null,
                     DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
-            // STOPSHIP need to implement.
+            long ident = Binder.clearCallingIdentity();
+            try {
+                mIPowerManager.goToSleepWithReason(SystemClock.uptimeMillis(),
+                        WindowManagerPolicy.OFF_BECAUSE_OF_ADMIN);
+            } catch (RemoteException e) {
+            } finally {
+                Binder.restoreCallingIdentity(ident);
+            }
         }
     }
     
@@ -702,7 +742,7 @@
             try {
                 mFailedPasswordAttempts++;
                 saveSettingsLocked();
-                int max = getMaximumFailedPasswordsForWipe();
+                int max = getMaximumFailedPasswordsForWipe(null);
                 if (max > 0 && mFailedPasswordAttempts >= max) {
                     wipeDataLocked(0);
                 }