MainLogic.java 4.64 KB
Newer Older
Kim Jinsung committed
1 2 3 4 5 6
package jp.odakyu.toiletsignage.logic;

import android.content.Context;
import android.os.Environment;

import org.json.JSONArray;
7
import org.json.JSONException;
Kim Jinsung committed
8 9 10 11 12 13 14
import org.json.JSONObject;

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

15
import jp.odakyu.toiletsignage.constants.ToiletConstants;
Kim Jinsung committed
16 17
import jp.odakyu.toiletsignage.file.FileUtil;
import jp.odakyu.toiletsignage.info.ToiletInfo;
18
import jp.odakyu.toiletsignage.json.USBToiletDefaultJson;
Kim Jinsung committed
19
import jp.odakyu.toiletsignage.log.Logger;
Kim Jinsung committed
20 21 22 23 24 25 26 27 28 29

/**
 * Created by agentec on 2017/12/08.
 */

public class MainLogic {
    private static final String TOILET_INFO_JSON = "/toiletInfo.json";
    private static final String KEY_TOILET_ARRAY = "toiletArray";
    private static final String TAG = "MainLogic";

30 31 32 33 34 35 36 37 38 39 40 41
    /**
     * USBドライブからトイレ情報ファイルを外部ストレージ「Download」フォルダにコピー
     * @param context ToiletApplication Context
     * @return VALUE_GET_USB_INFO_SUCCESS:成功、
     * VALUE_GET_USB_INFO_VALIDATION_FAIL:バリデーションチェック失敗
     * VALUE_GET_USB_INFO_FAIL:トイレ情報ファイル取得失敗
     *
     * @throws IOException Fileオブジェクト取得及びコピー時の例外
     * @throws JSONException Json情報取得からの例外
     *
     */
    public static int getToiletDefaultInfo(Context context) throws IOException, JSONException {
Kim Jinsung committed
42 43 44 45 46 47 48
        String []rootStorageDirectory = getExtSdCardPathsForActivity(context);

        File downloadFilePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        String copyFilePath = downloadFilePath.getPath() + TOILET_INFO_JSON;

        for(String directoryName:rootStorageDirectory) {
            String jsonFilePath = directoryName + TOILET_INFO_JSON;
Kim Jinsung committed
49
            Logger.d(TAG, jsonFilePath);
Kim Jinsung committed
50 51 52 53 54 55
            File uspJsonFile = new File(jsonFilePath);

            if (uspJsonFile.exists() && uspJsonFile.isFile()) {
                FileUtil.fileCopy(jsonFilePath, copyFilePath);
                break;
            } else {
Kim Jinsung committed
56
                Logger.w(TAG, "usb json file not exists = " + jsonFilePath);
Kim Jinsung committed
57 58 59 60
            }
        }

        if (new File(copyFilePath).exists()) {
61
            JSONObject jsonObject = new JSONObject(FileUtil.getStringFromFile(copyFilePath));
Kim Jinsung committed
62
            JSONArray jsonArray = jsonObject.getJSONArray(KEY_TOILET_ARRAY);
63 64
            ArrayList<USBToiletDefaultJson> toiletDefaultJsonArray = new ArrayList<>();
            boolean isValidation = false;
Kim Jinsung committed
65
            for (int i = 0; i < jsonArray.length(); i++){
66
                USBToiletDefaultJson toiletDefaultJson = new USBToiletDefaultJson(jsonArray.getJSONObject(i));
Kim Jinsung committed
67 68 69 70 71 72 73 74
                isValidation = toiletDefaultJson.validation(jsonArray.getJSONObject(i));

                if (isValidation) {
                    toiletDefaultJsonArray.add(toiletDefaultJson);
                } else {
                    break;
                }
            }
75 76 77 78
            if (!isValidation) {
                return ToiletConstants.VALUE_GET_USB_INFO_VALIDATION_FAIL;
            }
            if (toiletDefaultJsonArray.size() != 0) {
Kim Jinsung committed
79
                ToiletInfo.getInstance().setToiletDefaultJsonArray(toiletDefaultJsonArray);
80 81
            } else {
                return ToiletConstants.VALUE_GET_USB_INFO_FAIL;
Kim Jinsung committed
82
            }
Kim Jinsung committed
83 84
        } else {
            return ToiletConstants.VALUE_GET_USB_INFO_FAIL;
Kim Jinsung committed
85
        }
Kim Jinsung committed
86

87
        return ToiletConstants.VALUE_GET_USB_INFO_SUCCESS;
Kim Jinsung committed
88 89
    }

90 91 92 93 94
    /**
     * 外部ストレージのパス情報を取得
     * @param context ToiletApplication Context
     * @return Storage直下のPath List
     */
95
    private static String[] getExtSdCardPathsForActivity(Context context) {
96
        List<String> pathList = new ArrayList<>();
Kim Jinsung committed
97 98 99 100
        for (File file : context.getExternalFilesDirs("external")) {
            if (file != null) {
                int index = file.getAbsolutePath().lastIndexOf("/Android/data");
                if (index < 0) {
Kim Jinsung committed
101
                    Logger.w("AmazeFileUtils", "Unexpected external file dir: " + file.getAbsolutePath());
Kim Jinsung committed
102 103 104
                } else {
                    String path = file.getAbsolutePath().substring(0, index);
                    try {
105 106
                        String canonicalPath = new File(path).getCanonicalPath();
                        pathList.add(canonicalPath);
Kim Jinsung committed
107 108
                    } catch (IOException e) {
                        // Keep non-canonical path.
Kim Jinsung committed
109
                        Logger.e(TAG, e.getLocalizedMessage(), e);
Kim Jinsung committed
110 111 112 113
                    }
                }
            }
        }
114 115
        if (pathList.isEmpty()) pathList.add("/storage/sdcard1");
        return pathList.toArray(new String[0]);
Kim Jinsung committed
116 117
    }
}