0

I have already given my app the administer privileges, now I want to monitor no. of failed password attempts using DeviceAdminReceiver or BoradcastReceiver. Can someone provide the code or some help? Also, where should I count the no. of failed attempts? In the class that is broadcasting or in the one which is receiving? Please explain in detail if you can. Thank you!

My code:

MainActivity.java

package com.bew.locksmith;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {


    int counter=0;
    public int attempts=0;

    private static final int ADMIN_INTENT = 15;

    private static final String description = "Sample Administrator description";
    public static final String ACTION_PASSWORD_FAILED= "android.app.action.ACTION_PASSWORD_FAILED";
    private DevicePolicyManager mDevicePolicyManager, mDevicePolicyManager2; 
    private ComponentName mComponentName,mComponentName2;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDevicePolicyManager = (DevicePolicyManager)getSystemService(  
                Context.DEVICE_POLICY_SERVICE);  
        mComponentName = new ComponentName(this, MyAdminReceiver.class);  //MyadminReciever changed here 
        Button btnEnableAdmin = (Button) findViewById(R.id.btnEnable);
        Button btnDisableAdmin = (Button) findViewById(R.id.btnDisable);
        Button btnLock = (Button) findViewById(R.id.btnLock);
        btnEnableAdmin.setOnClickListener(this);
        btnDisableAdmin.setOnClickListener(this);
        btnLock.setOnClickListener(this);

    /*  mDevicePolicyManager2 = (DevicePolicyManager)getSystemService(  
                Context.);  */

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnEnable:
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,description);
            startActivityForResult(intent, ADMIN_INTENT);
            break;

        case R.id.btnDisable:
            mDevicePolicyManager.removeActiveAdmin(mComponentName);  
            Toast.makeText(getApplicationContext(), "Admin registration removed", Toast.LENGTH_SHORT).show();
            break;

        case R.id.btnLock:
            boolean isAdmin = mDevicePolicyManager.isAdminActive(mComponentName);  
            if (isAdmin) {  
                mDevicePolicyManager.lockNow();  
            }else{
                Toast.makeText(getApplicationContext(), "Not Registered as admin", Toast.LENGTH_SHORT).show();
            }
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ADMIN_INTENT) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(getApplicationContext(), "Registered As Admin", Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(getApplicationContext(), "Failed to register as Admin", Toast.LENGTH_SHORT).show();
            }
        }
    }

}

MyAdminReceiver.java

 package com.bew.locksmith;



import android.app.admin.DeviceAdminReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class MyAdminReceiver extends DeviceAdminReceiver{





          private static final String KEY_ATTEMPTS_NO = "attempts_no";
          private static final int LIMIT = 3;

          @Override
          public void onPasswordFailed(Context context, Intent intent) {
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
            int attempts = sharedPrefs.getInt(KEY_ATTEMPTS_NO, 0) + 1;

            if (attempts == LIMIT) {
              // Reset the counter
              sharedPrefs.edit().putInt(KEY_ATTEMPTS_NO, 0).apply();

              // Launch your activity
              context.startActivity(new Intent(context, test.class));
              Toast.makeText(context, "start activity", Toast.LENGTH_LONG).show();
            } else {
              // Save the new attempts number
              sharedPrefs.edit().putInt(KEY_ATTEMPTS_NO, attempts).apply();
            }
          }

          @Override
          public void onPasswordSucceeded(Context context, Intent intent) {
            // Reset number of attempts
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
            sharedPrefs.edit().putInt(KEY_ATTEMPTS_NO, 0).apply();
          }








    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        // TODO Auto-generated method stub
        Toast.makeText(arg0, "here", Toast.LENGTH_LONG).show();

        /*Intent i = new Intent();
        i.setClassName("com.bew.locksmith","com.bew.locksmith.test");
        arg0.startActivity(i);*/

        //String action = arg1.getAction();


    }




}

test.java

    package com.bew.locksmith;

import android.app.Activity;
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.Toast;

public class test extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
    }
}

Manifest:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bew.locksmith"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-permission android:name="android.permission.CAMERA"/>

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.bew.locksmith.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <receiver
            android:name="MyAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/admin"/>

            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>

         <activity
            android:name="com.bew.locksmith.test"
            android:label="@string/app_name" >
          <category android:name="android.intent.category.DEFAULT" />

        </activity>

    </application>

</manifest>

Update: Added my code, now remove that -1, whoever had done that!

Aman B.
  • 138
  • 4
  • 12
  • For the record, if anyone cares I still haven't got any solution, so thank you stackoverflow, many thanks to the user @commonsware and two more whom I can't tag because S.O. isn't allowing me. – Aman B. Jan 15 '15 at 14:05

1 Answers1

0

See CommonsWare's answer:

You can set up a DeviceAdminReceiver that will be notified about failed password attempts, as well as a successful password attempt that occurred after a failed attempt. This is covered in the documentation of Android's device administration APIs.

Note that the user will have to agree to allow your app to serve as a device administrator, via the Settings app, before you will get these events.

This sample project demonstrates listening for those events, plus setting up a password quality policy. The key pieces are:

What you need to do is change the DeviceAdminReceiver implementation to this:

public class AdminReceiver extends DeviceAdminReceiver {
    @Override
    public void onPasswordFailed(Context ctxt, Intent intent) {
        DevicePolicyManager mgr = (DevicePolicyManager) ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
        int no = mgr.getCurrentFailedPasswordAttempts();

        if (no >= 3) {
            context.startActivity(new Intent(context, MyActivity.class));
        }
    }
}
Community
  • 1
  • 1
aluxian
  • 986
  • 10
  • 27
  • Thanks, for answering but nothing happens if the password is entered incorrectly more than three times, I am trying to open a class having a layout with just a string in it, but nothing is happening it's just normal. – Aman B. Jan 10 '15 at 20:17
  • Okay, to be honest I'm not sure whether you can launch an activity like this. But let's test the logic, add `Toast.makeText(context, "start activity", Toast.LENGTH_LONG).show();` after `// Launch your activity` and tell me if it shows up. – aluxian Jan 10 '15 at 20:19
  • Also, in Logcat, the first error I see is Tag : "SurfaceFlinger" in Text " ro.sf.lcd_density must be defined as a build property" – Aman B. Jan 10 '15 at 20:20
  • Nothing changed with the previous code, I'll try the new one now. – Aman B. Jan 10 '15 at 20:31
  • Also check [this](http://stackoverflow.com/a/13975019/1133344) out. – aluxian Jan 10 '15 at 20:33
  • Not helping, the link you gave just says there is way to send data through sharedpreferences not sure how to use that and commonsware's code is confusing as well. :/ – Aman B. Jan 10 '15 at 20:41