package com.eaglesakura.android.device.external;

import android.os.Build;
import android.os.Environment;
import android.os.StatFs;

import com.eaglesakura.util.LogUtil;
import com.eaglesakura.util.StringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 端末のストレージ情報を管理する
 */
public class StorageInfo {
    /**
     * 外部接続されたストレージであればtrue
     */
    final boolean sdcard;

    /**
     * ストレージのRootパス
     */
    final File path;


    /**
     * ストレージの空き容量
     */
    long freeSize = -1;

    /**
     * ストレージの最大容量
     */
    long maxSize = -1;

    private StorageInfo(boolean external, File path) {
        this.sdcard = external;
        this.path = path;
    }

    public boolean isSdcard() {
        return sdcard;
    }

    public File getPath() {
        return path;
    }

    public long getMaxSize() {
        if (maxSize < 0) {
            throw new IllegalStateException("call loadStorageInfo()");
        }
        return maxSize;
    }

    public long getFreeSize() {
        if (freeSize < 0) {
            throw new IllegalStateException("call loadStorageInfo()");
        }
        return freeSize;
    }

    /**
     * ストレージ情報を読み込む
     */
    public synchronized void loadStorageInfo() {
        StatFs stat = new StatFs(path.getAbsolutePath());
        if (Build.VERSION.SDK_INT >= 18) {
            if (this.maxSize < 0) {
                this.maxSize = stat.getBlockSizeLong() * stat.getBlockCountLong();
            }
            this.freeSize = stat.getBlockSizeLong() * stat.getFreeBlocksLong();
        } else {
            if (this.maxSize < 0) {
                this.maxSize = stat.getBlockSize() * stat.getBlockCount();
            }
            this.freeSize = stat.getBlockSize() * stat.getFreeBlocks();
        }
    }

    /**
     * 外部ストレージ一覧を取得する
     *
     * @return
     */
    public synchronized static List<StorageInfo> listExternalStorages() {
        List<StorageInfo> result = new ArrayList<>();
        File check = null;
        if ((check = new File("/storage/sdcard1")).exists()) {
            // Android 4.4標準パスから検索
            result.add(new StorageInfo(true, check));
            result.add(new StorageInfo(true, new File("/storage/sdcard0")));
        } else {
            String path = null;
            final String[] ENV_LIST = {
                    "EXTERNAL_ALT_STORAGE",
                    "EXTERNAL_STORAGE2",
                    "EXTERNAL_STORAGE"
            };

            for (String env : ENV_LIST) {
                String envPath = System.getenv(env);
                if (!StringUtil.isEmpty(envPath)) {
                    path = envPath;
                }
            }

            if (!"/storage/emulated/legacy".equals(path) &&
                    (check = new File(path)).exists()) {
                // emulatedじゃないパスがひっかかれば、それがSDカードパスになる
                result.add(new StorageInfo(true, check));
                result.add(new StorageInfo(false, Environment.getExternalStorageDirectory()));
            }
        }

        if (result.isEmpty()) {
            // 例外的な機種や状況の場合、標準の外部パスを設定する
            result.add(new StorageInfo(false, Environment.getExternalStorageDirectory()));
        }
        return result;
    }
}

@@ -375,24 +375,32 @@
      */%0A    
+private 
 final boolea
@@ -409,16 +409,16 @@
 sdcard;%0A
-
 %0A    /**
@@ -446,24 +446,32 @@
      */%0A    
+private 
 final File p
@@ -511,24 +511,32 @@
      */%0A    
+private 
 long freeSiz
@@ -578,20 +578,28 @@
      */%0A
-
     
+private 
 long max
@@ -2261,24 +2261,274 @@
 dcard0%22)));%0A
+        %7D else if ((check = new File(%22/mnt/sdcard/external_sd%22)).exists()) %7B%0A            // for Xperia GX%0A            result.add(new StorageInfo(true, check));%0A            result.add(new StorageInfo(true, Environment.getExternalStorageDirectory()));%0A
         %7D el
@@ -2524,32 +2524,32 @@
        %7D else %7B%0A
-
             Stri
@@ -2728,16 +2728,16 @@
 TORAGE%22%0A
+
         
@@ -2743,17 +2743,16 @@
      %7D;%0A
-%0A
         

support xperia gx