001package com.gigya.android.sdk;
002
003import android.annotation.TargetApi;
004import android.os.Build;
005import android.os.Bundle;
006import android.support.annotation.Nullable;
007
008import com.gigya.android.sdk.utils.FileUtils;
009import com.google.gson.Gson;
010
011public class ConfigFactory {
012
013    private FileUtils _fileUtils;
014
015    public ConfigFactory(FileUtils fileUtils) {
016        _fileUtils = fileUtils;
017    }
018
019    @Nullable
020    public Config load() {
021        Config config = loadFromJson();
022        if (config == null) {
023            config = loadFromManifest();
024        }
025        return config;
026    }
027
028    @Nullable
029    public Config loadFromJson() {
030        String configFileName = "gigyaSdkConfiguration.json";
031        if (_fileUtils.containsFile(configFileName)) {
032            try {
033                String json = _fileUtils.loadFile(configFileName);
034                GigyaLogger.debug("Configuration", json);
035                return new Gson().fromJson(json, Config.class);
036            } catch (Exception ex) {
037                ex.printStackTrace();
038            }
039        }
040        return null;
041    }
042
043    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
044    @Nullable
045    public Config loadFromManifest() {
046        Bundle bundle = _fileUtils.getMetaData();
047        if (bundle == null) {
048            return null;
049        }
050        final String apiKey = bundle.getString("apiKey", null);
051        final String domain = bundle.getString("apiDomain", "us1.gigya.com");
052        final int accountCacheTime = bundle.getInt("accountCacheTime", 5);
053        final int sessionVerificationIInterval = bundle.getInt("sessionVerificationInterval", 0);
054        return new Config().updateWith(apiKey, domain, accountCacheTime, sessionVerificationIInterval);
055    }
056}