Skip to content

Commit 24c6b35

Browse files
committed
Warning notification when password attempt fails
Shows a notification of the form "Password entered incorrectly X times" and "User is set to wipe after Y attempts" which is updated after each failed password attempt. Change-Id: I57ef893681d2883195ce009a84382a011436aa7b
1 parent 909addb commit 24c6b35

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

app/src/main/java/com/afwsamples/testdpc/DeviceAdminReceiver.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class DeviceAdminReceiver extends android.app.admin.DeviceAdminReceiver {
5959
"com.afwsamples.testdpc.policy.PASSWORD_REQUIREMENTS_CHANGED";
6060

6161
private static final int CHANGE_PASSWORD_NOTIFICATION_ID = 101;
62+
private static final int PASSWORD_FAILED_NOTIFICATION_ID = 102;
6263

6364
@Override
6465
public void onReceive(Context context, Intent intent) {
@@ -235,6 +236,45 @@ public static ComponentName getComponentName(Context context) {
235236
return new ComponentName(context.getApplicationContext(), DeviceAdminReceiver.class);
236237
}
237238

239+
@Override
240+
public void onPasswordFailed(Context context, Intent intent) {
241+
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(
242+
Context.DEVICE_POLICY_SERVICE);
243+
/*
244+
* Post a notification to show:
245+
* - how many wrong passwords have been entered;
246+
* - how many wrong passwords need to be entered for the device to be wiped.
247+
*/
248+
int attempts = devicePolicyManager.getCurrentFailedPasswordAttempts();
249+
int maxAttempts = devicePolicyManager.getMaximumFailedPasswordsForWipe(null);
250+
251+
String title = context.getQuantityString(
252+
R.plurals.password_failed_attempts_title, attempts, attempts);
253+
String content = maxAttempts == 0
254+
? context.getString(R.string.password_failed_no_limit_set)
255+
: context.getQuantityString(
256+
R.plurals.password_failed_attempts_content, maxAttempts, maxAttempts);
257+
258+
Notification.Builder warn = new Notification.Builder(context)
259+
.setSmallIcon(R.drawable.ic_launcher)
260+
.setTicker(title)
261+
.setContentTitle(title)
262+
.setContentText(content)
263+
.setContentIntent(PendingIntent.getActivity(context, /* requestCode */ -1,
264+
new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD), /* flags */ 0));
265+
266+
NotificationManager nm = (NotificationManager)
267+
context.getSystemService(Context.NOTIFICATION_SERVICE);
268+
nm.notify(PASSWORD_FAILED_NOTIFICATION_ID, warn.getNotification());
269+
}
270+
271+
@Override
272+
public void onPasswordSucceeded(Context context, Intent intent) {
273+
NotificationManager nm = (NotificationManager)
274+
context.getSystemService(Context.NOTIFICATION_SERVICE);
275+
nm.cancel(PASSWORD_FAILED_NOTIFICATION_ID);
276+
}
277+
238278
@Override
239279
public void onPasswordChanged(Context context, Intent intent) {
240280
updatePasswordQualityNotification(context);

app/src/main/res/values/strings.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,16 @@
479479
<string name="password_not_compliant_title">Password is not compliant</string>
480480
<string name="password_not_compliant_content">Touch to set a new lock screen password.</string>
481481

482+
<plurals name="password_failed_attempts_title">
483+
<item quantity="one">Password entered incorrectly %d time</item>
484+
<item quantity="other">Password entered incorrectly %d times</item>
485+
</plurals>
486+
<plurals name="password_failed_attempts_content">
487+
<item quantity="one">Set to wipe after %d attempt.</item>
488+
<item quantity="other">Set to wipe after %d attempts.</item>
489+
</plurals>
490+
<string name="password_failed_no_limit_set">No limit set on failed password attempts.</string>
491+
482492
<!-- Strings for managing Settings -->
483493
<string name="settings_management_title">Settings management</string>
484494
<string name="stay_on_while_plugged_in">Keep the device on while plugged in</string>

0 commit comments

Comments
 (0)