Fix 2673731: Adding support for password history to Device Admin.

Change-Id: If3240048813e32b2bae79fe5cb8a73aea20ec56c
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index 35e7ee6..67992a4 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -46,7 +46,7 @@
 
     private final Context mContext;
     private final IDevicePolicyManager mService;
-    
+
     private final Handler mHandler;
 
     private DevicePolicyManager(Context context, Handler handler) {
@@ -61,14 +61,14 @@
         DevicePolicyManager me = new DevicePolicyManager(context, handler);
         return me.mService != null ? me : null;
     }
-    
+
     /**
      * Activity action: ask the user to add a new device administrator to the system.
      * The desired policy is the ComponentName of the policy in the
      * {@link #EXTRA_DEVICE_ADMIN} extra field.  This will invoke a UI to
      * bring the user through adding the device administrator to the system (or
      * allowing them to reject it).
-     * 
+     *
      * <p>You can optionally include the {@link #EXTRA_ADD_EXPLANATION}
      * field to provide the user with additional explanation (in addition
      * to your component's description) about what is being added.
@@ -76,14 +76,14 @@
     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
     public static final String ACTION_ADD_DEVICE_ADMIN
             = "android.app.action.ADD_DEVICE_ADMIN";
-    
+
     /**
      * The ComponentName of the administrator component.
      *
      * @see #ACTION_ADD_DEVICE_ADMIN
      */
     public static final String EXTRA_DEVICE_ADMIN = "android.app.extra.DEVICE_ADMIN";
-    
+
     /**
      * An optional CharSequence providing additional explanation for why the
      * admin is being added.
@@ -91,22 +91,21 @@
      * @see #ACTION_ADD_DEVICE_ADMIN
      */
     public static final String EXTRA_ADD_EXPLANATION = "android.app.extra.ADD_EXPLANATION";
-    
+
     /**
-     * Activity action: have the user enter a new password.  This activity
-     * should be launched after using {@link #setPasswordQuality(ComponentName, int)}
-     * or {@link #setPasswordMinimumLength(ComponentName, int)} to have the
-     * user enter a new password that meets the current requirements.  You can
-     * use {@link #isActivePasswordSufficient()} to determine whether you need
-     * to have the user select a new password in order to meet the current
-     * constraints.  Upon being resumed from this activity,
-     * you can check the new password characteristics to see if they are
-     * sufficient.
+     * Activity action: have the user enter a new password. This activity should
+     * be launched after using {@link #setPasswordQuality(ComponentName, int)},
+     * or {@link #setPasswordMinimumLength(ComponentName, int)} to have the user
+     * enter a new password that meets the current requirements. You can use
+     * {@link #isActivePasswordSufficient()} to determine whether you need to
+     * have the user select a new password in order to meet the current
+     * constraints. Upon being resumed from this activity, you can check the new
+     * password characteristics to see if they are sufficient.
      */
     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
     public static final String ACTION_SET_NEW_PASSWORD
             = "android.app.action.SET_NEW_PASSWORD";
-    
+
     /**
      * Return true if the given administrator component is currently
      * active (enabled) in the system.
@@ -121,7 +120,7 @@
         }
         return false;
     }
-    
+
     /**
      * Return a list of all currently active device administrator's component
      * names.  Note that if there are no administrators than null may be
@@ -137,7 +136,7 @@
         }
         return null;
     }
-    
+
     /**
      * @hide
      */
@@ -151,7 +150,7 @@
         }
         return false;
     }
-    
+
     /**
      * Remove a current administration component.  This can only be called
      * by the application that owns the administration component; if you
@@ -167,28 +166,28 @@
             }
         }
     }
-    
+
     /**
      * Constant for {@link #setPasswordQuality}: the policy has no requirements
      * for the password.  Note that quality constants are ordered so that higher
      * values are more restrictive.
      */
     public static final int PASSWORD_QUALITY_UNSPECIFIED = 0;
-    
+
     /**
      * Constant for {@link #setPasswordQuality}: the policy requires some kind
      * of password, but doesn't care what it is.  Note that quality constants
      * are ordered so that higher values are more restrictive.
      */
     public static final int PASSWORD_QUALITY_SOMETHING = 0x10000;
-    
+
     /**
      * Constant for {@link #setPasswordQuality}: the user must have entered a
      * password containing at least numeric characters.  Note that quality
      * constants are ordered so that higher values are more restrictive.
      */
     public static final int PASSWORD_QUALITY_NUMERIC = 0x20000;
-    
+
     /**
      * Constant for {@link #setPasswordQuality}: the user must have entered a
      * password containing at least alphabetic (or other symbol) characters.
@@ -196,7 +195,7 @@
      * restrictive.
      */
     public static final int PASSWORD_QUALITY_ALPHABETIC = 0x40000;
