Prevent Android notification.confirm from closing on background tap

404 views
Skip to first unread message

Cory

unread,
Jan 17, 2013, 12:06:14 PM1/17/13
to phon...@googlegroups.com
Hello,
I just realized that the Android native notification.confirm dialog can be closed by tapping the background.
Is there a way to prevent it?

Thanks!

Using phonegap 2.3.0 , android 4.0.4 Xoom Tablet.

Simon MacDonald

unread,
Jan 17, 2013, 12:11:47 PM1/17/13
to phonegap
You'd have to modify the Notification.java file in and change 

setCancelable(true); 

to

setCancelable(false);


--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
 
To compile in the cloud, check out build.phonegap.com
 
 

Cory

unread,
Jan 17, 2013, 12:44:10 PM1/17/13
to phon...@googlegroups.com
Ahh i remember that when I coded in native.
Where is this file by the way?
All I could find is the android jar file and the notification.class file.

Thanks

Cory

unread,
Jan 17, 2013, 12:48:03 PM1/17/13
to phon...@googlegroups.com
Ok I think we are talking about the same file , but I dont see setCancelable(true) anywhere in that file.

Cory

unread,
Jan 17, 2013, 1:11:46 PM1/17/13
to phon...@googlegroups.com
I think you meant dialog class right?

Also , i set mCancelable to false by default and also on the show method , i used the method "setCloseOnTouchOutside" on  mWindow  ,setting it to be false on the show method.

Hmmm, ok its not working.

Simon MacDonald

unread,
Jan 17, 2013, 1:30:05 PM1/17/13
to phonegap

Cory

unread,
Jan 17, 2013, 2:10:42 PM1/17/13
to phon...@googlegroups.com
Oh in phonegap itself...
Ok, so how do I do this?
Sorry , I havent done such a thing before.
Do I download the source code from github,  edit the file and then export the jar file?
Thanks!

Airblader

unread,
Jan 17, 2013, 2:26:59 PM1/17/13
to phon...@googlegroups.com
Instead of changing the way confirm behaves my suggestion would be writing a plugin instead. That way you can update to future PG releases without the danger (and annoyance) of updating it everytime.
Message has been deleted

Cory

unread,
Jan 17, 2013, 2:46:54 PM1/17/13
to phon...@googlegroups.com
Well , I wish I knew how to do either. Found a plugin tutorial , will probably be able to do it , but it will probably take some time too.
I just want to know how to update/edit the notification.java file.
It'll be really helpful.
Thanks!

Airblader

unread,
Jan 17, 2013, 2:53:18 PM1/17/13
to phon...@googlegroups.com
Writing a plugin really isn't that hard, especially since you can basically copy the Notification plugin, give it a different name, throw out everything you don't need and add setCancelable(false). I wrote this just in a text editor, so I didn't test it and there are gonna be unused imports, but other than that it should hopefully work: http://pastebin.com/8G5KAnLe
Now you just gotta write the JS interface for the plugin, register it in your config.xml and you should be good to go.


On Thursday, January 17, 2013 8:46:01 PM UTC+1, Cory wrote:
Well , I wish I knew how to do either. Found a plugin tutorial , will probably be able to do it , but it will probably take some time too.
I just want to know how to update/edit the notification.java file.
Well be really helpful.
Thanks!


Cory

unread,
Jan 17, 2013, 3:24:51 PM1/17/13
to phon...@googlegroups.com
Well thanks for the help!

But all i got for the JS interface was this.
Im not sure how to continue. Really confused on the parameters for " confirm: function (success, fail, resultType) "
I just want to call it like how you call it in phonegap, but in this case it will be ConfirmDialogPlugin.confirm(.....) of course.

var ConfirmDialogPlugin = { 
    confirm: function (success, fail, resultType) { 
      return cordova.exec( success, fail, 
                           "com.example.ConfirmDialogPlugin", 
                           "confirm", [resultType]); 
    } 
};

Thanks

Airblader

unread,
Jan 17, 2013, 3:47:18 PM1/17/13
to phon...@googlegroups.com
Again, untested, but try this: http://pastebin.com/jeUZXtQs
You should be able to call it with window.modalConfirm.confirm(...) then. The signature is the same as the Notification plugin's confirm.

Simon MacDonald

unread,
Jan 17, 2013, 4:26:51 PM1/17/13
to phonegap
The easiest thing for you to do would be to extend the Notification class and over-ride the confirm method. The new method would be identical to the one that is already there except the one line change. Then in plugins.xml where it specifies the class that provides the Notification interface you would change it to be your class.

At least that is how I would do it. Mind you I wouldn't do it as the confirm dialog is setup that way in the Android OS and we are just following suit. 




--

Cory

unread,
Jan 17, 2013, 4:48:20 PM1/17/13
to phon...@googlegroups.com
Man, its not working. Got define is not defined in the logs.
I'm giving up.

Cant find a standard way to interface javascript with native code. I always thought there was , but cant seem to find it.
Thanks for the help anyways. Cant learn how to setup the plugins from javascript side.

Could you write down the steps to override the notification class?
In my mind it would be like:
1) Create a class in the package in src in the project (e.g com.projectname)
2) Copy the Notification code and edit
3) Change Notification plugin values in config.xml


Simon MacDonald

unread,
Jan 17, 2013, 9:56:57 PM1/17/13
to phonegap

Airblader

unread,
Jan 18, 2013, 2:21:09 AM1/18/13
to phon...@googlegroups.com
I agree, Simon's idea is better (if in fact you do want every confirm to be modal). You got the steps pretty much right:

1) Copy the Notification.java plugin into your project
2) Modify the setCancelable call
3) Adjust the class to be in the correct package (your IDE should warn you and do that for you anyway)
4) Go to res/xml/config.xml and change the Notification plugin to refer to the class you just created

That should be pretty much it.

Cory

unread,
Jan 18, 2013, 10:20:39 AM1/18/13
to phon...@googlegroups.com
@Simon Ahh yeah i saw that finally. I thought it would be atleast documented somewhere else.
I understand , I dont want to ask for code too , just tutorials or examples.

@Airblader
Ok thanks!

Anyway, I got it to work.
Well , to me since the confirms have atleast two choices , the code should expect a selection to determine which course of action should be taken.
Luckily for me , the default action does nothing so older versions of my app wont have major implications. But I just want to make sure the user reads the message and presses a button.

Its sort of like javascript confirms and alerts where you have to click the ok button before moving on.

Once again , thanks for the great help!




Airblader

unread,
Jan 18, 2013, 12:17:07 PM1/18/13
to phon...@googlegroups.com
I'm completely with you, confirm dialogs feel like a modal thing to me too – but I also agree with Simon that the native default behavior is what PG should go with. I had to do the same thing in my app, but I didn't just change the confirm dialog, I actually made a (modal) list dialog out of it to allow for more than three options.
Reply all
Reply to author
Forward
0 new messages