summaryrefslogtreecommitdiff
path: root/hostsidetests/scopedstorage/libs/ScopedStorageTestLib/src/android/scopedstorage/cts/lib/TestUtils.java
blob: 8d25bc421a56494a1d95d6ee11ad0e13fc499110 (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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
/**
 * Copyright (C) 2020 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.scopedstorage.cts.lib;

import static android.scopedstorage.cts.lib.RedactionTestHelper.EXIF_METADATA_QUERY;

import static androidx.test.InstrumentationRegistry.getContext;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.fail;

import android.Manifest;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.app.UiAutomation;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.test.InstrumentationRegistry;

import com.android.cts.install.lib.Install;
import com.android.cts.install.lib.InstallUtils;
import com.android.cts.install.lib.TestApp;
import com.android.cts.install.lib.Uninstall;

import com.google.common.io.ByteStreams;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;

/**
 * General helper functions for ScopedStorageTest tests.
 */
public class TestUtils {
    static final String TAG = "ScopedStorageTest";

    public static final String QUERY_TYPE = "android.scopedstorage.cts.queryType";
    public static final String INTENT_EXTRA_PATH = "android.scopedstorage.cts.path";
    public static final String INTENT_EXTRA_CALLING_PKG = "android.scopedstorage.cts.calling_pkg";
    public static final String INTENT_EXCEPTION = "android.scopedstorage.cts.exception";
    public static final String CREATE_FILE_QUERY = "android.scopedstorage.cts.createfile";
    public static final String CREATE_IMAGE_ENTRY_QUERY =
            "android.scopedstorage.cts.createimageentry";
    public static final String DELETE_FILE_QUERY = "android.scopedstorage.cts.deletefile";
    public static final String CAN_OPEN_FILE_FOR_READ_QUERY =
            "android.scopedstorage.cts.can_openfile_read";
    public static final String CAN_OPEN_FILE_FOR_WRITE_QUERY =
            "android.scopedstorage.cts.can_openfile_write";
    public static final String OPEN_FILE_FOR_READ_QUERY =
            "android.scopedstorage.cts.openfile_read";
    public static final String OPEN_FILE_FOR_WRITE_QUERY =
            "android.scopedstorage.cts.openfile_write";
    public static final String CAN_READ_WRITE_QUERY =
            "android.scopedstorage.cts.can_read_and_write";
    public static final String READDIR_QUERY = "android.scopedstorage.cts.readdir";
    public static final String SETATTR_QUERY = "android.scopedstorage.cts.setattr";

    public static final String STR_DATA1 = "Just some random text";
    public static final String STR_DATA2 = "More arbitrary stuff";

    public static final byte[] BYTES_DATA1 = STR_DATA1.getBytes();
    public static final byte[] BYTES_DATA2 = STR_DATA2.getBytes();

    // Root of external storage
    private static File sExternalStorageDirectory = Environment.getExternalStorageDirectory();
    private static String sStorageVolumeName = MediaStore.VOLUME_EXTERNAL;

    private static final long POLLING_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(20);
    private static final long POLLING_SLEEP_MILLIS = 100;

    /**
     * Creates the top level default directories.
     *
     * <p>Those are usually created by MediaProvider, but some naughty tests might delete them
     * and not restore them afterwards, so we make sure we create them before we make any
     * assumptions about their existence.
     */
    public static void setupDefaultDirectories() {
        for (File dir : getDefaultTopLevelDirs()) {
            dir.mkdir();
            assertThat(dir.exists()).isTrue();
        }
    }

    /**
     * Grants {@link Manifest.permission#GRANT_RUNTIME_PERMISSIONS} to the given package.
     */
    public static void grantPermission(String packageName, String permission) {
        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        uiAutomation.adoptShellPermissionIdentity("android.permission.GRANT_RUNTIME_PERMISSIONS");
        try {
            uiAutomation.grantRuntimePermission(packageName, permission);
        } finally {
            uiAutomation.dropShellPermissionIdentity();
        }
        try {
            pollForPermission(packageName, permission, true);
        } catch (Exception e) {
            fail("Exception on polling for permission grant for " + packageName + " for "
                    + permission + ": " + e.getMessage());
        }
    }

    /**
     * Revokes permissions from the given package.
     */
    public static void revokePermission(String packageName, String permission) {
        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        uiAutomation.adoptShellPermissionIdentity("android.permission.REVOKE_RUNTIME_PERMISSIONS");
        try {
            uiAutomation.revokeRuntimePermission(packageName, permission);
        } finally {
            uiAutomation.dropShellPermissionIdentity();
        }
        try {
            pollForPermission(packageName, permission, false);
        } catch (Exception e) {
            fail("Exception on polling for permission revoke for " + packageName + " for "
                    + permission + ": " + e.getMessage());
        }
    }

    /**
     * Adopts shell permission identity for the given permissions.
     */
    public static void adoptShellPermissionIdentity(String... permissions) {
        InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity(
                permissions);
    }

    /**
     * Drops shell permission identity for all permissions.
     */
    public static void dropShellPermissionIdentity() {
        InstrumentationRegistry.getInstrumentation().getUiAutomation()
                .dropShellPermissionIdentity();
    }

    /**
     * Executes a shell command.
     */
    public static String executeShellCommand(String pattern, Object...args) throws IOException {
        String command = String.format(pattern, args);
        int attempt = 0;
        while (attempt++ < 5) {
            try {
                return executeShellCommandInternal(command);
            } catch (InterruptedIOException e) {
                // Hmm, we had trouble executing the shell command; the best we
                // can do is try again a few more times
                Log.v(TAG, "Trouble executing " + command + "; trying again", e);
            }
        }
        throw new IOException("Failed to execute " + command);
    }

    private static String executeShellCommandInternal(String cmd) throws IOException {
        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        try (FileInputStream output = new FileInputStream(
                     uiAutomation.executeShellCommand(cmd).getFileDescriptor())) {
            return new String(ByteStreams.toByteArray(output));
        }
    }

    /**
     * Makes the given {@code testApp} list the content of the given directory and returns the
     * result as an {@link ArrayList}
     */
    public static ArrayList<String> listAs(TestApp testApp, String dirPath) throws Exception {
        return getContentsFromTestApp(testApp, dirPath, READDIR_QUERY);
    }

    /**
     * Returns {@code true} iff the given {@code path} exists and is readable and
     * writable for for {@code testApp}.
     */
    public static boolean canReadAndWriteAs(TestApp testApp, String path) throws Exception {
        return getResultFromTestApp(testApp, path, CAN_READ_WRITE_QUERY);
    }

    /**
     * Makes the given {@code testApp} read the EXIF metadata from the given file and returns the
     * result as an {@link HashMap}
     */
    public static HashMap<String, String> readExifMetadataFromTestApp(
            TestApp testApp, String filePath) throws Exception {
        HashMap<String, String> res =
                getMetadataFromTestApp(testApp, filePath, EXIF_METADATA_QUERY);
        return res;
    }

    /**
     * Makes the given {@code testApp} create a file.
     *
     * <p>This method drops shell permission identity.
     */
    public static boolean createFileAs(TestApp testApp, String path) throws Exception {
        return getResultFromTestApp(testApp, path, CREATE_FILE_QUERY);
    }

    /**
     * Makes the given {@code testApp} create a mediastore DB entry under
     * {@code MediaStore.Media.Images}.
     *
     * The {@code path} argument is treated as a relative path and a name separated
     * by an {@code '/'}.
     */
    public static boolean createImageEntryAs(TestApp testApp, String path) throws Exception {
        return getResultFromTestApp(testApp, path, CREATE_IMAGE_ENTRY_QUERY);
    }

    /**
     * Makes the given {@code testApp} delete a file.
     *
     * <p>This method drops shell permission identity.
     */
    public static boolean deleteFileAs(TestApp testApp, String path) throws Exception {
        return getResultFromTestApp(testApp, path, DELETE_FILE_QUERY);
    }

    /**
     * Makes the given {@code testApp} delete a file. Doesn't throw in case of failure.
     */
    public static boolean deleteFileAsNoThrow(TestApp testApp, String path) {
        try {
            return deleteFileAs(testApp, path);
        } catch (Exception e) {
            Log.e(TAG,
                    "Error occurred while deleting file: " + path + " on behalf of app: " + testApp,
                    e);
            return false;
        }
    }

    /**
     * Makes the given {@code testApp} open {@code file} for read or write.
     *
     * <p>This method drops shell permission identity.
     */
    public static boolean canOpenFileAs(TestApp testApp, File file, boolean forWrite)
            throws Exception {
        String actionName = forWrite ? CAN_OPEN_FILE_FOR_WRITE_QUERY : CAN_OPEN_FILE_FOR_READ_QUERY;
        return getResultFromTestApp(testApp, file.getPath(), actionName);
    }

    /**
     * Makes the given {@code testApp} open a file for read or write.
     *
     * <p>This method drops shell permission identity.
     */
    public static ParcelFileDescriptor openFileAs(TestApp testApp, File file, boolean forWrite)
            throws Exception {
        String actionName = forWrite ? OPEN_FILE_FOR_WRITE_QUERY : OPEN_FILE_FOR_READ_QUERY;
        String mode = forWrite ? "rw" : "r";
        return getPfdFromTestApp(testApp, file, actionName, mode);
    }

    /**
     * Makes the given {@code testApp} setattr for given file path.
     *
     * <p>This method drops shell permission identity.
     */
    public static boolean setAttrAs(TestApp testApp, String path)
            throws Exception {
        return getResultFromTestApp(testApp, path, SETATTR_QUERY);
    }

    /**
     * Installs a {@link TestApp} without storage permissions.
     */
    public static void installApp(TestApp testApp) throws Exception {
        installApp(testApp, /* grantStoragePermission */ false);
    }

    /**
     * Installs a {@link TestApp} with storage permissions.
     */
    public static void installAppWithStoragePermissions(TestApp testApp) throws Exception {
        installApp(testApp, /* grantStoragePermission */ true);
    }

    /**
     * Installs a {@link TestApp} and may grant it storage permissions.
     */
    public static void installApp(TestApp testApp, boolean grantStoragePermission)
            throws Exception {
        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        try {
            final String packageName = testApp.getPackageName();
            uiAutomation.adoptShellPermissionIdentity(
                    Manifest.permission.INSTALL_PACKAGES, Manifest.permission.DELETE_PACKAGES);
            if (isAppInstalled(testApp)) {
                Uninstall.packages(packageName);
            }
            Install.single(testApp).commit();
            assertThat(InstallUtils.getInstalledVersion(packageName)).isEqualTo(1);
            if (grantStoragePermission) {
                grantPermission(packageName, Manifest.permission.READ_EXTERNAL_STORAGE);
            }
        } finally {
            uiAutomation.dropShellPermissionIdentity();
        }
    }

    public static boolean isAppInstalled(TestApp testApp) {
        return InstallUtils.getInstalledVersion(testApp.getPackageName()) != -1;
    }

    /**
     * Uninstalls a {@link TestApp}.
     */
    public static void uninstallApp(TestApp testApp) throws Exception {
        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        try {
            final String packageName = testApp.getPackageName();
            uiAutomation.adoptShellPermissionIdentity(Manifest.permission.DELETE_PACKAGES);

            Uninstall.packages(packageName);
            assertThat(InstallUtils.getInstalledVersion(packageName)).isEqualTo(-1);
        } finally {
            uiAutomation.dropShellPermissionIdentity();
        }
    }

    /**
     * Uninstalls a {@link TestApp}. Doesn't throw in case of failure.
     */
    public static void uninstallAppNoThrow(TestApp testApp) {
        try {
            uninstallApp(testApp);
        } catch (Exception e) {
            Log.e(TAG, "Exception occurred while uninstalling app: " + testApp, e);
        }
    }

    public static ContentResolver getContentResolver() {
        return getContext().getContentResolver();
    }

    /**
     * Inserts a file into the database using {@link MediaStore.MediaColumns#DATA}.
     */
    public static Uri insertFileUsingDataColumn(@NonNull File file) {
        final ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, file.getPath());
        return getContentResolver().insert(MediaStore.Files.getContentUri(sStorageVolumeName),
                values);
    }

    /**
     * Returns the content URI for images based on the current storage volume.
     */
    public static Uri getImageContentUri() {
        return MediaStore.Images.Media.getContentUri(sStorageVolumeName);
    }

    /**
     * Renames the given file using {@link ContentResolver} and {@link MediaStore} and APIs.
     * This method uses the data column, and not all apps can use it.
     * @see MediaStore.MediaColumns#DATA
     */
    public static int renameWithMediaProvider(@NonNull File oldPath, @NonNull File newPath) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, newPath.getPath());
        return getContentResolver().update(MediaStore.Files.getContentUri(sStorageVolumeName),
                values, /*where*/ MediaStore.MediaColumns.DATA + "=?",
                /*whereArgs*/ new String[] {oldPath.getPath()});
    }

    /**
     * Queries {@link ContentResolver} for a file and returns the corresponding {@link Uri} for its
     * entry in the database. Returns {@code null} if file doesn't exist in the database.
     */
    @Nullable
    public static Uri getFileUri(@NonNull File file) {
        final Uri contentUri = MediaStore.Files.getContentUri(sStorageVolumeName);
        final int id = getFileRowIdFromDatabase(file);
        return id == -1 ? null : ContentUris.withAppendedId(contentUri, id);
    }

    /**
     * Queries {@link ContentResolver} for a file and returns the corresponding row ID for its
     * entry in the database. Returns {@code -1} if file is not found.
     */
    public static int getFileRowIdFromDatabase(@NonNull File file) {
        int id = -1;
        try (Cursor c = queryFile(file, MediaStore.MediaColumns._ID)) {
            if (c.moveToFirst()) {
                id = c.getInt(0);
            }
        }
        return id;
    }

    /**
     * Queries {@link ContentResolver} for a file and returns the corresponding owner package name
     * for its entry in the database.
     */
    @Nullable
    public static String getFileOwnerPackageFromDatabase(@NonNull File file) {
        String ownerPackage = null;
        try (Cursor c = queryFile(file, MediaStore.MediaColumns.OWNER_PACKAGE_NAME)) {
            if (c.moveToFirst()) {
                ownerPackage = c.getString(0);
            }
        }
        return ownerPackage;
    }

    /**
     * Queries {@link ContentResolver} for a file and returns the corresponding file size for its
     * entry in the database. Returns {@code -1} if file is not found.
     */
    @Nullable
    public static int getFileSizeFromDatabase(@NonNull File file) {
        int size = -1;
        try (Cursor c = queryFile(file, MediaStore.MediaColumns.SIZE)) {
            if (c.moveToFirst()) {
                size = c.getInt(0);
            }
        }
        return size;
    }

    /**
     * Queries {@link ContentResolver} for a video file and returns a {@link Cursor} with the given
     * columns.
     */
    @NonNull
    public static Cursor queryVideoFile(File file, String... projection) {
        return queryFile(MediaStore.Video.Media.getContentUri(sStorageVolumeName), file,
                /*includePending*/ true, projection);
    }

    /**
     * Queries {@link ContentResolver} for an image file and returns a {@link Cursor} with the given
     * columns.
     */
    @NonNull
    public static Cursor queryImageFile(File file, String... projection) {
        return queryFile(MediaStore.Images.Media.getContentUri(sStorageVolumeName), file,
                /*includePending*/ true, projection);
    }

    /**
     * Queries {@link ContentResolver} for a file and returns the corresponding mime type for its
     * entry in the database.
     */
    @NonNull
    public static String getFileMimeTypeFromDatabase(@NonNull File file) {
        String mimeType = "";
        try (Cursor c = queryFile(file, MediaStore.MediaColumns.MIME_TYPE)) {
            if (c.moveToFirst()) {
                mimeType = c.getString(0);
            }
        }
        return mimeType;
    }

    /**
     * Sets {@link AppOpsManager#MODE_ALLOWED} for the given {@code ops} and the given {@code uid}.
     *
     * <p>This method drops shell permission identity.
     */
    public static void allowAppOpsToUid(int uid, @NonNull String... ops) {
        setAppOpsModeForUid(uid, AppOpsManager.MODE_ALLOWED, ops);
    }

    /**
     * Sets {@link AppOpsManager#MODE_ERRORED} for the given {@code ops} and the given {@code uid}.
     *
     * <p>This method drops shell permission identity.
     */
    public static void denyAppOpsToUid(int uid, @NonNull String... ops) {
        setAppOpsModeForUid(uid, AppOpsManager.MODE_ERRORED, ops);
    }

    /**
     * Deletes the given file through {@link ContentResolver} and {@link MediaStore} APIs,
     * and asserts that the file was successfully deleted from the database.
     */
    public static void deleteWithMediaProvider(@NonNull File file) {
        Bundle extras = new Bundle();
        extras.putString(ContentResolver.QUERY_ARG_SQL_SELECTION,
                MediaStore.MediaColumns.DATA + " = ?");
        extras.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS,
                new String[] {file.getPath()});
        extras.putInt(MediaStore.QUERY_ARG_MATCH_PENDING, MediaStore.MATCH_INCLUDE);
        extras.putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_INCLUDE);
        assertThat(getContentResolver().delete(
                MediaStore.Files.getContentUri(sStorageVolumeName), extras)).isEqualTo(1);
    }

    /**
     * Deletes db rows and files corresponding to uri through {@link ContentResolver} and
     * {@link MediaStore} APIs.
     */
    public static void deleteWithMediaProviderNoThrow(Uri... uris) {
        for (Uri uri : uris) {
            if (uri == null) continue;

            try {
                getContentResolver().delete(uri, Bundle.EMPTY);
            } catch (Exception ignored) {
            }
        }
    }

    /**
     * Renames the given file through {@link ContentResolver} and {@link MediaStore} APIs,
     * and asserts that the file was updated in the database.
     */
    public static void updateDisplayNameWithMediaProvider(Uri uri, String relativePath,
            String oldDisplayName, String newDisplayName) {
        String selection = MediaStore.MediaColumns.RELATIVE_PATH + " = ? AND "
                + MediaStore.MediaColumns.DISPLAY_NAME + " = ?";
        String[] selectionArgs = {relativePath + '/', oldDisplayName};
        Bundle extras = new Bundle();
        extras.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection);
        extras.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
        extras.putInt(MediaStore.QUERY_ARG_MATCH_PENDING, MediaStore.MATCH_INCLUDE);
        extras.putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_INCLUDE);

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DISPLAY_NAME, newDisplayName);

        assertThat(getContentResolver().update(uri, values, extras)).isEqualTo(1);
    }

    /**
     * Opens the given file through {@link ContentResolver} and {@link MediaStore} APIs.
     */
    @NonNull
    public static ParcelFileDescriptor openWithMediaProvider(@NonNull File file, String mode)
            throws Exception {
        final Uri fileUri = getFileUri(file);
        assertThat(fileUri).isNotNull();
        Log.i(TAG, "Uri: " + fileUri + ". Data: " + file.getPath());
        ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(fileUri, mode);
        assertThat(pfd).isNotNull();
        return pfd;
    }

    /**
     * Opens the given file via file path
     */
    @NonNull
    public static ParcelFileDescriptor openWithFilePath(File file, boolean forWrite)
            throws IOException {
        return ParcelFileDescriptor.open(file,
                forWrite
                ? ParcelFileDescriptor.MODE_READ_WRITE : ParcelFileDescriptor.MODE_READ_ONLY);
    }

    /**
     * Returns whether we can open the file.
     */
    public static boolean canOpen(File file, boolean forWrite) {
        try {
            openWithFilePath(file, forWrite);
            return true;
        } catch (IOException expected) {
            return false;
        }
    }

    /**
     * Asserts the given operation throws an exception of type {@code T}.
     */
    public static <T extends Exception> void assertThrows(Class<T> clazz, Operation<Exception> r)
            throws Exception {
        assertThrows(clazz, "", r);
    }

    /**
     * Asserts the given operation throws an exception of type {@code T}.
     */
    public static <T extends Exception> void assertThrows(
            Class<T> clazz, String errMsg, Operation<Exception> r) throws Exception {
        try {
            r.run();
            fail("Expected " + clazz + " to be thrown");
        } catch (Exception e) {
            if (!clazz.isAssignableFrom(e.getClass()) || !e.getMessage().contains(errMsg)) {
                Log.e(TAG, "Expected " + clazz + " exception with error message: " + errMsg, e);
                throw e;
            }
        }
    }

    /**
     * A functional interface representing an operation that takes no arguments,
     * returns no arguments and might throw an {@link Exception} of any kind.
     *
     * @param T the subclass of {@link java.lang.Exception} that this operation might throw.
     */
    @FunctionalInterface
    public interface Operation<T extends Exception> {
        /**
         * This is the method that gets called for any object that implements this interface.
         */
        void run() throws T;
    }

    /**
     * Deletes the given file. If the file is a directory, then deletes all of its children (files
     * or directories) recursively.
     */
    public static boolean deleteRecursively(@NonNull File path) {
        if (path.isDirectory()) {
            for (File child : path.listFiles()) {
                if (!deleteRecursively(child)) {
                    return false;
                }
            }
        }
        return path.delete();
    }

    /**
     * Asserts can rename file.
     */
    public static void assertCanRenameFile(File oldFile, File newFile) {
        assertCanRenameFile(oldFile, newFile, /* checkDB */ true);
    }

    /**
     * Asserts can rename file and optionally checks if the database is updated after rename.
     */
    public static void assertCanRenameFile(File oldFile, File newFile, boolean checkDatabase) {
        assertThat(oldFile.renameTo(newFile)).isTrue();
        assertThat(oldFile.exists()).isFalse();
        assertThat(newFile.exists()).isTrue();
        if (checkDatabase) {
            assertThat(getFileRowIdFromDatabase(oldFile)).isEqualTo(-1);
            assertThat(getFileRowIdFromDatabase(newFile)).isNotEqualTo(-1);
        }
    }

    /**
     * Asserts cannot rename file.
     */
    public static void assertCantRenameFile(File oldFile, File newFile) {
        final int rowId = getFileRowIdFromDatabase(oldFile);
        assertThat(oldFile.renameTo(newFile)).isFalse();
        assertThat(oldFile.exists()).isTrue();
        assertThat(getFileRowIdFromDatabase(oldFile)).isEqualTo(rowId);
    }

    /**
     * Asserts can rename directory.
     */
    public static void assertCanRenameDirectory(File oldDirectory, File newDirectory,
            @Nullable File[] oldFilesList, @Nullable File[] newFilesList) {
        assertThat(oldDirectory.renameTo(newDirectory)).isTrue();
        assertThat(oldDirectory.exists()).isFalse();
        assertThat(newDirectory.exists()).isTrue();
        for (File file : oldFilesList != null ? oldFilesList : new File[0]) {
            assertThat(file.exists()).isFalse();
            assertThat(getFileRowIdFromDatabase(file)).isEqualTo(-1);
        }
        for (File file : newFilesList != null ? newFilesList : new File[0]) {
            assertThat(file.exists()).isTrue();
            assertThat(getFileRowIdFromDatabase(file)).isNotEqualTo(-1);
        }
    }

    /**
     * Asserts cannot rename directory.
     */
    public static void assertCantRenameDirectory(
            File oldDirectory, File newDirectory, @Nullable File[] oldFilesList) {
        assertThat(oldDirectory.renameTo(newDirectory)).isFalse();
        assertThat(oldDirectory.exists()).isTrue();
        for (File file : oldFilesList != null ? oldFilesList : new File[0]) {
            assertThat(file.exists()).isTrue();
            assertThat(getFileRowIdFromDatabase(file)).isNotEqualTo(-1);
        }
    }

    /**
     * Polls for external storage to be mounted.
     */
    public static void pollForExternalStorageState() throws Exception {
        pollForCondition(
                () -> Environment.getExternalStorageState(getExternalStorageDir())
                        .equals(Environment.MEDIA_MOUNTED),
                "Timed out while waiting for ExternalStorageState to be MEDIA_MOUNTED");
    }

    /**
     * Polls until we're granted or denied a given permission.
     */
    public static void pollForPermission(String perm, boolean granted) throws Exception {
        pollForCondition(() -> granted == checkPermissionAndAppOp(perm),
                "Timed out while waiting for permission " + perm + " to be "
                        + (granted ? "granted" : "revoked"));
    }

    /**
     * Polls until {@code app} is granted or denied the given permission.
     */
    public static void pollForPermission(TestApp app, String perm, boolean granted)
            throws Exception {
        pollForPermission(app.getPackageName(), perm, granted);
    }

    /**
     * Polls until {@code packageName} is granted or denied the given permission.
     */
    public static void pollForPermission(String packageName, String perm, boolean granted)
            throws Exception {
        pollForCondition(
                () -> granted == checkPermission(packageName, perm),
                "Timed out while waiting for permission " + perm + " to be "
                        + (granted ? "granted" : "revoked"));
    }

    /**
     * Returns true iff {@code packageName} is granted a given permission.
     */
    public static boolean checkPermission(String packageName, String perm) {
        try {
            int uid = getContext().getPackageManager().getPackageUid(packageName, 0);

            Optional<ActivityManager.RunningAppProcessInfo> process = getAppProcessInfo(
                    packageName);
            int pid = process.isPresent() ? process.get().pid : -1;
            return checkPermissionAndAppOp(perm, packageName, pid, uid);
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    /**
     * Returns true iff {@code app} is granted a given permission.
     */
    public static boolean checkPermission(TestApp app, String perm) {
        return checkPermission(app.getPackageName(), perm);
    }

    /**
     * Asserts the entire content of the file equals exactly {@code expectedContent}.
     */
    public static void assertFileContent(File file, byte[] expectedContent) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            assertInputStreamContent(fis, expectedContent);
        }
    }

    /**
     * Asserts the entire content of the file equals exactly {@code expectedContent}.
     * <p>Sets {@code fd} to beginning of file first.
     */
    public static void assertFileContent(FileDescriptor fd, byte[] expectedContent)
            throws IOException, ErrnoException {
        Os.lseek(fd, 0, OsConstants.SEEK_SET);
        try (FileInputStream fis = new FileInputStream(fd)) {
            assertInputStreamContent(fis, expectedContent);
        }
    }

    /**
     * Asserts that {@code dir} is a directory and that it doesn't contain any of
     * {@code unexpectedContent}
     */
    public static void assertDirectoryDoesNotContain(@NonNull File dir, File... unexpectedContent) {
        assertThat(dir.isDirectory()).isTrue();
        assertThat(Arrays.asList(dir.listFiles())).containsNoneIn(unexpectedContent);
    }

    /**
     * Asserts that {@code dir} is a directory and that it contains all of {@code expectedContent}
     */
    public static void assertDirectoryContains(@NonNull File dir, File... expectedContent) {
        assertThat(dir.isDirectory()).isTrue();
        assertThat(Arrays.asList(dir.listFiles())).containsAllIn(expectedContent);
    }

    public static File getExternalStorageDir() {
        return sExternalStorageDirectory;
    }

    public static void setExternalStorageVolume(@NonNull String volName) {
        sStorageVolumeName = volName.toLowerCase(Locale.ROOT);
        sExternalStorageDirectory = new File("/storage/" + volName);
    }

    /**
     * Resets the root directory of external storage to the default.
     *
     * @see Environment#getExternalStorageDirectory()
     */
    public static void resetDefaultExternalStorageVolume() {
        sStorageVolumeName = MediaStore.VOLUME_EXTERNAL;
        sExternalStorageDirectory = Environment.getExternalStorageDirectory();
    }

    /**
     * Asserts the default volume used in helper methods is the primary volume.
     */
    public static void assertDefaultVolumeIsPrimary() {
        assertVolumeType(true /* isPrimary */);
    }

    /**
     * Asserts the default volume used in helper methods is a public volume.
     */
    public static void assertDefaultVolumeIsPublic() {
        assertVolumeType(false /* isPrimary */);
    }

    /**
     * Creates and returns the Android data sub-directory belonging to the calling package.
     */
    public static File getExternalFilesDir() {
        final String packageName = getContext().getPackageName();
        final File res = new File(getAndroidDataDir(), packageName + "/files");
        if (!res.equals(getContext().getExternalFilesDir(null))) {
            res.mkdirs();
        }
        return res;
    }

    /**
     * Creates and returns the Android media sub-directory belonging to the calling package.
     */
    public static File getExternalMediaDir() {
        final String packageName = getContext().getPackageName();
        final File res = new File(getAndroidMediaDir(), packageName);
        if (!res.equals(getContext().getExternalMediaDirs()[0])) {
            res.mkdirs();
        }
        return res;
    }

    public static File getAlarmsDir() {
        return new File(getExternalStorageDir(),
                Environment.DIRECTORY_ALARMS);
    }

    public static File getAndroidDir() {
        return new File(getExternalStorageDir(),
                "Android");
    }

    public static File getAudiobooksDir() {
        return new File(getExternalStorageDir(),
                Environment.DIRECTORY_AUDIOBOOKS);
    }

    public static File getDcimDir() {
        return new File(getExternalStorageDir(), Environment.DIRECTORY_DCIM);
    }

    public static File getDocumentsDir() {
        return new File(getExternalStorageDir(),
                Environment.DIRECTORY_DOCUMENTS);
    }

    public static File getDownloadDir() {
        return new File(getExternalStorageDir(),
                Environment.DIRECTORY_DOWNLOADS);
    }

    public static File getMusicDir() {
        return new File(getExternalStorageDir(),
                Environment.DIRECTORY_MUSIC);
    }

    public static File getMoviesDir() {
        return new File(getExternalStorageDir(),
                Environment.DIRECTORY_MOVIES);
    }

    public static File getNotificationsDir() {
        return new File(getExternalStorageDir(),
                Environment.DIRECTORY_NOTIFICATIONS);
    }

    public static File getPicturesDir() {
        return new File(getExternalStorageDir(),
                Environment.DIRECTORY_PICTURES);
    }

    public static File getPodcastsDir() {
        return new File(getExternalStorageDir(),
                Environment.DIRECTORY_PODCASTS);
    }

    public static File getRingtonesDir() {
        return new File(getExternalStorageDir(),
                Environment.DIRECTORY_RINGTONES);
    }

    public static File getAndroidDataDir() {
        return new File(getAndroidDir(), "data");
    }

    public static File getAndroidMediaDir() {
        return new File(getAndroidDir(), "media");
    }

    public static File[] getDefaultTopLevelDirs() {
        return new File [] { getAlarmsDir(), getAndroidDir(), getAudiobooksDir(), getDcimDir(),
                getDocumentsDir(), getDownloadDir(), getMusicDir(), getMoviesDir(),
                getNotificationsDir(), getPicturesDir(), getPodcastsDir(), getRingtonesDir() };
    }

    private static void assertInputStreamContent(InputStream in, byte[] expectedContent)
            throws IOException {
        assertThat(ByteStreams.toByteArray(in)).isEqualTo(expectedContent);
    }

    /**
     * Checks if the given {@code permission} is granted and corresponding AppOp is MODE_ALLOWED.
     */
    private static boolean checkPermissionAndAppOp(String permission) {
        final int pid = Os.getpid();
        final int uid = Os.getuid();
        final String packageName = getContext().getPackageName();
        return checkPermissionAndAppOp(permission, packageName, pid, uid);
    }

    /**
     * Checks if the given {@code permission} is granted and corresponding AppOp is MODE_ALLOWED.
     */
    private static boolean checkPermissionAndAppOp(String permission, String packageName, int pid,
            int uid) {
        final Context context = getContext();
        if (context.checkPermission(permission, pid, uid) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }

        final String op = AppOpsManager.permissionToOp(permission);
        // No AppOp associated with the given permission, skip AppOp check.
        if (op == null) {
            return true;
        }

        final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
        try {
            appOps.checkPackage(uid, packageName);
        } catch (SecurityException e) {
            return false;
        }

        return appOps.unsafeCheckOpNoThrow(op, uid, packageName) == AppOpsManager.MODE_ALLOWED;
    }

    /**
     * <p>This method drops shell permission identity.
     */
    private static void forceStopApp(String packageName) throws Exception {
        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        try {
            uiAutomation.adoptShellPermissionIdentity(Manifest.permission.FORCE_STOP_PACKAGES);

            getContext().getSystemService(ActivityManager.class).forceStopPackage(packageName);
            pollForCondition(() -> {
                return !isProcessRunning(packageName);
            }, "Timed out while waiting for " + packageName + " to be stopped");
        } finally {
            uiAutomation.dropShellPermissionIdentity();
        }
    }

    /**
     * <p>This method drops shell permission identity.
     */
    private static void sendIntentToTestApp(TestApp testApp, String dirPath, String actionName,
            BroadcastReceiver broadcastReceiver, CountDownLatch latch) throws Exception {
        final String packageName = testApp.getPackageName();
        forceStopApp(packageName);
        // Register broadcast receiver
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(actionName);
        intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
        getContext().registerReceiver(broadcastReceiver, intentFilter);

        // Launch the test app.
        final Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setPackage(packageName);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(QUERY_TYPE, actionName);
        intent.putExtra(INTENT_EXTRA_PATH, dirPath);
        intent.putExtra(INTENT_EXTRA_CALLING_PKG, getContext().getPackageName());
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        getContext().startActivity(intent);
        if (!latch.await(POLLING_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
            final String errorMessage = "Timed out while waiting to receive " + actionName
                    + " intent from " + packageName;
            throw new TimeoutException(errorMessage);
        }
        getContext().unregisterReceiver(broadcastReceiver);
    }

    /**
     * Gets images/video metadata from a test app.
     *
     * <p>This method drops shell permission identity.
     */
    private static HashMap<String, String> getMetadataFromTestApp(
            TestApp testApp, String dirPath, String actionName) throws Exception {
        Bundle bundle = getFromTestApp(testApp, dirPath, actionName);
        return (HashMap<String, String>) bundle.get(actionName);
    }

    /**
     * <p>This method drops shell permission identity.
     */
    private static ArrayList<String> getContentsFromTestApp(
            TestApp testApp, String dirPath, String actionName) throws Exception {
        Bundle bundle = getFromTestApp(testApp, dirPath, actionName);
        return bundle.getStringArrayList(actionName);
    }

    /**
     * <p>This method drops shell permission identity.
     */
    private static boolean getResultFromTestApp(TestApp testApp, String dirPath, String actionName)
            throws Exception {
        Bundle bundle = getFromTestApp(testApp, dirPath, actionName);
        return bundle.getBoolean(actionName, false);
    }

    private static ParcelFileDescriptor getPfdFromTestApp(TestApp testApp, File dirPath,
            String actionName, String mode) throws Exception {
        Bundle bundle = getFromTestApp(testApp, dirPath.getPath(), actionName);
        return getContentResolver().openFileDescriptor(bundle.getParcelable(actionName), mode);
    }

    /**
     * <p>This method drops shell permission identity.
     */
    private static Bundle getFromTestApp(TestApp testApp, String dirPath, String actionName)
            throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);
        final Bundle[] bundle = new Bundle[1];
        final Exception[] exception = new Exception[1];
        exception[0] = null;
        BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.hasExtra(INTENT_EXCEPTION)) {
                    exception[0] = (Exception) (intent.getSerializableExtra(INTENT_EXCEPTION));
                } else {
                    bundle[0] = intent.getExtras();
                }
                latch.countDown();
            }
        };

        sendIntentToTestApp(testApp, dirPath, actionName, broadcastReceiver, latch);
        if (exception[0] != null) {
            throw exception[0];
        }
        return bundle[0];
    }

    /**
     * Sets {@code mode} for the given {@code ops} and the given {@code uid}.
     *
     * <p>This method drops shell permission identity.
     */
    private static void setAppOpsModeForUid(int uid, int mode, @NonNull String... ops) {
        adoptShellPermissionIdentity(null);
        try {
            for (String op : ops) {
                getContext().getSystemService(AppOpsManager.class).setUidMode(op, uid, mode);
            }
        } finally {
            dropShellPermissionIdentity();
        }
    }

    /**
     * Queries {@link ContentResolver} for a file IS_PENDING=0 and returns a {@link Cursor} with the
     * given columns.
     */
    @NonNull
    public static Cursor queryFileExcludingPending(@NonNull File file, String... projection) {
        return queryFile(MediaStore.Files.getContentUri(sStorageVolumeName), file,
                /*includePending*/ false, projection);
    }

    @NonNull
    public static Cursor queryFile(@NonNull File file, String... projection) {
        return queryFile(MediaStore.Files.getContentUri(sStorageVolumeName), file,
                /*includePending*/ true, projection);
    }

    @NonNull
    private static Cursor queryFile(@NonNull Uri uri, @NonNull File file, boolean includePending,
            String... projection) {
        Bundle queryArgs = new Bundle();
        queryArgs.putString(ContentResolver.QUERY_ARG_SQL_SELECTION,
                MediaStore.MediaColumns.DATA + " = ?");
        queryArgs.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS,
                new String[] { file.getAbsolutePath() });
        queryArgs.putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_INCLUDE);

        if (includePending) {
            queryArgs.putInt(MediaStore.QUERY_ARG_MATCH_PENDING, MediaStore.MATCH_INCLUDE);
        } else {
            queryArgs.putInt(MediaStore.QUERY_ARG_MATCH_PENDING, MediaStore.MATCH_EXCLUDE);
        }

        final Cursor c = getContentResolver().query(uri, projection, queryArgs, null);
        assertThat(c).isNotNull();
        return c;
    }

    /**
     * Creates a new virtual public volume and returns the volume's name.
     */
    public static void createNewPublicVolume() throws Exception {
        executeShellCommand("sm set-force-adoptable on");
        executeShellCommand("sm set-virtual-disk true");
        Thread.sleep(2000);
        pollForCondition(TestUtils::partitionDisk, "Timed out while waiting for disk partitioning");
    }

    private static boolean partitionDisk() {
        try {
            final String listDisks = executeShellCommand("sm list-disks").trim();
            executeShellCommand("sm partition " + listDisks + " public");
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * Gets the name of the public volume, waiting for a bit for it to be available.
     */
    public static String getPublicVolumeName() throws Exception {
        final String[] volName = new String[1];
        pollForCondition(() -> {
            volName[0] = getCurrentPublicVolumeName();
            return volName[0] != null;
        }, "Timed out while waiting for public volume to be ready");

        return volName[0];
    }

    /**
     * @return the currently mounted public volume, if any.
     */
    public static String getCurrentPublicVolumeName() {
        final String[] allVolumeDetails;
        try {
            allVolumeDetails = executeShellCommand("sm list-volumes")
                    .trim().split("\n");
        } catch (Exception e) {
            Log.e(TAG, "Failed to execute shell command", e);
            return null;
        }
        for (String volDetails : allVolumeDetails) {
            if (volDetails.startsWith("public")) {
                final String[] publicVolumeDetails = volDetails.trim().split(" ");
                String res = publicVolumeDetails[publicVolumeDetails.length - 1];
                if ("null".equals(res)) {
                    continue;
                }
                return res;
            }
        }
        return null;
    }

    /**
     * Returns the content URI of the volume on which the test is running.
     */
    public static Uri getTestVolumeFileUri() {
        return MediaStore.Files.getContentUri(sStorageVolumeName);
    }

    private static void pollForCondition(Supplier<Boolean> condition, String errorMessage)
            throws Exception {
        for (int i = 0; i < POLLING_TIMEOUT_MILLIS / POLLING_SLEEP_MILLIS; i++) {
            if (condition.get()) {
                return;
            }
            Thread.sleep(POLLING_SLEEP_MILLIS);
        }
        throw new TimeoutException(errorMessage);
    }

    /**
     * Polls for all files access to be allowed.
     */
    public static void pollForManageExternalStorageAllowed() throws Exception {
        pollForCondition(
                () -> Environment.isExternalStorageManager(),
                "Timed out while waiting for MANAGE_EXTERNAL_STORAGE");
    }

    private static void assertVolumeType(boolean isPrimary) {
        String[] parts = getExternalFilesDir().getAbsolutePath().split("/");
        assertThat(parts.length).isAtLeast(3);
        assertThat(parts[1]).isEqualTo("storage");
        if (isPrimary) {
            assertThat(parts[2]).isEqualTo("emulated");
        } else {
            assertThat(parts[2]).isNotEqualTo("emulated");
        }
    }

    private static boolean isProcessRunning(String packageName) {
        return getAppProcessInfo(packageName).isPresent();
    }

    private static Optional<ActivityManager.RunningAppProcessInfo> getAppProcessInfo(
            String packageName) {
        return getContext().getSystemService(
                ActivityManager.class).getRunningAppProcesses().stream().filter(
                        p -> packageName.equals(p.processName)).findFirst();
    }
}