-    
+
     /**
      * Constant for {@link #setPasswordQuality}: the user must have entered a
      * password containing at least <em>both></em> numeric <em>and</em>
@@ -204,7 +203,7 @@
      * ordered so that higher values are more restrictive.
      */
     public static final int PASSWORD_QUALITY_ALPHANUMERIC = 0x50000;
-    
+
     /**
      * Called by an application that is administering the device to set the
      * password restrictions it is imposing.  After setting this, the user
@@ -213,16 +212,16 @@
      * will remain until the user has set a new one, so the change does not
      * take place immediately.  To prompt the user for a new password, use
      * {@link #ACTION_SET_NEW_PASSWORD} after setting this value.
-     * 
+     *
      * <p>Quality constants are ordered so that higher values are more restrictive;
      * thus the highest requested quality constant (between the policy set here,
      * the user's preference, and any other considerations) is the one that
      * is in effect.
-     * 
+     *
      * <p>The calling device admin must have requested
      * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
      * this method; if it has not, a security exception will be thrown.
-     * 
+     *
      * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
      * @param quality The new desired quality.  One of
      * {@link #PASSWORD_QUALITY_UNSPECIFIED}, {@link #PASSWORD_QUALITY_SOMETHING},
@@ -238,7 +237,7 @@
             }
         }
     }
-    
+
     /**
      * Retrieve the current minimum password quality for all admins
      * or a particular one.
@@ -255,7 +254,7 @@
         }
         return PASSWORD_QUALITY_UNSPECIFIED;
     }
-    
+
     /**
      * Called by an application that is administering the device to set the
      * minimum allowed password length.  After setting this, the user
@@ -268,11 +267,11 @@
      * {@link #PASSWORD_QUALITY_NUMERIC}, {@link #PASSWORD_QUALITY_ALPHABETIC},
      * or {@link #PASSWORD_QUALITY_ALPHANUMERIC}
      * with {@link #setPasswordQuality}.
-     * 
+     *
      * <p>The calling device admin must have requested
      * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
      * this method; if it has not, a security exception will be thrown.
-     * 
+     *
      * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
      * @param length The new desired minimum password length.  A value of 0
      * means there is no restriction.
@@ -286,7 +285,7 @@
             }
         }
     }
-    
+
     /**
      * Retrieve the current minimum password length for all admins
      * or a particular one.
@@ -303,7 +302,57 @@
         }
         return 0;
     }
-    
+
+  /**
+   * Called by an application that is administering the device to set the length
+   * of the password history. After setting this, the user will not be able to
+   * enter a new password that is the same as any password in the history. Note
+   * that the current password will remain until the user has set a new one, so
+   * the change does not take place immediately. To prompt the user for a new
+   * password, use {@link #ACTION_SET_NEW_PASSWORD} after setting this value.
+   * This constraint is only imposed if the administrator has also requested
+   * either {@link #PASSWORD_QUALITY_NUMERIC},
+   * {@link #PASSWORD_QUALITY_ALPHABETIC}, or
+   * {@link #PASSWORD_QUALITY_ALPHANUMERIC} with {@link #setPasswordQuality}.
+   *
+   * <p>
+   * The calling device admin must have requested
+   * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call this
+   * method; if it has not, a security exception will be thrown.
+   *
+   * @param admin Which {@link DeviceAdminReceiver} this request is associated
+   *        with.
+   * @param length The new desired length of password history. A value of 0
+   *        means there is no restriction.
+   */
+    public void setPasswordHistoryLength(ComponentName admin, int length) {
+        if (mService != null) {
+            try {
+                mService.setPasswordHistoryLength(admin, length);
+            } catch (RemoteException e) {
+                Log.w(TAG, "Failed talking with device policy service", e);
+            }
+        }
+    }
+
+    /**
+     * Retrieve the current password history length for all admins
+     * or a particular one.
+     * @param admin The name of the admin component to check, or null to aggregate
+     * all admins.
+     * @return The length of the password history
+     */
+    public int getPasswordHistoryLength(ComponentName admin) {
+        if (mService != null) {
+            try {
+                return mService.getPasswordHistoryLength(admin);
+            } catch (RemoteException e) {
+                Log.w(TAG, "Failed talking with device policy service", e);
+            }
+        }
+        return 0;
+    }
+
     /**
      * Return the maximum password length that the device supports for a
      * particular password quality.
@@ -314,16 +363,16 @@
         // Kind-of arbitrary.
         return 16;
     }
-    
+
     /**
      * Determine whether the current password the user has set is sufficient
      * to meet the policy requirements (quality, minimum length) that have been
      * requested.
-     * 
+     *
      * <p>The calling device admin must have requested
      * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
      * this method; if it has not, a security exception will be thrown.
-     * 
+     *
      * @return Returns true if the password meets the current requirements,
      * else false.
      */
