summaryrefslogtreecommitdiff
path: root/tests/cts/hostsidetests/healthconnect/HealthConnectTestHelper/src/android/healthconnect/cts/testhelper/HealthConnectTestHelper.java
blob: b882b89c75bcc1ff5d34291a7f8f775229b7ed74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.healthconnect.cts.testhelper;

import static android.healthconnect.cts.lib.MultiAppTestUtils.APP_PKG_NAME_USED_IN_DATA_ORIGIN;
import static android.healthconnect.cts.lib.MultiAppTestUtils.CHANGE_LOGS_RESPONSE;
import static android.healthconnect.cts.lib.MultiAppTestUtils.CHANGE_LOG_TOKEN;
import static android.healthconnect.cts.lib.MultiAppTestUtils.CLIENT_ID;
import static android.healthconnect.cts.lib.MultiAppTestUtils.DELETE_RECORDS_QUERY;
import static android.healthconnect.cts.lib.MultiAppTestUtils.END_TIME;
import static android.healthconnect.cts.lib.MultiAppTestUtils.EXERCISE_SESSION;
import static android.healthconnect.cts.lib.MultiAppTestUtils.GET_CHANGE_LOG_TOKEN_QUERY;
import static android.healthconnect.cts.lib.MultiAppTestUtils.INSERT_RECORD_QUERY;
import static android.healthconnect.cts.lib.MultiAppTestUtils.INTENT_EXCEPTION;
import static android.healthconnect.cts.lib.MultiAppTestUtils.PAUSE_END;
import static android.healthconnect.cts.lib.MultiAppTestUtils.PAUSE_START;
import static android.healthconnect.cts.lib.MultiAppTestUtils.QUERY_TYPE;
import static android.healthconnect.cts.lib.MultiAppTestUtils.READ_CHANGE_LOGS_QUERY;
import static android.healthconnect.cts.lib.MultiAppTestUtils.READ_RECORDS_QUERY;
import static android.healthconnect.cts.lib.MultiAppTestUtils.READ_RECORDS_SIZE;
import static android.healthconnect.cts.lib.MultiAppTestUtils.READ_RECORD_CLASS_NAME;
import static android.healthconnect.cts.lib.MultiAppTestUtils.READ_USING_DATA_ORIGIN_FILTERS;
import static android.healthconnect.cts.lib.MultiAppTestUtils.RECORD_IDS;
import static android.healthconnect.cts.lib.MultiAppTestUtils.RECORD_TYPE;
import static android.healthconnect.cts.lib.MultiAppTestUtils.START_TIME;
import static android.healthconnect.cts.lib.MultiAppTestUtils.STEPS_COUNT;
import static android.healthconnect.cts.lib.MultiAppTestUtils.STEPS_RECORD;
import static android.healthconnect.cts.lib.MultiAppTestUtils.SUCCESS;
import static android.healthconnect.cts.lib.MultiAppTestUtils.UPDATE_EXERCISE_ROUTE;
import static android.healthconnect.cts.lib.MultiAppTestUtils.UPDATE_RECORDS_QUERY;
import static android.healthconnect.cts.lib.MultiAppTestUtils.UPSERT_EXERCISE_ROUTE;
import static android.healthconnect.cts.utils.TestUtils.buildExerciseSession;
import static android.healthconnect.cts.utils.TestUtils.buildStepsRecord;
import static android.healthconnect.cts.utils.TestUtils.getChangeLogs;
import static android.healthconnect.cts.utils.TestUtils.getExerciseSessionRecord;
import static android.healthconnect.cts.utils.TestUtils.getTestRecords;
import static android.healthconnect.cts.utils.TestUtils.insertRecords;
import static android.healthconnect.cts.utils.TestUtils.insertRecordsAndGetIds;
import static android.healthconnect.cts.utils.TestUtils.readRecords;
import static android.healthconnect.cts.utils.TestUtils.updateRecords;
import static android.healthconnect.cts.utils.TestUtils.verifyDeleteRecords;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.health.connect.ReadRecordsRequestUsingFilters;
import android.health.connect.RecordIdFilter;
import android.health.connect.changelog.ChangeLogTokenRequest;
import android.health.connect.changelog.ChangeLogTokenResponse;
import android.health.connect.changelog.ChangeLogsRequest;
import android.health.connect.changelog.ChangeLogsResponse;
import android.health.connect.datatypes.DataOrigin;
import android.health.connect.datatypes.ExerciseSessionRecord;
import android.health.connect.datatypes.Record;
import android.healthconnect.cts.utils.TestUtils;
import android.os.Bundle;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HealthConnectTestHelper extends Activity {
    private static final String TAG = "HealthConnectTestHelper";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Context context = getApplicationContext();
        Bundle bundle = getIntent().getExtras();
        String queryType = bundle.getString(QUERY_TYPE);
        Intent returnIntent;
        try {
            switch (queryType) {
                case INSERT_RECORD_QUERY:
                    if (bundle.containsKey(APP_PKG_NAME_USED_IN_DATA_ORIGIN)) {
                        returnIntent =
                                insertRecordsWithDifferentPkgName(
                                        queryType,
                                        bundle.getString(APP_PKG_NAME_USED_IN_DATA_ORIGIN),
                                        context);
                        break;
                    }
                    if (bundle.containsKey(CLIENT_ID)) {
                        returnIntent =
                                insertRecordsWithGivenClientId(
                                        queryType, bundle.getDouble(CLIENT_ID), context);
                        break;
                    }
                    if (bundle.containsKey(RECORD_TYPE)) {
                        if (bundle.getString(RECORD_TYPE).equals(STEPS_RECORD)) {
                            returnIntent =
                                    insertStepsRecord(
                                            queryType,
                                            bundle.getString(START_TIME),
                                            bundle.getString(END_TIME),
                                            bundle.getInt(STEPS_COUNT),
                                            context);
                            break;
                        } else if (bundle.getString(RECORD_TYPE).equals(EXERCISE_SESSION)) {
                            returnIntent =
                                    insertExerciseSession(
                                            queryType,
                                            bundle.getString(START_TIME),
                                            bundle.getString(END_TIME),
                                            bundle.getString(PAUSE_START),
                                            bundle.getString(PAUSE_END),
                                            context);
                            break;
                        }
                    }
                    returnIntent = insertRecord(queryType, context);
                    break;
                case DELETE_RECORDS_QUERY:
                    returnIntent =
                            deleteRecords(
                                    queryType,
                                    (List<TestUtils.RecordTypeAndRecordIds>)
                                            bundle.getSerializable(RECORD_IDS),
                                    context);
                    break;
                case UPDATE_EXERCISE_ROUTE:
                    returnIntent = updateRouteAs(queryType, context);
                    break;
                case UPSERT_EXERCISE_ROUTE:
                    returnIntent = upsertRouteAs(queryType, context);
                    break;
                case UPDATE_RECORDS_QUERY:
                    returnIntent =
                            updateRecordsAs(
                                    queryType,
                                    (List<TestUtils.RecordTypeAndRecordIds>)
                                            bundle.getSerializable(RECORD_IDS),
                                    context);
                    break;
                case READ_RECORDS_QUERY:
                    if (bundle.containsKey(READ_USING_DATA_ORIGIN_FILTERS)) {
                        returnIntent =
                                readRecordsUsingDataOriginFilters(
                                        queryType,
                                        bundle.getStringArrayList(READ_RECORD_CLASS_NAME),
                                        context);
                        break;
                    }
                    returnIntent =
                            readRecordsAs(
                                    queryType,
                                    bundle.getStringArrayList(READ_RECORD_CLASS_NAME),
                                    context);
                    break;
                case READ_CHANGE_LOGS_QUERY:
                    returnIntent =
                            readChangeLogsUsingDataOriginFilters(
                                    queryType, bundle.getString(CHANGE_LOG_TOKEN), context);
                    break;
                case GET_CHANGE_LOG_TOKEN_QUERY:
                    if (bundle.containsKey(READ_RECORD_CLASS_NAME)) {
                        returnIntent =
                                getChangeLogToken(
                                        queryType,
                                        bundle.getString(APP_PKG_NAME_USED_IN_DATA_ORIGIN),
                                        bundle.getStringArrayList(READ_RECORD_CLASS_NAME),
                                        context);
                        break;
                    }
                    returnIntent =
                            getChangeLogToken(
                                    queryType,
                                    bundle.getString(APP_PKG_NAME_USED_IN_DATA_ORIGIN),
                                    context);
                    break;
                default:
                    throw new IllegalStateException(
                            "Unknown query received from launcher app: " + queryType);
            }
        } catch (Exception e) {
            returnIntent = new Intent(queryType);
            returnIntent.putExtra(INTENT_EXCEPTION, e);
        }

        sendBroadcast(returnIntent);
        this.finish();
    }

    /**
     * Method to get test records, insert them, and put the list of recordId and recordClass in the
     * intent
     *
     * @param queryType - specifies the action, here it should be INSERT_RECORDS_QUERY
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     */
    private Intent insertRecord(String queryType, Context context) {
        List<Record> records = getTestRecords(context.getPackageName());
        final Intent intent = new Intent(queryType);
        try {
            List<TestUtils.RecordTypeAndRecordIds> listOfRecordIdsAndClass =
                    insertRecordsAndGetIds(records, context);
            intent.putExtra(RECORD_IDS, (Serializable) listOfRecordIdsAndClass);
            intent.putExtra(SUCCESS, true);
        } catch (Exception e) {
            intent.putExtra(SUCCESS, false);
            intent.putExtra(INTENT_EXCEPTION, e);
        }

        return intent;
    }

    /**
     * Method to delete the records and put the Exception in the intent if deleting records throws
     * an exception
     *
     * @param queryType - specifies the action, here it should be DELETE_RECORDS_QUERY
     * @param listOfRecordIdsAndClassName - list of recordId and recordClass of records to be
     *     deleted
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     * @throws ClassNotFoundException if a record category class is not found for any class name
     *     present in the list @listOfRecordIdsAndClassName
     */
    private Intent deleteRecords(
            String queryType,
            List<TestUtils.RecordTypeAndRecordIds> listOfRecordIdsAndClassName,
            Context context)
            throws ClassNotFoundException {
        final Intent intent = new Intent(queryType);

        List<RecordIdFilter> recordIdFilters = new ArrayList<>();
        for (TestUtils.RecordTypeAndRecordIds recordTypeAndRecordIds :
                listOfRecordIdsAndClassName) {
            for (String recordId : recordTypeAndRecordIds.getRecordIds()) {
                recordIdFilters.add(
                        RecordIdFilter.fromId(
                                (Class<? extends Record>)
                                        Class.forName(recordTypeAndRecordIds.getRecordType()),
                                recordId));
            }
        }
        try {
            verifyDeleteRecords(recordIdFilters, context);
            intent.putExtra(SUCCESS, true);
        } catch (Exception e) {
            intent.putExtra(INTENT_EXCEPTION, e);
        }
        return intent;
    }

    /**
     * Method to update the records and put the exception in the intent if updating the records
     * throws an exception
     *
     * @param queryType - specifies the action, here it should be UPDATE_RECORDS_QUERY
     * @param listOfRecordIdsAndClassName - list of recordId and recordClass of records to be
     *     updated
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     */
    private Intent updateRecordsAs(
            String queryType,
            List<TestUtils.RecordTypeAndRecordIds> listOfRecordIdsAndClassName,
            Context context) {
        final Intent intent = new Intent(queryType);

        try {
            for (TestUtils.RecordTypeAndRecordIds recordTypeAndRecordIds :
                    listOfRecordIdsAndClassName) {
                List<? extends Record> recordsToBeUpdated =
                        readRecords(
                                new ReadRecordsRequestUsingFilters.Builder<>(
                                                (Class<? extends Record>)
                                                        Class.forName(
                                                                recordTypeAndRecordIds
                                                                        .getRecordType()))
                                        .build(),
                                context);
                updateRecords((List<Record>) recordsToBeUpdated, context);
            }
            intent.putExtra(SUCCESS, true);
        } catch (Exception e) {
            intent.putExtra(INTENT_EXCEPTION, e);
            intent.putExtra(SUCCESS, false);
        }

        return intent;
    }

    /**
     * Method to update the session record to the session without route and put the exception in the
     * intent if updating the record throws an exception
     *
     * @param queryType - specifies the action, here it should be UPDATE_RECORDS_QUERY
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     */
    private Intent updateRouteAs(String queryType, Context context) {
        final Intent intent = new Intent(queryType);
        try {
            ExerciseSessionRecord existingSession =
                    readRecords(
                                    new ReadRecordsRequestUsingFilters.Builder<>(
                                                    ExerciseSessionRecord.class)
                                            .build(),
                                    context)
                            .get(0);
            updateRecords(
                    List.of(
                            getExerciseSessionRecord(
                                    context.getPackageName(),
                                    Double.parseDouble(
                                            existingSession.getMetadata().getClientRecordId()),
                                    false)),
                    context);
            intent.putExtra(SUCCESS, true);
        } catch (Exception e) {
            intent.putExtra(INTENT_EXCEPTION, e);
            intent.putExtra(SUCCESS, false);
        }

        return intent;
    }

    /**
     * Method to upsert the session record to the session without route and put the exception in the
     * intent if updating the record throws an exception
     *
     * @param queryType - specifies the action, here it should be UPDATE_RECORDS_QUERY
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     */
    private Intent upsertRouteAs(String queryType, Context context) {
        final Intent intent = new Intent(queryType);
        try {
            ExerciseSessionRecord existingSession =
                    readRecords(
                                    new ReadRecordsRequestUsingFilters.Builder<>(
                                                    ExerciseSessionRecord.class)
                                            .build(),
                                    context)
                            .get(0);
            insertRecords(
                    List.of(
                            getExerciseSessionRecord(
                                    context.getPackageName(),
                                    Double.parseDouble(
                                            existingSession.getMetadata().getClientRecordId()),
                                    false)),
                    context);
            intent.putExtra(SUCCESS, true);
        } catch (Exception e) {
            intent.putExtra(INTENT_EXCEPTION, e);
            intent.putExtra(SUCCESS, false);
        }

        return intent;
    }

    /**
     * Method to insert records with different package name in dataOrigin of the record and add the
     * details in the intent
     *
     * @param queryType - specifies the action, here it should be INSERT_RECORDS_QUERY
     * @param pkgNameUsedInDataOrigin - package name to be added in the dataOrigin of the records
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     * @throws InterruptedException
     */
    private Intent insertRecordsWithDifferentPkgName(
            String queryType, String pkgNameUsedInDataOrigin, Context context)
            throws InterruptedException {
        final Intent intent = new Intent(queryType);

        List<Record> recordsToBeInserted = getTestRecords(pkgNameUsedInDataOrigin);
        List<TestUtils.RecordTypeAndRecordIds> listOfRecordIdsAndClass =
                insertRecordsAndGetIds(recordsToBeInserted, context);

        intent.putExtra(RECORD_IDS, (Serializable) listOfRecordIdsAndClass);
        return intent;
    }

    /**
     * Method to read records and put the number of records read in the intent or put the exception
     * in the intent in case reading records throws exception
     *
     * @param queryType - specifies the action, here it should be READ_RECORDS_QUERY
     * @param recordClassesToRead - List of Record Class names for the records to be read
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     */
    private Intent readRecordsAs(
            String queryType, ArrayList<String> recordClassesToRead, Context context) {
        final Intent intent = new Intent(queryType);
        int recordsSize = 0;
        try {
            for (String recordClass : recordClassesToRead) {
                List<? extends Record> recordsRead =
                        readRecords(
                                new ReadRecordsRequestUsingFilters.Builder<>(
                                                (Class<? extends Record>)
                                                        Class.forName(recordClass))
                                        .build(),
                                context);

                recordsSize += recordsRead.size();
            }
            intent.putExtra(SUCCESS, true);
        } catch (Exception e) {
            intent.putExtra(INTENT_EXCEPTION, e);
            intent.putExtra(SUCCESS, false);
        }

        intent.putExtra(READ_RECORDS_SIZE, recordsSize);

        return intent;
    }

    /**
     * Method to insert records with given clientId in their dataOrigin and put SUCCESS as true if
     * insertion is successfule or SUCCESS as false if insertion throws an exception
     *
     * @param queryType - specifies the action, here it should be INSERT_RECORDS_QUERY
     * @param clientId - clientId to be specified in the dataOrigin of the records to be inserted
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     */
    private Intent insertRecordsWithGivenClientId(
            String queryType, double clientId, Context context) {
        final Intent intent = new Intent(queryType);

        List<Record> records = getTestRecords(context.getPackageName(), clientId);

        try {
            insertRecords(records, context);
            intent.putExtra(SUCCESS, true);
        } catch (Exception e) {
            intent.putExtra(SUCCESS, false);
        }

        return intent;
    }

    /**
     * Method to read records using data origin filters and add number of records read to the intent
     *
     * @param queryType - specifies the action, here it should be READ_RECORDS_QUERY
     * @param recordClassesToRead - List of Record Class names for the records to be read
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     */
    private Intent readRecordsUsingDataOriginFilters(
            String queryType, ArrayList<String> recordClassesToRead, Context context) {
        final Intent intent = new Intent(queryType);

        int recordsSize = 0;
        try {
            for (String recordClass : recordClassesToRead) {
                List<? extends Record> recordsRead =
                        readRecords(
                                new ReadRecordsRequestUsingFilters.Builder<>(
                                                (Class<? extends Record>)
                                                        Class.forName(recordClass))
                                        .addDataOrigins(
                                                new DataOrigin.Builder()
                                                        .setPackageName(context.getPackageName())
                                                        .build())
                                        .build(),
                                context);
                recordsSize += recordsRead.size();
            }
        } catch (Exception e) {
            intent.putExtra(READ_RECORDS_SIZE, 0);
            intent.putExtra(INTENT_EXCEPTION, e);
        }

        intent.putExtra(READ_RECORDS_SIZE, recordsSize);

        return intent;
    }

    /**
     * Method to read changeLogs using dataOriginFilters and add the changeLogToken
     *
     * @param queryType - specifies the action, here it should be
     *     READ_CHANGE_LOGS_USING_DATA_ORIGIN_FILTERS_QUERY
     * @param context - application context
     * @param changeLogToken - Token corresponding to which changeLogs have to be read
     * @return Intent to send back to the main app which is running the tests
     */
    private Intent readChangeLogsUsingDataOriginFilters(
            String queryType, String changeLogToken, Context context) {
        final Intent intent = new Intent(queryType);

        ChangeLogsRequest changeLogsRequest = new ChangeLogsRequest.Builder(changeLogToken).build();

        try {
            ChangeLogsResponse response = getChangeLogs(changeLogsRequest, context);
            intent.putExtra(CHANGE_LOGS_RESPONSE, response);
        } catch (Exception e) {
            intent.putExtra(INTENT_EXCEPTION, e);
        }

        return intent;
    }

    /**
     * Method to get changeLogToken for an app
     *
     * @param queryType - specifies the action, here it should be GET_CHANGE_LOG_TOKEN_QUERY
     * @param pkgName - pkgName of the app whose changeLogs we have to read using the returned token
     * @param context - application context
     * @return - Intent to send back to the main app which is running the tests
     */
    private Intent getChangeLogToken(String queryType, String pkgName, Context context)
            throws Exception {
        final Intent intent = new Intent(queryType);

        ChangeLogTokenResponse tokenResponse =
                TestUtils.getChangeLogToken(
                        new ChangeLogTokenRequest.Builder()
                                .addDataOriginFilter(
                                        new DataOrigin.Builder().setPackageName(pkgName).build())
                                .build(),
                        context);

        intent.putExtra(CHANGE_LOG_TOKEN, tokenResponse.getToken());
        return intent;
    }

    /**
     * Method to get changeLogToken for an app
     *
     * @param queryType - specifies the action, here it should be GET_CHANGE_LOG_TOKEN_QUERY
     * @param pkgName - pkgName of the app whose changeLogs we have to read using the returned token
     * @param recordClassesToRead - Record Classes whose changeLogs to be read using the returned
     *     token
     * @param context - application context
     * @return - Intent to send back to the main app which is running the tests
     */
    private Intent getChangeLogToken(
            String queryType,
            String pkgName,
            ArrayList<String> recordClassesToRead,
            Context context)
            throws Exception {
        final Intent intent = new Intent(queryType);

        ChangeLogTokenRequest.Builder changeLogTokenRequestBuilder =
                new ChangeLogTokenRequest.Builder()
                        .addDataOriginFilter(
                                new DataOrigin.Builder().setPackageName(pkgName).build());
        for (String recordClass : recordClassesToRead) {
            changeLogTokenRequestBuilder.addRecordType(
                    (Class<? extends Record>) Class.forName(recordClass));
        }
        ChangeLogTokenResponse tokenResponse =
                TestUtils.getChangeLogToken(changeLogTokenRequestBuilder.build(), context);

        intent.putExtra(CHANGE_LOG_TOKEN, tokenResponse.getToken());
        return intent;
    }

    /**
     * Method to build steps record and insert them
     *
     * @param queryType - specifies the action, here it should be INSERT_RECORDS_QUERY
     * @param startTime - start time for the steps record to build
     * @param endTime - end time for the steps record to build
     * @param stepsCount - number of steps to be added in the steps record
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     */
    private Intent insertStepsRecord(
            String queryType, String startTime, String endTime, int stepsCount, Context context) {
        final Intent intent = new Intent(queryType);
        try {
            List<Record> recordToInsert =
                    Arrays.asList(
                            buildStepsRecord(
                                    startTime, endTime, stepsCount, context.getPackageName()));
            insertRecords(recordToInsert, context);
            intent.putExtra(SUCCESS, true);
        } catch (Exception e) {
            intent.putExtra(SUCCESS, false);
        }
        return intent;
    }

    /**
     * Method to build Exercise Session records and insert them
     *
     * @param queryType - specifies the action, here it should be INSERT_RECORDS_QUERY
     * @param sessionStartTime - start time of the exercise session to build
     * @param sessionEndTime - end time of the exercise session t build
     * @param pauseStart - start time of the pause segment in the exercise session
     * @param pauseEnd - end time of the pause segment in the exercise session
     * @param context - application context
     * @return Intent to send back to the main app which is running the tests
     */
    private Intent insertExerciseSession(
            String queryType,
            String sessionStartTime,
            String sessionEndTime,
            String pauseStart,
            String pauseEnd,
            Context context) {
        final Intent intent = new Intent(queryType);
        try {
            List<Record> recordToInsert;
            if (pauseStart == null) {
                recordToInsert =
                        Arrays.asList(
                                buildExerciseSession(sessionStartTime, sessionEndTime, context));
            } else {
                recordToInsert =
                        Arrays.asList(
                                buildExerciseSession(
                                        sessionStartTime,
                                        sessionEndTime,
                                        pauseStart,
                                        pauseEnd,
                                        context));
            }
            insertRecords(recordToInsert, context);
            intent.putExtra(SUCCESS, true);
        } catch (Exception e) {
            intent.putExtra(SUCCESS, false);
        }
        return intent;
    }
}