1
00:00:04,400 --> 00:00:06,600
Good everyone, and welcome to the new section.

2
00:00:07,370 --> 00:00:08,730
Our app's coming along.

3
00:00:09,130 --> 00:00:11,730
We can save the timing data for the tasks.

4
00:00:12,030 --> 00:00:15,030
But at the moment, we've got no way to view that data.

5
00:00:15,690 --> 00:00:17,290
We need to add reports to the app,

6
00:00:17,790 --> 00:00:20,390
then we'll need to test them and make sure they're accurate.

7
00:00:20,990 --> 00:00:24,790
To do that, we're going to need far more data than we've got at the moment.

8
00:00:25,290 --> 00:00:28,490
Rather than spending a lot of time tapping on our tasks

9
00:00:28,490 --> 00:00:30,090
to create timings for them,

10
00:00:30,590 --> 00:00:33,950
I'm going to create a class to generate timings for us.

11
00:00:34,350 --> 00:00:36,950
That will let us create timings in the past,

12
00:00:36,950 --> 00:00:40,250
so we'll end up with a lot of data for testing our reports.

13
00:00:42,250 --> 00:00:43,850
We're going to create a new class,

14
00:00:44,400 --> 00:00:48,000
but we don't want to distribute it with the release version of our app.

15
00:00:48,500 --> 00:00:52,100
This class is only for our use, while we're testing the app.

16
00:00:53,100 --> 00:00:56,400
One way to create classes for debug use only

17
00:00:56,600 --> 00:00:58,400
is to put them in a debug package.

18
00:01:00,000 --> 00:01:03,600
We can't create that when the project panes in android view though.

19
00:01:04,200 --> 00:01:06,000
You'll have to switch the project view.

20
00:01:08,660 --> 00:01:10,020
Expand app,

21
00:01:13,320 --> 00:01:17,320
then src, and all our files so far

22
00:01:17,320 --> 00:01:19,920
have been created inside the main folder.

23
00:01:22,120 --> 00:01:24,720
If we put classes in a folder called debug,

24
00:01:25,320 --> 00:01:28,620
they won't be compiled into the release version of our app.

25
00:01:28,980 --> 00:01:32,640
Right click on src, and choose new directory.

26
00:01:37,440 --> 00:01:39,440
Call it debug.

27
00:01:41,100 --> 00:01:43,100
The name is important.

28
00:01:43,300 --> 00:01:45,900
It has to match the name of our debug build.

29
00:01:46,260 --> 00:01:48,860
You can also create a release directory

30
00:01:48,860 --> 00:01:51,360
to include files only in the release build.

31
00:01:51,760 --> 00:01:55,260
Everything in the main directory will be included in all builds,

32
00:01:55,460 --> 00:01:58,960
and that's normally where we create all our java and Kotlin files.

33
00:01:59,560 --> 00:02:03,760
Okay. Right click debug, and choose new directory again.

34
00:02:08,419 --> 00:02:10,419
This one will be called java.

35
00:02:11,670 --> 00:02:14,370
Everything else can now be done back in the android view.

36
00:02:16,030 --> 00:02:20,530
We could do it here but the android view lets us see our package name

37
00:02:20,530 --> 00:02:23,830
which we'll need in a minute. So I'll switch back to the android view.

38
00:02:27,030 --> 00:02:30,330
Notice that we don't see main or debug now.

39
00:02:30,730 --> 00:02:31,730
They're still there.

40
00:02:32,230 --> 00:02:33,730
This view just doesn't show them.

41
00:02:34,130 --> 00:02:37,130
Expand the java folder if it isn't already,

42
00:02:37,590 --> 00:02:38,590
and right click.

43
00:02:40,590 --> 00:02:42,590
We want to create a new package.

44
00:02:46,950 --> 00:02:51,350
In the dialog that appears, we get a chance to choose main or debug.

45
00:02:51,750 --> 00:02:54,750
So they are there. They just don't show up normally.

46
00:02:54,950 --> 00:02:56,950
Choose the debug directory,

47
00:02:58,750 --> 00:02:59,650
and click okay.

48
00:03:00,450 --> 00:03:03,350
The reason I expanded the java folder a minute ago

49
00:03:03,350 --> 00:03:05,550
is so that I can see my package name,

50
00:03:05,950 --> 00:03:09,350
learnprogramming.academy.tasktimer.

51
00:03:09,750 --> 00:03:13,050
I'm going to call this new package the same with

52
00:03:13,050 --> 00:03:14,850
.debug at the end.

53
00:03:14,850 --> 00:03:16,850
So the new package becomes

54
00:03:18,150 --> 00:03:19,510
learnprogramming

55
00:03:22,310 --> 00:03:23,310
.academy

