summaryrefslogtreecommitdiff
path: root/apps/CtsVerifier/src/com/android/cts/verifier/ManifestTestListAdapter.java
blob: c37788d97a78ebfae66f56756a4c06a7e8836ede (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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
 * Copyright (C) 2011 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 com.android.cts.verifier;

import static com.android.cts.verifier.TestListActivity.sCurrentDisplayMode;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.hardware.SensorPrivacyManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.UserManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.ListView;

import com.android.cts.verifier.TestListActivity.DisplayMode;
import com.android.modules.utils.build.SdkLevel;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * {@link TestListAdapter} that populates the {@link TestListActivity}'s {@link ListView} by reading
 * data from the CTS Verifier's AndroidManifest.xml.
 *
 * <p>Making a new test activity to appear in the list requires the following steps:
 *
 * <ol>
 *   <li>REQUIRED: Add an activity to the AndroidManifest.xml with an intent filter with a main
 *       action and the MANUAL_TEST category.
 *       <pre>
 *             <intent-filter>
 *                <action android:name="android.intent.action.MAIN" />
 *                <category android:name="android.cts.intent.category.MANUAL_TEST" />
 *             </intent-filter>
 *         </pre>
 *   <li>REQUIRED: Add a meta data attribute to indicate which display modes of tests the activity
 *       should belong to. "single_display_mode" indicates a test is only needed to run on the main
 *       display mode (i.e. unfolded), and "multi_display_mode" indicates a test is required to run
 *       under both modes (i.e. both folded and unfolded).If you don't add this attribute, your test
 *       will show up in both unfolded and folded modes.
 *       <pre>
 *             <meta-data android:name="display_mode" android:value="multi_display_mode" />
 *         </pre>
 *   <li>OPTIONAL: Add a meta data attribute to indicate what category of tests the activity should
 *       belong to. If you don't add this attribute, your test will show up in the "Other" tests
 *       category.
 *       <pre>
 *             <meta-data android:name="test_category" android:value="@string/test_category_security" />
 *         </pre>
 *   <li>OPTIONAL: Add a meta data attribute to indicate whether this test has a parent test.
 *       <pre>
 *             <meta-data android:name="test_parent" android:value="com.android.cts.verifier.bluetooth.BluetoothTestActivity" />
 *         </pre>
 *   <li>OPTIONAL: Add a meta data attribute to indicate what features are required to run the test.
 *       If the device does not have all of the required features then it will not appear in the
 *       test list. Use a colon (:) to specify multiple required features.
 *       <pre>
 *             <meta-data android:name="test_required_features" android:value="android.hardware.sensor.accelerometer" />
 *         </pre>
 *   <li>OPTIONAL: Add a meta data attribute to indicate features such that, if any present, the
 *       test gets excluded from being shown. If the device has any of the excluded features then
 *       the test will not appear in the test list. Use a colon (:) to specify multiple features to
 *       exclude for the test. Note that the colon means "or" in this case.
 *       <pre>
 *             <meta-data android:name="test_excluded_features" android:value="android.hardware.type.television" />
 *         </pre>
 *   <li>OPTIONAL: Add a meta data attribute to indicate features such that, if any present, the
 *       test is applicable to run. If the device has any of the applicable features then the test
 *       will appear in the test list. Use a colon (:) to specify multiple features
 *       <pre>
 *             <meta-data android:name="test_applicable_features" android:value="android.hardware.sensor.compass" />
 *         </pre>
 *   <li>OPTIONAL: Add a meta data attribute to indicate which intent actions are required to run
 *       the test. If the device does not have activities that handle all those actions, then it
 *       will not appear in the test list. Use a colon (:) to specify multiple required intent
 *       actions.
 *       <pre>
 *             <meta-data android:name="test_required_actions" android:value="android.app.action.ADD_DEVICE_ADMIN" />
 *         </pre>
 *   <li>OPTIONAL: Add a meta data attribute to indicate which intent actions should not run when
 *       the user running the test is of the given "type" (notice that the type here is not
 *       necessarily the same as {@link UserManager#getUserType()}). Use a colon (:) to specify
 *       multiple user types.
 *       <pre>
 *             <meta-data android:name="test_excluded_user_types" android:value="visible_background_non-profile_user" />
 *         </pre>
 * </ol>
 */
public class ManifestTestListAdapter extends TestListAdapter {
    private static final String LOG_TAG = "ManifestTestListAdapter";

    private static final String TEST_CATEGORY_META_DATA = "test_category";

    private static final String TEST_PARENT_META_DATA = "test_parent";

    private static final String TEST_REQUIRED_FEATURES_META_DATA = "test_required_features";

    private static final String TEST_EXCLUDED_FEATURES_META_DATA = "test_excluded_features";

    private static final String TEST_APPLICABLE_FEATURES_META_DATA = "test_applicable_features";

    private static final String TEST_REQUIRED_CONFIG_META_DATA = "test_required_configs";

    private static final String TEST_REQUIRED_ACTIONS_META_DATA = "test_required_actions";

    private static final String TEST_EXCLUDED_USER_TYPES_META_DATA = "test_excluded_user_types";

    private static final String TEST_DISPLAY_MODE_META_DATA = "display_mode";

    private static final String TEST_PASS_MODE = "test_pass_mode";

    private static final String CONFIG_BATTERY_SUPPORTED = "config_battery_supported";

    private static final String CONFIG_NO_EMULATOR = "config_no_emulator";

    private static final String CONFIG_VOICE_CAPABLE = "config_voice_capable";

    private static final String CONFIG_HAS_RECENTS = "config_has_recents";

    private static final String CONFIG_HDMI_SOURCE = "config_hdmi_source";

    private static final String CONFIG_QUICK_SETTINGS_SUPPORTED = "config_quick_settings_supported";

    private static final String CONFIG_HAS_MIC_TOGGLE = "config_has_mic_toggle";

    private static final String CONFIG_HAS_CAMERA_TOGGLE = "config_has_camera_toggle";

    /**
     * The config to represent that a test is only needed to run in the main display mode (i.e.
     * unfolded).
     */
    private static final String SINGLE_DISPLAY_MODE = "single_display_mode";

    /**
     * The config to represent that a test is needed to run in the multiple display modes (i.e. both
     * unfolded and folded).
     */
    private static final String MULTIPLE_DISPLAY_MODE = "multi_display_mode";

    /** The config to represent that a test is only needed to run in the folded display mode. */
    private static final String FOLDED_DISPLAY_MODE = "folded_display_mode";

    /**
     * The config to represent that a test is marked as pass when it passes either in folded mode or
     * in unfolded mode.
     */
    private static final String EITHER_MODE = "either_mode";

    /**
     * The user is not a {@link UserManager#isProfile() profile} and is running in the background,
     * but {@link UserManager#isUserVisible() visible} in a display.
     */
    private static final String USER_TYPE_VISIBLE_BG_USER = "visible_background_non-profile_user";

    private final HashSet<String> mDisabledTests;

    private Context mContext;

    private String mTestParent;

    public ManifestTestListAdapter(Context context, String testParent) {
        this(context, testParent, context.getResources().getStringArray(R.array.disabled_tests));
    }

    public ManifestTestListAdapter(Context context, String testParent, String[] disabledTestArray) {
        super(context);
        mContext = context;
        mTestParent = testParent;
        mDisabledTests = new HashSet<>(disabledTestArray.length);
        for (int i = 0; i < disabledTestArray.length; i++) {
            mDisabledTests.add(disabledTestArray[i]);
        }
    }

    @Override
    protected List<TestListItem> getRows() {
        // When launching at the first time or after killing the process, needs to fetch the
        // test items of all display modes as the bases for switching.
        if (mDisplayModesTests.isEmpty()) {
            for (DisplayMode mode : DisplayMode.values()) {
                mDisplayModesTests.put(mode.toString(), getRowsWithDisplayMode(mode.toString()));
                PackageManager packageManager = mContext.getPackageManager();
                boolean isCustomLauncher =
                        packageManager.hasSystemFeature("com.google.android.tv.custom_launcher");
                if (isCustomLauncher) {
                    mDisabledTests.add(
                            "com.android.cts.verifier.net.ConnectivityBackgroundTestActivity");
                }
            }
        }

        if (mTestFilter != null) {
            // Filter test rows dynamically when the filter is specified.
            return getRowsWithDisplayMode(sCurrentDisplayMode.toString());
        } else {
            return mDisplayModesTests.getOrDefault(
                    sCurrentDisplayMode.toString(), new ArrayList<>());
        }
    }

    /**
     * Gets all rows based on the specific display mode.
     *
     * @param mode the given display mode
     * @return a list containing all test itmes in the given display mode
     */
    private List<TestListItem> getRowsWithDisplayMode(String mode) {
        /*
         * 1. Get all the tests belonging to the test parent.
         * 2. Get all the tests keyed by their category.
         * 3. Flatten the tests and categories into one giant list for the list view.
         */
        List<TestListItem> allRows = new ArrayList<TestListItem>();
        List<ResolveInfo> infos = getResolveInfosForParent();
        Map<String, List<TestListItem>> testsByCategory = getTestsByCategory(infos);

        List<String> testCategories = new ArrayList<String>(testsByCategory.keySet());
        Collections.sort(testCategories);
        for (String testCategory : testCategories) {
            List<TestListItem> tests = filterTests(testsByCategory.get(testCategory), mode);
            if (!tests.isEmpty()) {
                allRows.add(TestListItem.newCategory(testCategory));
                Collections.sort(tests, Comparator.comparing(item -> item.title));
                allRows.addAll(tests);
            }
        }
        return allRows;
    }

    List<ResolveInfo> getResolveInfosForParent() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN);
        mainIntent.addCategory(CATEGORY_MANUAL_TEST);
        mainIntent.setPackage(mContext.getPackageName());

        PackageManager packageManager = mContext.getPackageManager();
        List<ResolveInfo> list =
                packageManager.queryIntentActivities(
                        mainIntent, PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA);
        int size = list.size();

        List<ResolveInfo> matchingList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            ResolveInfo info = list.get(i);
            String parent = getTestParent(info.activityInfo.metaData);
            if ((mTestParent == null && parent == null)
                    || (mTestParent != null && mTestParent.equals(parent))) {
                matchingList.add(info);
            }
        }
        return matchingList;
    }

    Map<String, List<TestListItem>> getTestsByCategory(List<ResolveInfo> list) {
        Map<String, List<TestListItem>> testsByCategory = new HashMap<>();

        int size = list.size();
        for (int i = 0; i < size; i++) {
            ResolveInfo info = list.get(i);
            if (info.activityInfo == null || mDisabledTests.contains(info.activityInfo.name)) {
                Log.w(LOG_TAG, "ignoring disabled test: " + info.activityInfo.name);
                continue;
            }
            String title = getTitle(mContext, info.activityInfo);
            String testName = info.activityInfo.name;
            Intent intent = getActivityIntent(info.activityInfo);
            String[] requiredFeatures = getRequiredFeatures(info.activityInfo.metaData);
            String[] requiredConfigs = getRequiredConfigs(info.activityInfo.metaData);
            String[] requiredActions = getRequiredActions(info.activityInfo.metaData);
            String[] excludedFeatures = getExcludedFeatures(info.activityInfo.metaData);
            String[] excludedUserTypes = getExcludedUserTypes(info.activityInfo.metaData);
            String[] applicableFeatures = getApplicableFeatures(info.activityInfo.metaData);
            String displayMode = getDisplayMode(info.activityInfo.metaData);
            boolean passInEitherMode = getTestPassMode(info.activityInfo.metaData, displayMode);

            TestListItem item =
                    TestListItem.newTest(
                            title,
                            testName,
                            intent,
                            requiredFeatures,
                            requiredConfigs,
                            requiredActions,
                            excludedFeatures,
                            applicableFeatures,
                            excludedUserTypes,
                            displayMode,
                            passInEitherMode);

            String testCategory = getTestCategory(mContext, info.activityInfo.metaData);
            addTestToCategory(testsByCategory, testCategory, item);
        }

        return testsByCategory;
    }

    static String getTestCategory(Context context, Bundle metaData) {
        String testCategory = null;
        if (metaData != null) {
            testCategory = metaData.getString(TEST_CATEGORY_META_DATA);
        }
        if (testCategory != null) {
            return testCategory;
        } else {
            return context.getString(R.string.test_category_other);
        }
    }

    static String getTestParent(Bundle metaData) {
        return metaData != null ? metaData.getString(TEST_PARENT_META_DATA) : null;
    }

    static String[] getRequiredFeatures(Bundle metaData) {
        return getMetadataAttributes(metaData, TEST_REQUIRED_FEATURES_META_DATA);
    }

    static String[] getRequiredActions(Bundle metaData) {
        return getMetadataAttributes(metaData, TEST_REQUIRED_ACTIONS_META_DATA);
    }

    static String[] getRequiredConfigs(Bundle metaData) {
        return getMetadataAttributes(metaData, TEST_REQUIRED_CONFIG_META_DATA);
    }

    static String[] getExcludedFeatures(Bundle metaData) {
        return getMetadataAttributes(metaData, TEST_EXCLUDED_FEATURES_META_DATA);
    }

    static String[] getApplicableFeatures(Bundle metaData) {
        return getMetadataAttributes(metaData, TEST_APPLICABLE_FEATURES_META_DATA);
    }

    static String[] getExcludedUserTypes(Bundle metaData) {
        return getMetadataAttributes(metaData, TEST_EXCLUDED_USER_TYPES_META_DATA);
    }

    private static String[] getMetadataAttributes(Bundle metaData, String attribute) {
        if (metaData == null) {
            return null;
        } else {
            String value = metaData.getString(attribute);
            if (value == null) {
                return null;
            } else {
                return value.split(":");
            }
        }
    }

    /**
     * Gets the configuration of the display mode per test. The default value is multi_display_mode.
     *
     * @param metaData the given metadata of the display mode
     * @return a string representing the display mode of the test
     */
    static String getDisplayMode(Bundle metaData) {
        if (metaData == null) {
            return MULTIPLE_DISPLAY_MODE;
        }
        String displayMode = metaData.getString(TEST_DISPLAY_MODE_META_DATA);
        return displayMode == null ? MULTIPLE_DISPLAY_MODE : displayMode;
    }

    /**
     * Gets the configuration of the test pass mode per test.
     *
     * @param metaData the given metadata of the test pass mode
     * @return a boolean representing whether the test can be marked as pass when it passes either
     *     in the folded mode or in the unfolded mode
     */
    static boolean getTestPassMode(Bundle metaData, String displayMode) {
        if (metaData == null || !displayMode.equals(MULTIPLE_DISPLAY_MODE)) {
            return false;
        }
        String testPassMode = metaData.getString(TEST_PASS_MODE);
        return testPassMode != null && testPassMode.equals(EITHER_MODE);
    }

    static String getTitle(Context context, ActivityInfo activityInfo) {
        if (activityInfo.labelRes != 0) {
            return context.getString(activityInfo.labelRes);
        } else {
            return activityInfo.name;
        }
    }

    static Intent getActivityIntent(ActivityInfo activityInfo) {
        Intent intent = new Intent();
        intent.setClassName(activityInfo.packageName, activityInfo.name);
        return intent;
    }

    static void addTestToCategory(
            Map<String, List<TestListItem>> testsByCategory,
            String testCategory,
            TestListItem item) {
        List<TestListItem> tests;
        if (testsByCategory.containsKey(testCategory)) {
            tests = testsByCategory.get(testCategory);
        } else {
            tests = new ArrayList<TestListItem>();
        }
        testsByCategory.put(testCategory, tests);
        tests.add(item);
    }

    private boolean hasAnyFeature(String[] features) {
        if (features != null) {
            PackageManager packageManager = mContext.getPackageManager();
            for (String feature : features) {
                if (packageManager.hasSystemFeature(feature)) {
                    return true;
                }
            }
            Log.v(LOG_TAG, "Missing features " + Arrays.toString(features));
        }
        return false;
    }

    private boolean hasAllFeatures(String[] features) {
        if (features != null) {
            PackageManager packageManager = mContext.getPackageManager();
            for (String feature : features) {
                if (!packageManager.hasSystemFeature(feature)) {
                    Log.v(LOG_TAG, "Missing feature " + feature);
                    return false;
                }
            }
        }
        return true;
    }

    private boolean hasAllActions(String[] actions) {
        if (actions != null) {
            PackageManager packageManager = mContext.getPackageManager();
            for (String action : actions) {
                Intent intent = new Intent(action);
                if (packageManager.queryIntentActivities(intent, /* flags= */ 0).isEmpty()) {
                    Log.v(LOG_TAG, "Missing action " + action);
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean matchAllConfigs(Context context, String[] configs) {
        if (configs != null) {
            for (String config : configs) {
                switch (config) {
                    case CONFIG_NO_EMULATOR:
                        try {
                            Method getStringMethod =
                                    ClassLoader.getSystemClassLoader()
                                            .loadClass("android.os.SystemProperties")
                                            .getMethod("get", String.class);
                            String emulatorKernel =
                                    (String) getStringMethod.invoke("0", "ro.boot.qemu");
                            if (emulatorKernel.equals("1")) {
                                return false;
                            }
                        } catch (Exception e) {
                            Log.e(LOG_TAG, "Exception while checking for emulator support.", e);
                        }
                        break;
                    case CONFIG_VOICE_CAPABLE:
                        TelephonyManager telephonyManager =
                                context.getSystemService(TelephonyManager.class);
                        if (!telephonyManager.isVoiceCapable()) {
                            return false;
                        }
                        break;
                    case CONFIG_HAS_RECENTS:
                        if (!getSystemResourceFlag(context, "config_hasRecents")) {
                            return false;
                        }
                        break;
                    case CONFIG_HDMI_SOURCE:
                        final int DEVICE_TYPE_HDMI_SOURCE = 4;
                        try {
                            if (!getHdmiDeviceType().contains(DEVICE_TYPE_HDMI_SOURCE)) {
                                return false;
                            }
                        } catch (Exception exception) {
                            Log.e(
                                    LOG_TAG,
                                    "Exception while looking up HDMI device type.",
                                    exception);
                        }
                        break;
                    case CONFIG_BATTERY_SUPPORTED:
                        if (!hasBattery(context)) {
                            return false;
                        }
                        break;
                    case CONFIG_QUICK_SETTINGS_SUPPORTED:
                        if (!getSystemResourceFlag(context, "config_quickSettingsSupported")) {
                            return false;
                        }
                        break;
                    case CONFIG_HAS_MIC_TOGGLE:
                        return isHardwareToggleSupported(
                                context, SensorPrivacyManager.Sensors.MICROPHONE);
                    case CONFIG_HAS_CAMERA_TOGGLE:
                        return isHardwareToggleSupported(
                                context, SensorPrivacyManager.Sensors.CAMERA);
                    default:
                        break;
                }
            }
        }
        return true;
    }

    /**
     * Checks if the test should be ran by the given display mode.
     *
     * @param mode the display mode config of the test
     * @param currentMode the given display mode
     * @return true if the given display mode matches the configs, otherwise, return false
     */
    private boolean matchDisplayMode(String mode, String currentMode) {
        if (mode == null) {
            return false;
        }
        switch (mode) {
            case SINGLE_DISPLAY_MODE:
                return currentMode.equals(DisplayMode.UNFOLDED.toString());
            case MULTIPLE_DISPLAY_MODE:
                return true;
            case FOLDED_DISPLAY_MODE:
                return currentMode.equals(DisplayMode.FOLDED.toString());
            default:
                return false;
        }
    }

    /** Checks whether the test is being run by a user type that doesn't support it. */
    private boolean matchAnyExcludedUserType(String[] userTypes) {
        if (userTypes == null) {
            return false;
        }

        for (String userType : userTypes) {
            switch (userType) {
                case USER_TYPE_VISIBLE_BG_USER:
                    if (isVisibleBackgroundNonProfileUser()) {
                        Log.d(LOG_TAG, "Match for " + USER_TYPE_VISIBLE_BG_USER);
                        return true;
                    }
                    return false;
                default:
                    throw new IllegalArgumentException(
                            "Invalid "
                                    + TEST_EXCLUDED_USER_TYPES_META_DATA
                                    + " value: "
                                    + userType);
            }
        }

        return false;
    }

    /** Checks whether the title of the test matches the test filter. */
    private boolean macthTestFilter(String testTitle) {
        if (mTestFilter == null) {
            return true;
        }
        return testTitle != null
                && testTitle
                        .toLowerCase(Locale.getDefault())
                        .contains(mTestFilter.toLowerCase(Locale.getDefault()));
    }

    private boolean isVisibleBackgroundNonProfileUser() {
        if (!SdkLevel.isAtLeastU()) {
            Log.d(LOG_TAG, "isVisibleBagroundNonProfileUser() returning false on pre-UDC device");
            return false;
        }
        UserManager userMgr = mContext.getSystemService(UserManager.class);
        return userMgr.isUserVisible() && !userMgr.isUserForeground() && !userMgr.isProfile();
    }

    private static boolean getSystemResourceFlag(Context context, String key) {
        final Resources systemRes = context.getResources().getSystem();
        final int id = systemRes.getIdentifier(key, "bool", "android");
        if (id == Resources.ID_NULL) {
            // The flag being queried should exist in
            // frameworks/base/core/res/res/values/config.xml.
            throw new RuntimeException("System resource flag " + key + " not found");
        }
        return systemRes.getBoolean(id);
    }

    private static List<Integer> getHdmiDeviceType()
            throws InvocationTargetException,
                    IllegalAccessException,
                    ClassNotFoundException,
                    NoSuchMethodException {
        Method getStringMethod =
                ClassLoader.getSystemClassLoader()
                        .loadClass("android.os.SystemProperties")
                        .getMethod("get", String.class);
        String deviceTypesStr = (String) getStringMethod.invoke(null, "ro.hdmi.device_type");
        if (deviceTypesStr.equals("")) {
            return new ArrayList<>();
        }
        return Arrays.stream(deviceTypesStr.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
    }

    private static boolean hasBattery(Context context) {
        final Intent batteryInfo =
                context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        return batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
    }

    List<TestListItem> filterTests(List<TestListItem> tests, String mode) {
        List<TestListItem> filteredTests = new ArrayList<>();
        for (TestListItem test : tests) {
            if (!hasAnyFeature(test.excludedFeatures)
                    && hasAllFeatures(test.requiredFeatures)
                    && hasAllActions(test.requiredActions)
                    && matchAllConfigs(mContext, test.requiredConfigs)
                    && matchDisplayMode(test.displayMode, mode)
                    && !matchAnyExcludedUserType(test.excludedUserTypes)
                    && macthTestFilter(test.title)) {
                if (test.applicableFeatures == null || hasAnyFeature(test.applicableFeatures)) {
                    // Add suffix in test name if the test is in the folded mode.
                    test.testName = setTestNameSuffix(mode, test.testName);
                    filteredTests.add(test);
                } else {
                    Log.d(LOG_TAG, "Skipping " + test.testName + " due to metadata filtering");
                }
            } else {
                Log.d(LOG_TAG, "Skipping " + test.testName + " due to metadata filtering");
            }
        }
        return filteredTests;
    }

    @SuppressLint("NewApi")
    private static boolean isHardwareToggleSupported(Context context, final int sensorType) {
        boolean isToggleSupported = false;
        SensorPrivacyManager sensorPrivacyManager =
                context.getSystemService(SensorPrivacyManager.class);
        if (sensorPrivacyManager != null) {
            isToggleSupported =
                    sensorPrivacyManager.supportsSensorToggle(
                            SensorPrivacyManager.TOGGLE_TYPE_HARDWARE, sensorType);
        }
        return isToggleSupported;
    }
}