1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.testutils.shadow;
18 
19 import android.os.storage.DiskInfo;
20 import android.os.storage.StorageManager;
21 import android.os.storage.VolumeInfo;
22 import android.os.storage.VolumeRecord;
23 
24 import androidx.annotation.NonNull;
25 
26 import org.robolectric.annotation.Implementation;
27 import org.robolectric.annotation.Implements;
28 import org.robolectric.annotation.Resetter;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 @Implements(StorageManager.class)
34 public class ShadowStorageManager extends org.robolectric.shadows.ShadowStorageManager {
35 
36     private static boolean sIsUnmountCalled;
37     private static boolean sIsForgetCalled;
38     private static boolean sIsFileEncrypted = true;
39 
isUnmountCalled()40     public static boolean isUnmountCalled() {
41         return sIsUnmountCalled;
42     }
43 
isForgetCalled()44     public static boolean isForgetCalled() {
45         return sIsForgetCalled;
46     }
47 
getVolumes()48     public @NonNull List<VolumeInfo> getVolumes() {
49         return new ArrayList<VolumeInfo>();
50     }
51 
52     @Resetter
reset()53     public static void reset() {
54         sIsUnmountCalled = false;
55         sIsForgetCalled = false;
56         sIsFileEncrypted = true;
57     }
58 
59     @Implementation
findVolumeById(String id)60     protected VolumeInfo findVolumeById(String id) {
61         return createVolumeInfo(id);
62     }
63 
64     @Implementation
findDiskById(String id)65     protected DiskInfo findDiskById(String id) {
66         return new DiskInfo(id, DiskInfo.FLAG_SD);
67     }
68 
69     @Implementation
findRecordByUuid(String fsUuid)70     protected VolumeRecord findRecordByUuid(String fsUuid) {
71         return createVolumeRecord(fsUuid);
72     }
73 
74     @Implementation
unmount(String volId)75     protected void unmount(String volId) {
76         sIsUnmountCalled = true;
77     }
78 
79     @Implementation
forgetVolume(String fsUuid)80     protected void forgetVolume(String fsUuid) {
81         sIsForgetCalled = true;
82     }
83 
84     @Implementation
isFileEncrypted()85     protected static boolean isFileEncrypted() {
86         return sIsFileEncrypted;
87     }
88 
setIsFileEncrypted(boolean encrypted)89     public static void setIsFileEncrypted(boolean encrypted) {
90         sIsFileEncrypted = encrypted;
91     }
92 
createVolumeInfo(String volumeId)93     private VolumeInfo createVolumeInfo(String volumeId) {
94         final DiskInfo disk = new DiskInfo("fakeid", 0);
95         return new VolumeInfo(volumeId, 0, disk, "guid");
96     }
97 
createVolumeRecord(String fsUuid)98     private VolumeRecord createVolumeRecord(String fsUuid) {
99         VolumeRecord record = new VolumeRecord(VolumeRecord.USER_FLAG_INITED, fsUuid);
100         record.nickname = "nickname";
101         return record;
102     }
103 }
104