1
00:00:03,890 --> 00:00:06,210
Alright, so we're now ready to run the

2
00:00:06,210 --> 00:00:09,480
app again, to see how it behaves. We're

3
00:00:09,480 --> 00:00:11,070
going to do that, then talk about what's

4
00:00:11,070 --> 00:00:13,500
wrong with the code. So we need to see,

5
00:00:13,500 --> 00:00:15,420
firstly, how the app behaves some scratch.

6
00:00:15,420 --> 00:00:17,760
So I'm going to go back to our emulator,

7
00:00:17,760 --> 00:00:20,580
and I'm going to delete this app so

8
00:00:20,580 --> 00:00:23,730
we're starting from a clean slate. So I'm

9
00:00:23,730 --> 00:00:28,980
going to install that, and I'll run it

10
00:00:28,980 --> 00:00:32,940
again, and we'll open our logcat so that

11
00:00:32,940 --> 00:00:35,610
we can see what's going on. So the first

12
00:00:35,610 --> 00:00:37,920
test that we want to do here, is to see

13
00:00:37,920 --> 00:00:41,250
how it behaves when we deny access. I'm just

14
00:00:41,250 --> 00:00:42,210
gonna move this over a little bit more,

15
00:00:42,210 --> 00:00:45,920
so we can see a bit more of the log.

16
00:00:45,920 --> 00:00:48,360
Alright, so as I said, the first test is

17
00:00:48,360 --> 00:00:51,390
going to be to see how it behaves when

18
00:00:51,390 --> 00:00:53,190
we deny access. So I'm gonna click on

19
00:00:53,190 --> 00:00:57,420
Deny, and now when I click on the FAB, it

20
00:00:57,420 --> 00:01:00,120
should display our Snackbar and the link

21
00:01:00,120 --> 00:01:02,280
there to Grant Access, which it is. And

22
00:01:02,280 --> 00:01:04,470
that should continue to appear whenever

23
00:01:04,470 --> 00:01:06,450
we click the FAB. So if I swipe that off

24
00:01:06,450 --> 00:01:09,210
screen - the Snackbar. Click it again - it

25
00:01:09,210 --> 00:01:10,830
comes back again, which is good and which is

26
00:01:10,830 --> 00:01:13,110
the expected behavior. And in the logcat,

27
00:01:13,110 --> 00:01:15,509
you can see as I'm doing that,  the FAB's

28
00:01:15,509 --> 00:01:17,250
onClick method starting and ending.

29
00:01:17,250 --> 00:01:19,439
Alright, so let's see now what happens

30
00:01:19,439 --> 00:01:23,700
when the Grant Access link is clicked. So

31
00:01:23,700 --> 00:01:25,740
the logcat now, you can see, has the

32
00:01:25,740 --> 00:01:27,299
onClick method starting again - this time

33
00:01:27,299 --> 00:01:29,400
a call to requestPermissions, and then

34
00:01:29,400 --> 00:01:31,290
the onClick method ending. And as a

35
00:01:31,290 --> 00:01:33,030
result, we've now got the Android dialogue

36
00:01:33,030 --> 00:01:34,890
asking for permission to access the

37
00:01:34,890 --> 00:01:36,900
contacts. So when I come over here and

38
00:01:36,900 --> 00:01:40,079
click on Deny again, we see permission

39
00:01:40,079 --> 00:01:41,520
refused over here, in the log.

40
00:01:41,520 --> 00:01:43,950
We're now onRequestPermissionsResult:

41
00:01:43,950 --> 00:01:46,649
callback is called. So let's do that

42
00:01:46,649 --> 00:01:47,720
again,

43
00:01:47,720 --> 00:01:50,399
but this time, when I click on Grant

44
00:01:50,399 --> 00:01:52,350
Access, I'm going to check the Don't ask

45
00:01:52,350 --> 00:01:56,729
again checkbox and click on Deny. So now

46
00:01:56,729 --> 00:01:59,070
effectively, what I've done, is told

47
00:01:59,070 --> 00:02:01,950
Android not to ask again. So in terms

48
00:02:01,950 --> 00:02:06,450
of our code, the call to shouldShowRequest

49
00:02:06,450 --> 00:02:08,788
PermissionRationale should now return

50
00:02:08,788 --> 00:02:10,679
false. So in other words, this bit of

51
00:02:10,679 --> 00:02:12,540
code down here should now be

52
00:02:12,540 --> 00:02:13,500
executed.