56
00:03:24,210 --> 00:03:27,810
.tasktimer.debug.

57
00:03:29,410 --> 00:03:31,410
Right click the new debug package,

58
00:03:34,310 --> 00:03:35,710
and choose new

59
00:03:37,310 --> 00:03:39,110
Kotlin file class.

60
00:03:39,610 --> 00:03:41,610
The name will be TestData

61
00:03:43,410 --> 00:03:46,610
and change the kind to object before you click okay.

62
00:03:49,110 --> 00:03:51,110
What we're going to do in this object

63
00:03:51,110 --> 00:03:54,110
is loop through all the tasks in the database

64
00:03:54,110 --> 00:03:57,110
and create random timing data for each one.

65
00:03:57,410 --> 00:04:00,410
We'll create a random number of timing instances

66
00:04:00,410 --> 00:04:03,610
and give each one a random start date and duration.

67
00:04:04,050 --> 00:04:08,510
Unfortunately, a timing class doesn't allow the duration to be set.

68
00:04:09,170 --> 00:04:11,170
The duration is calculated automatically.

69
00:04:11,770 --> 00:04:15,270
I'll create a TestTiming class at the start of this file.

70
00:04:15,770 --> 00:04:16,769
It's very simple.

71
00:04:17,269 --> 00:04:20,870
The only code is to convert a date in milliseconds to seconds,

72
00:04:21,070 --> 00:04:23,370
and we'll do that in the init block.

73
00:04:39,970 --> 00:04:41,630
In the TestData object,

74
00:04:41,990 --> 00:04:45,290
we'll create a generate test data function.

75
00:04:45,790 --> 00:04:49,550
The only thing we'll need to pass to it is a content resolver

76
00:04:49,550 --> 00:04:52,150
from the activity that uses this class.

77
00:05:03,550 --> 00:05:07,350
We're going to use the random number generator from the math class

78
00:05:07,350 --> 00:05:09,710
to generate random dates and durations.

79
00:05:10,110 --> 00:05:12,710
All but one of the values we need will be ints,

80
00:05:13,110 --> 00:05:16,430
and we can keep the code tidier by creating a

81
00:05:16,430 --> 00:05:19,430
GetRandomInt function to generate our ints,

82
00:05:20,430 --> 00:05:23,130
add it just after the generate test data function.

83
00:05:30,490 --> 00:05:34,850
The math random function returns a random value from 0 to 1.

84
00:05:35,450 --> 00:05:39,250
It can return 0 but the value will be less than 1.

85
00:05:39,610 --> 00:05:41,910
You can check what the random function does

86
00:05:41,910 --> 00:05:44,210
use,ctrl Q on a PC

87
00:05:44,410 --> 00:05:47,770
or ctrl J On a mac to get the documentation.

88
00:05:48,430 --> 00:05:51,430
Multiplying the result by 59, for example,

89
00:05:52,030 --> 00:05:55,930
will give us a value from 0 to 58

90
00:05:55,930 --> 00:05:56,930
point something.

91
00:05:57,330 --> 00:06:01,320
If we round 58.9, we get 59.

92
00:06:01,620 --> 00:06:05,220
So rounding the random number will give us a long

93
00:06:05,220 --> 00:06:07,420
from 0 to 59.

94
00:06:08,020 --> 00:06:11,420
That's exactly what we need when generating a random number of seconds.

95
00:06:11,920 --> 00:06:14,520
We just have to cast the result to an int.

96
00:06:15,180 --> 00:06:17,280
To generate the random timings,

97
00:06:17,280 --> 00:06:20,640
I'll create a function called random date time.

98
00:06:21,040 --> 00:06:24,240
We use that to create our test timing objects,

99
00:06:24,240 --> 00:06:27,540
just add it after the GetRandomInt function.

100
00:06:41,200 --> 00:06:45,540
We use our GetRandomInt function to generate a number from 0

101
00:06:45,540 --> 00:06:46,740
to 59.

102
00:06:47,340 --> 00:06:50,040
We can do the same for the minutes, hours and months.

103
00:06:50,480 --> 00:06:54,580
Copy that line three more times, and change the variable name

104
00:06:54,580 --> 00:06:55,880
and the multiplier.

105
00:07:20,680 --> 00:07:24,580
That's just more of the same. The year's only a bit more complicated.

106
00:07:24,880 --> 00:07:28,880
We want to produce values between our start year and end year.

107
00:07:29,680 --> 00:07:32,280
To generate a random number between a range,

108
00:07:32,480 --> 00:07:34,480
generate one between the difference.

109
00:07:34,840 --> 00:07:37,340
NDM minus start year in our example

110
00:07:37,740 --> 00:07:40,140
and then add it to the lower value in the range.

