Office Hours — Today, July 31

Saturday, July 28

Jul 31
8:55 AM
Mark M.
has entered the room
Mark M.
turned on guest access
9:15 AM
trocchietto_Ivano
has entered the room
trocchietto_Ivano
Good morning Mark!
9:20 AM
trocchietto_Ivano
hello
View paste
not sure is working, anyway: I need to insert silenty in the calendar(content provider) some events, but does not work at all. I get an id passing after // Insert event to calendar
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);   the method getpathlastsegment, but although i have the ID the event is not registered in my calendar at all, I was wondering if can be do to the fact I have a gmail accounts, but cannot find nothing on the internet, and the example from your guide is about getting(querying) the events
9:30 AM
Mark M.
sorry! I lost track of this window!
I have not worked much with CalendarContract, beyond what is in the book
9:35 AM
trocchietto_Ivano
ok
Mark M.
when you say "the event is not registered in my calendar at all", what do you mean?
trocchietto_Ivano
that I am deploying the small prototype app on my samsung
and although I do not have any error, and the flow is correct according to the debug, after I launch contentprovider.insert, I do not get any result
what can go wrong is 1)contentProvider.Insert is not enough to insert an event to the Calendar 2) I have multiple accounts and does not work 3) gmail needs a special oauth
Mark M.
what do you mean by "I do not get any result"? do you mean that the local calendar app is not showing the event? do you mean that the event is not being synchronized to the server? do you mean something else?
trocchietto_Ivano
and most important maybe I did not put the method in the right place, because of the permissions
I mean the local calendar( the one I have on my phone with 3 gmail accounts) do not show the event
Mark M.
is this the official Google Calendar app, or is it some Samsung replacement?
9:40 AM
trocchietto_Ivano
I tried the samsung replacement but also the google default app(where also are registered 3 gmail accounts)
Mark M.
have you tried querying the provider using a ContentResolver, to see if you can find your event that way? if that fails, then there is some problem with the insert itself -- if that succeeds, though, that suggests that something is out of sync between the event data and what the apps are trying to show
trocchietto_Ivano
this is a good idea, but looks really not pratical to me to insert a query, but is a reasonable way to go
to see if the event is registered in the db by the calendar content provider!
but can i ask you about permissions
basically when the ide(android studio) say me that I need to register the permissions to READ_CALENDAR
I do not put nothing in the call back onResult
but the command is passed directly into the method where I have everything
so I guess maybe I miss something with permissions
Mark M.
for insert(), you should need WRITE_CALENDAR, though I would expect a SecurityException if you do not hold that
trocchietto_Ivano
exaclty
that is why the system set up all the marshmellow thing
9:45 AM
trocchietto_Ivano
the manifest check, the rationale and the insert that will trigger the onResult call back
my question is should I put some command into the onResult switch case Tag related to write calendar? because I did not put any command in the callback
(hope is clear)
Mark M.
I do not completely understand your permission flow, but you should not be doing anything that needs a runtime permission until you get it, and usually that requires you to do work in onRequestPermissionsResult()
so, you check to see if you hold the permission, and if you do, you go ahead with the protected work... but if you do not hold the permission, you request it, and in onRequestPermissionsResult() you go ahead with the protected work
trocchietto_Ivano
i am logging it on the pc where i have the snippets..
one sec
Ivano M.
has entered the room
Ivano M.
here I am back
9:50 AM
Mark M.
um, hi!
Ivano M.
basically in the call back I have this
View paste (2 more lines)
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch(requestCode){
    case RC_WRITE_CALENDAR:
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // permission grante
            // here i did not write nothing!!!!!!!!!!!!!


        } else {
            // do stuff without the permission
        }
        break;

}
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
...
instead in the onCreate I have all the stuff
View paste (31 more lines)

        Calendar beginTime = Calendar.getInstance();
        beginTime.set(2018, 10, 10, 8, 30);
        startMillis = beginTime.getTimeInMillis();
        Calendar endTime = Calendar.getInstance();
        endTime.set(2018, 10, 10, 10, 30);
        endMillis = endTime.getTimeInMillis();
        checkPermission();

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_CALENDAR)) {
                Log.d(TAG, "checkPermission: ");
                new AlertDialog.Builder(this)
                        .setMessage("You need to allow access to the calendar.")
...
Mark M.
that code will not work if you do not hold the permission
ActivityCompat.requestPermissions() is asynchronous
so, you call that, then blow right on to your CalendarContract code, even though you do not yet hold the permission
the code starting with cr = this.getContentResolver(); through the end should be moved into a separate method
Ivano M.
you mean a kind of asyncTask?
Mark M.
no, just a method
call that method from onCreate() if you already hold the permission, or call it from onRequestPermissionResult() if you are then granted the permission
Ivano M.
but the compiler will not know that
Mark M.
so?
this is covered in many places in my book -- pretty much every example of mine that uses runtime permissions has an AbstractPermissionActivity that wraps up this pattern
Ivano M.
ah you mean an utility class
Mark M.
in my case, it's a method -- the AbstractPermissionActivity handles all the permission work, delegating to the subclass for the actual protected work
9:55 AM
Ivano M.
cool
Mark M.
yes, there are many samples that use that same basic class
Ivano M.
OK I am going to read your chapter on the permissions
and then look into your AbstractPermissionActivity classes
and how they get implmented by the one that inherit
Mark M.
when you request permissions in onCreate(), you also run into configuration-change issues, which AbstractPermissionActivity handles
Ivano M.
is also really efficient in terms of clean code
i mean single responsibility
by the way if in future I have questions about clean architecture you are skilled on that?
Mark M.
not really
Ivano M.
are you*
OK
I love oncle bob
Mark M.
I mean, I know what it is, but I do not claim to be an expert in the subject
Ivano M.
I understand
thank you for your help you adressed me on the right direction
10:00 AM
Mark M.
OK, that's all for today's chat
the transcript will go up on https://commonsware.com/office-hours/ shortly
the next chat is Thursday at 7:30pm US Eastern
have a pleasant day!
trocchietto_Ivano
has left the room
Ivano M.
has left the room
Mark M.
turned off guest access

Saturday, July 28

 

Office Hours

People in this transcript

  • Ivano M0n1anin0
  • Mark Murphy
  • trocchietto_Ivano