53
00:02:13,500 --> 00:02:17,820
So let's actually check that. I'm going

54
00:02:17,820 --> 00:02:19,170
to click on the FAB button again,

55
00:02:19,170 --> 00:02:22,980
click on Grant Access, and you can see

56
00:02:22,980 --> 00:02:24,360
what's happened in this case - that code

57
00:02:24,360 --> 00:02:27,690
was executed, and the settings for the

58
00:02:27,690 --> 00:02:29,280
application is now showing on the screen

59
00:02:29,280 --> 00:02:31,560
again. so that bit's working well, and

60
00:02:31,560 --> 00:02:33,390
looking at the logcat down here as well,

61
00:02:33,390 --> 00:02:34,830
you can see the URI showing on the

62
00:02:34,830 --> 00:02:37,530
screen there; package:academy.learn

63
00:02:37,530 --> 00:02:40,290
programming.contentproviderexample. So

64
00:02:40,290 --> 00:02:42,900
it's that URI, together with the ACTION

65
00:02:42,900 --> 00:02:44,700
_APPLICATION_

66
00:02:44,700 --> 00:02:46,890
DETAILS_SETTING - a value that

67
00:02:46,890 --> 00:02:49,980
we used in setAction - that sets, or let's

68
00:02:49,980 --> 00:02:51,780
rather, Android open the settings for

69
00:02:51,780 --> 00:02:53,580
this app, rather than just going into the

70
00:02:53,580 --> 00:02:56,670
main settings screen. And notice down here

71
00:02:56,670 --> 00:03:00,600
also, in the log, that the Snackbar's

72
00:03:00,600 --> 00:03:03,000
onClick method has finished. So you can

73
00:03:03,000 --> 00:03:05,430
see the Snackbar onClick: ends entry

74
00:03:05,430 --> 00:03:07,800
after the launching of the settings, and

75
00:03:07,800 --> 00:03:09,750
we'll see why that's important in a

76
00:03:09,750 --> 00:03:11,940
moment. Alright, so back over in our app,

77
00:03:11,940 --> 00:03:15,630
I'm going to click on Permissions, and

78
00:03:15,630 --> 00:03:17,280
then I'm going to grant access to our

79
00:03:17,280 --> 00:03:20,010
Contacts, by turning that on. Once I've

80
00:03:20,010 --> 00:03:23,090
done that, tapping back a few times

81
00:03:23,090 --> 00:03:25,200
should take us back to our app, which it's

82
00:03:25,200 --> 00:03:27,450
done here now. But notice that when I'm

83
00:03:27,450 --> 00:03:29,040
doing that, nothing's appeared over here

84
00:03:29,040 --> 00:03:31,380
in the logcat. The app now has the

85
00:03:31,380 --> 00:03:33,840
permission it needs, but it doesn't know

86
00:03:33,840 --> 00:03:36,750
that it has got that permission. So you

87
00:03:36,750 --> 00:03:38,100
can see, basically at this point, where

88
00:03:38,100 --> 00:03:40,320
I've tapped the FAB again, the Snackbar

89
00:03:40,320 --> 00:03:41,850
still appearing instead of our contact

90
00:03:41,850 --> 00:03:44,700
details. Now if I quit the app, close this

91
00:03:44,700 --> 00:03:52,480
down, and run it again and

92
00:03:52,480 --> 00:03:55,629
click on our button - our FAB - it is actually

93
00:03:55,629 --> 00:03:57,670
working correctly. And you can see, in

94
00:03:57,670 --> 00:03:59,409
fact, the permission's granted now

95
00:03:59,409 --> 00:04:02,290
showing in our logcat. But really,

96
00:04:02,290 --> 00:04:03,879
that's not the best user experience

97
00:04:03,879 --> 00:04:06,010
requiring the user to shut down the app

98
00:04:06,010 --> 00:04:07,810
and start it up again, in order to

99
00:04:07,810 --> 00:04:08,379
proceed.

100
00:04:08,379 --> 00:04:09,819
So let's actually have a look at the

101
00:04:09,819 --> 00:04:12,819
code, to see what's gone wrong here. So

102
00:04:12,819 --> 00:04:15,280
the actual problem is caused by the test

103
00:04:15,280 --> 00:04:18,470
in the FAB's onClick method - and

104
00:04:18,470 --> 00:04:21,750
this is the code here, basically line

105
00:04:21,750 --> 00:04:25,590
46. So we've used readGranted here, to