111
00:07:44,500 --> 00:07:47,500
Okay. That's most of the date values.

112
00:07:47,500 --> 00:07:49,500
The only thing missing is the day.

113
00:07:50,100 --> 00:07:52,500
We had to generate a random month first

114
00:07:52,500 --> 00:07:56,000
because we need to know how many days are in the month we're using.

115
00:07:56,360 --> 00:07:58,960
As we need a calendar object anyway,

116
00:07:58,960 --> 00:08:02,560
we can just create it now and ask it how many days there are.

117
00:08:12,560 --> 00:08:15,160
If you're prompted for which package to import

118
00:08:15,160 --> 00:08:17,660
choose the java.util package.

119
00:08:18,320 --> 00:08:20,570
Check the documentation for the

120
00:08:20,570 --> 00:08:22,570
get actual maximum function

121
00:08:23,450 --> 00:08:24,950
if it's not obvious what it does.

122
00:08:25,550 --> 00:08:27,850
It takes into account leap years

123
00:08:27,850 --> 00:08:31,150
giving February 29 days when appropriate.

124
00:08:31,810 --> 00:08:35,610
All right. We initialized a Gregorian calendar instance

125
00:08:35,970 --> 00:08:37,570
for the first day of the month.

126
00:08:38,370 --> 00:08:40,970
Now we need to set it to the correct date and time,

127
00:08:41,570 --> 00:08:42,770
then return the time.

128
00:08:51,570 --> 00:08:54,170
When we need a random start date for our data,

129
00:08:54,420 --> 00:08:56,720
we can call this function to provide one.

130
00:08:57,420 --> 00:09:01,420
The other thing we'll need to do is store our timing records in the database.

131
00:09:02,080 --> 00:09:05,080
I'll create a function for that next after

132
00:09:05,080 --> 00:09:06,580
random date time.

133
00:09:07,240 --> 00:09:08,840
The code isn't very long,

134
00:09:08,840 --> 00:09:11,840
and you may decide not to use a separate function for it,

135
00:09:12,090 --> 00:09:14,090
but it keeps our main code a bit cleaner.

136
00:09:58,290 --> 00:10:00,290
We've seen code like that before.

137
00:10:00,590 --> 00:10:02,590
It just sets up the content values

138
00:10:02,950 --> 00:10:06,150
then uses the content resolvers insert function

139
00:10:06,150 --> 00:10:08,350
to store the record in the timings table.

140
00:10:09,950 --> 00:10:13,830
You might need to import the TimingsContract class from your main project

141
00:10:13,830 --> 00:10:16,830
if android studio doesn't import it automatically.

142
00:10:18,030 --> 00:10:22,330
Okay. The main code goes in the GenerateTestData function.

143
00:10:23,130 --> 00:10:26,630
I'll start by declaring some constants to avoid having

144
00:10:26,630 --> 00:10:28,230
magic numbers in the code.

145
00:10:29,030 --> 00:10:31,630
If values like 86400

146
00:10:32,130 --> 00:10:34,730
and 500 suddenly appeared in the code,

147
00:10:35,090 --> 00:10:37,690
anyone else reading it will wonder where they're from.

148
00:10:51,490 --> 00:10:54,040
By making them named constants like this

149
00:10:54,040 --> 00:10:56,340
it's much easier to see what they represent.

150
00:10:56,940 --> 00:11:00,300
Next, we query the database for our tasks.

151
00:11:28,900 --> 00:11:31,800
Once again that's code that we've seen before.

152
00:11:32,600 --> 00:11:35,600
After calling the query function of the content resolver,

153
00:11:35,800 --> 00:11:38,500
we'll loop over each of the rows in the cursor

154
00:11:38,500 --> 00:11:41,500
and generate random timings records for each one.

155
00:11:42,050 --> 00:11:46,450
Accept the import of our tasks contract class if you're prompted.

156
00:11:46,950 --> 00:11:49,750
We don't want every task to have the same number of timings,

157
00:11:50,300 --> 00:11:53,100
but we've got a random integer function that we can use.

158
00:12:28,100 --> 00:12:30,360
It should be pretty obvious what we're doing here.

159
00:12:31,260 --> 00:12:33,860
A few comments and our constant names

160
00:12:33,860 --> 00:12:36,860
means we don't have to struggle to remember what the code's doing.

161
00:12:37,460 --> 00:12:42,060
It's also easy to change it to create a different number of records if we wanted to.

162
00:12:42,560 --> 00:12:46,560
Well, that's the class finished. In the next video, we'll test it.

163
00:12:47,360 --> 00:12:50,260
We'll also look at what happens in the release build of our app.

164
00:12:50,760 --> 00:12:51,760
I'll see you there.