@@ -337,11 +386,11 @@
         }
         return false;
     }
-    
+
     /**
      * Retrieve the number of times the user has failed at entering a
      * password since that last successful password entry.
-     * 
+     *
      * <p>The calling device admin must have requested
      * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to be able to call
      * this method; if it has not, a security exception will be thrown.
@@ -364,14 +413,14 @@
      * watching for failed passwords and wiping the device, and requires
      * that you request both {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} and
      * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA}}.
-     * 
+     *
      * <p>To implement any other policy (e.g. wiping data for a particular
      * application only, erasing or revoking credentials, or reporting the
      * failure to a server), you should implement
      * {@link DeviceAdminReceiver#onPasswordFailed(Context, android.content.Intent)}
      * instead.  Do not use this API, because if the maximum count is reached,
      * the device will be wiped immediately, and your callback will not be invoked.
-     * 
+     *
      * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
      * @param num The number of failed password attempts at which point the
      * device will wipe its data.
@@ -385,7 +434,7 @@
             }
         }
     }
-    
+
     /**
      * Retrieve the current maximum number of login attempts that are allowed
      * before the device wipes itself, for all admins
@@ -403,13 +452,13 @@
         }
         return 0;
     }
-    
+
     /**
      * Flag for {@link #resetPassword}: don't allow other admins to change
      * the password again until the user has entered it.
      */
     public static final int RESET_PASSWORD_REQUIRE_ENTRY = 0x0001;
-    
+
     /**
      * Force a new device unlock password (the password needed to access the
      * entire device, not for individual accounts) on the user.  This takes
@@ -422,11 +471,11 @@
      * that the password may be a stronger quality (containing alphanumeric
      * characters when the requested quality is only numeric), in which case
      * the currently active quality will be increased to match.
-     * 
+     *
      * <p>The calling device admin must have requested
      * {@link DeviceAdminInfo#USES_POLICY_RESET_PASSWORD} to be able to call
      * this method; if it has not, a security exception will be thrown.
-     * 
+     *
      * @param password The new password for the user.
      * @param flags May be 0 or {@link #RESET_PASSWORD_REQUIRE_ENTRY}.
      * @return Returns true if the password was applied, or false if it is
@@ -442,16 +491,16 @@
         }
         return false;
     }
-    
+
     /**
      * Called by an application that is administering the device to set the
      * maximum time for user activity until the device will lock.  This limits
      * the length that the user can set.  It takes effect immediately.
-     * 
+     *
      * <p>The calling device admin must have requested
      * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
      * this method; if it has not, a security exception will be thrown.
-     * 
+     *
      * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
      * @param timeMs The new desired maximum time to lock in milliseconds.
      * A value of 0 means there is no restriction.
@@ -465,7 +514,7 @@
             }
         }
     }
-    
+
     /**
      * Retrieve the current maximum time to unlock for all admins
      * or a particular one.
@@ -482,11 +531,11 @@
         }
         return 0;
     }
-    
+
     /**
      * Make the device lock immediately, as if the lock screen timeout has
      * expired at the point of this call.
-     * 
+     *
      * <p>The calling device admin must have requested
      * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
      * this method; if it has not, a security exception will be thrown.
@@ -500,16 +549,16 @@
             }
         }
     }
-    
+
     /**
      * Ask the user date be wiped.  This will cause the device to reboot,
      * erasing all user data while next booting up.  External storage such
      * as SD cards will not be erased.
-     * 
+     *
      * <p>The calling device admin must have requested
      * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} to be able to call
      * this method; if it has not, a security exception will be thrown.
-     * 
+     *
      * @param flags Bit mask of additional options: currently must be 0.
      */
     public void wipeData(int flags) {
@@ -521,7 +570,7 @@
             }
         }
     }
-    
+
     /**
      * @hide
      */
@@ -534,7 +583,7 @@
             }
         }
     }
-    
+
     /**
      * @hide
      */
@@ -547,10 +596,10 @@
             Log.w(TAG, "Unable to retrieve device policy " + cn, e);
             return null;
         }
-        
+
         ResolveInfo ri = new ResolveInfo();
         ri.activityInfo = ai;
-        
+
         try {
             return new DeviceAdminInfo(mContext, ri);
         } catch (XmlPullParserException e) {
@@ -561,7 +610,7 @@
             return null;
         }
     }
-    
+
     /**
      * @hide
      */
@@ -587,7 +636,7 @@
             }
         }
     }
-    
+
     /**
      * @hide
      */
@@ -600,7 +649,7 @@
             }
         }
     }
-    
+
     /**
      * @hide
      */