-1

I have a Enter Password Dialog which is an Activity in a Theme.Dialog theme so it actually looks like a AlertDialog as i have to use it in a broadcast receiver but the problem is i want to block the HOME button as i need it for a security applications, the blocking of the HOME button works when i use this

@Override
public void onAttachedToWindow()
{  
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();  
}

but it doesn't relaunch my PasswordDialog activity if the password is wrong after clicking a button, any suggestions?

Validation code:

login.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        password = inputPassword.getText().toString();               
        final String SHA1hash = PhysicalTheftPassword.getSHA1(password); 

        if (correctSHA1.equals(SHA1hash)) {

            //SharedPreferences sp = getSharedPreferences("isPhysicalTheftEnabled", MODE_WORLD_READABLE);
            //SharedPreferences.Editor ed = sp.edit();
            //ed.putBoolean("isPhysicalTheftEnabled", false);
            //ed.commit();

            Toast.makeText(PhysicalTheftDialog.this, "Correct", Toast.LENGTH_LONG).show();
            finish();   
            stopService(new Intent(PhysicalTheftDialog.this, MyService.class));
            Log.v(TAG, "SHA1 Hash:" + SHA1hash);
            Log.v(TAG, "Correct SHA1:" + correctSHA1);
        }
        else {
            Toast.makeText(PhysicalTheftDialog.this, "Wrong", Toast.LENGTH_LONG).show();
            Intent Act2Intent = new Intent(PhysicalTheftDialog.this, PhysicalTheftDialog.class);              
            finish();
            startActivity(Act2Intent);
            Log.v(TAG, "SHA1 Hash:" + SHA1hash);
            Log.v(TAG, "Correct SHA1:" + correctSHA1);


        }
Kara
  • 6,115
  • 16
  • 50
  • 57
dythe
  • 840
  • 5
  • 21
  • 45
  • http://stackoverflow.com/questions/6836869/android-how-to-control-the-home-button?rq=1 ? – t0mm13b Jul 29 '12 at 00:45
  • I've seen an example of someone doing what you were looking for. Hope it helps! http://stackoverflow.com/a/6530002/1559836 – Mintrus Jul 29 '12 at 00:46
  • well that is referring to some "phantom" java source *DisableAllKey.java* that the person who answered mentioned.... – t0mm13b Jul 29 '12 at 00:49
  • Worrying about re-launching your password dialog activity should be secondary to making sure no other activity in your app is willing to do anything until the password condition has been satisfied. – Chris Stratton Jul 29 '12 at 01:55
  • Maybe then, but the user won't have any chance to type in the password again – dythe Jul 29 '12 at 14:22
  • 1
    If they push the home button, its presumably because they don't want to use your app right now. – Chris Stratton Jul 29 '12 at 15:21

1 Answers1

0

That seems more like a problem with the validation you are using on your dialog enter button. Rather than having trouble with suppressing the home button as your title states.

If you post the code you are using for that perhaps we can help. Ideally though what you want to do is not dismiss the dialog if the password was incorrect, that way there would be no need to re-show it, as it would still be showing.

Also just as a heads up there is no officially supported way to suppress the home button within the public APIs. The method that you are using to do so has been fixed in newer versions of Android and no longer works.

EDIT: I have two suggestions

if you remove these 3 lines:

Intent Act2Intent = new Intent(PhysicalTheftDialog.this, PhysicalTheftDialog.class);              
finish();
startActivity(Act2Intent);

inside the Wrong branch of your if statement the dialog should remain visible on the screen waiting for the user to try again. You could do something like this instead of these 3 lines:

password.setText("");

which will clear out the password EditText for them so they don't have to backspace to clear their old (incorrect) password when they try again.

My other suggestion, try changing the order of those 3 lines to be like this:

Intent Act2Intent = new Intent(PhysicalTheftDialog.this, PhysicalTheftDialog.class);              
startActivity(Act2Intent);
finish();

Quite honestly I am a bit surprised if that does actually work with the order that you had them in even without the home button suppression bit. Calling finish would (I think) not allow any code that happens after that to execute (in this case the startActivity();) Since your activity will be gone as soon as you call finish() By calling startActivity() before finish it should allow it to execute properly.

If it were me I'd work toward trying to make it work the way I posted as the first suggestion though. To simply leave the current password dialog showing awaiting another attempt rather than to hide it and then show a new instance of the same thing.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • nah the validation works when this piece of code is non-existant – dythe Jul 29 '12 at 14:22
  • if it doesn't work when you add this code, it seems to me that you have 2 options. You can either change your validation so that it *will* work while using this code. Or you can remove the above code. My honest opinion is that you should go with the latter. The exploit that you are using to suppress the home button is consider malicious by the people who create the platform (and as such has been fixed in newer versions of the OS). If you want to go with the former then post your validation code and I can try to help you correct it. If you are unwilling to do either, there is little we can do. – FoamyGuy Jul 29 '12 at 14:33
  • Updated the post with my validation code. If it is considered malicious, is there other workaround that i can do to produce the same effect? – dythe Jul 29 '12 at 15:15
  • Nope, the act of suppressing the home button is what is considered malicious. By doing so you are attempting to disallow the user from leaving your Activity. The system was built specifically the way it was specifically so that an application cannot "take control" of a users device by not allowing them to leave whenever they want (by pressing the home button) – FoamyGuy Jul 29 '12 at 15:45
  • @dythe see my edit, I think you need to call `startActivity()` before `finish()` OR (and this is probably a better solution) instead of calling `finish()`/`startActivity()` at all just leave the current activity instance showing and wait for the user to try again. – FoamyGuy Jul 29 '12 at 15:57
  • Your approach works! but the problem is the user is still able to swipe the notification manager down is there a way to block it? – dythe Jul 29 '12 at 16:11