106
00:04:25,590 --> 00:04:27,300
store whether we have access or not,

107
00:04:27,300 --> 00:04:29,759
but we haven't updated it after going

108
00:04:29,759 --> 00:04:32,219
into the settings. So consequently, it

109
00:04:32,219 --> 00:04:34,199
actually failed there and returned false,

110
00:04:34,199 --> 00:04:36,569
and we weren't able to get access to our

111
00:04:36,569 --> 00:04:38,879
contacts, until we closed down the app and

112
00:04:38,879 --> 00:04:40,770
started it again. Now I'm not going to

113
00:04:40,770 --> 00:04:43,169
say never store state in a variable, like

114
00:04:43,169 --> 00:04:45,300
we've done here, but what I will say is

115
00:04:45,300 --> 00:04:47,310
think long and hard before doing so.

116
00:04:47,310 --> 00:04:50,039
Sometimes it can be necessary, but

117
00:04:50,039 --> 00:04:52,319
generally, it's much tidier to check the

118
00:04:52,319 --> 00:04:54,360
state when you need it, rather than

119
00:04:54,360 --> 00:04:55,770
storing it in a field, as we've done here.

120
00:04:55,770 --> 00:04:57,990
And the problem we've got here, is that

121
00:04:57,990 --> 00:05:00,180
we check to see if we had access in the

122
00:05:00,180 --> 00:05:02,759
onCreate method, and set readGranted to

123
00:05:02,759 --> 00:05:05,400
true or false, to reflect the state of

124
00:05:05,400 --> 00:05:08,039
our access. But down in the onRequest

125
00:05:08,039 --> 00:05:10,169
PermissionResult callback method, we

126
00:05:10,169 --> 00:05:13,139
also set it to true. Scroll down and

127
00:05:13,139 --> 00:05:16,169
have a look at that - this one here, code

128
00:05:16,169 --> 00:05:18,449
standing on line 91. So we also set that

129
00:05:18,449 --> 00:05:21,000
to true if access is granted, after

130
00:05:21,000 --> 00:05:23,729
calling requestPermissions. So that

131
00:05:23,729 --> 00:05:25,949
means that the app would work, after

132
00:05:25,949 --> 00:05:28,349
tapping Allow in the dialogue. But our

133
00:05:28,349 --> 00:05:30,719
code that launches the app's Settings, has

134
00:05:30,719 --> 00:05:32,699
no way of knowing whether the user

135
00:05:32,699 --> 00:05:35,370
granted access or not. Now of course, we

136
00:05:35,370 --> 00:05:36,690
know that they went into the settings

137
00:05:36,690 --> 00:05:39,180
because we sent them there, but we've got

138
00:05:39,180 --> 00:05:41,430
no control over what they did, when they

139
00:05:41,430 --> 00:05:43,589
got into the settings. So it's very

140
00:05:43,589 --> 00:05:45,930
tempting to store things like, whether

141
00:05:45,930 --> 00:05:47,339
you have a particular permission, and

142
00:05:47,339 --> 00:05:50,610
it's also very common. But once you start

143
00:05:50,610 --> 00:05:52,800
doing that, you'll find yourself adding

144
00:05:52,800 --> 00:05:54,479
more code to keep the variable

145
00:05:54,479 --> 00:05:57,060
up-to-date as state changes. We saw

146
00:05:57,060 --> 00:05:58,589
earlier that the Snackbar's onClick

147
00:05:58,589 --> 00:06:01,020
method ends while the user is still in

148
00:06:01,020 --> 00:06:03,180
the Settings, so we don't really have a

149
00:06:03,180 --> 00:06:05,550
convenient way to check the permissions

150
00:06:05,550 --> 00:06:08,190
again. So a good rule is, if you want to

151
00:06:08,190 --> 00:06:10,020
know the state of your environment, then

152
00:06:10,020 --> 00:06:11,789
check it each time you need to know,

153
00:06:11,789 --> 00:06:14,069
rather than storing that state in a

154
00:06:14,069 --> 00:06:16,110
field. So don't store the state, because

155
00:06:16,110 --> 00:06:17,370
you'll end up struggling to keep your

156
00:06:17,370 --> 00:06:20,699
stored value up-to-date. Alright, so how

157
00:06:20,699 --> 00:06:22,349
do we go about fixing that? Well let's

158
00:06:22,349 --> 00:06:25,759
work on that, in the next video.

