package com.datarecovery.master.utils.xfile;

import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class XStorageManager {

    /**
     * 获取所有可读的存储卷
     */
    public static List<StorageVolume> getReadableStorageVolumes(Context context) {
        StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
        List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
        ArrayList<StorageVolume> result = new ArrayList<>(storageVolumes.size());
        for (StorageVolume storageVolume : storageVolumes) {
            if (isReadable(storageVolume)) {
                result.add(storageVolume);
            }
        }
        return result;
    }

    private static boolean isReadable(StorageVolume storageVolume) {
        if (storageVolume == null) {
            return false;
        }
        return Objects.equals(storageVolume.getState(), Environment.MEDIA_MOUNTED)
                || Objects.equals(storageVolume.getState(), Environment.MEDIA_MOUNTED_READ_ONLY);
    }
}
