• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.M;
4 import static android.os.Build.VERSION_CODES.N;
5 import static org.robolectric.RuntimeEnvironment.application;
6 
7 import android.os.UserManager;
8 import android.os.storage.StorageManager;
9 import android.os.storage.StorageVolume;
10 import com.google.common.base.Preconditions;
11 import java.io.File;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.robolectric.annotation.HiddenApi;
15 import org.robolectric.annotation.Implementation;
16 import org.robolectric.annotation.Implements;
17 import org.robolectric.shadow.api.Shadow;
18 
19 /**
20  * Fake implementation of {@link android.os.storage.StorageManager}
21  */
22 @Implements(StorageManager.class)
23 public class ShadowStorageManager {
24 
25   private static boolean isFileEncryptionSupported = true;
26   private final List<StorageVolume> storageVolumeList = new ArrayList<>();
27 
28   @Implementation(minSdk = M)
getVolumeList(int userId, int flags)29   protected static StorageVolume[] getVolumeList(int userId, int flags) {
30     return new StorageVolume[0];
31   }
32 
33   /**
34    * Gets the volume list from {@link #getVolumeList(int, int)}
35    *
36    * @return volume list
37    */
getVolumeList()38   public StorageVolume[] getVolumeList() {
39     return getVolumeList(0, 0);
40   }
41 
42   /**
43    * Adds a {@link StorageVolume} to the list returned by {@link #getStorageVolumes()}.
44    *
45    * @param StorageVolume to add to list
46    */
addStorageVolume(StorageVolume storageVolume)47   public void addStorageVolume(StorageVolume storageVolume) {
48     Preconditions.checkNotNull(storageVolume);
49     storageVolumeList.add(storageVolume);
50   }
51 
52   /**
53    * Returns the storage volumes configured via {@link #addStorageVolume()}.
54    *
55    * @return StorageVolume list
56    */
57   @Implementation(minSdk = N)
getStorageVolumes()58   protected List<StorageVolume> getStorageVolumes() {
59     return storageVolumeList;
60   }
61 
62   /** Clears the storageVolumeList. */
resetStorageVolumeList()63   public void resetStorageVolumeList() {
64     storageVolumeList.clear();
65   }
66 
67   /**
68    * Checks whether File belongs to any {@link StorageVolume} in the list returned by {@link
69    * #getStorageVolumes()}.
70    *
71    * @param File to check
72    * @return StorageVolume for the file
73    */
74   @Implementation(minSdk = N)
getStorageVolume(File file)75   public StorageVolume getStorageVolume(File file) {
76     for (StorageVolume volume : storageVolumeList) {
77       File volumeFile = volume.getPathFile();
78       if (file.getAbsolutePath().equals(volumeFile.getAbsolutePath())) {
79         return volume;
80       }
81     }
82     return null;
83   }
84 
85   @HiddenApi
86   @Implementation(minSdk = N)
isFileEncryptedNativeOrEmulated()87   protected static boolean isFileEncryptedNativeOrEmulated() {
88     return isFileEncryptionSupported;
89   }
90 
91   /**
92    * Setter for {@link #isFileEncryptedNativeOrEmulated()}
93    *
94    * @param isSupported a boolean value to set file encrypted native or not
95    */
setFileEncryptedNativeOrEmulated(boolean isSupported)96   public void setFileEncryptedNativeOrEmulated(boolean isSupported) {
97     isFileEncryptionSupported = isSupported;
98   }
99 
100   @HiddenApi
101   @Implementation(minSdk = N)
isUserKeyUnlocked(int userId)102   protected static boolean isUserKeyUnlocked(int userId) {
103     ShadowUserManager extract = Shadow.extract(application.getSystemService(UserManager.class));
104     return extract.isUserUnlocked();
105   }
106 }
107