All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.robolectric.shadows.ShadowEnvironment Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
package org.robolectric.shadows;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.HashMap;
import android.os.Environment;
import org.robolectric.annotation.Resetter;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Implementation;
import org.robolectric.util.TempDirectory;

/**
 * Shadow for {@link android.os.Environment}.
 */
@Implements(Environment.class)
public class ShadowEnvironment {
  private static String externalStorageState = Environment.MEDIA_REMOVED;
  private static final Map STORAGE_EMULATED = new HashMap<>();
  private static final Map STORAGE_REMOVABLE = new HashMap<>();

  static Path EXTERNAL_CACHE_DIR;
  static Path EXTERNAL_FILES_DIR;

  @Implementation
  public static String getExternalStorageState() {
    return externalStorageState;
  }

  /**
   * Non-Android accessor. Sets the return value of {@link #getExternalStorageState()}.
   *
   * @param externalStorageState Value to return from {@link #getExternalStorageState()}.
   */
  public static void setExternalStorageState(String externalStorageState) {
    ShadowEnvironment.externalStorageState = externalStorageState;
  }

  @Implementation
  public static File getExternalStorageDirectory() {
    if (!exists(EXTERNAL_CACHE_DIR)) EXTERNAL_CACHE_DIR = TempDirectory.create();
    return EXTERNAL_CACHE_DIR.toFile();
  }

  @Implementation
  public static File getExternalStoragePublicDirectory(String type) {
    if (!exists(EXTERNAL_FILES_DIR)) EXTERNAL_FILES_DIR = TempDirectory.create();
    if (type == null) return EXTERNAL_FILES_DIR.toFile();
    Path path = EXTERNAL_FILES_DIR.resolve(type);
    try {
      Files.createDirectories(path);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return path.toFile();
  }

  @Resetter
  public static void reset() {
    TempDirectory.destroy(EXTERNAL_CACHE_DIR);
    TempDirectory.destroy(EXTERNAL_FILES_DIR);

    EXTERNAL_CACHE_DIR = null;
    EXTERNAL_FILES_DIR = null;

    STORAGE_EMULATED.clear();
    STORAGE_REMOVABLE.clear();
  }

  private static boolean exists(Path path) {
    return path != null && Files.exists(path);
  }

  @Implementation
  public static boolean isExternalStorageRemovable() {
    final Boolean exists = STORAGE_REMOVABLE.get(getExternalStorageDirectory());
    return exists != null ? exists : false;
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy