mattsilber / applock

A simple library for locking and unlocking Activities with a PIN code or Fingerprint (e.g. a child lock).
Other
87 stars 43 forks source link

Getting next activity without asking for lock when installed first time . #5

Closed abhi0611 closed 7 years ago

abhi0611 commented 7 years ago

Hello @mattsilber ,

Please find my code below: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    if (android.os.Build.VERSION.SDK_INT >= 23) {
        fingerScannerMain();
    } else {
        passwordLock();
    }
}

public void passwordLock(){

    new CreateLockDialogBuilder(this,
            new CreateLockDialogBuilder.LockCreationListener(){
                public void onLockCanceled(){ } // Dialog was closed without entry
                public void onLockSuccessful(){
                    Intent intent = new Intent(MainActivity.this, WelcomeClass.class);
                    startActivity(intent);
                }
            })
            .show();

    ActionLockingHelper.unlockIfRequired(this, new UnlockDialogBuilder.UnlockEventListener() {
        public void onCanceled() {
        } // Dialog was closed without entry

        public void onUnlockFailed(String reason) {
        } // Not called with default Dialog, instead is handled internally

        public void onUnlockSuccessful() {
            Intent intent = new Intent(MainActivity.this, WelcomeClass.class);
            startActivity(intent);
        }
    });

}

When I am running the code from android studio, it gets installed in my phone and directly opens the WelcomeClass activity rather than asking for "Enter a 4-digit code to lock app". When I press back at that time it is asking me for the lock at that time it works fine but I am not sure why it is going directly to the next activity before lock. Getting exception as well "E/WindowManager: android.view.WindowLeaked:". Also if I clicked outside the dialog box, it does not exit the program it continues with the next activity, ideally it should not let you proceed if you touched outside the dialog box.

mattsilber commented 7 years ago

You're making 2 async calls and expecting them to work in sequence. i.e. You're asking to create a PIN, and then asking them to unlock before they've had the chance to even choose one.

You're getting that exception for the same reason: you're showing a dialog and then immediately moving to another Activity.

If you want to prevent them from getting to the success without a PIN, check to see if a PIN exists before using the unlocker. e.g.

@Override
public void onResume(){
    super.onResume();

    if(LockingHelper.hasSavedPIN(this))
        showUnlockDialog();
    else 
        new CreateLockDialogBuilder(this, new CreateLockDialogBuilder.LockCreationListener(){
                public void onLockCanceled(){ 
                    finish(); // No PIN entered, don't let them through
                } // Dialog was closed without entry
                public void onLockSuccessful(){
                    showUnlockDialog();
                }})
                .show();
}

private void showUnlockDialog(){
    ActionLockingHelper.unlockIfRequired(this, new UnlockDialogBuilder.UnlockEventListener() {
        public void onCanceled() { 
            finish(); // No PIN entered, don't let them through
        } // Dialog was closed without entry
        public void onUnlockFailed(String reason) { 
            finish(); // No PIN entered, don't let them through
        } // Not called with default Dialog, instead is handled internally
        public void onUnlockSuccessful() {
            Intent intent = new Intent(MainActivity.this, WelcomeClass.class);
            startActivity(intent);
        }
    });
}
abhi0611 commented 7 years ago

Hello @mattsilber ,

Sorry, I forgot to write to you. I got your point I was making asyc calls, thanks for the explanation and I was looking for has saved pin function in other two java classes not inside the lockingHelper class there I was going wrong. Thanks for the help, I think you can close this issue.